Skip to content

Commit

Permalink
init patch; using Object.is to diff between pos/neg zero (palantir#3903)
Browse files Browse the repository at this point in the history
  • Loading branch information
aervin_ authored and suchanlee committed May 30, 2018
1 parent 1f16db8 commit 0bc48cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/rules/noMagicNumbersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ export class Rule extends Lint.Rules.AbstractRule {
public static DEFAULT_ALLOWED = [ -1, 0, 1 ];

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const allowedNumbers = this.ruleArguments.length > 0 ? this.ruleArguments : Rule.DEFAULT_ALLOWED;
return this.applyWithWalker(new NoMagicNumbersWalker(sourceFile, this.ruleName, new Set(allowedNumbers.map(String))));
return this.applyWithWalker(
new NoMagicNumbersWalker(
sourceFile,
this.ruleName,
this.ruleArguments.length > 0 ? this.ruleArguments : Rule.DEFAULT_ALLOWED,
),
);
}
}

class NoMagicNumbersWalker extends Lint.AbstractWalker<Set<string>> {
class NoMagicNumbersWalker extends Lint.AbstractWalker<number[]> {
public walk(sourceFile: ts.SourceFile) {
const cb = (node: ts.Node): void => {
if (isCallExpression(node) && isIdentifier(node.expression) && node.expression.text === "parseInt") {
Expand All @@ -88,7 +93,11 @@ class NoMagicNumbersWalker extends Lint.AbstractWalker<Set<string>> {
}

private checkNumericLiteral(node: ts.Node, num: string) {
if (!Rule.ALLOWED_NODES.has(node.parent!.kind) && !this.options.has(num)) {
/* Using Object.is() to differentiate between pos/neg zero */
if (
!Rule.ALLOWED_NODES.has(node.parent!.kind) &&
!this.options.some((allowedNum) => Object.is(allowedNum, parseFloat(num)))
) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/rules/no-magic-numbers/custom/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ parseInt('123', 2);
parseInt('123', 8);
parseInt('123', 10);
parseInt('123', 16);
console.log(-0);
console.log(1337);
console.log(-1337);
console.log(- 1337);
Expand All @@ -13,8 +14,10 @@ console.log(-1338)
~~~~~ ['magic numbers' are not allowed]
parseInt(foo === 4711 ? bar : baz, 10);
~~~~ ['magic numbers' are not allowed]
parseInt(foo === -0 ? bar : baz, 10);
export let x = 1337;
export let x = -1337;
export let x = 1337.7;
export let x = 1338;
export let x = -1338;
export let x = -0;
2 changes: 1 addition & 1 deletion test/rules/no-magic-numbers/custom/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"no-magic-numbers": [true, 1337, 1337.7, -1337]
"no-magic-numbers": [true, 1337, 1337.7, -1337, -0]
}
}
2 changes: 2 additions & 0 deletions test/rules/no-magic-numbers/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
console.log(-1, 0, 1);
console.log(42.42);
~~~~~ ['magic numbers' are not allowed]
console.log(-0);
~~ ['magic numbers' are not allowed]
const a = 1337;
const b = {
a: 1338,
Expand Down

0 comments on commit 0bc48cd

Please sign in to comment.