eslint-plugin-prefer-arrow
Advanced tools
Comparing version 1.1.6 to 1.1.7
@@ -5,3 +5,2 @@ /** | ||
*/ | ||
'use strict'; | ||
@@ -130,3 +129,3 @@ | ||
fixer.replaceText(node, newText), | ||
fixer.replaceTextRange([asyncToken.start, nextToken.start], ''), | ||
fixer.replaceTextRange([tokenStart(asyncToken), tokenStart(nextToken)], ''), | ||
] | ||
@@ -147,2 +146,5 @@ } | ||
const tokenStart = (token) => token.start === undefined ? token.range[0] : token.start; | ||
const tokenEnd = (token) => token.end === undefined ? token.range[1] : token.end; | ||
const replaceTokens = (origSource, tokens, replacements) => { | ||
@@ -152,5 +154,6 @@ let removeNextLeadingSpace = false; | ||
let lastTokenEnd = -1; | ||
for (const token of tokens) { | ||
if (lastTokenEnd >= 0) { | ||
let between = origSource.substring(lastTokenEnd, token.start); | ||
let between = origSource.substring(lastTokenEnd, tokenStart(token)); | ||
if (removeNextLeadingSpace) { | ||
@@ -162,4 +165,4 @@ between = between.replace(/^\s+/, ''); | ||
removeNextLeadingSpace = false; | ||
if (token.start in replacements) { | ||
const replaceInfo = replacements[token.start]; | ||
if (tokenStart(token) in replacements) { | ||
const replaceInfo = replacements[tokenStart(token)]; | ||
if (replaceInfo[2]) { | ||
@@ -171,5 +174,5 @@ result = result.replace(/\s+$/, ''); | ||
} else { | ||
result += origSource.substring(token.start, token.end); | ||
result += origSource.substring(tokenStart(token), tokenEnd(token)); | ||
} | ||
lastTokenEnd = token.end; | ||
lastTokenEnd = tokenEnd(token); | ||
} | ||
@@ -192,6 +195,6 @@ return result; | ||
if (fnKeyword) { | ||
swap[fnKeyword.start] = ['', true]; | ||
swap[tokenStart(fnKeyword)] = ['', true]; | ||
const nameToken = src.getTokenAfter(fnKeyword); | ||
if (nameToken.type === 'Identifier') { | ||
swap[nameToken.start] = ['']; | ||
swap[tokenStart(nameToken)] = ['']; | ||
} | ||
@@ -213,13 +216,13 @@ } else if (node.parent.type === 'MethodDefinition') { | ||
} | ||
swap[bodyTokens.find(tokenMatcher('Punctuator', '{')).start] = ['=> ', true]; | ||
swap[tokenStart(bodyTokens.find(tokenMatcher('Punctuator', '{')))] = ['=> ', true]; | ||
const parens = node.body.body[0].argument.type === 'ObjectExpression'; | ||
swap[bodyTokens.find(tokenMatcher('Keyword', 'return')).start] = [parens ? '(' : '', true]; | ||
swap[tokenStart(bodyTokens.find(tokenMatcher('Keyword', 'return')))] = [parens ? '(' : '', true]; | ||
const returnRange = node.body.body.find(n => n.type === 'ReturnStatement').range; | ||
const semicolon = bodyTokens.find(t => | ||
t.end == returnRange[1] && | ||
tokenEnd(t) == returnRange[1] && | ||
t.value === ';' && | ||
t.type === 'Punctuator'); | ||
if (semicolon) { | ||
swap[semicolon.start] = [parens ? ')' : '', true]; | ||
swap[tokenStart(semicolon)] = [parens ? ')' : '', true]; | ||
} | ||
@@ -229,3 +232,3 @@ | ||
const lastCloseBrace = closeBraces[closeBraces.length - 1]; | ||
swap[lastCloseBrace.start] = ['', false, true]; | ||
swap[tokenStart(lastCloseBrace)] = ['', false, true]; | ||
return prefix + replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolon ? ')' : '') + suffix; | ||
@@ -242,7 +245,7 @@ } | ||
const parens = node.body.body[0].argument.type === 'ObjectExpression'; | ||
swap[tokens.find(tokenMatcher('Keyword', 'function')).start] = omitVar ? ['', true] : ['const']; | ||
swap[tokens.find(tokenMatcher('Punctuator', '(')).start] = [omitVar ? `${asyncKeyword}(` : ` = ${asyncKeyword}(`]; | ||
swap[tokenStart(tokens.find(tokenMatcher('Keyword', 'function')))] = omitVar ? ['', true] : ['const']; | ||
swap[tokenStart(tokens.find(tokenMatcher('Punctuator', '(')))] = [omitVar ? `${asyncKeyword}(` : ` = ${asyncKeyword}(`]; | ||
if (node.async) { | ||
swap[tokens.find(tokenMatcher('Identifier', 'async')).start] = ['', true]; | ||
swap[tokenStart(tokens.find(tokenMatcher('Identifier', 'async')))] = ['', true]; | ||
} | ||
@@ -254,15 +257,15 @@ | ||
if (nameToken.type === 'Identifier') { | ||
swap[nameToken.start] = ['']; | ||
swap[tokenStart(nameToken)] = ['']; | ||
} | ||
} | ||
swap[bodyTokens.find(tokenMatcher('Punctuator', '{')).start] = ['=> ', true]; | ||
swap[bodyTokens.find(tokenMatcher('Keyword', 'return')).start] = [parens ? '(' : '', true]; | ||
swap[tokenStart(bodyTokens.find(tokenMatcher('Punctuator', '{')))] = ['=> ', true]; | ||
swap[tokenStart(bodyTokens.find(tokenMatcher('Keyword', 'return')))] = [parens ? '(' : '', true]; | ||
const returnRange = node.body.body.find(n => n.type === 'ReturnStatement').range; | ||
const semicolon = bodyTokens.find(t => | ||
t.end == returnRange[1] && | ||
tokenEnd(t) == returnRange[1] && | ||
t.value === ';' && | ||
t.type === 'Punctuator'); | ||
if (semicolon) { | ||
swap[semicolon.start] = [parens ? ')' : '', true]; | ||
swap[tokenStart(semicolon)] = [parens ? ')' : '', true]; | ||
} | ||
@@ -272,4 +275,4 @@ | ||
const lastCloseBrace = closeBraces[closeBraces.length-1]; | ||
swap[lastCloseBrace.start] = ['', false, true]; | ||
swap[tokenStart(lastCloseBrace)] = ['', false, true]; | ||
return replaceTokens(orig, tokens, swap).replace(/ $/, '') + (parens && !semicolon ? ');' : ';'); | ||
} |
{ | ||
"name": "eslint-plugin-prefer-arrow", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"description": "Prefer arrow functions in most cases", | ||
@@ -19,5 +19,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@typescript-eslint/parser": "^2.4.0", | ||
"babel-eslint": "^10.0.3", | ||
"eslint": ">=2.0.0", | ||
"mocha": "^3.4.1" | ||
"mocha": "^3.4.1", | ||
"typescript": "^3.6.4" | ||
}, | ||
@@ -24,0 +26,0 @@ "keywords": [ |
@@ -84,3 +84,3 @@ /** | ||
['var MyClass = { render(a, b) { return 3; }, b: false }', 'var MyClass = { render: (a, b) => 3, b: false }'], | ||
['const foo = { barProp() { return "bar"; } };', 'const foo = { barProp: () => "bar" };'], | ||
// Make sure named function declarations work | ||
@@ -158,3 +158,39 @@ ['function foo() { return 3; }', 'const foo = () => 3;'], | ||
], | ||
].map(asyncTest => [...asyncTest, null, { parserOptions: { ecmaVersion: 2017 } }]) | ||
].map(asyncTest => [...asyncTest, null, { parserOptions: { ecmaVersion: 2017 } }]), | ||
// Support fixes with typescript typings | ||
...[ | ||
[ | ||
'const foo = { bar(x: string) { return "bar"; } }', | ||
'const foo = { bar: (x: string) => "bar" }' | ||
], | ||
[ | ||
'const foo = { bar(x: string): string { return "bar"; } }', | ||
'const foo = { bar: (x: string): string => "bar" }' | ||
], | ||
[ | ||
'function foo(x: string): string { return x; }', | ||
'const foo = (x: string): string => x;' | ||
], | ||
[ | ||
'async function foo(x: number): Promise<number> { return x; }', | ||
'const foo = async (x: number): Promise<number> => x;' | ||
], | ||
[ | ||
'const nested = { foo: { bar(name: string) { return name; } } }', | ||
'const nested = { foo: { bar: (name: string) => name } }', | ||
], | ||
[ | ||
'const foo = function x(n: number): number { return n + 1; };', | ||
'const foo = (n: number): number => n + 1;', | ||
], | ||
[ | ||
'export function test(str: string): string { return str; }', | ||
'export const test = (str: string): string => str;', | ||
], | ||
[ | ||
'function str(n: number) { return n as string; }', | ||
'const str = (n: number) => n as string;', | ||
] | ||
].map(test => [...test, null, { parser: require.resolve('@typescript-eslint/parser') }]) | ||
].map(inputOutput => Object.assign( | ||
@@ -161,0 +197,0 @@ { |
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
23081
438
5