Skip to content

Commit 523b475

Browse files
author
Alexander Afanasyev
committed
feat(rules): add 'empty-script' rule
#86
1 parent d709456 commit 523b475

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ There are various types of rules implemented in the plugin. Here is a rough cate
6464
* [no-get-location-abs-url][]: Warn about using deprecated `getLocationAbsUrl()` method
6565
* [no-promise-in-if][]: Warn if promise is checked for truthiness inside an `if` condition
6666
* [bare-element-finders][]: Warn if a bare `ElementFinder` or `ElementArrayFinder` is declared with no applied action
67+
* [empty-script][]: Warn if `executeScript()` or `executeAsyncScript()` are called with missing or empty script
6768
6869
#### Locating Elements
6970
@@ -137,6 +138,7 @@ Rule | Default Error Level | Auto-fixable | Options
137138
[valid-by-tagname][] | 1 | |
138139
[limit-selector-depth][] | 1 | | number of nodes (default 5)
139140
[bare-element-finders][] | 1 | |
141+
[empty-script][] | 1 | |
140142
[use-promise-all][] | 0 (Turned off) | |
141143
[by-css-shortcut][] | 0 | |
142144
[no-browser-driver][] | 0 | |
@@ -195,6 +197,7 @@ See [configuring rules][] for more information.
195197
[no-get-raw-id]: docs/rules/no-get-raw-id.md
196198
[no-get-location-abs-url]: docs/rules/no-get-location-abs-url.md
197199
[bare-element-finders]: docs/rules/bare-element-finders.md
200+
[empty-script]: docs/rules/empty-script.md
198201
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules
199202
200203
## Recommended configuration

docs/rules/empty-script.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Warn if `executeScript()` or `executeAsyncScript()` are called with missing or empty script
2+
3+
This is a simple rule that would warn if `executeScript()`/`executeAsyncScript()` calls are missing arguments, or if the first argument is an empty string.
4+
5+
## Rule details
6+
7+
:thumbsdown: Any use of the following patterns are considered warnings:
8+
9+
```js
10+
browser.executeScript();
11+
browser.executeAsyncScript();
12+
browser.executeScript("");
13+
browser.executeAsyncScript('');
14+
```
15+
16+
:thumbsup: The following patterns are not errors:
17+
18+
```js
19+
browser.executeScript("var a = 1;");
20+
browser.executeAsyncScript("var a = 1;");
21+
var tag = browser.executeScript('return arguments[0].tagName', el);
22+
browser.executeAsyncScript('var callback = arguments[arguments.length - 1];');
23+
```

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var noGetRawId = require('./lib/rules/no-get-raw-id')
3737
var noGetLocationAbsUrl = require('./lib/rules/no-get-location-abs-url')
3838
var limitSelectorDepth = require('./lib/rules/limit-selector-depth')
3939
var bareElementFinders = require('./lib/rules/bare-element-finders')
40+
var emptyScript = require('./lib/rules/empty-script')
4041

4142
module.exports = {
4243
rules: {
@@ -76,7 +77,8 @@ module.exports = {
7677
'no-get-raw-id': noGetRawId,
7778
'no-get-location-abs-url': noGetLocationAbsUrl,
7879
'limit-selector-depth': limitSelectorDepth,
79-
'bare-element-finders': bareElementFinders
80+
'bare-element-finders': bareElementFinders,
81+
'empty-script': emptyScript
8082
},
8183
configs: {
8284
recommended: {
@@ -115,6 +117,7 @@ module.exports = {
115117
'protractor/limit-selector-depth': 1,
116118
'protractor/no-get-location-abs-url': 1,
117119
'protractor/bare-element-finders': 1,
120+
'protractor/empty-script': 1,
118121
'protractor/use-promise-all': 0,
119122
'protractor/by-css-shortcut': 0,
120123
'protractor/no-browser-driver': 0

lib/rules/empty-script.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
/**
4+
* @fileoverview Warn if `executeScript()` or `executeAsyncScript()` are called with no arguments
5+
* @author Alexander Afanasyev
6+
*/
7+
8+
module.exports = {
9+
meta: {
10+
schema: []
11+
},
12+
13+
create: function (context) {
14+
return {
15+
'CallExpression': function (node) {
16+
var object = node.callee.object
17+
var property = node.callee.property
18+
19+
if (object && property && object.name === 'browser') {
20+
if (property.name === 'executeScript' || property.name === 'executeAsyncScript') {
21+
var argumentExists = node.arguments && node.arguments.length
22+
23+
if (!argumentExists) {
24+
context.report({
25+
node: property,
26+
message: property.name + '() call without arguments'
27+
})
28+
29+
return
30+
}
31+
32+
var firstArgumentNonEmpty = argumentExists && node.arguments[0].value
33+
if (!firstArgumentNonEmpty) {
34+
context.report({
35+
node: property,
36+
message: property.name + '() called with an empty script'
37+
})
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}

test/rules/empty-script.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
var rule = require('../../lib/rules/empty-script')
4+
var RuleTester = require('eslint').RuleTester
5+
6+
var eslintTester = new RuleTester()
7+
8+
eslintTester.run('empty-script', rule, {
9+
valid: [
10+
'browser.executeScript("var a = 1;");',
11+
'browser.executeAsyncScript("var a = 1;");',
12+
'var tag = browser.executeScript("return arguments[0].tagName", el);',
13+
'browser.executeAsyncScript("var callback = arguments[arguments.length - 1];");'
14+
],
15+
16+
invalid: [
17+
{
18+
code: 'browser.executeScript();',
19+
errors: [{
20+
message: 'executeScript() call without arguments'
21+
}]
22+
},
23+
{
24+
code: 'browser.executeAsyncScript();',
25+
errors: [{
26+
message: 'executeAsyncScript() call without arguments'
27+
}]
28+
},
29+
{
30+
code: 'browser.executeScript("");',
31+
errors: [{
32+
message: 'executeScript() called with an empty script'
33+
}]
34+
},
35+
{
36+
code: 'browser.executeAsyncScript("");',
37+
errors: [{
38+
message: 'executeAsyncScript() called with an empty script'
39+
}]
40+
}
41+
]
42+
})

0 commit comments

Comments
 (0)