eslint-plugin-prefer-arrow
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -93,2 +93,4 @@ /** | ||
const isGeneratorFunction = node => node.generator === true; | ||
const inspectNode = (node, context) => { | ||
@@ -98,5 +100,7 @@ const opts = context.options[0] || {}; | ||
if(containsThis(node)) return; | ||
if(isGeneratorFunction(node)) return; | ||
if (opts.singleReturnOnly) { | ||
if (functionOnlyContainsReturnStatement(node) && !isNamedDefaultExport(node) | ||
&& (opts.classPropertiesAllowed || !isClassMethod(node))) | ||
if (functionOnlyContainsReturnStatement(node) && | ||
!isNamedDefaultExport(node) && | ||
(opts.classPropertiesAllowed || !isClassMethod(node))) | ||
return context.report({ | ||
@@ -186,10 +190,16 @@ node, | ||
swap[bodyTokens.find(tokenMatcher('Keyword', 'return')).start] = [parens ? '(' : '', true]; | ||
const semicolons = bodyTokens.filter(tokenMatcher('Punctuator', ';')); | ||
if (semicolons.length) { | ||
swap[semicolons[semicolons.length - 1].start] = [parens ? ')' : '', true]; | ||
const returnRange = node.body.body.find(n => n.type === 'ReturnStatement').range; | ||
const semicolon = bodyTokens.find(t => | ||
t.end == returnRange[1] && | ||
t.value === ';' && | ||
t.type === 'Punctuator'); | ||
if (semicolon) { | ||
swap[semicolon.start] = [parens ? ')' : '', true]; | ||
} | ||
const closeBraces = bodyTokens.filter(tokenMatcher('Punctuator', '}')); | ||
const lastCloseBrace = closeBraces[closeBraces.length - 1]; | ||
swap[lastCloseBrace.start] = ['', false, true]; | ||
return prefix + replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolons.length ? ')' : '') + suffix; | ||
return prefix + replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolon ? ')' : '') + suffix; | ||
} | ||
@@ -216,10 +226,16 @@ | ||
swap[bodyTokens.find(tokenMatcher('Keyword', 'return')).start] = [parens ? '(' : '', true]; | ||
const semicolons = bodyTokens.filter(tokenMatcher('Punctuator', ';')); | ||
if (semicolons.length) { | ||
swap[semicolons[semicolons.length-1].start] = [parens ? ')' : '', true]; | ||
const returnRange = node.body.body.find(n => n.type === 'ReturnStatement').range; | ||
const semicolon = bodyTokens.find(t => | ||
t.end == returnRange[1] && | ||
t.value === ';' && | ||
t.type === 'Punctuator'); | ||
if (semicolon) { | ||
swap[semicolon.start] = [parens ? ')' : '', true]; | ||
} | ||
const closeBraces = bodyTokens.filter(tokenMatcher('Punctuator', '}')); | ||
const lastCloseBrace = closeBraces[closeBraces.length-1]; | ||
swap[lastCloseBrace.start] = ['', false, true]; | ||
return replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolons.length ? ');' : ';'); | ||
} | ||
return replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolon ? ');' : ';'); | ||
} |
{ | ||
"name": "eslint-plugin-prefer-arrow", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Prefer arrow functions in most cases", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,2 +31,4 @@ /** | ||
'var foo = function() { return this.bar; };', | ||
'function * testGenerator() { return yield 1; }', | ||
...[ | ||
@@ -95,2 +97,22 @@ 'var foo = (bar) => {return bar();}', | ||
'/*1*/const/*2*/ /*3*/foo/*4*/ = (/*5*/a/*6*/)/*7*/ /*8*/=> /*9*/ /*10*//*11*/ /*12*/false/*13*//*14*/ /*15*/;/*16*/' | ||
], | ||
// Make sure we don't mess up inner generator functions | ||
[ | ||
'function foo() { return function * gen() { return yield 1; }; }', | ||
'const foo = () => function * gen() { return yield 1; };' | ||
], | ||
// Make sure we don't mess with the semicolon in for statements | ||
[ | ||
'function withLoop() { return () => { for (i = 0; i < 5; i++) {}}}', | ||
'const withLoop = () => () => { for (i = 0; i < 5; i++) {}};' | ||
], | ||
[ | ||
'var withLoop = function() { return () => { for (i = 0; i < 5; i++) {}}}', | ||
'var withLoop = () => () => { for (i = 0; i < 5; i++) {}}' | ||
], | ||
[ | ||
'function withLoop() { return () => { for (i = 0; i < 5; i++) {}} /* foo */; }', | ||
'const withLoop = () => () => { for (i = 0; i < 5; i++) {}} /* foo */;' | ||
] | ||
@@ -97,0 +119,0 @@ ].map(inputOutput => Object.assign( |
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
18193
332