From 10bce3f01b51dacbdbd6620543db7ee2f69a1f8a Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 18 Nov 2021 16:39:04 +0900 Subject: [PATCH] Fix indent rule bugs --- src/rules/indent-helpers/es.ts | 23 ++++++++---- .../invalid/script-export02-errors.json | 37 +++++++++++++++++++ .../invalid/script-export02-input.svelte | 12 ++++++ .../invalid/script-export02-output.svelte | 12 ++++++ .../ts-v5/ts-import-assertion03-errors.json | 4 +- .../ts-v5/ts-import-assertion03-output.svelte | 4 +- .../ts-v5/ts-import-assertion04-errors.json | 4 +- .../ts-v5/ts-import-assertion04-output.svelte | 4 +- 8 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/rules/indent/invalid/script-export02-errors.json create mode 100644 tests/fixtures/rules/indent/invalid/script-export02-input.svelte create mode 100644 tests/fixtures/rules/indent/invalid/script-export02-output.svelte diff --git a/src/rules/indent-helpers/es.ts b/src/rules/indent-helpers/es.ts index bdbd871e5..a4a029419 100644 --- a/src/rules/indent-helpers/es.ts +++ b/src/rules/indent-helpers/es.ts @@ -11,6 +11,7 @@ import { isClosingParenToken, isNotClosingParenToken, isNotOpeningParenToken, + isNotSemicolonToken, isOpeningBraceToken, isOpeningBracketToken, isOpeningParenToken, @@ -336,7 +337,10 @@ export function defineVisitor(context: IndentContext): NodeListener { offsets.setOffsetToken(afterTokens, 1, fromToken) // assertions - const lastToken = sourceCode.getLastToken(node)! + const lastToken = sourceCode.getLastToken(node, { + filter: isNotSemicolonToken, + includeComments: false, + })! const assertionTokens = sourceCode.getTokensBetween( node.source, lastToken, @@ -382,10 +386,9 @@ export function defineVisitor(context: IndentContext): NodeListener { const firstSpecifier = node.specifiers[0] if (!firstSpecifier || firstSpecifier.type === "ExportSpecifier") { // export {foo, bar}; or export {foo, bar} from "mod"; - const leftBraceTokens = sourceCode.getTokensBetween( - exportToken, - firstSpecifier, - ) + const leftBraceTokens = firstSpecifier + ? sourceCode.getTokensBetween(exportToken, firstSpecifier) + : [sourceCode.getTokenAfter(exportToken)!] const rightBraceToken = node.source ? sourceCode.getTokenBefore(node.source, { filter: isClosingBraceToken, @@ -417,7 +420,10 @@ export function defineVisitor(context: IndentContext): NodeListener { ) // assertions - const lastToken = sourceCode.getLastToken(node)! + const lastToken = sourceCode.getLastToken(node, { + filter: isNotSemicolonToken, + includeComments: false, + })! const assertionTokens = sourceCode.getTokensBetween( node.source, lastToken, @@ -673,7 +679,10 @@ export function defineVisitor(context: IndentContext): NodeListener { } // assertions - const lastToken = sourceCode.getLastToken(node)! + const lastToken = sourceCode.getLastToken(node, { + filter: isNotSemicolonToken, + includeComments: false, + })! const assertionTokens = sourceCode.getTokensBetween( node.source, lastToken, diff --git a/tests/fixtures/rules/indent/invalid/script-export02-errors.json b/tests/fixtures/rules/indent/invalid/script-export02-errors.json new file mode 100644 index 000000000..0f3b22238 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-export02-errors.json @@ -0,0 +1,37 @@ +[ + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 3, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 4, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 5, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 6, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 7, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 8, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 9, + "column": 1 + } +] diff --git a/tests/fixtures/rules/indent/invalid/script-export02-input.svelte b/tests/fixtures/rules/indent/invalid/script-export02-input.svelte new file mode 100644 index 000000000..765dde5cf --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-export02-input.svelte @@ -0,0 +1,12 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/script-export02-output.svelte b/tests/fixtures/rules/indent/invalid/script-export02-output.svelte new file mode 100644 index 000000000..88ac11847 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-export02-output.svelte @@ -0,0 +1,12 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-errors.json b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-errors.json index 323eb468f..8d72423f4 100644 --- a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-errors.json +++ b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-errors.json @@ -25,12 +25,12 @@ "column": 1 }, { - "message": "Expected indentation of 6 spaces but found 0 spaces.", + "message": "Expected indentation of 4 spaces but found 0 spaces.", "line": 8, "column": 1 }, { - "message": "Expected indentation of 4 spaces but found 0 spaces.", + "message": "Expected indentation of 2 spaces but found 0 spaces.", "line": 9, "column": 1 } diff --git a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-output.svelte b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-output.svelte index df47d6b49..e5f5101f8 100644 --- a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-output.svelte +++ b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-output.svelte @@ -5,8 +5,8 @@ type : "json" - } - ; + } + ; diff --git a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-errors.json b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-errors.json index 323eb468f..8d72423f4 100644 --- a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-errors.json +++ b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-errors.json @@ -25,12 +25,12 @@ "column": 1 }, { - "message": "Expected indentation of 6 spaces but found 0 spaces.", + "message": "Expected indentation of 4 spaces but found 0 spaces.", "line": 8, "column": 1 }, { - "message": "Expected indentation of 4 spaces but found 0 spaces.", + "message": "Expected indentation of 2 spaces but found 0 spaces.", "line": 9, "column": 1 } diff --git a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-output.svelte b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-output.svelte index fac283133..aea8cb85c 100644 --- a/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-output.svelte +++ b/tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-output.svelte @@ -5,8 +5,8 @@ type : "json" - } - ; + } + ;