-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule for const outside of module scope.
Good: ``` const FOO = 'FOO'; export const BAR = 'BAR'; ``` Bad: ``` function derp() { const FOO = 'FOO'; } if (false) { const BLAH = 'BLAH'; } ``` --- I did not add configuration for this rule in the default preset (but I can if we want it)...
- Loading branch information
Showing
5 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"./rules/*.js" | ||
], | ||
|
||
"disallowConstOutsideModuleScope": true, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rwjblue
Author
Collaborator
|
||
"disallowEmptyBlocks": true, | ||
"disallowKeywordsOnNewLine": ["else"], | ||
"disallowMultipleLineBreaks": true, | ||
|
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,38 @@ | ||
var assert = require('assert'); | ||
|
||
module.exports = function() { }; | ||
|
||
module.exports.prototype = { | ||
configure: function(option) { | ||
assert(option === true, this.getOptionName() + ' requires a true value'); | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'disallowConstOutsideModuleScope'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
file.iterateNodesByType('VariableDeclaration', function(node) { | ||
if (node.parentNode.type === 'Program') { | ||
// declaration is in root of module | ||
return; | ||
} | ||
|
||
if (node.parentNode.type === 'ExportNamedDeclaration' && node.parentNode.parentNode.type === 'Program') { | ||
// declaration is a `export const foo = 'asdf'` in root of the module | ||
return; | ||
} | ||
|
||
for (var i = 0; i < node.declarations.length; i++) { | ||
var thisDeclaration = node.declarations[i]; | ||
|
||
if (thisDeclaration.parentNode.kind === 'const') { | ||
errors.add( | ||
'`const` should only be used in module scope (not inside functions/blocks).', | ||
node.loc.start | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/disallow-const-outside-module-scope/bad/inside-function.js
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,3 @@ | ||
function something() { | ||
const foo = 'bar'; | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/disallow-const-outside-module-scope/bad/inside-if.js
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,3 @@ | ||
if (true) { | ||
const foo = 'bar'; | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/disallow-const-outside-module-scope/good/example.js
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,6 @@ | ||
const foo = 'blah'; | ||
export const bar = 'derp'; | ||
|
||
function something() { | ||
let hey = 'you!'; | ||
} |
@rwjblue per your comment in the commit, shouldn't this be false?