Skip to content

Commit 0ee35d3

Browse files
authored
Update indent rule to support Class static block and typescript-eslint v5(rc) (#1619)
* Update indent rule to support typescript-eslint v5 * Update ts-static-block-01.vue * static block to es * fix job * ignore cache * use cache * ignore cache * use cache * remove cache
1 parent f8aa689 commit 0ee35d3

File tree

10 files changed

+187
-43
lines changed

10 files changed

+187
-43
lines changed

.circleci/config.yml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ workflows:
66
- node-v10
77
- eslint-v7
88
- eslint-v8
9+
- ts-eslint-v4
910
- node-v12
1011
- node-v14
1112
- lint
@@ -20,19 +21,19 @@ jobs:
2021
name: Versions
2122
command: npm version
2223
- checkout
23-
- restore_cache:
24-
keys:
25-
- v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
24+
# - restore_cache:
25+
# keys:
26+
# - v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
2627
- run:
2728
name: Install dependencies
2829
command: npm install
29-
- save_cache:
30-
key: v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
31-
paths:
32-
- node_modules
3330
- run:
3431
name: Test
3532
command: npm test
33+
# - save_cache:
34+
# key: v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
35+
# paths:
36+
# - node_modules
3637

3738
node-v8:
3839
docker:
@@ -53,9 +54,23 @@ jobs:
5354
name: Test
5455
command: npm test
5556
node-v10:
56-
<<: *node-base
5757
docker:
5858
- image: node:10
59+
steps:
60+
- run:
61+
name: Versions
62+
command: npm version
63+
- checkout
64+
- run:
65+
name: Install @typescript-eslint/parser@4
66+
command: |
67+
npm install @typescript-eslint/parser@^4
68+
- run:
69+
name: Install dependencies
70+
command: npm install
71+
- run:
72+
name: Test
73+
command: npm test
5974
eslint-v7:
6075
docker:
6176
- image: node:10
@@ -67,7 +82,7 @@ jobs:
6782
- run:
6883
name: Install eslint@7
6984
command: |
70-
npm install --save-exact eslint@7
85+
npm install eslint@7
7186
- run:
7287
name: Install dependencies
7388
command: npm install
@@ -85,7 +100,25 @@ jobs:
85100
- run:
86101
name: Install eslint@8
87102
command: |
88-
npm install --save-exact eslint@^8.0.0-0
103+
npm install eslint@^8.0.0-0
104+
- run:
105+
name: Install dependencies
106+
command: npm install
107+
- run:
108+
name: Test
109+
command: npm test
110+
ts-eslint-v4:
111+
docker:
112+
- image: node:14
113+
steps:
114+
- run:
115+
name: Versions
116+
command: npm version
117+
- checkout
118+
- run:
119+
name: Install @typescript-eslint/parser@4
120+
command: |
121+
npm install @typescript-eslint/parser@^4
89122
- run:
90123
name: Install dependencies
91124
command: npm install

lib/utils/indent-common.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
isNotClosingParenToken,
1717
isOpeningBraceToken,
1818
isClosingBraceToken,
19+
isNotOpeningBraceToken,
1920
isOpeningBracketToken,
2021
isClosingBracketToken,
2122
isSemicolonToken
@@ -1150,6 +1151,16 @@ module.exports.defineVisitor = function create(
11501151
1
11511152
)
11521153
},
1154+
StaticBlock(node) {
1155+
const firstToken = tokenStore.getFirstToken(node)
1156+
let next = tokenStore.getTokenAfter(firstToken)
1157+
while (next && isNotOpeningBraceToken(next)) {
1158+
setOffset(next, 0, firstToken)
1159+
next = tokenStore.getTokenAfter(next)
1160+
}
1161+
setOffset(next, 0, firstToken)
1162+
processNodeList(node.body, next, tokenStore.getLastToken(node), 1)
1163+
},
11531164
/** @param {BreakStatement | ContinueStatement | ReturnStatement | ThrowStatement} node */
11541165
'BreakStatement, ContinueStatement, ReturnStatement, ThrowStatement'(node) {
11551166
if (

lib/utils/indent-ts.js

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727
* @typedef {import('@typescript-eslint/types').TSESTree.TSConstructSignatureDeclaration} TSConstructSignatureDeclaration
2828
* @typedef {import('@typescript-eslint/types').TSESTree.TSImportEqualsDeclaration} TSImportEqualsDeclaration
2929
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractMethodDefinition} TSAbstractMethodDefinition
30-
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractClassProperty} TSAbstractClassProperty
30+
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractPropertyDefinition
3131
* @typedef {import('@typescript-eslint/types').TSESTree.TSEnumMember} TSEnumMember
3232
* @typedef {import('@typescript-eslint/types').TSESTree.TSPropertySignature} TSPropertySignature
3333
* @typedef {import('@typescript-eslint/types').TSESTree.TSIndexSignature} TSIndexSignature
@@ -50,12 +50,13 @@ const {
5050
* @typedef {import('@typescript-eslint/types').TSESTree.TSOptionalType} TSOptionalType
5151
* @typedef {import('@typescript-eslint/types').TSESTree.TSNonNullExpression} TSNonNullExpression
5252
* @typedef {import('@typescript-eslint/types').TSESTree.JSXChild} JSXChild
53+
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
5354
*
5455
*/
5556
/**
56-
* Perhaps this node will be deprecated in the future.
57-
* It was present in @typescript-eslint/parser@4.1.0.
58-
* @typedef {import('@typescript-eslint/types').TSESTree.ClassProperty} ClassProperty
57+
* Deprecated in @typescript-eslint/parser v5
58+
* @typedef {import('@typescript-eslint/types').TSESTree.PropertyDefinition} ClassProperty
59+
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractClassProperty
5960
*/
6061

6162
module.exports = {
@@ -203,6 +204,7 @@ function defineVisitor({
203204
* | TSConstructSignatureDeclaration
204205
* | TSImportEqualsDeclaration
205206
* | TSAbstractMethodDefinition
207+
* | TSAbstractPropertyDefinition
206208
* | TSAbstractClassProperty
207209
* | TSEnumMember
208210
* | ClassProperty
@@ -211,10 +213,80 @@ function defineVisitor({
211213
* | TSMethodSignature} node
212214
*/
213215
['TSTypeAliasDeclaration, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSImportEqualsDeclaration,' +
214-
'TSAbstractMethodDefinition, TSAbstractClassProperty, TSEnumMember, ClassProperty,' +
215-
'TSPropertySignature, TSIndexSignature, TSMethodSignature'](node) {
216+
'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
217+
'TSPropertySignature, TSIndexSignature, TSMethodSignature,' +
218+
// Deprecated in @typescript-eslint/parser v5
219+
'ClassProperty, TSAbstractClassProperty'](node) {
216220
processSemicolons(node)
217221
},
222+
/**
223+
* @param {TSESTreeNode} node
224+
*/
225+
// eslint-disable-next-line complexity -- ignore
226+
'*[type=/^TS/]'(node) {
227+
if (
228+
node.type !== 'TSAnyKeyword' &&
229+
node.type !== 'TSArrayType' &&
230+
node.type !== 'TSBigIntKeyword' &&
231+
node.type !== 'TSBooleanKeyword' &&
232+
node.type !== 'TSConditionalType' &&
233+
node.type !== 'TSConstructorType' &&
234+
node.type !== 'TSFunctionType' &&
235+
node.type !== 'TSImportType' &&
236+
node.type !== 'TSIndexedAccessType' &&
237+
node.type !== 'TSInferType' &&
238+
node.type !== 'TSIntersectionType' &&
239+
node.type !== 'TSIntrinsicKeyword' &&
240+
node.type !== 'TSLiteralType' &&
241+
node.type !== 'TSMappedType' &&
242+
node.type !== 'TSNamedTupleMember' &&
243+
node.type !== 'TSNeverKeyword' &&
244+
node.type !== 'TSNullKeyword' &&
245+
node.type !== 'TSNumberKeyword' &&
246+
node.type !== 'TSObjectKeyword' &&
247+
node.type !== 'TSOptionalType' &&
248+
node.type !== 'TSRestType' &&
249+
node.type !== 'TSStringKeyword' &&
250+
node.type !== 'TSSymbolKeyword' &&
251+
node.type !== 'TSTemplateLiteralType' &&
252+
node.type !== 'TSThisType' &&
253+
node.type !== 'TSTupleType' &&
254+
node.type !== 'TSTypeLiteral' &&
255+
node.type !== 'TSTypeOperator' &&
256+
node.type !== 'TSTypePredicate' &&
257+
node.type !== 'TSTypeQuery' &&
258+
node.type !== 'TSTypeReference' &&
259+
node.type !== 'TSUndefinedKeyword' &&
260+
node.type !== 'TSUnionType' &&
261+
node.type !== 'TSUnknownKeyword' &&
262+
node.type !== 'TSVoidKeyword'
263+
) {
264+
return
265+
}
266+
/** @type {TypeNode} */
267+
const typeNode = node
268+
if (/** @type {any} */ (typeNode.parent).type === 'TSParenthesizedType') {
269+
return
270+
}
271+
// Process parentheses.
272+
let leftToken = tokenStore.getTokenBefore(node)
273+
let rightToken = tokenStore.getTokenAfter(node)
274+
let firstToken = tokenStore.getFirstToken(node)
275+
276+
while (
277+
leftToken &&
278+
rightToken &&
279+
isOpeningParenToken(leftToken) &&
280+
isClosingParenToken(rightToken)
281+
) {
282+
setOffset(firstToken, 1, leftToken)
283+
setOffset(rightToken, 0, leftToken)
284+
285+
firstToken = leftToken
286+
leftToken = tokenStore.getTokenBefore(leftToken)
287+
rightToken = tokenStore.getTokenAfter(rightToken)
288+
}
289+
},
218290
/**
219291
* Process type annotation
220292
*
@@ -535,15 +607,6 @@ function defineVisitor({
535607
setOffset(typeTokens.firstToken, offset, firstToken)
536608
}
537609
},
538-
TSParenthesizedType(node) {
539-
// (T)
540-
processNodeList(
541-
[node.typeAnnotation],
542-
tokenStore.getFirstToken(node),
543-
tokenStore.getLastToken(node),
544-
1
545-
)
546-
},
547610
TSMappedType(node) {
548611
// {[key in foo]: bar}
549612
const leftBraceToken = tokenStore.getFirstToken(node)
@@ -1026,12 +1089,12 @@ function defineVisitor({
10261089
* // ^^^^^^^
10271090
* ```
10281091
*
1029-
* @param {TSAbstractMethodDefinition | TSAbstractClassProperty | TSEnumMember | ClassProperty} node
1092+
* @param {TSAbstractMethodDefinition | TSAbstractPropertyDefinition | TSEnumMember | TSAbstractClassProperty | ClassProperty} node
10301093
*
10311094
*/
1032-
'TSAbstractMethodDefinition, TSAbstractClassProperty, TSEnumMember, ClassProperty'(
1033-
node
1034-
) {
1095+
['TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
1096+
// Deprecated in @typescript-eslint/parser v5
1097+
'ClassProperty, TSAbstractClassProperty'](node) {
10351098
const { keyNode, valueNode } =
10361099
node.type === 'TSEnumMember'
10371100
? { keyNode: node.id, valueNode: node.initializer }
@@ -1275,6 +1338,21 @@ function defineVisitor({
12751338
setOffset(atToken, 0, tokenStore.getFirstToken(decorators[0]))
12761339
}
12771340
},
1341+
1342+
// ----------------------------------------------------------------------
1343+
// DEPRECATED NODES
1344+
// ----------------------------------------------------------------------
1345+
/** @param {any} node */
1346+
TSParenthesizedType(node) {
1347+
// Deprecated in @typescript-eslint/parser v5
1348+
// (T)
1349+
processNodeList(
1350+
[node.typeAnnotation],
1351+
tokenStore.getFirstToken(node),
1352+
tokenStore.getLastToken(node),
1353+
1
1354+
)
1355+
},
12781356
// ----------------------------------------------------------------------
12791357
// SINGLE TOKEN NODES
12801358
// ----------------------------------------------------------------------

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@types/natural-compare": "^1.4.0",
6565
"@types/node": "^13.13.5",
6666
"@types/semver": "^7.2.0",
67-
"@typescript-eslint/parser": "^4.28.0",
67+
"@typescript-eslint/parser": "^5.0.0-0",
6868
"@vuepress/plugin-pwa": "^1.4.1",
6969
"env-cmd": "^10.1.0",
7070
"eslint": "^7.0.0",

tests/fixtures/script-indent/ts-abstract-class-property-02.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ abstract class A {
66
1
77
;
88
abstract public b
9-
=
10-
's'
9+
// parser v5 does not parse value.
10+
// =
11+
// 's'
1112
;
1213
protected abstract c
13-
=
14-
i
14+
// parser v5 does not parse value.
15+
// =
16+
// i
1517
;
1618
}
1719
</script>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=4.30.0 || ^5.0.0-0" } }-->
2+
<script lang="ts">
3+
class Foo {
4+
static {
5+
processFn()
6+
}
7+
}
8+
</script>

tests/fixtures/script-indent/ts-type-annotation-03.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ class Foo {
1818
?
1919
:
2020
number;
21-
abstract absopt2
22-
?
23-
:
24-
number
25-
=
26-
42;
21+
// parser v5 does not parse value.
22+
// abstract absopt2
23+
// ?
24+
// :
25+
// number
26+
// =
27+
// 42;
2728
}
2829
</script>

typings/eslint-plugin-vue/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ declare global {
137137
type ClassBody = VAST.ClassBody
138138
type MethodDefinition = VAST.MethodDefinition
139139
type PropertyDefinition = VAST.PropertyDefinition
140+
type StaticBlock = VAST.StaticBlock
140141
type ModuleDeclaration = VAST.ModuleDeclaration
141142
type ImportDeclaration = VAST.ImportDeclaration
142143
type ExportNamedDeclaration = VAST.ExportNamedDeclaration

typings/eslint-plugin-vue/util-types/ast/ast.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ export type ESNodeListenerMap = {
333333
'MethodDefinition:exit': ES.MethodDefinition
334334
PropertyDefinition: ES.PropertyDefinition
335335
'PropertyDefinition:exit': ES.PropertyDefinition
336+
StaticBlock: ES.StaticBlock
337+
'StaticBlock:exit': ES.StaticBlock
336338
ImportDeclaration: ES.ImportDeclaration
337339
'ImportDeclaration:exit': ES.ImportDeclaration
338340
ExportNamedDeclaration: ES.ExportNamedDeclaration

0 commit comments

Comments
 (0)