-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add 'no-shadowing' rule
- Loading branch information
Showing
5 changed files
with
180 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
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,41 @@ | ||
# Don't allow to shadow the built-in Protractor globals | ||
|
||
The rule is to prevent shadowing the built-in Protractor globals, like `element` or `by`. | ||
|
||
## Rule details | ||
|
||
Here is the list of global variables checked for not to be shadowed: | ||
|
||
* `browser` | ||
* `protractor` | ||
* `element` | ||
* `by` | ||
* `$` | ||
* `$$` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var element = "something"; | ||
function test (browser) {}; | ||
var protractor; | ||
function by () {}; | ||
var $ = 1; | ||
var a = 2, $$ = 3; | ||
for (var by = 0; by < 10; ++by) {} | ||
try { json = JSON.parse(input) } catch (browser) {} | ||
switch (element) { case 1: break; default: break; } | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
var element2 = "something"; | ||
element(by.id("test")); | ||
var EC = protractor.ExpectedConditions; | ||
var elm = $(".myclass"); | ||
var elements = $$(".myclass"); | ||
for (var i = 0; i < 10; ++i) {} | ||
try { json = JSON.parse(input) } catch (e) {} | ||
switch (a) { case 1: break; default: break; } | ||
``` |
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,41 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Don't allow to shadow the built-in Protractor globals | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
module.exports = function (context) { | ||
var protractorGlobals = [ | ||
'browser', | ||
'protractor', | ||
'element', | ||
'by', | ||
'$', | ||
'$$' | ||
] | ||
|
||
function checkVariables (node) { | ||
var variables = context.getDeclaredVariables(node) | ||
for (var i = 0; i < variables.length; ++i) { | ||
if (protractorGlobals.indexOf(variables[i].name) !== -1) { | ||
context.report(node, 'Unexpected Protractor built-in global variable shadowing') | ||
} | ||
} | ||
|
||
// handling switch manually - getDeclaredVariables() does not return the switch discriminant | ||
if (node.type === 'SwitchStatement' && node.discriminant) { | ||
if (protractorGlobals.indexOf(node.discriminant.name) !== -1) { | ||
context.report(node, 'Unexpected Protractor built-in global variable shadowing') | ||
} | ||
} | ||
} | ||
|
||
return { | ||
'VariableDeclaration': checkVariables, | ||
'FunctionDeclaration': checkVariables, | ||
'FunctionExpression': checkVariables, | ||
'CatchClause': checkVariables, | ||
'SwitchStatement': checkVariables | ||
} | ||
} |
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,94 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/no-shadowing') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('no-shadowing', rule, { | ||
valid: [ | ||
'var element2 = "something";', | ||
'element(by.id("test"));', | ||
'var EC = protractor.ExpectedConditions;', | ||
'var elm = $(".myclass");', | ||
'var elements = $$(".myclass");', | ||
'for (var i = 0; i < variables.length; ++i) {}', | ||
'try { json = JSON.parse(input) } catch (e) {}', | ||
'switch (a) { case 1: break; default: break; }' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'var element = "something";', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'function test (browser) {};', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'var protractor;', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'function by () {};', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'var $ = 1;', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'var a = 2, $$ = 3;', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'for (var by = 0; by < 10; ++by) {}', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'try { json = JSON.parse(input) } catch (browser) {}', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'switch (element) { case 1: break; default: break; }', | ||
errors: [ | ||
{ | ||
message: 'Unexpected Protractor built-in global variable shadowing' | ||
} | ||
] | ||
} | ||
] | ||
}) |