diff --git a/rules/prefer-spread.js b/rules/prefer-spread.js index e00d63ab11..54758292de 100644 --- a/rules/prefer-spread.js +++ b/rules/prefer-spread.js @@ -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); @@ -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; diff --git a/test/prefer-spread.js b/test/prefer-spread.js index 2c8b05ffa5..44fb4c5f72 100644 --- a/test/prefer-spread.js +++ b/test/prefer-spread.js @@ -124,6 +124,8 @@ ruleTester.run('prefer-spread', rule, { ], output: '[...document.querySelectorAll("*")].map(() => {});' }, + + // Semicolon // #254 { code: ` @@ -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: `