This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Set --lib es6
, and use Map
/Set
methods where possible.
#1984
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7c73890
Set `--lib es6`, and use `Map`/`Set` methods where possible.
andy-hanson 5e72879
Remove underscore dependency; use own camelize
andy-hanson 2093f6f
Merge remote-tracking branch 'upstream/master' into es6
andy-hanson b9066b6
Fix createScope
andy-hanson 2dacba4
Merge branch 'master' into es6
nchen63 049db1b
Update noMagicNumbersRule.ts
nchen63 381914c
Merge branch 'master' into es6
nchen63 2800aa4
Remove unnecessary constructor
andy-hanson 9e1e9ad
Fix merge: re-implement #2004
andy-hanson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,17 +47,17 @@ export class Rule extends Lint.Rules.AbstractRule { | |
|
||
public static FAILURE_STRING = "'magic numbers' are not allowed"; | ||
|
||
public static ALLOWED_NODES = { | ||
[ts.SyntaxKind.ExportAssignment]: true, | ||
[ts.SyntaxKind.FirstAssignment]: true, | ||
[ts.SyntaxKind.LastAssignment]: true, | ||
[ts.SyntaxKind.PropertyAssignment]: true, | ||
[ts.SyntaxKind.ShorthandPropertyAssignment]: true, | ||
[ts.SyntaxKind.VariableDeclaration]: true, | ||
[ts.SyntaxKind.VariableDeclarationList]: true, | ||
[ts.SyntaxKind.EnumMember]: true, | ||
[ts.SyntaxKind.PropertyDeclaration]: true, | ||
}; | ||
public static ALLOWED_NODES = new Set<ts.SyntaxKind>([ | ||
ts.SyntaxKind.ExportAssignment, | ||
ts.SyntaxKind.FirstAssignment, | ||
ts.SyntaxKind.LastAssignment, | ||
ts.SyntaxKind.PropertyAssignment, | ||
ts.SyntaxKind.ShorthandPropertyAssignment, | ||
ts.SyntaxKind.VariableDeclaration, | ||
ts.SyntaxKind.VariableDeclarationList, | ||
ts.SyntaxKind.EnumMember, | ||
ts.SyntaxKind.PropertyDeclaration, | ||
]); | ||
|
||
public static DEFAULT_ALLOWED = [ -1, 0, 1 ]; | ||
|
||
|
@@ -68,25 +68,21 @@ export class Rule extends Lint.Rules.AbstractRule { | |
|
||
class NoMagicNumbersWalker extends Lint.RuleWalker { | ||
// lookup object for allowed magic numbers | ||
private allowed: { [prop: string]: boolean } = {}; | ||
private allowed: Set<string>; | ||
constructor (sourceFile: ts.SourceFile, options: IOptions) { | ||
super(sourceFile, options); | ||
|
||
const configOptions = this.getOptions(); | ||
const allowedNumbers: number[] = configOptions.length > 0 ? configOptions : Rule.DEFAULT_ALLOWED; | ||
|
||
allowedNumbers.forEach((value) => { | ||
this.allowed[value] = true; | ||
}); | ||
this.allowed = new Set(allowedNumbers.map(String)); | ||
} | ||
|
||
public visitNode(node: ts.Node) { | ||
const isUnary = this.isUnaryNumericExpression(node); | ||
if (node.kind === ts.SyntaxKind.NumericLiteral && node.parent !== undefined && !Rule.ALLOWED_NODES[node.parent.kind] || isUnary) { | ||
const text = node.getText(); | ||
if (!this.allowed[text]) { | ||
this.addFailureAtNode(node, Rule.FAILURE_STRING); | ||
} | ||
const isNumber = node.kind === ts.SyntaxKind.NumericLiteral && !Rule.ALLOWED_NODES.has(node.parent!.kind); | ||
const isMagicNumber = (isNumber || isUnary) && !this.allowed.has(node.getText()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. Opened issue #2005 |
||
if (isMagicNumber) { | ||
this.addFailureAtNode(node, Rule.FAILURE_STRING); | ||
} | ||
if (!isUnary) { | ||
super.visitNode(node); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
don't need constructor anymore