Skip to content

Commit

Permalink
Merge 5862427 into 446ff37
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 18, 2020
2 parents 446ff37 + 5862427 commit a168fb5
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rules/prefer-spread.js
Expand Up @@ -13,6 +13,17 @@ 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',
'Template',
'RegularExpression',
'Identifier'
]);

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

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

const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]);
if (lastBlockNode && lastBlockNode.type === 'ObjectExpression') {
return true;
Expand Down
126 changes: 126 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,130 @@ 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/gatsbyjs/gatsby/blob/e720d8efe58eba0f6fae9f26ec8879128967d0b5/packages/gatsby/src/bootstrap/page-hot-reloader.js#L30
{
code: `
Expand Down

0 comments on commit a168fb5

Please sign in to comment.