From eec86d78835fef9871552e795d650a36de47bc7d Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 30 Aug 2021 13:34:09 +0900 Subject: [PATCH 1/9] Update indent rule to support typescript-eslint v5 --- .circleci/config.yml | 23 +++- lib/utils/indent-ts.js | 129 +++++++++++++++--- package.json | 2 +- .../ts-abstract-class-property-02.vue | 10 +- .../script-indent/ts-static-block-01.vue | 8 ++ .../script-indent/ts-type-annotation-03.vue | 13 +- 6 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 tests/fixtures/script-indent/ts-static-block-01.vue diff --git a/.circleci/config.yml b/.circleci/config.yml index b80032ae8..a4e370723 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,6 +6,7 @@ workflows: - node-v10 - eslint-v7 - eslint-v8 + - ts-eslint-v4 - node-v12 - node-v14 - lint @@ -67,7 +68,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 @@ -85,7 +86,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 diff --git a/lib/utils/indent-ts.js b/lib/utils/indent-ts.js index 9afcdb34f..fec0effbd 100644 --- a/lib/utils/indent-ts.js +++ b/lib/utils/indent-ts.js @@ -10,7 +10,8 @@ const { isOpeningBraceToken, isNotClosingParenToken, isClosingBracketToken, - isOpeningBracketToken + isOpeningBracketToken, + isNotOpeningBraceToken } = require('eslint-utils') /** @@ -27,7 +28,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 @@ -50,12 +51,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 = { @@ -203,6 +205,7 @@ function defineVisitor({ * | TSConstructSignatureDeclaration * | TSImportEqualsDeclaration * | TSAbstractMethodDefinition + * | TSAbstractPropertyDefinition * | TSAbstractClassProperty * | TSEnumMember * | ClassProperty @@ -211,10 +214,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 * @@ -535,15 +608,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) @@ -1026,12 +1090,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 } @@ -1275,6 +1339,31 @@ function defineVisitor({ setOffset(atToken, 0, tokenStore.getFirstToken(decorators[0])) } }, + 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) + }, + + // ---------------------------------------------------------------------- + // 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 // ---------------------------------------------------------------------- diff --git a/package.json b/package.json index 661acff1e..7076d867e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/fixtures/script-indent/ts-abstract-class-property-02.vue b/tests/fixtures/script-indent/ts-abstract-class-property-02.vue index 9e3214a0f..6b10b8033 100644 --- a/tests/fixtures/script-indent/ts-abstract-class-property-02.vue +++ b/tests/fixtures/script-indent/ts-abstract-class-property-02.vue @@ -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 ; } diff --git a/tests/fixtures/script-indent/ts-static-block-01.vue b/tests/fixtures/script-indent/ts-static-block-01.vue new file mode 100644 index 000000000..dfbebfb5a --- /dev/null +++ b/tests/fixtures/script-indent/ts-static-block-01.vue @@ -0,0 +1,8 @@ + + diff --git a/tests/fixtures/script-indent/ts-type-annotation-03.vue b/tests/fixtures/script-indent/ts-type-annotation-03.vue index ed1bab907..6569f4e53 100644 --- a/tests/fixtures/script-indent/ts-type-annotation-03.vue +++ b/tests/fixtures/script-indent/ts-type-annotation-03.vue @@ -18,11 +18,12 @@ class Foo { ? : number; - abstract absopt2 - ? - : - number - = - 42; +// parser v5 does not parse value. +// abstract absopt2 +// ? +// : +// number +// = +// 42; } From 54f72468f6e58e5f1affb25bac8768cd3c4d4763 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Mon, 30 Aug 2021 16:49:09 +0900 Subject: [PATCH 2/9] Update ts-static-block-01.vue --- tests/fixtures/script-indent/ts-static-block-01.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/script-indent/ts-static-block-01.vue b/tests/fixtures/script-indent/ts-static-block-01.vue index dfbebfb5a..522271628 100644 --- a/tests/fixtures/script-indent/ts-static-block-01.vue +++ b/tests/fixtures/script-indent/ts-static-block-01.vue @@ -1,4 +1,4 @@ - +