Skip to content

Commit

Permalink
Merge 41547fc into 446ff37
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 18, 2020
2 parents 446ff37 + 41547fc commit eb0c21f
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
22 changes: 22 additions & 0 deletions rules/prefer-spread.js
Expand Up @@ -13,6 +13,15 @@ const selector = [
'[arguments.0.type!="ObjectExpression"]'
].join('');

// https://github.com/eslint/espree/blob/6b7d0b8100537dcd5c84a7fb17bbe28edcabe05d/lib/token-translator.js#L20
const tokenTypesCantFollowOpenBracket = new Set([
'String',
'Null',
'Boolean',
'Numeric',
'RegularExpression'
]);

const create = context => {
const sourceCode = context.getSourceCode();
const getSource = node => sourceCode.getText(node);
Expand All @@ -32,10 +41,23 @@ const create = context => {
}
}

if (tokenTypesCantFollowOpenBracket.has(type)) {
return true;
}

if (type === 'Template') {
return !value.endsWith('${');
}

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

// `for...of`
if (type === 'Identifier') {
return !(value === 'of' && lastBlockNode && lastBlockNode.type === 'ForOfStatement');
}
}

return false;
Expand Down
159 changes: 159 additions & 0 deletions test/prefer-spread.js
Expand Up @@ -124,6 +124,8 @@ ruleTester.run('prefer-spread', rule, {
],
output: '[...document.querySelectorAll("*")].map(() => {});'
},

// Semicolon
// #254
{
code: `
Expand All @@ -140,6 +142,163 @@ ruleTester.run('prefer-spread', rule, {
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = "1"
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = "1"
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = null
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = null
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = true
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = true
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = 1
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = 1
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = /./
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = /./
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = /./g
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = /./g
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = bar
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = bar
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = bar.baz
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = bar.baz
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
function* foo() {
yield Array.from(arrayLike).forEach(doSomething)
}
`,
errors: [{}],
output: `
function* foo() {
yield [...arrayLike].forEach(doSomething)
}
`
},
{
code: `
const foo = \`bar\`
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = \`bar\`
;[...arrayLike].forEach(doSomething)
`
},
{
code: `
const foo = [];
Array.from(arrayLike).forEach(doSomething)
`,
errors: [{}],
output: `
const foo = [];
[...arrayLike].forEach(doSomething)
`
},
// https://github.com/angular/angular/blob/9e70bcb34f91d439f5203dc22a44f323d02c4648/packages/benchpress/src/webdriver/selenium_webdriver_adapter.ts#L37
// TokenType of `of` is `Identifier`
{
code: `
for (const key of Array.from(arrayLike)) {
}
`,
errors: [{}],
output: `
for (const key of [...arrayLike]) {
}
`
},
// TokenType of `in` is `Keyword`
{
code: `
for (const key in Array.from(arrayLike)) {
}
`,
errors: [{}],
output: `
for (const key in [...arrayLike]) {
}
`
},
// https://github.com/facebook/relay/blob/c7dd4cc33eb2dba82629884bff865f0905fc269e/packages/relay-compiler/transforms/ValidateUnusedVariablesTransform.js#L57
{
// eslint-disable-next-line no-template-curly-in-string
code: 'const foo = `${Array.from(arrayLike)}`',
errors: [{}],
// eslint-disable-next-line no-template-curly-in-string
output: 'const foo = `${[...arrayLike]}`'
},

// https://github.com/gatsbyjs/gatsby/blob/e720d8efe58eba0f6fae9f26ec8879128967d0b5/packages/gatsby/src/bootstrap/page-hot-reloader.js#L30
{
code: `
Expand Down

0 comments on commit eb0c21f

Please sign in to comment.