-
-
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.
Support solium-enable comment directive
- Loading branch information
duaraghav8
committed
Jan 20, 2019
1 parent
fd841e8
commit 46ad5f1
Showing
6 changed files
with
214 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* @fileoverview Analyzes the given comment token stream for solium configurtion directives | ||
* @fileoverview Analyzes the given comment token stream for configuration directives | ||
* @author Raghav Dua <[email protected]> | ||
*/ | ||
|
||
|
@@ -9,11 +9,6 @@ | |
const astUtils = require("./utils/ast-utils"); | ||
|
||
/** | ||
* Parser can currently detect configuration to | ||
* - Disable linting on the next line | ||
* - Disable linting on the current line | ||
* - Disable linting on entire file | ||
* (all rules or specific ones (comma-separated)) | ||
* NOTE: Constructor & Public functions of this class should have argument validations | ||
*/ | ||
class CommentDirectiveParser { | ||
|
@@ -38,7 +33,7 @@ class CommentDirectiveParser { | |
// Need not ensure that line > this.lastLine. This func's purpose is only to determine whether | ||
// given rule is disabled on given line, regardless of whether line is within bounds or not. | ||
if (typeof ruleName !== "string" || ruleName.length < 1) { | ||
throw new Error("Rule name should be a non-empty string string."); | ||
throw new Error("Rule name should be a non-empty string."); | ||
} | ||
|
||
if (!Number.isInteger(line) || line < 1) { | ||
|
@@ -75,9 +70,45 @@ class CommentDirectiveParser { | |
this.lineConfigurations[line] = (this.lineConfigurations[line] || []).concat(rules); | ||
} | ||
|
||
_removeRulesFromLineConfig(line, rules) { | ||
if (rules === this.ALL_RULES) { | ||
// delete disabled-rule configuration for this line, since all rules | ||
// are to be enabled. | ||
delete this.lineConfigurations[line]; | ||
return; | ||
} | ||
|
||
/** | ||
* A special case arises when the user has disabled all rules | ||
* and then wants to enable a select few. When all rules are disabled, | ||
* the configuration for a line is set to `all`. This means there is no | ||
* array of rules to delete the enabled ones from. | ||
* | ||
* Right now, this edge case is not accounted for. In the below example, | ||
* `throw;` will not be reported, which goes against expectations. | ||
* | ||
* contract Foo { | ||
* // solium-disable | ||
* function b11d() { | ||
* // solium-enable security/no-throw | ||
* throw; | ||
* } | ||
* } | ||
* | ||
* TODO: Account for this edge case. | ||
*/ | ||
if (this.lineConfigurations[line] === this.ALL_RULES) { | ||
return; | ||
} | ||
|
||
// Delete rules from line's disabled-rule configuration list | ||
this.lineConfigurations[line] = this.lineConfigurations[line].filter(lc => { return !rules.includes(lc); }); | ||
} | ||
|
||
_constructLineConfigurationFromComment(token) { | ||
const SD = "solium-disable", SDL = `${SD}-line`, | ||
SDNL = `${SD}-next-line`, SDPL = `${SD}-previous-line`, text = this._cleanCommentText(token.text); | ||
const text = this._cleanCommentText(token.text); | ||
const S = "solium", SD = `${S}-disable`, SDL = `${SD}-line`, | ||
SDNL = `${SD}-next-line`, SDPL = `${SD}-previous-line`, SE = `${S}-enable`; | ||
|
||
// Important that we check for SDNL, SDPL & SDL first. If they exist, then includes() will return true for SD anyway. | ||
if (text.includes(SDL)) { | ||
|
@@ -95,7 +126,7 @@ class CommentDirectiveParser { | |
return this._addRulesToLineConfig(targetLine, this._parseRuleNames(text, SDPL)); | ||
} | ||
|
||
// Notice how we use getEndingLine() for SDNL & SD to ensure that in case of block | ||
// Notice how we use getEndingLine() for below directives to ensure that in case of a block | ||
// comment directive spanning over multiple lines, the disabling starts after the comment ends. | ||
// (SDL should disable on line on which the comment starts, so getLine() is used for it) | ||
if (text.includes(SDNL)) { | ||
|
@@ -106,9 +137,22 @@ class CommentDirectiveParser { | |
const currLine = astUtils.getEndingLine(token), rulesToDisable = this._parseRuleNames(text, SD); | ||
const objContext = this; | ||
|
||
// Set desired configuration for all lines below the SD comment directive | ||
// Disable rules for all lines below the SD comment directive | ||
this._toEndOfFile( | ||
currLine + 1, | ||
lineNum => { objContext._addRulesToLineConfig(lineNum, rulesToDisable); } | ||
); | ||
} | ||
|
||
if (text.includes(SE)) { | ||
const currLine = astUtils.getEndingLine(token), rulesToEnable = this._parseRuleNames(text, SE); | ||
const objContext = this; | ||
|
||
// Remove disabled-rule entries for all lines below the SE comment directive | ||
this._toEndOfFile( | ||
currLine + 1, lineNum => { objContext._addRulesToLineConfig(lineNum, rulesToDisable); }); | ||
currLine + 1, | ||
lineNum => { objContext._removeRulesFromLineConfig(lineNum, rulesToEnable); } | ||
); | ||
} | ||
|
||
// If none of the above branches were executed, then the current comment is not a solium directive. | ||
|
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