Skip to content

Commit

Permalink
Update indent rule to support typescript v4.5 (#71)
Browse files Browse the repository at this point in the history
* Update indent rule to support typescript v4.5

* update

* update
  • Loading branch information
ota-meshi authored Nov 18, 2021
1 parent 2cd9355 commit f31be21
Show file tree
Hide file tree
Showing 25 changed files with 817 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
!/.github
/prettier-playground
/tests/fixtures/rules/indent/invalid/ts
/tests/fixtures/rules/indent/invalid/ts-v5
/tests/fixtures/rules/valid-compile/invalid/ts
/tests/fixtures/rules/valid-compile/valid/ts
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
},
"devDependencies": {
"@ota-meshi/eslint-plugin": "^0.10.0",
"@types/eslint": "^7.2.10",
"@types/eslint": "^8.0.0",
"@types/eslint-scope": "^3.7.0",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0-0",
"@typescript-eslint/parser": "^5.0.0-0",
"@typescript-eslint/parser-v4": "npm:@typescript-eslint/parser@5",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.1-0",
"@typescript-eslint/parser-v4": "npm:@typescript-eslint/parser@4",
"env-cmd": "^10.1.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.3.0",
Expand Down Expand Up @@ -101,7 +101,7 @@
"stylelint-config-standard": "^24.0.0",
"svelte": "^3.37.0",
"ts-node": "^10.0.0",
"typescript": "^4.4.2",
"typescript": "^4.5.2",
"vue-eslint-editor": "^1.1.0",
"vue-eslint-parser": "^8.0.0",
"vuepress": "^1.8.2"
Expand Down
92 changes: 81 additions & 11 deletions src/rules/indent-helpers/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
}
offsets.setOffsetToken(fromToken, 0, exportToken)
offsets.setOffsetToken(afterTokens, 1, fromToken)

// assertions
const lastToken = sourceCode.getLastToken(node)!
const assertionTokens = sourceCode.getTokensBetween(
node.source,
lastToken,
)
if (assertionTokens.length) {
const assertToken = assertionTokens.shift()!
offsets.setOffsetToken(assertToken, 0, exportToken)
const assertionOpen = assertionTokens.shift()
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken)
offsets.setOffsetElementList(
assertionTokens,
assertionOpen,
lastToken,
1,
)
}
}
},
ExportDefaultDeclaration(node: ESTree.ExportDefaultDeclaration) {
const exportToken = sourceCode.getFirstToken(node)
Expand Down Expand Up @@ -365,10 +386,15 @@ export function defineVisitor(context: IndentContext): NodeListener {
exportToken,
firstSpecifier,
)
const rightBraceToken = sourceCode.getLastToken(node, {
filter: isClosingBraceToken,
includeComments: false,
})!
const rightBraceToken = node.source
? sourceCode.getTokenBefore(node.source, {
filter: isClosingBraceToken,
includeComments: false,
})!
: sourceCode.getLastToken(node, {
filter: isClosingBraceToken,
includeComments: false,
})!
offsets.setOffsetToken(leftBraceTokens, 0, exportToken)
offsets.setOffsetElementList(
node.specifiers,
Expand All @@ -389,14 +415,41 @@ export function defineVisitor(context: IndentContext): NodeListener {
1,
fromToken,
)

// assertions
const lastToken = sourceCode.getLastToken(node)!
const assertionTokens = sourceCode.getTokensBetween(
node.source,
lastToken,
)
if (assertionTokens.length) {
const assertToken = assertionTokens.shift()!
offsets.setOffsetToken(assertToken, 0, exportToken)
const assertionOpen = assertionTokens.shift()
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken)
offsets.setOffsetElementList(
assertionTokens,
assertionOpen,
lastToken,
1,
)
}
}
}
} else {
// maybe babel-eslint
}
}
},
ExportSpecifier(node: ESTree.ExportSpecifier) {
const [firstToken, ...tokens] = sourceCode.getTokens(node)
ExportSpecifier(node: ESTree.ExportSpecifier | ESTree.ImportSpecifier) {
const tokens = sourceCode.getTokens(node)
let firstToken = tokens.shift()!
if (firstToken.value === "type") {
const typeToken = firstToken
firstToken = tokens.shift()!
offsets.setOffsetToken(firstToken, 0, typeToken)
}
offsets.setOffsetToken(tokens, 1, firstToken)
},
ForInStatement(node: ESTree.ForInStatement | ESTree.ForOfStatement) {
Expand Down Expand Up @@ -618,6 +671,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
offsets.setOffsetToken(fromToken, 0, importToken)
offsets.setOffsetToken(afterTokens, 1, fromToken)
}

// assertions
const lastToken = sourceCode.getLastToken(node)!
const assertionTokens = sourceCode.getTokensBetween(
node.source,
lastToken,
)
if (assertionTokens.length) {
const assertToken = assertionTokens.shift()!
offsets.setOffsetToken(assertToken, 0, importToken)
const assertionOpen = assertionTokens.shift()
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken)
offsets.setOffsetElementList(
assertionTokens,
assertionOpen,
lastToken,
1,
)
}
}
},
ImportExpression(node: ESTree.ImportExpression) {
const firstToken = sourceCode.getFirstToken(node)
Expand All @@ -636,11 +710,7 @@ export function defineVisitor(context: IndentContext): NodeListener {
offsets.setOffsetToken(tokens, 1, firstToken)
},
ImportSpecifier(node: ESTree.ImportSpecifier) {
if (node.local.range![0] !== node.imported.range![0]) {
const tokens = sourceCode.getTokens(node)
const firstToken = tokens.shift()!
offsets.setOffsetToken(tokens, 1, firstToken)
}
visitor.ExportSpecifier(node)
},
LabeledStatement(
node: ESTree.LabeledStatement | AST.SvelteReactiveStatement,
Expand Down
21 changes: 21 additions & 0 deletions src/rules/indent-helpers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
1,
)
},
ImportAttribute(node: TSESTree.ImportAttribute) {
const firstToken = sourceCode.getFirstToken(node)
const keyTokens = getFirstAndLastTokens(sourceCode, node.key)
const prefixTokens = sourceCode.getTokensBetween(
firstToken,
keyTokens.firstToken,
)
offsets.setOffsetToken(prefixTokens, 0, firstToken)

offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken)

const initToken = sourceCode.getFirstToken(node.value)
offsets.setOffsetToken(
[
...sourceCode.getTokensBetween(keyTokens.lastToken, initToken),
initToken,
],
1,
keyTokens.lastToken,
)
},
// ----------------------------------------------------------------------
// SINGLE TOKEN NODES
// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"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 6 spaces but found 0 spaces.",
"line": 5,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 6,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 7,
"column": 1
},
{
"message": "Expected indentation of 4 spaces but found 0 spaces.",
"line": 8,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- prettier-ignore -->
<script lang="ts">
import foo from "./foo.json" assert
{
type
:
"json"
}
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion01-input-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- prettier-ignore -->
<script lang="ts">
import foo from "./foo.json" assert
{
type
:
"json"
}
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion01-input-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"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 6 spaces but found 0 spaces.",
"line": 6,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 7,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 8,
"column": 1
},
{
"message": "Expected indentation of 10 spaces but found 0 spaces.",
"line": 9,
"column": 1
},
{
"message": "Expected indentation of 12 spaces but found 0 spaces.",
"line": 10,
"column": 1
},
{
"message": "Expected indentation of 12 spaces but found 0 spaces.",
"line": 11,
"column": 1
},
{
"message": "Expected indentation of 8 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
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- prettier-ignore -->
<script lang="ts">
import(
"./foo.json",
{
assert
:
{
type
:
"json"
}
}
)
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion02-input-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- prettier-ignore -->
<script lang="ts">
import(
"./foo.json",
{
assert
:
{
type
:
"json"
}
}
)
</script>

<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion02-input-->
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 4 spaces but found 0 spaces.",
"line": 4,
"column": 1
},
{
"message": "Expected indentation of 6 spaces but found 0 spaces.",
"line": 5,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 6,
"column": 1
},
{
"message": "Expected indentation of 8 spaces but found 0 spaces.",
"line": 7,
"column": 1
},
{
"message": "Expected indentation of 6 spaces but found 0 spaces.",
"line": 8,
"column": 1
},
{
"message": "Expected indentation of 4 spaces but found 0 spaces.",
"line": 9,
"column": 1
}
]
Loading

0 comments on commit f31be21

Please sign in to comment.