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

no-unbound-method: refactor and add option "ignore-static" #2751

Merged
merged 3 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 26 additions & 14 deletions src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@
* limitations under the License.
*/

import { hasModifier, isPropertyAccessExpression } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";

const OPTION_IGNORE_STATIC = "ignore-static";

interface Options {
ignoreStatic: boolean;
}

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],
optionsDescription: `You may optionally pass "${OPTION_IGNORE_STATIC}" to ignore static methods.`,
options: {
type: "string",
enum: [OPTION_IGNORE_STATIC],
},
optionExamples: [true, [true, OPTION_IGNORE_STATIC]],
type: "functionality",
typescriptOnly: true,
requiresTypeInfo: true,
Expand All @@ -35,28 +45,30 @@ export class Rule extends Lint.Rules.TypedRule {
public static FAILURE_STRING = "Avoid referencing unbound methods which may cause unintentional scoping of 'this'.";

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), program));
return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<Options>) => walk(ctx, program.getTypeChecker()), {
ignoreStatic: this.ruleArguments.indexOf(OPTION_IGNORE_STATIC) !== -1,
});
}
}

class Walker extends Lint.ProgramAwareRuleWalker {
public visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
if (!isSafeUse(node)) {
const symbol = this.getTypeChecker().getSymbolAtLocation(node);
function walk(ctx: Lint.WalkContext<Options>, tc: ts.TypeChecker) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
if (isPropertyAccessExpression(node) && !isSafeUse(node)) {
const symbol = tc.getSymbolAtLocation(node);
const declaration = symbol === undefined ? undefined : symbol.valueDeclaration;
if (declaration !== undefined && isMethod(declaration)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
if (declaration !== undefined && isMethod(declaration, ctx.options.ignoreStatic)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
}
}
super.visitPropertyAccessExpression(node);
}
return ts.forEachChild(node, cb);
});
}

function isMethod(node: ts.Node): boolean {
function isMethod(node: ts.Node, ignoreStatic: boolean): boolean {
switch (node.kind) {
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
return true;
return !(ignoreStatic && hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword));
default:
return false;
}
Expand Down
47 changes: 47 additions & 0 deletions test/rules/no-unbound-method/default/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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;

c.method === i.foo;
[0].forEach(c.method || i.foo);
~~~~~~~~ [0]
~~~~~ [0]
[0].forEach(c.method.bind(c));

<button onClick={c.method}>Click me!</button>;
~~~~~~~~ [0]

class Validators {
static required() {
return null;
}
static compose(...args: Function[]) {}
}

Validators.compose(Validators.required);
~~~~~~~~~~~~~~~~~~~ [0]

[0]: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"compilerOptions": {
"module": "commonjs"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ c.method === i.foo;
<button onClick={c.method}>Click me!</button>;
~~~~~~~~ [0]

class Validators {
static required() {
return null;
}
static compose(...args: Function[]) {}
}

Validators.compose(Validators.required);

[0]: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.
5 changes: 5 additions & 0 deletions test/rules/no-unbound-method/ignore-static/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "commonjs"
}
}
8 changes: 8 additions & 0 deletions test/rules/no-unbound-method/ignore-static/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"linterOptions": {
"typeCheck": true
},
"rules": {
"no-unbound-method": [true, "ignore-static"]
}
}