From 4ec039dd56d002410adfd3e9cb955628ecd04fb8 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 25 Jun 2021 19:28:01 +0900 Subject: [PATCH] Fixed false positives for trailing comma with import in indent rule. --- src/rules/indent-helpers/es.ts | 26 +- .../invalid/script-import02-errors.json | 72 ++++++ .../invalid/script-import02-input.svelte | 19 ++ .../invalid/script-import02-output.svelte | 19 ++ .../indent/invalid/ts/ts-union01-errors.json | 242 ++++++++++++++++++ .../indent/invalid/ts/ts-union01-input.svelte | 56 ++++ .../invalid/ts/ts-union01-output.svelte | 56 ++++ .../indent/invalid/ts/ts-union02-errors.json | 242 ++++++++++++++++++ .../indent/invalid/ts/ts-union02-input.svelte | 56 ++++ .../invalid/ts/ts-union02-output.svelte | 56 ++++ 10 files changed, 835 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/rules/indent/invalid/script-import02-errors.json create mode 100644 tests/fixtures/rules/indent/invalid/script-import02-input.svelte create mode 100644 tests/fixtures/rules/indent/invalid/script-import02-output.svelte create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union01-errors.json create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union01-input.svelte create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union01-output.svelte create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union02-errors.json create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union02-input.svelte create mode 100644 tests/fixtures/rules/indent/invalid/ts/ts-union02-output.svelte diff --git a/src/rules/indent-helpers/es.ts b/src/rules/indent-helpers/es.ts index 1b8ccba3a..3d1249113 100644 --- a/src/rules/indent-helpers/es.ts +++ b/src/rules/indent-helpers/es.ts @@ -576,18 +576,16 @@ export function defineVisitor(context: IndentContext): NodeListener { const namedSpecifiers: ESTree.ImportSpecifier[] = [] for (const specifier of node.specifiers) { - let removeTokens if (specifier.type === "ImportSpecifier") { namedSpecifiers.push(specifier) - removeTokens = sourceCode.getTokens(specifier) } else { - removeTokens = sourceCode.getTokens(specifier) + const removeTokens = sourceCode.getTokens(specifier) removeTokens.shift() - } - for (const token of removeTokens) { - const i = beforeTokens.indexOf(token) - if (i >= 0) { - beforeTokens.splice(i, 1) + for (const token of removeTokens) { + const i = beforeTokens.indexOf(token) + if (i >= 0) { + beforeTokens.splice(i, 1) + } } } } @@ -595,8 +593,18 @@ export function defineVisitor(context: IndentContext): NodeListener { const leftBrace = sourceCode.getTokenBefore(namedSpecifiers[0])! const rightBrace = sourceCode.getTokenAfter( namedSpecifiers[namedSpecifiers.length - 1], - ) + { filter: isClosingBraceToken, includeComments: false }, + )! offsets.setOffsetElementList(namedSpecifiers, leftBrace, rightBrace, 1) + for (const token of sourceCode.getTokensBetween( + leftBrace, + rightBrace, + )) { + const i = beforeTokens.indexOf(token) + if (i >= 0) { + beforeTokens.splice(i, 1) + } + } } if ( diff --git a/tests/fixtures/rules/indent/invalid/script-import02-errors.json b/tests/fixtures/rules/indent/invalid/script-import02-errors.json new file mode 100644 index 000000000..4993a4ce2 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-import02-errors.json @@ -0,0 +1,72 @@ +[ + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 3, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 4, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 5, + "column": 1 + }, + { + "message": "Expected indentation of 4 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 4 spaces but found 0 spaces.", + "line": 8, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 9, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 10, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 11, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 12, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 13, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 14, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 15, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 16, + "column": 1 + } +] diff --git a/tests/fixtures/rules/indent/invalid/script-import02-input.svelte b/tests/fixtures/rules/indent/invalid/script-import02-input.svelte new file mode 100644 index 000000000..1c9569eda --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-import02-input.svelte @@ -0,0 +1,19 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/script-import02-output.svelte b/tests/fixtures/rules/indent/invalid/script-import02-output.svelte new file mode 100644 index 000000000..239521da0 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/script-import02-output.svelte @@ -0,0 +1,19 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union01-errors.json b/tests/fixtures/rules/indent/invalid/ts/ts-union01-errors.json new file mode 100644 index 000000000..6009da092 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union01-errors.json @@ -0,0 +1,242 @@ +[ + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 3, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 4, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 5, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 6, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 7, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 10, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 11, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 12, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 13, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 14, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 15, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 16, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 17, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 18, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 19, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 20, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 21, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 22, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 23, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 24, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 25, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 26, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 27, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 28, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 29, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 30, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 31, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 32, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 33, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 34, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 36, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 37, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 38, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 39, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 40, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 41, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 42, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 43, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 44, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 45, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 46, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 47, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 48, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 49, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 50, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 51, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 52, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 53, + "column": 1 + } +] diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union01-input.svelte b/tests/fixtures/rules/indent/invalid/ts/ts-union01-input.svelte new file mode 100644 index 000000000..7961207e1 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union01-input.svelte @@ -0,0 +1,56 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union01-output.svelte b/tests/fixtures/rules/indent/invalid/ts/ts-union01-output.svelte new file mode 100644 index 000000000..6a75cfad6 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union01-output.svelte @@ -0,0 +1,56 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union02-errors.json b/tests/fixtures/rules/indent/invalid/ts/ts-union02-errors.json new file mode 100644 index 000000000..1607e6ba6 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union02-errors.json @@ -0,0 +1,242 @@ +[ + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 3, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 4, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 5, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 6, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 9, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 10, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 11, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 12, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 13, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 14, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 15, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 16, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 17, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 18, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 19, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 20, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 21, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 22, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 23, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 24, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 25, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 26, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 27, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 28, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 29, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 30, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 31, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 32, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 33, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 34, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 36, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 37, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 38, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 39, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 40, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 41, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 42, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 43, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 44, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 45, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 46, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 47, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 48, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 49, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 50, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 51, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 52, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 53, + "column": 1 + } +] diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union02-input.svelte b/tests/fixtures/rules/indent/invalid/ts/ts-union02-input.svelte new file mode 100644 index 000000000..1acf20a88 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union02-input.svelte @@ -0,0 +1,56 @@ + + + + diff --git a/tests/fixtures/rules/indent/invalid/ts/ts-union02-output.svelte b/tests/fixtures/rules/indent/invalid/ts/ts-union02-output.svelte new file mode 100644 index 000000000..7de09ce58 --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/ts/ts-union02-output.svelte @@ -0,0 +1,56 @@ + + + +