-
Notifications
You must be signed in to change notification settings - Fork 885
Conversation
Thanks for your interest in palantir/tslint, @SimonSchick! 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. |
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-magic-numbers", | ||
description: "Disallows the use constant number values outside of variable assignment. -1, 0 and 1 are allowed by default.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEFAULT_ALLOWED
does not include -1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is as the minus sign is not part of number literal.
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-magic-numbers", | ||
description: "Disallows the use constant number values outside of variable assignment. -1, 0 and 1 are allowed by default.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description should say that the default values are set if there are no list of numbers is supplied. When I read this, it sounded like the default values are unioned with the user-supplied list
rationale: Lint.Utils.dedent` | ||
Magic numbers should be avoided as they often lack documentation, forcing | ||
them to be stored in variables gives them implicit documentation.`, | ||
optionsDescription: "A list of allowed number.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
number
-> numbers
Rule.ALLOWED_NODES.indexOf(node.parent.kind) === -1) { | ||
const options = this.getOptions(); | ||
|
||
const allowed: number[] = options.length > 0 ? options : Rule.DEFAULT_ALLOWED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
read options
and do this logic in a constructor so you don't have to do this for every node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I wasn't entirely sure if this.getOptions
might change during operation and would thus break things. Thanks for clearing that up.
|
||
public static FAILURE_STRING = "'magic numbers' are no allowed"; | ||
|
||
public static ALLOWED_NODES = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use object literal with computed property names for faster lookups:
public static ALLOWED_NODES = {
[ts.SyntaxKind.ExportAssignment]: true
...
}
Rule.ALLOWED_NODES[node.parent.kind]
Looks pretty good in general. Have some minor changes |
|
||
public visitNode(node: ts.Node) { | ||
if (node.kind === ts.SyntaxKind.NumericLiteral && !Rule.ALLOWED_NODES[node.parent.kind]) { | ||
if (!this.allowed[node.getText()]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be changed to handle negative numbers. If someone configures the rule to allow -1
, it would never be matched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to look into that, I'm a little short on time right now tho...
@@ -0,0 +1,30 @@ | |||
console.log(-1, 0, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add test for floats
[ts.SyntaxKind.PropertyDeclaration]: true, | ||
}; | ||
|
||
public static DEFAULT_ALLOWED = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: can you just make this a one liner? i.e. ... = [0, 1];
super(sourceFile, options); | ||
|
||
const configOptions = this.getOptions(); | ||
const allowedArray: number[] = configOptions.length > 0 ? configOptions : Rule.DEFAULT_ALLOWED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please be more specific in the variable name and omit "Array" since it is redundant with the typdef. call this const allowedNumbers
instead.
} | ||
|
||
class NoMagicNumbersWalker extends Lint.RuleWalker { | ||
private allowed: { [prop: string]: boolean }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the point of this object? its values are always true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1799 (comment) it's used as a lookup map (which is faster).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a comment so people know that this is for fast lookup
I think I addressed all comments for now. |
ruleName: "no-magic-numbers", | ||
description: Lint.Utils.dedent` | ||
Disallows the use constant number values outside of variable assignments. | ||
When no list of allowed values is specified, 0 and 1 are allowed by default.`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't match DEFAULT_ALLOWED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not anymore, right 😛
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "'magic numbers' are no allowed"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
-> not
} | ||
|
||
class NoMagicNumbersWalker extends Lint.RuleWalker { | ||
private allowed: { [prop: string]: boolean }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a comment so people know that this is for fast lookup
@SimonSchick looks good. Thanks! |
PR checklist
What changes did you make?
Added a rule to prevent magic numbers.
See the added tests for examples.
Basic usage:
"no-magic-numbers": true
disallows all except 0 and 1/-1.Custom allow list:
"no-magic-numbers": [true, 1337, 1338]"
Only 1337 and 1338 are allowed.Is there anything you'd like reviewers to focus on?
I am not very familiar with tslint and the TS API and it would love to get feedback on the filter method.