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

fix 'no-magic-number' ignores parseInt radix parameter #3536

Merged

Conversation

mateuszwitkowski
Copy link
Contributor

PR checklist

  • Addresses an existing issue: #0000
  • [x ] New feature, bugfix, or enhancement
    • [x ] Includes tests
  • Documentation update

Overview of change:

Enhancement of 'no-magic-number' based on issue #3514

CHANGELOG.md entry:

[enhancement]: 'no-magic-numbers' ignores parseInt radix parameter

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @mateuszwitkowski! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably want to return node.arguments.length === 0 ? undefined : cb(node.arguments[0]);
Otherwise magic numbers in the first argument are not checked.

You can also add a test to ensure it works as expected:

parseInt(foo === 4711 ? bar : baz, 10);
                 ~~~~ [error message]

And another test to ensure it doesn't crash if there are no arguments at all:

parseInt();

@@ -1,3 +1,4 @@
parseInt('123', 10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding tests with other numbers: 2, 8, 16

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I've updated PR based on your hints.

@ajafff ajafff merged commit 889a51e into palantir:master Dec 3, 2017
@ajafff
Copy link
Contributor

ajafff commented Dec 3, 2017

Thanks @mateuszwitkowski

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants