Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

convert no-unnecessary-bind rule to use a walk function #787

Merged
merged 1 commit into from
Jan 19, 2019
Merged
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
61 changes: 32 additions & 29 deletions src/noUnnecessaryBindRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { AstUtils } from './utils/AstUtils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
Expand Down Expand Up @@ -56,46 +57,48 @@ export class Rule extends Lint.Rules.AbstractRule {

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
if (Rule.isWarningShown === false) {
console.warn(
'Warning: no-unnecessary-bind rule is deprecated. Replace your usage with the TSLint unnecessary-bind rule.'
);
console.warn('Warning: no-unnecessary-bind rule is deprecated. Replace your usage with the TSLint unnecessary-bind rule.');
Rule.isWarningShown = true;
}

return this.applyWithWalker(new NoUnnecessaryBindRuleWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk);
}
}

class NoUnnecessaryBindRuleWalker extends Lint.RuleWalker {
protected visitCallExpression(node: ts.CallExpression): void {
const analyzers: CallAnalyzer[] = [
new TypeScriptFunctionAnalyzer(),
new UnderscoreStaticAnalyzer(),
new UnderscoreInstanceAnalyzer()
];

analyzers.forEach(
(analyzer: CallAnalyzer): void => {
if (analyzer.canHandle(node)) {
const contextArgument = analyzer.getContextArgument(node);
const functionArgument = analyzer.getFunctionArgument(node);
if (contextArgument === undefined || functionArgument === undefined) {
return;
}
if (contextArgument.getText() === 'this') {
if (isArrowFunction(functionArgument)) {
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_ARROW_WITH_BIND);
} else if (isFunctionLiteral(functionArgument)) {
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_FUNCTION_WITH_BIND);
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (tsutils.isCallExpression(node)) {
const analyzers: CallAnalyzer[] = [
new TypeScriptFunctionAnalyzer(),
new UnderscoreStaticAnalyzer(),
new UnderscoreInstanceAnalyzer()
];

analyzers.forEach(
(analyzer: CallAnalyzer): void => {
if (analyzer.canHandle(node)) {
const contextArgument = analyzer.getContextArgument(node);
const functionArgument = analyzer.getFunctionArgument(node);
if (contextArgument === undefined || functionArgument === undefined) {
return;
}
if (contextArgument.getText() === 'this') {
if (isArrowFunction(functionArgument)) {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_ARROW_WITH_BIND);
} else if (isFunctionLiteral(functionArgument)) {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_FUNCTION_WITH_BIND);
}
}
}
}
}
);
super.visitCallExpression(node);
);
}

return ts.forEachChild(node, cb);
}
}

return ts.forEachChild(ctx.sourceFile, cb);
}
interface CallAnalyzer {
canHandle(node: ts.CallExpression): boolean;
getContextArgument(node: ts.CallExpression): ts.Expression | undefined;
Expand Down