Skip to content

Commit

Permalink
Show offending value in 'no-magic-numbers' error message (palantir#4332)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariobanfi authored and Josh Goldberg committed Dec 2, 2018
1 parent 431a1f1 commit 19b359e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/rules/noMagicNumbersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "'magic numbers' are not allowed";

public static ALLOWED_NODES = new Set<ts.SyntaxKind>([
ts.SyntaxKind.ExportAssignment,
ts.SyntaxKind.FirstAssignment,
Expand All @@ -63,6 +61,10 @@ export class Rule extends Lint.Rules.AbstractRule {

public static DEFAULT_ALLOWED = [-1, 0, 1];

public static FAILURE_STRING(num: string): string {
return `'magic numbers' are not allowed: ${num}`;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NoMagicNumbersWalker(
Expand Down Expand Up @@ -105,7 +107,7 @@ class NoMagicNumbersWalker extends Lint.AbstractWalker<number[]> {
!Rule.ALLOWED_NODES.has(node.parent!.kind) &&
!this.options.some(allowedNum => Object.is(allowedNum, parseFloat(num)))
) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
this.addFailureAtNode(node, Rule.FAILURE_STRING(num));
}
}
}
6 changes: 3 additions & 3 deletions test/rules/no-magic-numbers/custom/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ console.log(-1337);
console.log(- 1337);
console.log(1337.7);
console.log(1338);
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 1338]
console.log(-1338)
~~~~~ ['magic numbers' are not allowed]
~~~~~ ['magic numbers' are not allowed: -1338]
parseInt(foo === 4711 ? bar : baz, 10);
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 4711]
parseInt(foo === -0 ? bar : baz, 10);
export let x = 1337;
export let x = -1337;
Expand Down
12 changes: 6 additions & 6 deletions test/rules/no-magic-numbers/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
console.log(-1, 0, 1);
console.log(42.42);
~~~~~ ['magic numbers' are not allowed]
~~~~~ ['magic numbers' are not allowed: 42.42]
console.log(-0);
~~ ['magic numbers' are not allowed]
~~ ['magic numbers' are not allowed: -0]
const a = 1337;
const b = {
a: 1338,
b: 0,
}
b.b = 1339;
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 1339]

console.log(1340);
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 1340]
for(let i = 0;i>1341;++i) {
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 1341]
}

throw 1342;
~~~~ ['magic numbers' are not allowed]
~~~~ ['magic numbers' are not allowed: 1342]

class A {
static test = 1337;
Expand Down

0 comments on commit 19b359e

Please sign in to comment.