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

Commit

Permalink
Improve error messages for no-any and object-literal-shorthand. (#2164)
Browse files Browse the repository at this point in the history
* Improve error messages for no-any and object-literal-shorthand.

Users often use `any` when they could equally well use `{}`, because
they are not aware of the empty type.

Users also often don't know what object shorthand is - giving them a
hint as to what syntax is expected makes it easier to fix the lint
warning.

* Make the no-any rule more verbose.

* Include actual syntax suggestions in the objectLiteralShorthand rule.
  • Loading branch information
mprobst authored and nchen63 committed Feb 9, 2017
1 parent bca26e4 commit e2a4c23
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/rules/noAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Type declaration of 'any' is forbidden";
public static FAILURE_STRING = "Type declaration of 'any' loses type-safety. " +
"Consider replacing it with a more precise type, the empty type ('{}'), " +
"or suppress this occurrence.";

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

public static LONGHAND_PROPERTY = "Expected property shorthand in object literal.";
public static LONGHAND_METHOD = "Expected method shorthand in object literal.";
public static LONGHAND_PROPERTY = "Expected property shorthand in object literal ";
public static LONGHAND_METHOD = "Expected method shorthand in object literal ";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const objectLiteralShorthandWalker = new ObjectLiteralShorthandWalker(sourceFile, this.getOptions());
Expand All @@ -55,16 +55,16 @@ class ObjectLiteralShorthandWalker extends Lint.RuleWalker {
const fix = this.createFix(
this.deleteText(name.getStart(), lengthToValueStart),
);
this.addFailureAtNode(node, Rule.LONGHAND_PROPERTY, fix);
this.addFailureAtNode(node, Rule.LONGHAND_PROPERTY + `('{${name.getText()}}').`, fix);
}

if (value.kind === ts.SyntaxKind.FunctionExpression) {
const fnNode = value as ts.FunctionExpression;
if (fnNode.name) {
return; // named function expressions are OK.
}

this.addFailureAtNode(node, Rule.LONGHAND_METHOD);
const star = fnNode.asteriskToken ? fnNode.asteriskToken.getText() : "";
this.addFailureAtNode(node, Rule.LONGHAND_METHOD + `('{${name.getText()}${star}() {...}}').`);
}

super.visitPropertyAssignment(node);
Expand Down
2 changes: 1 addition & 1 deletion test/rules/no-any/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ let a: any = 2, // error
let {a: c, b: d}: {c: any, d: number} = {c: 99, d: 100}; // error
~~~ [0]

[0]: Type declaration of 'any' is forbidden
[0]: Type declaration of 'any' loses type-safety. Consider replacing it with a more precise type, the empty type ('{}'), or suppress this occurrence.
15 changes: 6 additions & 9 deletions test/rules/object-literal-shorthand/test.js.lint
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const bad = {
w: function() {},
~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{w() {...}}').]
x: function *() {},
~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{x*() {...}}').]
[y]: function() {},
~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{[y]() {...}}').]
z: z
~~~~ [property]
~~~~ [Expected property shorthand in object literal ('{z}').]
};

const good = {
Expand All @@ -26,7 +26,7 @@ const namedFunctions = {

const quotes = {
"foo-bar": function() {},
~~~~~~~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{"foo-bar"() {...}}').]
"foo-bar"() {}
};

Expand All @@ -37,10 +37,7 @@ const extraCases = {
c: 'c',
["a" + "nested"]: {
x: x
~~~~ [property]
~~~~ [Expected property shorthand in object literal ('{x}').]
}
};


[property]: Expected property shorthand in object literal.
[method]: Expected method shorthand in object literal.
16 changes: 8 additions & 8 deletions test/rules/object-literal-shorthand/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const bad = {
w: function() {},
~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{w() {...}}').]
x: function *() {},
~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{x*() {...}}').]
[y]: function() {},
~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{[y]() {...}}').]
z: z
~~~~ [property]
~~~~ [Expected property shorthand in object literal ('{z}').]
};

const good = {
Expand All @@ -26,7 +26,7 @@ const namedFunctions = {

const quotes = {
"foo-bar": function() {},
~~~~~~~~~~~~~~~~~~~~~~~~ [method]
~~~~~~~~~~~~~~~~~~~~~~~~ [Expected method shorthand in object literal ('{"foo-bar"() {...}}').]
"foo-bar"() {}
};

Expand All @@ -37,10 +37,10 @@ const extraCases = {
c: 'c',
["a" + "nested"]: {
x: x
~~~~ [property]
~~~~ [Expected property shorthand in object literal ('{x}').]
}
};


[property]: Expected property shorthand in object literal.
[method]: Expected method shorthand in object literal.
[property]: Expected property shorthand in object literal ('{foo, bar}').
[method]: Expected method shorthand in object literal ('{foo() {...}}').

0 comments on commit e2a4c23

Please sign in to comment.