This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Add no-unbound-method
rule
#2089
Merged
nchen63
merged 3 commits into
palantir:master
from
andy-hanson:no_use_method_as_function
Jan 21, 2017
+111
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"linterOptions": { | ||
"typeCheck": true | ||
}, | ||
"rules": { | ||
"no-unbound-method": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'"
There was a problem hiding this comment.
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.