-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
177 additions
and
55 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @fileoverview Ensure that magic numbers are extracted to constants | ||
* @author Ivan Mushketyk <[email protected]> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
module.exports = { | ||
|
||
meta: { | ||
|
||
docs: { | ||
recommended: true, | ||
type: "warning", | ||
description: "Ensure that magic numbers are extracted to constants", | ||
replacedBy: ["quotes"] | ||
}, | ||
|
||
schema: [], | ||
deprecated: true | ||
|
||
}, | ||
|
||
create: function(context) { | ||
|
||
function inspectLiteral(emitted) { | ||
if (emitted.exit) { | ||
return; | ||
} | ||
|
||
let parentNode = context.getSourceCode().getParent(emitted.node); | ||
if (_isNumber(emitted.node.value) && | ||
!_constantInitialization(parentNode) && | ||
!_allowedMagicNumber(emitted.node.value)) { | ||
|
||
context.report({ | ||
node: emitted.node, | ||
message: `Value '${emitted.node.value}' should be extracted to a constant` | ||
}); | ||
} | ||
} | ||
|
||
function _isNumber(value) { | ||
return typeof(value) === "number"; | ||
} | ||
|
||
function _constantInitialization(parentNode) { | ||
return parentNode.type === "StateVariableDeclaration" && | ||
parentNode.is_constant === true; | ||
} | ||
|
||
function _allowedMagicNumber(value) { | ||
return value === 0 || value === 1; | ||
} | ||
|
||
return { | ||
Literal: inspectLiteral | ||
}; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @fileoverview Tests for magic numbers checking rule | ||
* @author Ivan Mushketyk <[email protected]> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
let Solium = require("../../../../lib/solium"); | ||
let wrappers = require("../../../utils/wrappers"); | ||
let acceptanceCases = wrappers.acceptanceCases; | ||
let rejectionCases = wrappers.rejectionCases; | ||
|
||
let userConfig = { | ||
"custom-rules-filename": null, | ||
"rules": { | ||
"magic-number": true | ||
} | ||
}; | ||
|
||
acceptanceCases("magic-number", userConfig, | ||
[ | ||
"uint constant x = 123;", | ||
"uint a = 1; uint b = 0; uint c = -1;", | ||
"string a = \"abc\";", | ||
"string a = \"1\";" | ||
] | ||
); | ||
|
||
rejectionCases("magic-number", userConfig, | ||
[ | ||
"uint a = 5;" | ||
] | ||
); |
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