diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index dfc89d57c..509d4497d 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -1446,23 +1446,24 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti // Process semicolons. ':statement' (node) { const info = offsets.get(tokenStore.getFirstToken(node)) - const lastToken = tokenStore.getLastToken(node) if (info == null) { return } - if (isSemicolon(lastToken)) { - offsets.set(lastToken, info) - } // Set to the semicolon of the previous token for semicolon-free style. // E.g., // foo // ;[1,2,3].forEach(f) - const prevToken = tokenStore.getTokenBefore(node) - if (isSemicolon(prevToken)) { - const prevPrevToken = tokenStore.getTokenBefore(prevToken) - if (prevPrevToken == null || prevToken.loc.end.line !== prevPrevToken.loc.start.line) { - offsets.set(prevToken, info) + const tokens = [ + tokenStore.getTokenBefore(node), + tokenStore.getLastToken(node) + ].filter(isSemicolon) + + // Set offsets if the semicolon is at beginning of line. + for (const token of tokens) { + const prevToken = tokenStore.getTokenBefore(token) + if (prevToken == null || token.loc.end.line !== prevToken.loc.start.line) { + offsets.set(token, info) } } }, diff --git a/tests/lib/rules/script-indent.js b/tests/lib/rules/script-indent.js index 03bd827c7..30a69aa8a 100644 --- a/tests/lib/rules/script-indent.js +++ b/tests/lib/rules/script-indent.js @@ -187,6 +187,29 @@ tester.run('script-indent', rule, loadPatterns( ] }, + // Expected Indentation + { + code: unIndent` + export default { + foo() { + bar = 1; + return bar; + }, + }; + `, + output: unIndent` + export default { + foo() { + bar = 1; + return bar; + }, + }; + `, + errors: [ + { message: 'Expected indentation of 4 spaces but found 6 spaces.', line: 4 } + ] + }, + // A mix of spaces and tabs. { code: unIndent`