Skip to content

Commit

Permalink
Update indent rule to support Class static block and typescript-eslin…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
ota-meshi committed Sep 5, 2021
1 parent f8aa689 commit 0ee35d3
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 43 deletions.
53 changes: 43 additions & 10 deletions .circleci/config.yml
Expand Up @@ -6,6 +6,7 @@ workflows:
- node-v10
- eslint-v7
- eslint-v8
- ts-eslint-v4
- node-v12
- node-v14
- lint
Expand All @@ -20,19 +21,19 @@ jobs:
name: Versions
command: npm version
- checkout
- restore_cache:
keys:
- v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
# - restore_cache:
# keys:
# - v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
- run:
name: Install dependencies
command: npm install
- save_cache:
key: v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
paths:
- node_modules
- run:
name: Test
command: npm test
# - save_cache:
# key: v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
# paths:
# - node_modules

node-v8:
docker:
Expand All @@ -53,9 +54,23 @@ jobs:
name: Test
command: npm test
node-v10:
<<: *node-base
docker:
- image: node:10
steps:
- run:
name: Versions
command: npm version
- checkout
- run:
name: Install @typescript-eslint/parser@4
command: |
npm install @typescript-eslint/parser@^4
- run:
name: Install dependencies
command: npm install
- run:
name: Test
command: npm test
eslint-v7:
docker:
- image: node:10
Expand All @@ -67,7 +82,7 @@ jobs:
- run:
name: Install eslint@7
command: |
npm install --save-exact eslint@7
npm install eslint@7
- run:
name: Install dependencies
command: npm install
Expand All @@ -85,7 +100,25 @@ jobs:
- run:
name: Install eslint@8
command: |
npm install --save-exact eslint@^8.0.0-0
npm install eslint@^8.0.0-0
- run:
name: Install dependencies
command: npm install
- run:
name: Test
command: npm test
ts-eslint-v4:
docker:
- image: node:14
steps:
- run:
name: Versions
command: npm version
- checkout
- run:
name: Install @typescript-eslint/parser@4
command: |
npm install @typescript-eslint/parser@^4
- run:
name: Install dependencies
command: npm install
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/indent-common.js
Expand Up @@ -16,6 +16,7 @@ const {
isNotClosingParenToken,
isOpeningBraceToken,
isClosingBraceToken,
isNotOpeningBraceToken,
isOpeningBracketToken,
isClosingBracketToken,
isSemicolonToken
Expand Down Expand Up @@ -1150,6 +1151,16 @@ module.exports.defineVisitor = function create(
1
)
},
StaticBlock(node) {
const firstToken = tokenStore.getFirstToken(node)
let next = tokenStore.getTokenAfter(firstToken)
while (next && isNotOpeningBraceToken(next)) {
setOffset(next, 0, firstToken)
next = tokenStore.getTokenAfter(next)
}
setOffset(next, 0, firstToken)
processNodeList(node.body, next, tokenStore.getLastToken(node), 1)
},
/** @param {BreakStatement | ContinueStatement | ReturnStatement | ThrowStatement} node */
'BreakStatement, ContinueStatement, ReturnStatement, ThrowStatement'(node) {
if (
Expand Down
116 changes: 97 additions & 19 deletions lib/utils/indent-ts.js
Expand Up @@ -27,7 +27,7 @@ const {
* @typedef {import('@typescript-eslint/types').TSESTree.TSConstructSignatureDeclaration} TSConstructSignatureDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.TSImportEqualsDeclaration} TSImportEqualsDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractMethodDefinition} TSAbstractMethodDefinition
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractClassProperty} TSAbstractClassProperty
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractPropertyDefinition
* @typedef {import('@typescript-eslint/types').TSESTree.TSEnumMember} TSEnumMember
* @typedef {import('@typescript-eslint/types').TSESTree.TSPropertySignature} TSPropertySignature
* @typedef {import('@typescript-eslint/types').TSESTree.TSIndexSignature} TSIndexSignature
Expand All @@ -50,12 +50,13 @@ const {
* @typedef {import('@typescript-eslint/types').TSESTree.TSOptionalType} TSOptionalType
* @typedef {import('@typescript-eslint/types').TSESTree.TSNonNullExpression} TSNonNullExpression
* @typedef {import('@typescript-eslint/types').TSESTree.JSXChild} JSXChild
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
*
*/
/**
* Perhaps this node will be deprecated in the future.
* It was present in @typescript-eslint/parser@4.1.0.
* @typedef {import('@typescript-eslint/types').TSESTree.ClassProperty} ClassProperty
* Deprecated in @typescript-eslint/parser v5
* @typedef {import('@typescript-eslint/types').TSESTree.PropertyDefinition} ClassProperty
* @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractClassProperty
*/

module.exports = {
Expand Down Expand Up @@ -203,6 +204,7 @@ function defineVisitor({
* | TSConstructSignatureDeclaration
* | TSImportEqualsDeclaration
* | TSAbstractMethodDefinition
* | TSAbstractPropertyDefinition
* | TSAbstractClassProperty
* | TSEnumMember
* | ClassProperty
Expand All @@ -211,10 +213,80 @@ function defineVisitor({
* | TSMethodSignature} node
*/
['TSTypeAliasDeclaration, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSImportEqualsDeclaration,' +
'TSAbstractMethodDefinition, TSAbstractClassProperty, TSEnumMember, ClassProperty,' +
'TSPropertySignature, TSIndexSignature, TSMethodSignature'](node) {
'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
'TSPropertySignature, TSIndexSignature, TSMethodSignature,' +
// Deprecated in @typescript-eslint/parser v5
'ClassProperty, TSAbstractClassProperty'](node) {
processSemicolons(node)
},
/**
* @param {TSESTreeNode} node
*/
// eslint-disable-next-line complexity -- ignore
'*[type=/^TS/]'(node) {
if (
node.type !== 'TSAnyKeyword' &&
node.type !== 'TSArrayType' &&
node.type !== 'TSBigIntKeyword' &&
node.type !== 'TSBooleanKeyword' &&
node.type !== 'TSConditionalType' &&
node.type !== 'TSConstructorType' &&
node.type !== 'TSFunctionType' &&
node.type !== 'TSImportType' &&
node.type !== 'TSIndexedAccessType' &&
node.type !== 'TSInferType' &&
node.type !== 'TSIntersectionType' &&
node.type !== 'TSIntrinsicKeyword' &&
node.type !== 'TSLiteralType' &&
node.type !== 'TSMappedType' &&
node.type !== 'TSNamedTupleMember' &&
node.type !== 'TSNeverKeyword' &&
node.type !== 'TSNullKeyword' &&
node.type !== 'TSNumberKeyword' &&
node.type !== 'TSObjectKeyword' &&
node.type !== 'TSOptionalType' &&
node.type !== 'TSRestType' &&
node.type !== 'TSStringKeyword' &&
node.type !== 'TSSymbolKeyword' &&
node.type !== 'TSTemplateLiteralType' &&
node.type !== 'TSThisType' &&
node.type !== 'TSTupleType' &&
node.type !== 'TSTypeLiteral' &&
node.type !== 'TSTypeOperator' &&
node.type !== 'TSTypePredicate' &&
node.type !== 'TSTypeQuery' &&
node.type !== 'TSTypeReference' &&
node.type !== 'TSUndefinedKeyword' &&
node.type !== 'TSUnionType' &&
node.type !== 'TSUnknownKeyword' &&
node.type !== 'TSVoidKeyword'
) {
return
}
/** @type {TypeNode} */
const typeNode = node
if (/** @type {any} */ (typeNode.parent).type === 'TSParenthesizedType') {
return
}
// Process parentheses.
let leftToken = tokenStore.getTokenBefore(node)
let rightToken = tokenStore.getTokenAfter(node)
let firstToken = tokenStore.getFirstToken(node)

while (
leftToken &&
rightToken &&
isOpeningParenToken(leftToken) &&
isClosingParenToken(rightToken)
) {
setOffset(firstToken, 1, leftToken)
setOffset(rightToken, 0, leftToken)

firstToken = leftToken
leftToken = tokenStore.getTokenBefore(leftToken)
rightToken = tokenStore.getTokenAfter(rightToken)
}
},
/**
* Process type annotation
*
Expand Down Expand Up @@ -535,15 +607,6 @@ function defineVisitor({
setOffset(typeTokens.firstToken, offset, firstToken)
}
},
TSParenthesizedType(node) {
// (T)
processNodeList(
[node.typeAnnotation],
tokenStore.getFirstToken(node),
tokenStore.getLastToken(node),
1
)
},
TSMappedType(node) {
// {[key in foo]: bar}
const leftBraceToken = tokenStore.getFirstToken(node)
Expand Down Expand Up @@ -1026,12 +1089,12 @@ function defineVisitor({
* // ^^^^^^^
* ```
*
* @param {TSAbstractMethodDefinition | TSAbstractClassProperty | TSEnumMember | ClassProperty} node
* @param {TSAbstractMethodDefinition | TSAbstractPropertyDefinition | TSEnumMember | TSAbstractClassProperty | ClassProperty} node
*
*/
'TSAbstractMethodDefinition, TSAbstractClassProperty, TSEnumMember, ClassProperty'(
node
) {
['TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
// Deprecated in @typescript-eslint/parser v5
'ClassProperty, TSAbstractClassProperty'](node) {
const { keyNode, valueNode } =
node.type === 'TSEnumMember'
? { keyNode: node.id, valueNode: node.initializer }
Expand Down Expand Up @@ -1275,6 +1338,21 @@ function defineVisitor({
setOffset(atToken, 0, tokenStore.getFirstToken(decorators[0]))
}
},

// ----------------------------------------------------------------------
// DEPRECATED NODES
// ----------------------------------------------------------------------
/** @param {any} node */
TSParenthesizedType(node) {
// Deprecated in @typescript-eslint/parser v5
// (T)
processNodeList(
[node.typeAnnotation],
tokenStore.getFirstToken(node),
tokenStore.getLastToken(node),
1
)
},
// ----------------------------------------------------------------------
// SINGLE TOKEN NODES
// ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -64,7 +64,7 @@
"@types/natural-compare": "^1.4.0",
"@types/node": "^13.13.5",
"@types/semver": "^7.2.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/parser": "^5.0.0-0",
"@vuepress/plugin-pwa": "^1.4.1",
"env-cmd": "^10.1.0",
"eslint": "^7.0.0",
Expand Down
10 changes: 6 additions & 4 deletions tests/fixtures/script-indent/ts-abstract-class-property-02.vue
Expand Up @@ -6,12 +6,14 @@ abstract class A {
1
;
abstract public b
=
's'
// parser v5 does not parse value.
// =
// 's'
;
protected abstract c
=
i
// parser v5 does not parse value.
// =
// i
;
}
</script>
8 changes: 8 additions & 0 deletions tests/fixtures/script-indent/ts-static-block-01.vue
@@ -0,0 +1,8 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=4.30.0 || ^5.0.0-0" } }-->
<script lang="ts">
class Foo {
static {
processFn()
}
}
</script>
13 changes: 7 additions & 6 deletions tests/fixtures/script-indent/ts-type-annotation-03.vue
Expand Up @@ -18,11 +18,12 @@ class Foo {
?
:
number;
abstract absopt2
?
:
number
=
42;
// parser v5 does not parse value.
// abstract absopt2
// ?
// :
// number
// =
// 42;
}
</script>
1 change: 1 addition & 0 deletions typings/eslint-plugin-vue/global.d.ts
Expand Up @@ -137,6 +137,7 @@ declare global {
type ClassBody = VAST.ClassBody
type MethodDefinition = VAST.MethodDefinition
type PropertyDefinition = VAST.PropertyDefinition
type StaticBlock = VAST.StaticBlock
type ModuleDeclaration = VAST.ModuleDeclaration
type ImportDeclaration = VAST.ImportDeclaration
type ExportNamedDeclaration = VAST.ExportNamedDeclaration
Expand Down
2 changes: 2 additions & 0 deletions typings/eslint-plugin-vue/util-types/ast/ast.ts
Expand Up @@ -333,6 +333,8 @@ export type ESNodeListenerMap = {
'MethodDefinition:exit': ES.MethodDefinition
PropertyDefinition: ES.PropertyDefinition
'PropertyDefinition:exit': ES.PropertyDefinition
StaticBlock: ES.StaticBlock
'StaticBlock:exit': ES.StaticBlock
ImportDeclaration: ES.ImportDeclaration
'ImportDeclaration:exit': ES.ImportDeclaration
ExportNamedDeclaration: ES.ExportNamedDeclaration
Expand Down

0 comments on commit 0ee35d3

Please sign in to comment.