Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add no-unbound-method rule #2089

Merged
merged 3 commits into from
Jan 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as ts from "typescript";
import * as Lint from "../index";

export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unbound-method",
description: "Warns when a method is used as outside of a method call.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "functionality",
typescriptOnly: true,
requiresTypeInfo: true,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "'this' may be unbound when a method is used as a property without being invoked.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about `Avoid referencing unbound class methods which may cause unintentional scoping of 'this'"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed "class" from that since it may be on an interface.


public applyWithProgram(srcFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(srcFile, this.getOptions(), langSvc.getProgram()));
}
}

class Walker extends Lint.ProgramAwareRuleWalker {
public visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
if (!isInCall(node)) {
const symbol = this.getTypeChecker().getSymbolAtLocation(node);
const declaration = symbol && symbol.valueDeclaration;
if (declaration && isMethod(declaration)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
}
}
super.visitPropertyAccessExpression(node);
}
}

function isMethod(node: ts.Node): boolean {
switch (node.kind) {
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
return true;
default:
return false;
}
}

function isInCall(node: ts.Node): boolean {
const parent = node.parent!;
switch (parent.kind) {
case ts.SyntaxKind.CallExpression:
return (parent as ts.CallExpression).expression === node;
case ts.SyntaxKind.TaggedTemplateExpression:
return (parent as ts.TaggedTemplateExpression).tag === node;
default:
return false;
}
}
28 changes: 28 additions & 0 deletions test/rules/no-unbound-method/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class C {
method(x: number) {}
property: () => void;
template(strs: TemplateStringsArray, x: any) {}
}

const c = new C();
[0].forEach(c.method);
~~~~~~~~ [0]
[0].forEach(x => c.method(x));
[0].forEach(c.property);

c.template;
~~~~~~~~~~ [0]
c.template`foo${0}`;
String.raw`${c.template}`;
~~~~~~~~~~ [0]

interface I {
foo(): void;
bar: () => void;
}
declare var i: I;
i.foo;
~~~~~ [0]
i.bar;

[0]: 'this' may be unbound when a method is used as a property without being invoked.
8 changes: 8 additions & 0 deletions test/rules/no-unbound-method/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"linterOptions": {
"typeCheck": true
},
"rules": {
"no-unbound-method": true
}
}