-
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 'empty-script' rule
- Loading branch information
Alexander Afanasyev
committed
Jul 30, 2019
1 parent
d709456
commit 523b475
Showing
5 changed files
with
116 additions
and
1 deletion.
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,23 @@ | ||
# Warn if `executeScript()` or `executeAsyncScript()` are called with missing or empty script | ||
|
||
This is a simple rule that would warn if `executeScript()`/`executeAsyncScript()` calls are missing arguments, or if the first argument is an empty string. | ||
|
||
## Rule details | ||
|
||
:thumbsdown: Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
browser.executeScript(); | ||
browser.executeAsyncScript(); | ||
browser.executeScript(""); | ||
browser.executeAsyncScript(''); | ||
``` | ||
|
||
:thumbsup: The following patterns are not errors: | ||
|
||
```js | ||
browser.executeScript("var a = 1;"); | ||
browser.executeAsyncScript("var a = 1;"); | ||
var tag = browser.executeScript('return arguments[0].tagName', el); | ||
browser.executeAsyncScript('var callback = arguments[arguments.length - 1];'); | ||
``` |
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,44 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Warn if `executeScript()` or `executeAsyncScript()` are called with no arguments | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'CallExpression': function (node) { | ||
var object = node.callee.object | ||
var property = node.callee.property | ||
|
||
if (object && property && object.name === 'browser') { | ||
if (property.name === 'executeScript' || property.name === 'executeAsyncScript') { | ||
var argumentExists = node.arguments && node.arguments.length | ||
|
||
if (!argumentExists) { | ||
context.report({ | ||
node: property, | ||
message: property.name + '() call without arguments' | ||
}) | ||
|
||
return | ||
} | ||
|
||
var firstArgumentNonEmpty = argumentExists && node.arguments[0].value | ||
if (!firstArgumentNonEmpty) { | ||
context.report({ | ||
node: property, | ||
message: property.name + '() called with an empty script' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,42 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/empty-script') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('empty-script', rule, { | ||
valid: [ | ||
'browser.executeScript("var a = 1;");', | ||
'browser.executeAsyncScript("var a = 1;");', | ||
'var tag = browser.executeScript("return arguments[0].tagName", el);', | ||
'browser.executeAsyncScript("var callback = arguments[arguments.length - 1];");' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'browser.executeScript();', | ||
errors: [{ | ||
message: 'executeScript() call without arguments' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.executeAsyncScript();', | ||
errors: [{ | ||
message: 'executeAsyncScript() call without arguments' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.executeScript("");', | ||
errors: [{ | ||
message: 'executeScript() called with an empty script' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.executeAsyncScript("");', | ||
errors: [{ | ||
message: 'executeAsyncScript() called with an empty script' | ||
}] | ||
} | ||
] | ||
}) |