Skip to content

Commit

Permalink
fix 'no-magic-number' ignores parseInt radix parameter (palantir#3536)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszwitkowski authored and ajafff committed Dec 3, 2017
1 parent 5e50a7a commit 889a51e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/rules/noMagicNumbersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import * as ts from "typescript";

import { isCallExpression, isIdentifier } from "tsutils";
import * as Lint from "../index";
import { isNegativeNumberLiteral } from "../language/utils";

Expand Down Expand Up @@ -70,6 +71,10 @@ export class Rule extends Lint.Rules.AbstractRule {
class NoMagicNumbersWalker extends Lint.AbstractWalker<Set<string>> {
public walk(sourceFile: ts.SourceFile) {
const cb = (node: ts.Node): void => {
if (isCallExpression(node) && isIdentifier(node.expression) && node.expression.text === "parseInt") {
return node.arguments.length === 0 ? undefined : cb(node.arguments[0]);
}

if (node.kind === ts.SyntaxKind.NumericLiteral) {
return this.checkNumericLiteral(node, (node as ts.NumericLiteral).text);
}
Expand Down
7 changes: 7 additions & 0 deletions test/rules/no-magic-numbers/custom/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
parseInt();
parseInt('123', 2);
parseInt('123', 8);
parseInt('123', 10);
parseInt('123', 16);
console.log(1337);
console.log(-1337);
console.log(- 1337);
Expand All @@ -6,6 +11,8 @@ console.log(1338);
~~~~ ['magic numbers' are not allowed]
console.log(-1338)
~~~~~ ['magic numbers' are not allowed]
parseInt(foo === 4711 ? bar : baz, 10);
~~~~ ['magic numbers' are not allowed]
export let x = 1337;
export let x = -1337;
export let x = 1337.7;
Expand Down

0 comments on commit 889a51e

Please sign in to comment.