From 356dfcf0ec3c47c3fcea8bb2b6542700e6cd531d Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Tue, 22 Nov 2022 20:51:41 +0900 Subject: [PATCH] support for AccessorProperty --- lib/utils/indent-ts.js | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/utils/indent-ts.js b/lib/utils/indent-ts.js index 446f15c2b..815235b10 100644 --- a/lib/utils/indent-ts.js +++ b/lib/utils/indent-ts.js @@ -29,6 +29,7 @@ const { isTypeNode } = require('./ts-ast-utils') * @typedef {import('@typescript-eslint/types').TSESTree.TSImportEqualsDeclaration} TSImportEqualsDeclaration * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractMethodDefinition} TSAbstractMethodDefinition * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractPropertyDefinition + * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractAccessorProperty} TSAbstractAccessorProperty * @typedef {import('@typescript-eslint/types').TSESTree.TSEnumMember} TSEnumMember * @typedef {import('@typescript-eslint/types').TSESTree.TSPropertySignature} TSPropertySignature * @typedef {import('@typescript-eslint/types').TSESTree.TSIndexSignature} TSIndexSignature @@ -206,15 +207,16 @@ function defineVisitor({ * | TSImportEqualsDeclaration * | TSAbstractMethodDefinition * | TSAbstractPropertyDefinition - * | TSAbstractClassProperty + * | TSAbstractAccessorProperty * | TSEnumMember - * | ClassProperty * | TSPropertySignature * | TSIndexSignature - * | TSMethodSignature} node + * | TSMethodSignature + * | ClassProperty + * | TSAbstractClassProperty} node */ ['TSTypeAliasDeclaration, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSImportEqualsDeclaration,' + - 'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' + + 'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSAbstractAccessorProperty, TSEnumMember,' + 'TSPropertySignature, TSIndexSignature, TSMethodSignature,' + // Deprecated in @typescript-eslint/parser v5 'ClassProperty, TSAbstractClassProperty'](node) { @@ -1073,10 +1075,10 @@ function defineVisitor({ * // ^^^^^^^ * ``` * - * @param {TSAbstractMethodDefinition | TSAbstractPropertyDefinition | TSEnumMember | TSAbstractClassProperty | ClassProperty} node + * @param {TSAbstractMethodDefinition | TSAbstractPropertyDefinition | TSAbstractAccessorProperty | TSEnumMember | TSAbstractClassProperty | ClassProperty} node * */ - ['TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' + + ['TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSAbstractAccessorProperty, TSEnumMember,' + // Deprecated in @typescript-eslint/parser v5 'ClassProperty, TSAbstractClassProperty'](node) { const { keyNode, valueNode } = @@ -1335,6 +1337,42 @@ function defineVisitor({ setOffset(atToken, 0, tokenStore.getFirstToken(decorators[0])) } }, + AccessorProperty(node) { + const keyNode = node.key + const valueNode = node.value + const firstToken = tokenStore.getFirstToken(node) + const keyTokens = getFirstAndLastTokens(keyNode) + const prefixTokens = tokenStore.getTokensBetween( + firstToken, + keyTokens.firstToken + ) + if (node.computed) { + prefixTokens.pop() // pop [ + } + setOffset(prefixTokens, 0, firstToken) + let lastKeyToken + if (node.computed) { + const leftBracketToken = tokenStore.getTokenBefore(keyTokens.firstToken) + const rightBracketToken = (lastKeyToken = tokenStore.getTokenAfter( + keyTokens.lastToken + )) + setOffset(leftBracketToken, 0, firstToken) + processNodeList([keyNode], leftBracketToken, rightBracketToken, 1) + } else { + setOffset(keyTokens.firstToken, 0, firstToken) + lastKeyToken = keyTokens.lastToken + } + + if (valueNode != null) { + const initToken = tokenStore.getFirstToken(valueNode) + setOffset( + [...tokenStore.getTokensBetween(lastKeyToken, initToken), initToken], + 1, + lastKeyToken + ) + } + processSemicolons(node) + }, ImportAttribute(node) { const firstToken = tokenStore.getFirstToken(node) const keyTokens = getFirstAndLastTokens(node.key)