Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indent rule bugs #72

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/rules/indent-helpers/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isClosingParenToken,
isNotClosingParenToken,
isNotOpeningParenToken,
isNotSemicolonToken,
isOpeningBraceToken,
isOpeningBracketToken,
isOpeningParenToken,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions tests/fixtures/rules/indent/invalid/script-export02-errors.json
Original file line number Diff line number Diff line change
@@ -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
}
]
12 changes: 12 additions & 0 deletions tests/fixtures/rules/indent/invalid/script-export02-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- prettier-ignore -->
<script>
export
{
}
;
export
{
}
</script>

<!--tests/fixtures/rules/indent/invalid/script-export02-input.svelte-->
12 changes: 12 additions & 0 deletions tests/fixtures/rules/indent/invalid/script-export02-output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- prettier-ignore -->
<script>
export
{
}
;
export
{
}
</script>

<!--tests/fixtures/rules/indent/invalid/script-export02-input.svelte-->
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
type
:
"json"
}
;
}
;
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion03-input-->
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
type
:
"json"
}
;
}
;
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion04-input-->