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

Improve error messages for no-any and object-literal-shorthand. #2164

Merged
merged 3 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
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
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() {...}}').