Skip to content

Commit

Permalink
prefer-spread: Insert semicolon if needed (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 5, 2020
1 parent ae983e2 commit 49c4acf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
29 changes: 27 additions & 2 deletions rules/prefer-spread.js
Expand Up @@ -14,7 +14,32 @@ const selector = [
].join('');

const create = context => {
const getSource = node => context.getSourceCode().getText(node);
const sourceCode = context.getSourceCode();
const getSource = node => sourceCode.getText(node);

const needsSemicolon = node => {
const tokenBefore = sourceCode.getTokenBefore(node);

if (tokenBefore) {
const {type, value} = tokenBefore;
if (type === 'Punctuator') {
if (value === ';') {
return false;
}

if (value === ']' || value === ')') {
return true;
}
}

const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]);
if (lastBlockNode && lastBlockNode.type === 'ObjectExpression') {
return true;
}
}

return false;
};

return {
[selector](node) {
Expand All @@ -23,7 +48,7 @@ const create = context => {
message: 'Prefer the spread operator over `Array.from()`.',
fix: fixer => {
const [arrayLikeArgument, mapFn, thisArgument] = node.arguments.map(getSource);
let replacement = `[...${arrayLikeArgument}]`;
let replacement = `${needsSemicolon(node) ? ';' : ''}[...${arrayLikeArgument}]`;

if (mapFn) {
const mapArguments = [mapFn, thisArgument].filter(Boolean);
Expand Down
4 changes: 1 addition & 3 deletions test/integration/test.js
Expand Up @@ -100,9 +100,7 @@ const projects = [
'https://github.com/sindresorhus/capture-website',
'https://github.com/sindresorhus/file-type',
'https://github.com/sindresorhus/slugify',
// TODO: add this project when #254 got fixed
// https://github.com/gatsbyjs/gatsby/blob/e720d8efe58eba0f6fae9f26ec8879128967d0b5/packages/gatsby/src/bootstrap/page-hot-reloader.js#L30
// 'https://github.com/gatsbyjs/gatsby',
'https://github.com/gatsbyjs/gatsby',
{
repository: 'https://github.com/puppeteer/puppeteer',
path: 'lib'
Expand Down
48 changes: 48 additions & 0 deletions test/prefer-spread.js
Expand Up @@ -123,6 +123,54 @@ ruleTester.run('prefer-spread', rule, {
}
],
output: '[...document.querySelectorAll("*")].map(() => {});'
},
// #254
{
code: `
const foo = []
Array.from(arrayLike).forEach(doSomething)
`,
errors: [
{
message: 'Prefer the spread operator over `Array.from()`.'
}
],
output: `
const foo = []
;[...arrayLike].forEach(doSomething)
`
},
// https://github.com/gatsbyjs/gatsby/blob/e720d8efe58eba0f6fae9f26ec8879128967d0b5/packages/gatsby/src/bootstrap/page-hot-reloader.js#L30
{
code: `
foo()
Array.from(arrayLike).forEach(doSomething)
`,
errors: [
{
message: 'Prefer the spread operator over `Array.from()`.'
}
],
output: `
foo()
;[...arrayLike].forEach(doSomething)
`
},
// https://github.com/gatsbyjs/gatsby/blob/4ab3f194cf5d6dcafcb2a75d9604aac79d963554/packages/gatsby/src/redux/__tests__/nodes.js#L277
{
code: `
const foo = {}
Array.from(arrayLike).forEach(doSomething)
`,
errors: [
{
message: 'Prefer the spread operator over `Array.from()`.'
}
],
output: `
const foo = {}
;[...arrayLike].forEach(doSomething)
`
}
]
});

0 comments on commit 49c4acf

Please sign in to comment.