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

Commit

Permalink
Better, more consistent error messages (palantir#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored and jkillian committed Jun 22, 2016
1 parent 53fbb13 commit 5f3dccd
Show file tree
Hide file tree
Showing 67 changed files with 349 additions and 311 deletions.
2 changes: 1 addition & 1 deletion src/rules/classNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "name must be in pascal case";
public static FAILURE_STRING = "Class name must be in pascal case";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NameWalker(sourceFile, this.getOptions()));
Expand Down
17 changes: 17 additions & 0 deletions src/rules/linebreakStyleRule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* @license
* Copyright 2016 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 "../lint";

Expand Down
7 changes: 4 additions & 3 deletions src/rules/maxLineLengthRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "exceeds maximum line length of ";
public static FAILURE_STRING_FACTORY = (lineLimit: number) => {
return `Exceeds maximum line length of ${lineLimit}`;
};

public isEnabled(): boolean {
if (super.isEnabled()) {
Expand All @@ -46,15 +48,14 @@ export class Rule extends Lint.Rules.AbstractRule {
return true;
}
}

return false;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const ruleFailures: Lint.RuleFailure[] = [];
const lineLimit = this.getOptions().ruleArguments[0];
const lineStarts = sourceFile.getLineStarts();
const errorString = Rule.FAILURE_STRING + lineLimit;
const errorString = Rule.FAILURE_STRING_FACTORY(lineLimit);
const disabledIntervals = this.getOptions().disabledIntervals;
const source = sourceFile.getFullText();

Expand Down
2 changes: 1 addition & 1 deletion src/rules/newParensRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "parentheses required when invoking a constructor";
public static FAILURE_STRING = "Parentheses are required when invoking a constructor";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const newParensWalker = new NewParensWalker(sourceFile, this.getOptions());
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "type decoration of 'any' is forbidden";
public static FAILURE_STRING = "Type declaration of 'any' is forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoAnyWalker(sourceFile, this.getOptions()));
Expand Down
4 changes: 2 additions & 2 deletions src/rules/noArgRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "access forbidden to arguments property";
public static FAILURE_STRING = "Access to arguments.callee is forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoArgWalker(sourceFile, this.getOptions()));
Expand All @@ -47,7 +47,7 @@ class NoArgWalker extends Lint.RuleWalker {
const name = node.name;

if (expression.kind === ts.SyntaxKind.Identifier && name.text === "callee") {
const identifierExpression = <ts.Identifier> expression;
const identifierExpression = expression as ts.Identifier;
if (identifierExpression.text === "arguments") {
this.addFailure(this.createFailure(expression.getStart(), expression.getWidth(), Rule.FAILURE_STRING));
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noBitwiseRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "forbidden bitwise operation";
public static FAILURE_STRING = "Forbidden bitwise operation";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoBitwiseWalker(sourceFile, this.getOptions()));
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noConditionalAssignmentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "assignment in conditional: ";
public static FAILURE_STRING = "Assignments in conditional expressions are forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const walker = new NoConditionalAssignmentWalker(sourceFile, this.getOptions());
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noConsecutiveBlankLinesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "consecutive blank lines are disallowed";
public static FAILURE_STRING = "Consecutive blank lines are forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoConsecutiveBlankLinesWalker(sourceFile, this.getOptions()));
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noConstructRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "undesirable constructor use";
public static FAILURE_STRING = "Forbidden constructor, use a literal or simple function call instead";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoConstructWalker(sourceFile, this.getOptions()));
Expand Down
5 changes: 2 additions & 3 deletions src/rules/noConstructorVarsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_PART = " cannot be declared in the constructor";
public static FAILURE_STRING_FACTORY = (ident: string) => `Property '${ident}' cannot be declared in the constructor`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoConstructorVarsWalker(sourceFile, this.getOptions()));
Expand All @@ -45,8 +45,7 @@ export class NoConstructorVarsWalker extends Lint.RuleWalker {
const parameters = node.parameters;
for (let parameter of parameters) {
if (parameter.modifiers != null && parameter.modifiers.length > 0) {
const name = <ts.Identifier> parameter.name;
const errorMessage = "'" + name.text + "'" + Rule.FAILURE_STRING_PART;
const errorMessage = Rule.FAILURE_STRING_FACTORY((parameter.name as ts.Identifier).text);
const lastModifier = parameter.modifiers[parameter.modifiers.length - 1];
const position = lastModifier.getEnd() - parameter.getStart();
this.addFailure(this.createFailure(parameter.getStart(), position, errorMessage));
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noDebuggerRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "use of debugger statements is disallowed";
public static FAILURE_STRING = "Use of debugger statements is forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDebuggerWalker(sourceFile, this.getOptions()));
Expand Down
19 changes: 18 additions & 1 deletion src/rules/noDefaultExportRule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* @license
* Copyright 2016 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 "../lint";

Expand All @@ -18,7 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "use of default exports is disallowed";
public static FAILURE_STRING = "Use of default exports is forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDefaultExportWalker(sourceFile, this.getOptions()));
Expand Down
4 changes: 2 additions & 2 deletions src/rules/noDuplicateKeyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "duplicate key '";
public static FAILURE_STRING_FACTORY = (name: string) => `Duplicate key '${name}'`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDuplicateKeyWalker(sourceFile, this.getOptions()));
Expand All @@ -56,7 +56,7 @@ class NoDuplicateKeyWalker extends Lint.RuleWalker {
if (keyNode.kind === ts.SyntaxKind.Identifier) {
const key = (<ts.Identifier> keyNode).text;
if (objectKeys[key]) {
const failureString = Rule.FAILURE_STRING + key + "'";
const failureString = Rule.FAILURE_STRING_FACTORY(key);
this.addFailure(this.createFailure(keyNode.getStart(), keyNode.getWidth(), failureString));
} else {
objectKeys[key] = true;
Expand Down
4 changes: 2 additions & 2 deletions src/rules/noDuplicateVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "duplicate variable: '";
public static FAILURE_STRING_FACTORY = (name: string) => `Duplicate variable: '${name}'`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDuplicateVariableWalker(sourceFile, this.getOptions()));
Expand Down Expand Up @@ -101,7 +101,7 @@ class NoDuplicateVariableWalker extends Lint.BlockScopeAwareRuleWalker<{}, Scope
}

private addFailureOnIdentifier(ident: ts.Identifier) {
const failureString = `${Rule.FAILURE_STRING}${ident.text}'`;
const failureString = Rule.FAILURE_STRING_FACTORY(ident.text);
this.addFailure(this.createFailure(ident.getStart(), ident.getWidth(), failureString));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noInternalModuleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "forbidden internal module";
public static FAILURE_STRING = "The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoInternalModuleWalker(sourceFile, this.getOptions()));
Expand Down
9 changes: 5 additions & 4 deletions src/rules/noShadowedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "shadowed variable: '";
public static FAILURE_STRING_FACTORY = (name: string) => `Shadowed variable: '${name}'`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoShadowedVariableWalker(sourceFile, this.getOptions()));
Expand All @@ -52,11 +52,12 @@ class NoShadowedVariableWalker extends Lint.BlockScopeAwareRuleWalker<ScopeInfo,
const variableDeclaration = Lint.getBindingElementVariableDeclaration(node);

if (isSingleVariable) {
const name = node.name as ts.Identifier;
if (variableDeclaration) {
const isBlockScopedVariable = Lint.isBlockScopedVariable(variableDeclaration);
this.handleSingleVariableIdentifier(<ts.Identifier> node.name, isBlockScopedVariable);
this.handleSingleVariableIdentifier(name, isBlockScopedVariable);
} else {
this.handleSingleParameterIdentifier(<ts.Identifier> node.name);
this.handleSingleParameterIdentifier(name);
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ class NoShadowedVariableWalker extends Lint.BlockScopeAwareRuleWalker<ScopeInfo,
}

private addFailureOnIdentifier(ident: ts.Identifier) {
const failureString = Rule.FAILURE_STRING + ident.text + "'";
const failureString = Rule.FAILURE_STRING_FACTORY(ident.text);
this.addFailure(this.createFailure(ident.getStart(), ident.getWidth(), failureString));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "unused variable: ";
public static FAILURE_STRING_FACTORY = (name: string) => `Unused variable: '${name}'`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
Expand Down Expand Up @@ -128,7 +128,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
const nameText = this.reactImport.name.getText();
if (!this.isIgnored(nameText)) {
const start = this.reactImport.name.getStart();
this.addFailure(this.createFailure(start, nameText.length, `${Rule.FAILURE_STRING}'${nameText}'`));
this.addFailure(this.createFailure(start, nameText.length, Rule.FAILURE_STRING_FACTORY(nameText)));
}
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
const fileName = this.getSourceFile().fileName;
const highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
if ((highlights == null || highlights[0].highlightSpans.length <= 1) && !this.isIgnored(name)) {
this.addFailure(this.createFailure(position, name.length, `${Rule.FAILURE_STRING}'${name}'`));
this.addFailure(this.createFailure(position, name.length, Rule.FAILURE_STRING_FACTORY(name)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/noVarKeywordRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "forbidden 'var' keyword, use 'let' or 'const' instead";
public static FAILURE_STRING = "Forbidden 'var' keyword, use 'let' or 'const' instead";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const noVarKeywordWalker = new NoVarKeywordWalker(sourceFile, this.getOptions());
Expand Down
5 changes: 2 additions & 3 deletions src/rules/objectLiteralSortKeysRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_PREFIX = "The key '";
public static FAILURE_STRING_POSTFIX = "' is not sorted alphabetically";
public static FAILURE_STRING_FACTORY = (name: string) => `The key '${name}' is not sorted alphabetically`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new ObjectLiteralSortKeysWalker(sourceFile, this.getOptions()));
Expand Down Expand Up @@ -65,7 +64,7 @@ class ObjectLiteralSortKeysWalker extends Lint.RuleWalker {
if (keyNode.kind === ts.SyntaxKind.Identifier) {
const key = (<ts.Identifier> keyNode).text;
if (key < lastSortedKey) {
const failureString = Rule.FAILURE_STRING_PREFIX + key + Rule.FAILURE_STRING_POSTFIX;
const failureString = Rule.FAILURE_STRING_FACTORY(key);
this.addFailure(this.createFailure(keyNode.getStart(), keyNode.getWidth(), failureString));
this.sortedStateStack[this.sortedStateStack.length - 1] = false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/radixRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "missing radix parameter";
public static FAILURE_STRING = "Missing radix parameter";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const radixWalker = new RadixWalker(sourceFile, this.getOptions());
Expand Down
4 changes: 2 additions & 2 deletions src/rules/semicolonRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_MISSING = "missing semicolon";
public static FAILURE_STRING_UNNECESSARY = "unnecessary semicolon";
public static FAILURE_STRING_MISSING = "Missing semicolon";
public static FAILURE_STRING_UNNECESSARY = "Unnecessary semicolon";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new SemicolonWalker(sourceFile, this.getOptions()));
Expand Down
2 changes: 1 addition & 1 deletion src/rules/switchDefaultRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "switch statement doesn't include a 'default' case";
public static FAILURE_STRING = "Switch statement should include a 'default' case";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new SwitchDefaultWalker(sourceFile, this.getOptions()));
Expand Down
4 changes: 2 additions & 2 deletions src/rules/trailingCommaRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_NEVER = "trailing comma";
public static FAILURE_STRING_ALWAYS = "missing trailing comma";
public static FAILURE_STRING_NEVER = "Unnecessary trailing comma";
public static FAILURE_STRING_ALWAYS = "Missing trailing comma";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new TrailingCommaWalker(sourceFile, this.getOptions()));
Expand Down
3 changes: 1 addition & 2 deletions src/rules/typedefRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "missing type declaration";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const typedefWalker = new TypedefWalker(sourceFile, this.getOptions());
return this.applyWithWalker(typedefWalker);
return this.applyWithWalker(new TypedefWalker(sourceFile, this.getOptions()));
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/rules/useIsnanRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import * as ts from "typescript";
import * as Lint from "../lint";

/**
* Implementation of the use-isnan rule.
*/
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Expand Down
Loading

0 comments on commit 5f3dccd

Please sign in to comment.