Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c62afea

Browse files
shixianqin9romise
andauthored
feat(indent): support TSTypeAliasDeclaration (#796)
Co-authored-by: Vida Xie <[email protected]> Co-authored-by: Vida Xie <[email protected]>
1 parent 4d44889 commit c62afea

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

packages/eslint-plugin/rules/indent/indent._ts_.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,13 @@ const map2 = Object.keys(map)
827827
`,
828828
options: [2],
829829
},
830+
$`
831+
type Foo = string
832+
declare type Foo = number
833+
namespace Foo {
834+
type Bar = boolean
835+
}
836+
`,
830837
{
831838
code: $`
832839
using a = foo(),
@@ -2115,6 +2122,27 @@ class Foo {
21152122
},
21162123
],
21172124
},
2125+
{
2126+
code: $`
2127+
type A = number
2128+
declare type B = number
2129+
namespace Foo {
2130+
declare type C = number
2131+
}
2132+
`,
2133+
output: $`
2134+
type A = number
2135+
declare type B = number
2136+
namespace Foo {
2137+
declare type C = number
2138+
}
2139+
`,
2140+
errors: [
2141+
{ messageId: 'wrongIndentation', data: { expected: '0 spaces', actual: 1 }, line: 1, column: 1 },
2142+
{ messageId: 'wrongIndentation', data: { expected: '0 spaces', actual: 2 }, line: 2, column: 1 },
2143+
{ messageId: 'wrongIndentation', data: { expected: '4 spaces', actual: 6 }, line: 4, column: 1 },
2144+
],
2145+
},
21182146
{
21192147
code: $`
21202148
using a = foo(),

packages/eslint-plugin/rules/indent/indent.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const KNOWN_NODES = new Set([
149149
AST_NODE_TYPES.TSRestType,
150150
AST_NODE_TYPES.TSThisType,
151151
AST_NODE_TYPES.TSTupleType,
152+
AST_NODE_TYPES.TSTypeAliasDeclaration,
152153
AST_NODE_TYPES.TSTypeAnnotation,
153154
AST_NODE_TYPES.TSTypeLiteral,
154155
AST_NODE_TYPES.TSTypeOperator,
@@ -1123,6 +1124,15 @@ export default createRule<RuleOptions, MessageIds>({
11231124

11241125
const ignoredNodeFirstTokens = new Set<Token>()
11251126

1127+
function checkDeclarator(node: Tree.VariableDeclarator | Tree.TSTypeAliasDeclaration, equalOperator: Token) {
1128+
const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator)!
1129+
1130+
offsets.ignoreToken(equalOperator)
1131+
offsets.ignoreToken(tokenAfterOperator)
1132+
offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1)
1133+
offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0)
1134+
}
1135+
11261136
function checkArrayLikeNode(node: Tree.ArrayExpression | Tree.ArrayPattern | Tree.TSTupleType) {
11271137
const elementList = node.type === AST_NODE_TYPES.TSTupleType ? node.elementTypes : node.elements
11281138
const openingBracket = sourceCode.getFirstToken(node)!
@@ -1815,12 +1825,8 @@ export default createRule<RuleOptions, MessageIds>({
18151825
VariableDeclarator(node) {
18161826
if (node.init) {
18171827
const equalOperator = sourceCode.getTokenBefore(node.init, isNotOpeningParenToken)!
1818-
const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator)!
18191828

1820-
offsets.ignoreToken(equalOperator)
1821-
offsets.ignoreToken(tokenAfterOperator)
1822-
offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1)
1823-
offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0)
1829+
checkDeclarator(node, equalOperator)
18241830
}
18251831
},
18261832

@@ -1953,6 +1959,12 @@ export default createRule<RuleOptions, MessageIds>({
19531959
checkMemberExpression(node, node.object, node.property)
19541960
},
19551961

1962+
TSTypeAliasDeclaration(node) {
1963+
const equalOperator = sourceCode.getTokenBefore(node.typeAnnotation, isNotOpeningParenToken)!
1964+
1965+
checkDeclarator(node, equalOperator)
1966+
},
1967+
19561968
'TSTupleType': checkArrayLikeNode,
19571969

19581970
TSEnumDeclaration(node) {

0 commit comments

Comments
 (0)