Skip to content

Commit

Permalink
chore(complexity): add lint&complexity checker
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jun 28, 2017
1 parent b174bff commit 464eeda
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build/**
/coverage/**
/docs/**
31 changes: 31 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"env": {
"node": true,
"browser": true,
"mocha": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2,
{"SwitchCase": 1}
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"complexity": [
"error",
{"max": 9}
]
}
}
28 changes: 17 additions & 11 deletions lib/is-browser-execute-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ module.exports = function (node) {
var isExecuteScript = property.name === 'executeScript'
var isExecuteAsyncScript = property.name === 'executeAsyncScript'

if (isExecuteScript || isExecuteAsyncScript) {
var isBrowser = object.name === 'browser'
var isBrowserDriver = object.object && object.object.name === 'browser' &&
object.property && object.property.name === 'driver'
if (isBrowser || isBrowserDriver) {
var objectName = isBrowser ? 'browser' : 'browser.driver'
var methodName = isExecuteScript ? 'executeScript()' : 'executeAsyncScript()'
return objectName + '.' + methodName
}
if (!isExecuteScript && !isExecuteAsyncScript) {
return false
}
}

return false
var isBrowser = object.name === 'browser'

var isBrowserCallee = object.object && object.object.name === 'browser'
var isDriverProperty = object.property && object.property.name === 'driver'

var isBrowserDriver = isBrowserCallee && isDriverProperty

if (isBrowser || isBrowserDriver) {
var objectName = isBrowser ? 'browser' : 'browser.driver'
var methodName = isExecuteScript ? 'executeScript()' : 'executeAsyncScript()'
return objectName + '.' + methodName
}

return false
}
}
33 changes: 20 additions & 13 deletions lib/is-element-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@ var elementArrayFinderGetMethods = [
'last',
'first'
]
var elementFinderConstructors = [
'$',
'element'
]

module.exports = function (node) {
var callee = node.callee
if (callee) {
// handling raw $ and element
if (callee.name === '$' || callee.name === 'element') {
if (!callee) {
return false
}

// handling raw $ and element
if (elementFinderConstructors.indexOf(callee.name) > -1) {
return true
}

// handling $ and element chaining
if (callee.type === 'MemberExpression' && callee.property) {
if (elementFinderConstructors.indexOf(callee.property.name) > -1) {
return true
}

// handling $ and element chaining
if (callee.type === 'MemberExpression' && callee.property) {
if (callee.property.name === '$' || callee.property.name === 'element') {
return true
}

// got element finder from element array finder
if (elementArrayFinderGetMethods.indexOf(callee.property.name) > -1 && callee.object && isElementArrayFinder(callee.object)) {
return true
}
// got element finder from element array finder
var isElementArrayFinderMethod = elementArrayFinderGetMethods.indexOf(callee.property.name) > -1
if (isElementArrayFinderMethod && callee.object && isElementArrayFinder(callee.object)) {
return true
}
}

Expand Down
61 changes: 36 additions & 25 deletions lib/rules/use-count-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,48 @@ module.exports = {
// find out if we are in an expect()
var expectAncestor
var thenAncestor
var thenCallback
var ancestors = context.getAncestors(node)

for (var i = 0; i < ancestors.length; i++) {
expectAncestor = ancestors[i]
if (expectAncestor && expectAncestor.type === 'CallExpression' && isExpect(expectAncestor)) {
// find out if we are inside a then callback
ancestors = context.getAncestors(expectAncestor)
for (var j = 0; j < ancestors.length; j++) {
thenAncestor = ancestors[j]
if (thenAncestor && thenAncestor.type === 'CallExpression') {
var thenCallback = isThenCallBack(thenAncestor)

// it has to have at least one argument
if (thenCallback && thenCallback.params && thenCallback.params.length > 0) {
var thenCallbackArgument = thenCallback.params[0]
// continue if not inside expect
if (!(expectAncestor && expectAncestor.type === 'CallExpression' && isExpect(expectAncestor))) {
continue
}

// find out if we are inside a then callback
ancestors = context.getAncestors(expectAncestor)
for (var j = 0; j < ancestors.length; j++) {
thenAncestor = ancestors[j]

// continue if not a call expression
if (!(thenAncestor && thenAncestor.type === 'CallExpression')) {
continue
}

thenCallback = isThenCallBack(thenAncestor)

// continue if not a "then" callback with at least 1 argument
if (!(thenCallback && thenCallback.params && thenCallback.params.length > 0)) {
continue
}

var thenCallbackArgument = thenCallback.params[0]

// continue if not the same variable used as a "then" callback function argument
if (!(thenCallbackArgument && thenCallbackArgument.name === variableName)) {
continue
}

// the same variable is a "then" callback function argument
if (thenCallbackArgument && thenCallbackArgument.name === variableName) {
// check that it was an ElementArrayFinder resolution
if (thenAncestor.callee && thenAncestor.callee.object) {
if (isElementArrayFinder(thenAncestor.callee.object)) {
context.report({
node: node,
message: 'Array.length inside promise resolution function detected. Use count() instead.'
})
return
}
}
}
}
}
// check that it was an ElementArrayFinder resolution
if (thenAncestor.callee && thenAncestor.callee.object && isElementArrayFinder(thenAncestor.callee.object)) {
context.report({
node: node,
message: 'Array.length inside promise resolution function detected. Use count() instead.'
})
return
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "tests"
},
"scripts": {
"pretest": "standard",
"pretest": "standard && eslint .",
"test": "mocha",
"watch": "mocha --watch",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
Expand Down
29 changes: 28 additions & 1 deletion test/rules/no-execute-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,35 @@ var eslintTester = new RuleTester()

eslintTester.run('no-execute-script', rule, {
valid: [
'var test = "bar"',
'test();',
'browser.get("test");',
'browser.browser',
'obj.executeScript("something");',
'',

{
code: 'test.test();',
settings: {
'eslint-plugin-protractor': {
paths: {
specs: ['**/*.spec.js', '*.spec.js']
}
}
},
filename: 'test.spec.js'
},

{
code: 'var test = "bar"'
code: 'driver.executeScript("something");',
settings: {
'eslint-plugin-protractor': {
paths: {
specs: ['**/*.spec.js', '*.spec.js']
}
}
},
filename: 'test.spec.js'
},

{
Expand Down

0 comments on commit 464eeda

Please sign in to comment.