Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Patch handle-callback-err
Browse files Browse the repository at this point in the history
* Improve usage of checking first character (the use of an `indexOf` when it should be a quick single character check always irks me)
* Only check the first parameter (consistent with ESLint behaviour)
* Update the failure string to match ESLint
* Fix the position usage to avoid highlighting unrelated whitespace
  • Loading branch information
blakeembrey committed Oct 1, 2016
1 parent 79d5f0b commit caf6ec6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/rules/handleCallbackErrRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from 'typescript';
import * as Lint from 'tslint/lib/lint';

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = 'error parameter not handled';
public static FAILURE_STRING = 'Expected error to be handled';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
Expand All @@ -19,28 +19,28 @@ class ErrCallbackHandlerWalker extends Lint.RuleWalker {
this.languageService = languageService;
const customExpression = options.ruleArguments[0] || 'err';

if (customExpression.indexOf('^') === 0) {
if (customExpression.charAt(0) === '^') {
this.errorRegex = new RegExp(customExpression);
} else {
this.errorRegex = new RegExp(`^${customExpression}$`);
}
}

public visitFunctionDeclaration(node: ts.FunctionDeclaration) {
node.parameters
.filter(parameter => this.errorRegex.test(parameter.name.getText()))
.forEach((parameter) => {
this.validateReferencesForVariable(parameter.name.getText(), parameter.pos);
});
const parameter = node.parameters[0];

if (parameter && this.errorRegex.test(parameter.name.getText())) {
this.validateReferencesForVariable(parameter);
}

super.visitFunctionDeclaration(node);
}

private validateReferencesForVariable(name: string, position: number) {
private validateReferencesForVariable(node: ts.ParameterDeclaration) {
const fileName = this.getSourceFile().fileName;
const highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
const highlights = this.languageService.getDocumentHighlights(fileName, node.pos, [fileName]);
if (!highlights || highlights[0].highlightSpans.length <= 1) {
this.addFailure(this.createFailure(position, name.length, `${Rule.FAILURE_STRING}'${name}'`));
this.addFailure(this.createFailure(node.name.getStart(), node.name.getWidth(), Rule.FAILURE_STRING));
}
}
}
10 changes: 8 additions & 2 deletions src/test/rules/handleCallbackErrRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ const scripts = {
console.log(err.stack);
}
doSomething();
}`,
}`,
`function loadData (Err, data) {
if (Err) {
console.log(Err.stack);
}
doSomething();
}`
}`,
`function test (cb) {
doSomething(function (err) {
cb(err)
})
}`,
`function handle (arg, err) {}`
],
invalid: [
`function(err, stream) { stream.on('done', function(){exit(0)} }`,
Expand Down

0 comments on commit caf6ec6

Please sign in to comment.