eslint-plugin-cucumber
Advanced tools
Comparing version 1.4.0 to 2.0.0
@@ -8,106 +8,114 @@ 'use strict'; | ||
module.exports = function(context) { | ||
const sourceCode = context.getSourceCode(); | ||
module.exports = { | ||
meta: {fixable: 'code'}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
function isGivenStep(node) { | ||
return isCucumberOneStep(node, 'Given') || isCucumberTwoStep(node, 'Given'); | ||
} | ||
function isGivenStep(node) { | ||
return ( | ||
isCucumberOneStep(node, 'Given') || isCucumberTwoStep(node, 'Given') | ||
); | ||
} | ||
function isWhenStep(node) { | ||
return isCucumberOneStep(node, 'When') || isCucumberTwoStep(node, 'When'); | ||
} | ||
function isWhenStep(node) { | ||
return isCucumberOneStep(node, 'When') || isCucumberTwoStep(node, 'When'); | ||
} | ||
function isThenStep(node) { | ||
return isCucumberOneStep(node, 'Then') || isCucumberTwoStep(node, 'Then'); | ||
} | ||
function isThenStep(node) { | ||
return isCucumberOneStep(node, 'Then') || isCucumberTwoStep(node, 'Then'); | ||
} | ||
function isStep(node) { | ||
return isGivenStep(node) || isWhenStep(node) || isThenStep(node); | ||
} | ||
function isStep(node) { | ||
return isGivenStep(node) || isWhenStep(node) || isThenStep(node); | ||
} | ||
function isCucumberOneStep(node, stepName) { | ||
return ( | ||
node.callee && | ||
node.callee.object && | ||
node.callee.object.type === 'ThisExpression' && | ||
node.callee.property && | ||
node.callee.property.name === stepName | ||
); | ||
} | ||
function isCucumberOneStep(node, stepName) { | ||
return ( | ||
node.callee && | ||
node.callee.object && | ||
node.callee.object.type === 'ThisExpression' && | ||
node.callee.property && | ||
node.callee.property.name === stepName | ||
); | ||
} | ||
function isCucumberTwoStep(node, stepName) { | ||
return ( | ||
node.type === 'CallExpression' && | ||
node.callee && | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === stepName | ||
); | ||
} | ||
function isCucumberTwoStep(node, stepName) { | ||
return ( | ||
node.type === 'CallExpression' && | ||
node.callee && | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === stepName | ||
); | ||
} | ||
function formatFunctionHead(fn) { | ||
const paramsLeftParen = sourceCode.getFirstToken(fn); | ||
const paramsRightParen = sourceCode.getTokenBefore( | ||
sourceCode.getTokenBefore(fn.body) | ||
); | ||
let paramsFullText = sourceCode.text.slice( | ||
paramsLeftParen.range[0], | ||
paramsRightParen.range[1] | ||
); | ||
let functionKeyword = 'function'; | ||
function formatFunctionHead(fn) { | ||
const paramsLeftParen = sourceCode.getFirstToken(fn); | ||
const paramsRightParen = sourceCode.getTokenBefore( | ||
sourceCode.getTokenBefore(fn.body) | ||
); | ||
let paramsFullText = sourceCode.text.slice( | ||
paramsLeftParen.range[0], | ||
paramsRightParen.range[1] | ||
); | ||
let functionKeyword = 'function'; | ||
if (fn.async) { | ||
// When 'async' specified, take care about the keyword. | ||
functionKeyword = 'async function'; | ||
// Strip 'async (...)' to ' (...)' | ||
paramsFullText = paramsFullText.slice(5); | ||
} | ||
if (fn.async) { | ||
// When 'async' specified, take care about the keyword. | ||
functionKeyword = 'async function'; | ||
// Strip 'async (...)' to ' (...)' | ||
paramsFullText = paramsFullText.slice(5); | ||
} | ||
if (fn.params.length > 0) { | ||
paramsFullText = `(${sourceCode.text.slice( | ||
fn.params[0].start, | ||
fn.params[fn.params.length - 1].end | ||
)})`; | ||
if (fn.params.length > 0) { | ||
paramsFullText = `(${sourceCode.text.slice( | ||
fn.params[0].range[0], | ||
fn.params[fn.params.length - 1].range[1] | ||
)})`; | ||
} | ||
return `${functionKeyword}${paramsFullText} `; | ||
} | ||
return `${functionKeyword}${paramsFullText} `; | ||
} | ||
function fixArrowFunction(fixer, fn) { | ||
if (fn.body.type === 'BlockStatement') { | ||
// When it((...) => { ... }), | ||
// simply replace '(...) => ' with 'function(...) ' | ||
return fixer.replaceTextRange( | ||
[fn.range[0], fn.body.range[0]], | ||
formatFunctionHead(fn) | ||
); | ||
} | ||
function fixArrowFunction(fixer, fn) { | ||
if (fn.body.type === 'BlockStatement') { | ||
// When it((...) => { ... }), | ||
// simply replace '(...) => ' with 'function () ' | ||
const bodyText = sourceCode.text.slice( | ||
fn.body.range[0], | ||
fn.body.range[1] | ||
); | ||
return fixer.replaceTextRange( | ||
[fn.start, fn.body.start], | ||
formatFunctionHead(fn) | ||
fn.range, | ||
`${formatFunctionHead(fn)}{ return ${bodyText}; }` | ||
); | ||
} | ||
const bodyText = sourceCode.text.slice(fn.body.range[0], fn.body.range[1]); | ||
return fixer.replaceTextRange( | ||
[fn.start, fn.end], | ||
`${formatFunctionHead(fn)}{ return ${bodyText}; }` | ||
); | ||
} | ||
return { | ||
CallExpression(node) { | ||
const name = | ||
node.callee && node.callee.property | ||
? node.callee.property.name | ||
: node.callee.name; | ||
return { | ||
CallExpression(node) { | ||
const name = | ||
node.callee && node.callee.property | ||
? node.callee.property.name | ||
: node.callee.name; | ||
if (isStep(node)) { | ||
const fnArg = node.arguments.slice(-1)[0]; | ||
if (fnArg && fnArg.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node, | ||
message: `Do not pass arrow functions to ${name}()`, | ||
fix(fixer) { | ||
return fixArrowFunction(fixer, fnArg); | ||
} | ||
}); | ||
if (isStep(node)) { | ||
const fnArg = node.arguments.slice(-1)[0]; | ||
if (fnArg && fnArg.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node, | ||
message: `Do not pass arrow functions to ${name}()`, | ||
fix(fixer) { | ||
return fixArrowFunction(fixer, fnArg); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
}; |
{ | ||
"name": "eslint-plugin-cucumber", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "eslint rules for cucumber steps", | ||
@@ -12,2 +12,10 @@ "keywords": [ | ||
"author": "Darrin Holst<darrinholst@gmail.com>", | ||
"contributors": [ | ||
"Darrin Holst <darrinholst@gmail.com>", | ||
"David Goss <dvdgoss@gmail.com>", | ||
"Colin Wren <colin@gimpneek.com>", | ||
"Matt Travi <programmer@travi.org>", | ||
"Piotr Kuczynski <piotr.kuczynski@gmail.com>", | ||
"Ian Wilkinson <null@sgtwilko.f9.co.uk>" | ||
], | ||
"license": "MIT", | ||
@@ -26,3 +34,3 @@ "licenses": [ | ||
"engines": { | ||
"node": ">=6.0.0" | ||
"node": ">=8.0.0" | ||
}, | ||
@@ -38,5 +46,5 @@ "scripts": { | ||
"devDependencies": { | ||
"eslint": "^4.0.0", | ||
"mocha": "^6.2.1", | ||
"prettier": "^1.14.2" | ||
"eslint": "^8.9.0", | ||
"mocha": "^6.2.2", | ||
"prettier": "^1.18.2" | ||
}, | ||
@@ -43,0 +51,0 @@ "prettier": { |
@@ -43,2 +43,3 @@ const rule = require('../../../lib/rules/no-arrow-functions'); | ||
code: 'this.Given(/step/, () => {return "anything";})', | ||
output: 'this.Given(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -52,2 +53,3 @@ { | ||
code: 'Given(/step/, () => {return "anything";})', | ||
output: 'Given(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -61,2 +63,3 @@ { | ||
code: 'this.Given(/step/, (done) => {})', | ||
output: 'this.Given(/step/, function(done) {})', | ||
errors: [ | ||
@@ -70,2 +73,3 @@ { | ||
code: 'Given(/step/, (done) => {})', | ||
output: 'Given(/step/, function(done) {})', | ||
errors: [ | ||
@@ -79,2 +83,3 @@ { | ||
code: 'this.Given(/step/, (next) => {})', | ||
output: 'this.Given(/step/, function(next) {})', | ||
errors: [ | ||
@@ -88,2 +93,3 @@ { | ||
code: 'Given(/step/, (next) => {})', | ||
output: 'Given(/step/, function(next) {})', | ||
errors: [ | ||
@@ -97,2 +103,3 @@ { | ||
code: 'this.Given(/step/, (callback) => {})', | ||
output: 'this.Given(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -106,2 +113,3 @@ { | ||
code: 'Given(/step/, (callback) => {})', | ||
output: 'Given(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -115,2 +123,3 @@ { | ||
code: 'this.Given(/step/, () => {})', | ||
output: 'this.Given(/step/, function() {})', | ||
errors: [ | ||
@@ -124,2 +133,3 @@ { | ||
code: 'Given(/step/, () => {})', | ||
output: 'Given(/step/, function() {})', | ||
errors: [ | ||
@@ -133,2 +143,3 @@ { | ||
code: 'this.When(/step/, () => {return "anything";})', | ||
output: 'this.When(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -142,2 +153,3 @@ { | ||
code: 'When(/step/, () => {return "anything";})', | ||
output: 'When(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -151,2 +163,3 @@ { | ||
code: 'this.When(/step/, (done) => {})', | ||
output: 'this.When(/step/, function(done) {})', | ||
errors: [ | ||
@@ -160,2 +173,3 @@ { | ||
code: 'When(/step/, (done) => {})', | ||
output: 'When(/step/, function(done) {})', | ||
errors: [ | ||
@@ -169,2 +183,3 @@ { | ||
code: 'this.When(/step/, (next) => {})', | ||
output: 'this.When(/step/, function(next) {})', | ||
errors: [ | ||
@@ -178,2 +193,3 @@ { | ||
code: 'When(/step/, (next) => {})', | ||
output: 'When(/step/, function(next) {})', | ||
errors: [ | ||
@@ -187,2 +203,3 @@ { | ||
code: 'this.When(/step/, (callback) => {})', | ||
output: 'this.When(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -196,2 +213,3 @@ { | ||
code: 'When(/step/, (callback) => {})', | ||
output: 'When(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -205,2 +223,3 @@ { | ||
code: 'this.When(/step/, () => {})', | ||
output: 'this.When(/step/, function() {})', | ||
errors: [ | ||
@@ -214,2 +233,3 @@ { | ||
code: 'When(/step/, () => {})', | ||
output: 'When(/step/, function() {})', | ||
errors: [ | ||
@@ -223,2 +243,3 @@ { | ||
code: 'this.Then(/step/, () => {return "anything";})', | ||
output: 'this.Then(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -232,2 +253,3 @@ { | ||
code: 'Then(/step/, () => {return "anything";})', | ||
output: 'Then(/step/, function() {return "anything";})', | ||
errors: [ | ||
@@ -241,2 +263,3 @@ { | ||
code: 'this.Then(/step/, (done) => {})', | ||
output: 'this.Then(/step/, function(done) {})', | ||
errors: [ | ||
@@ -250,2 +273,3 @@ { | ||
code: 'Then(/step/, (done) => {})', | ||
output: 'Then(/step/, function(done) {})', | ||
errors: [ | ||
@@ -259,2 +283,3 @@ { | ||
code: 'this.Then(/step/, (next) => {})', | ||
output: 'this.Then(/step/, function(next) {})', | ||
errors: [ | ||
@@ -268,2 +293,3 @@ { | ||
code: 'Then(/step/, (next) => {})', | ||
output: 'Then(/step/, function(next) {})', | ||
errors: [ | ||
@@ -277,2 +303,3 @@ { | ||
code: 'this.Then(/step/, (callback) => {})', | ||
output: 'this.Then(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -286,2 +313,3 @@ { | ||
code: 'Then(/step/, (callback) => {})', | ||
output: 'Then(/step/, function(callback) {})', | ||
errors: [ | ||
@@ -295,2 +323,3 @@ { | ||
code: 'this.Then(/step/, () => {})', | ||
output: 'this.Then(/step/, function() {})', | ||
errors: [ | ||
@@ -304,2 +333,3 @@ { | ||
code: 'Then(/step/, () => {})', | ||
output: 'Then(/step/, function() {})', | ||
errors: [ | ||
@@ -306,0 +336,0 @@ { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37484
19
980