From 65f6c55788f1391a995a99e8dce1b69de0a7244f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 28 Aug 2021 14:12:20 -0700 Subject: [PATCH] feat: align class property representation with ESTree Half of #3430 Closes #3077 --- packages/ast-spec/src/ast-node-types.ts | 4 +- ...pertyBase.ts => PropertyDefinitionBase.ts} | 8 +- .../src/element/ClassProperty/spec.ts | 19 ---- .../src/element/PropertyDefinition/spec.ts | 19 ++++ .../element/TSAbstractClassProperty/spec.ts | 21 ---- .../TSAbstractPropertyDefinition/spec.ts | 21 ++++ packages/ast-spec/src/element/spec.ts | 4 +- packages/ast-spec/src/unions/ClassElement.ts | 8 +- packages/ast-spec/src/unions/Node.ts | 8 +- .../src/rules/class-literal-property-style.ts | 2 +- .../rules/explicit-member-accessibility.ts | 26 +++-- .../rules/explicit-module-boundary-types.ts | 2 +- .../src/rules/indent-new-do-not-use/index.ts | 4 +- packages/eslint-plugin/src/rules/indent.ts | 12 +-- .../src/rules/member-ordering.ts | 10 +- .../src/rules/naming-convention.ts | 26 ++--- .../eslint-plugin/src/rules/no-extra-semi.ts | 8 +- .../src/rules/no-inferrable-types.ts | 8 +- .../src/rules/no-invalid-this.ts | 4 +- .../src/rules/no-invalid-void-type.ts | 2 +- .../src/rules/no-magic-numbers.ts | 6 +- .../src/rules/no-unsafe-assignment.ts | 4 +- .../src/rules/prefer-return-this-type.ts | 4 +- packages/eslint-plugin/src/rules/quotes.ts | 4 +- packages/eslint-plugin/src/rules/semi.ts | 4 +- .../src/rules/space-infix-ops.ts | 6 +- packages/eslint-plugin/src/rules/typedef.ts | 2 +- .../src/util/explicitReturnTypeUtils.ts | 14 +-- packages/eslint-plugin/src/util/misc.ts | 6 +- .../tests/rules/indent/indent.test.ts | 2 +- .../tests/rules/no-extra-semi.test.ts | 99 ++++++++++++++--- .../eslint-plugin/tests/rules/quotes.test.ts | 8 +- .../src/ast-utils/predicates.ts | 4 +- .../experimental-utils/src/ts-eslint/Rule.ts | 8 +- packages/scope-manager/src/ScopeManager.ts | 10 ++ .../src/referencer/ClassVisitor.ts | 30 ++++-- .../src/scope/ClassFieldInitializerScope.ts | 28 +++++ packages/scope-manager/src/scope/Scope.ts | 2 + packages/scope-manager/src/scope/ScopeBase.ts | 3 +- packages/scope-manager/src/scope/ScopeType.ts | 1 + packages/scope-manager/src/scope/index.ts | 1 + .../tests/eslint-scope/class-fields.test.ts | 101 ++++++++++++++++++ .../class/declaration/computed-member.ts.shot | 13 ++- .../fixtures/class/declaration/properties.ts | 6 +- .../class/declaration/properties.ts.shot | 94 ++++++++++++---- .../class/expression/computed-member.ts.shot | 13 ++- .../fixtures/class/expression/properties.ts | 4 +- .../class/expression/properties.ts.shot | 78 +++++++++++--- packages/scope-manager/tests/util/expect.ts | 7 ++ packages/typescript-estree/src/convert.ts | 6 +- .../src/ts-estree/estree-to-ts-node-types.ts | 4 +- .../tests/ast-alignment/parse.ts | 9 +- .../tests/ast-alignment/utils.ts | 6 +- .../__snapshots__/semanticInfo.test.ts.snap | 2 +- .../tests/lib/semanticInfo.test.ts | 2 +- ...class-with-abstract-properties.src.ts.shot | 4 +- ...ith-abstract-readonly-property.src.ts.shot | 2 +- ...-class-with-declare-properties.src.ts.shot | 10 +- ...t-class-with-override-property.src.ts.shot | 2 +- .../basics/class-static-blocks.src.ts.shot | 2 +- ...s-with-accessibility-modifiers.src.ts.shot | 4 +- .../class-with-declare-properties.src.ts.shot | 14 +-- ...class-with-definite-assignment.src.ts.shot | 2 +- ...ith-optional-computed-property.src.ts.shot | 2 +- ...class-with-optional-properties.src.ts.shot | 18 ++-- ...th-optional-property-undefined.src.ts.shot | 2 +- .../class-with-override-property.src.ts.shot | 2 +- .../class-with-property-function.src.ts.shot | 4 +- .../class-with-property-values.src.ts.shot | 10 +- .../class-with-readonly-property.src.ts.shot | 2 +- ...object-with-escaped-properties.src.ts.shot | 2 +- .../type-assertion-in-method.src.ts.shot | 2 +- ...assertion-with-guard-in-method.src.ts.shot | 2 +- .../basics/type-guard-in-method.src.ts.shot | 2 +- .../type-reference-comments.src.ts.shot | 2 +- ...orator-factory-instance-member.src.ts.shot | 4 +- ...ecorator-factory-static-member.src.ts.shot | 4 +- ...erty-decorator-instance-member.src.ts.shot | 4 +- ...operty-decorator-static-member.src.ts.shot | 4 +- .../types/this-type-expanded.src.ts.shot | 2 +- packages/visitor-keys/src/visitor-keys.ts | 4 +- 81 files changed, 651 insertions(+), 268 deletions(-) rename packages/ast-spec/src/base/{ClassPropertyBase.ts => PropertyDefinitionBase.ts} (77%) delete mode 100644 packages/ast-spec/src/element/ClassProperty/spec.ts create mode 100644 packages/ast-spec/src/element/PropertyDefinition/spec.ts delete mode 100644 packages/ast-spec/src/element/TSAbstractClassProperty/spec.ts create mode 100644 packages/ast-spec/src/element/TSAbstractPropertyDefinition/spec.ts create mode 100644 packages/scope-manager/src/scope/ClassFieldInitializerScope.ts create mode 100644 packages/scope-manager/tests/eslint-scope/class-fields.test.ts diff --git a/packages/ast-spec/src/ast-node-types.ts b/packages/ast-spec/src/ast-node-types.ts index 9a38d477acfc..3f6b2a81082a 100644 --- a/packages/ast-spec/src/ast-node-types.ts +++ b/packages/ast-spec/src/ast-node-types.ts @@ -14,7 +14,6 @@ export enum AST_NODE_TYPES { ClassBody = 'ClassBody', ClassDeclaration = 'ClassDeclaration', ClassExpression = 'ClassExpression', - ClassProperty = 'ClassProperty', ConditionalExpression = 'ConditionalExpression', ContinueStatement = 'ContinueStatement', DebuggerStatement = 'DebuggerStatement', @@ -64,6 +63,7 @@ export enum AST_NODE_TYPES { ObjectPattern = 'ObjectPattern', Program = 'Program', Property = 'Property', + PropertyDefinition = 'PropertyDefinition', RestElement = 'RestElement', ReturnStatement = 'ReturnStatement', SequenceExpression = 'SequenceExpression', @@ -88,9 +88,9 @@ export enum AST_NODE_TYPES { /** * TS-prefixed nodes */ - TSAbstractClassProperty = 'TSAbstractClassProperty', TSAbstractKeyword = 'TSAbstractKeyword', TSAbstractMethodDefinition = 'TSAbstractMethodDefinition', + TSAbstractPropertyDefinition = 'TSAbstractPropertyDefinition', TSAnyKeyword = 'TSAnyKeyword', TSArrayType = 'TSArrayType', TSAsExpression = 'TSAsExpression', diff --git a/packages/ast-spec/src/base/ClassPropertyBase.ts b/packages/ast-spec/src/base/PropertyDefinitionBase.ts similarity index 77% rename from packages/ast-spec/src/base/ClassPropertyBase.ts rename to packages/ast-spec/src/base/PropertyDefinitionBase.ts index 7717e02c4ca3..a70a21479ae7 100644 --- a/packages/ast-spec/src/base/ClassPropertyBase.ts +++ b/packages/ast-spec/src/base/PropertyDefinitionBase.ts @@ -9,7 +9,7 @@ import type { import type { Accessibility } from './Accessibility'; import type { BaseNode } from './BaseNode'; -interface ClassPropertyBase extends BaseNode { +interface PropertyDefinitionBase extends BaseNode { key: PropertyName; value: Expression | null; computed: boolean; @@ -24,12 +24,14 @@ interface ClassPropertyBase extends BaseNode { override?: boolean; } -export interface ClassPropertyComputedNameBase extends ClassPropertyBase { +export interface PropertyDefinitionComputedNameBase + extends PropertyDefinitionBase { key: PropertyNameComputed; computed: true; } -export interface ClassPropertyNonComputedNameBase extends ClassPropertyBase { +export interface PropertyDefinitionNonComputedNameBase + extends PropertyDefinitionBase { key: PropertyNameNonComputed; computed: false; } diff --git a/packages/ast-spec/src/element/ClassProperty/spec.ts b/packages/ast-spec/src/element/ClassProperty/spec.ts deleted file mode 100644 index 29fe75aa84e6..000000000000 --- a/packages/ast-spec/src/element/ClassProperty/spec.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { AST_NODE_TYPES } from '../../ast-node-types'; -import type { - ClassPropertyComputedNameBase, - ClassPropertyNonComputedNameBase, -} from '../../base/ClassPropertyBase'; - -export interface ClassPropertyComputedName - extends ClassPropertyComputedNameBase { - type: AST_NODE_TYPES.ClassProperty; -} - -export interface ClassPropertyNonComputedName - extends ClassPropertyNonComputedNameBase { - type: AST_NODE_TYPES.ClassProperty; -} - -export type ClassProperty = - | ClassPropertyComputedName - | ClassPropertyNonComputedName; diff --git a/packages/ast-spec/src/element/PropertyDefinition/spec.ts b/packages/ast-spec/src/element/PropertyDefinition/spec.ts new file mode 100644 index 000000000000..4f79c2eb8345 --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/spec.ts @@ -0,0 +1,19 @@ +import type { AST_NODE_TYPES } from '../../ast-node-types'; +import type { + PropertyDefinitionComputedNameBase, + PropertyDefinitionNonComputedNameBase, +} from '../../base/PropertyDefinitionBase'; + +export interface PropertyDefinitionComputedName + extends PropertyDefinitionComputedNameBase { + type: AST_NODE_TYPES.PropertyDefinition; +} + +export interface PropertyDefinitionNonComputedName + extends PropertyDefinitionNonComputedNameBase { + type: AST_NODE_TYPES.PropertyDefinition; +} + +export type PropertyDefinition = + | PropertyDefinitionComputedName + | PropertyDefinitionNonComputedName; diff --git a/packages/ast-spec/src/element/TSAbstractClassProperty/spec.ts b/packages/ast-spec/src/element/TSAbstractClassProperty/spec.ts deleted file mode 100644 index a7f1dc56b2d5..000000000000 --- a/packages/ast-spec/src/element/TSAbstractClassProperty/spec.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { AST_NODE_TYPES } from '../../ast-node-types'; -import type { - ClassPropertyComputedNameBase, - ClassPropertyNonComputedNameBase, -} from '../../base/ClassPropertyBase'; - -export interface TSAbstractClassPropertyComputedName - extends ClassPropertyComputedNameBase { - type: AST_NODE_TYPES.TSAbstractClassProperty; - value: null; -} - -export interface TSAbstractClassPropertyNonComputedName - extends ClassPropertyNonComputedNameBase { - type: AST_NODE_TYPES.TSAbstractClassProperty; - value: null; -} - -export type TSAbstractClassProperty = - | TSAbstractClassPropertyComputedName - | TSAbstractClassPropertyNonComputedName; diff --git a/packages/ast-spec/src/element/TSAbstractPropertyDefinition/spec.ts b/packages/ast-spec/src/element/TSAbstractPropertyDefinition/spec.ts new file mode 100644 index 000000000000..c7032669fc9f --- /dev/null +++ b/packages/ast-spec/src/element/TSAbstractPropertyDefinition/spec.ts @@ -0,0 +1,21 @@ +import type { AST_NODE_TYPES } from '../../ast-node-types'; +import type { + PropertyDefinitionComputedNameBase, + PropertyDefinitionNonComputedNameBase, +} from '../../base/PropertyDefinitionBase'; + +export interface TSAbstractPropertyDefinitionComputedName + extends PropertyDefinitionComputedNameBase { + type: AST_NODE_TYPES.TSAbstractPropertyDefinition; + value: null; +} + +export interface TSAbstractPropertyDefinitionNonComputedName + extends PropertyDefinitionNonComputedNameBase { + type: AST_NODE_TYPES.TSAbstractPropertyDefinition; + value: null; +} + +export type TSAbstractPropertyDefinition = + | TSAbstractPropertyDefinitionComputedName + | TSAbstractPropertyDefinitionNonComputedName; diff --git a/packages/ast-spec/src/element/spec.ts b/packages/ast-spec/src/element/spec.ts index a31caecced39..77ed3f2f283d 100644 --- a/packages/ast-spec/src/element/spec.ts +++ b/packages/ast-spec/src/element/spec.ts @@ -1,9 +1,9 @@ -export * from './ClassProperty/spec'; +export * from './PropertyDefinition/spec'; export * from './MethodDefinition/spec'; export * from './Property/spec'; export * from './SpreadElement/spec'; export * from './StaticBlock/spec'; -export * from './TSAbstractClassProperty/spec'; +export * from './TSAbstractPropertyDefinition/spec'; export * from './TSAbstractMethodDefinition/spec'; export * from './TSCallSignatureDeclaration/spec'; export * from './TSConstructSignatureDeclaration/spec'; diff --git a/packages/ast-spec/src/unions/ClassElement.ts b/packages/ast-spec/src/unions/ClassElement.ts index c174c4ce6c72..27d4ec4d752c 100644 --- a/packages/ast-spec/src/unions/ClassElement.ts +++ b/packages/ast-spec/src/unions/ClassElement.ts @@ -1,14 +1,14 @@ -import type { ClassProperty } from '../element/ClassProperty/spec'; import type { MethodDefinition } from '../element/MethodDefinition/spec'; +import type { PropertyDefinition } from '../element/PropertyDefinition/spec'; import type { StaticBlock } from '../element/StaticBlock/spec'; -import type { TSAbstractClassProperty } from '../element/TSAbstractClassProperty/spec'; import type { TSAbstractMethodDefinition } from '../element/TSAbstractMethodDefinition/spec'; +import type { TSAbstractPropertyDefinition } from '../element/TSAbstractPropertyDefinition/spec'; import type { TSIndexSignature } from '../element/TSIndexSignature/spec'; export type ClassElement = - | ClassProperty | MethodDefinition + | PropertyDefinition | StaticBlock - | TSAbstractClassProperty | TSAbstractMethodDefinition + | TSAbstractPropertyDefinition | TSIndexSignature; diff --git a/packages/ast-spec/src/unions/Node.ts b/packages/ast-spec/src/unions/Node.ts index 7fe5ced96dfc..4b3c54a09eb9 100644 --- a/packages/ast-spec/src/unions/Node.ts +++ b/packages/ast-spec/src/unions/Node.ts @@ -12,13 +12,13 @@ import type { TSModuleDeclaration } from '../declaration/TSModuleDeclaration/spe import type { TSNamespaceExportDeclaration } from '../declaration/TSNamespaceExportDeclaration/spec'; import type { TSTypeAliasDeclaration } from '../declaration/TSTypeAliasDeclaration/spec'; import type { VariableDeclaration } from '../declaration/VariableDeclaration/spec'; -import type { ClassProperty } from '../element/ClassProperty/spec'; import type { MethodDefinition } from '../element/MethodDefinition/spec'; import type { Property } from '../element/Property/spec'; +import type { PropertyDefinition } from '../element/PropertyDefinition/spec'; import type { SpreadElement } from '../element/SpreadElement/spec'; import type { StaticBlock } from '../element/StaticBlock/spec'; -import type { TSAbstractClassProperty } from '../element/TSAbstractClassProperty/spec'; import type { TSAbstractMethodDefinition } from '../element/TSAbstractMethodDefinition/spec'; +import type { TSAbstractPropertyDefinition } from '../element/TSAbstractPropertyDefinition/spec'; import type { TSCallSignatureDeclaration } from '../element/TSCallSignatureDeclaration/spec'; import type { TSConstructSignatureDeclaration } from '../element/TSConstructSignatureDeclaration/spec'; import type { TSEnumMember } from '../element/TSEnumMember/spec'; @@ -181,7 +181,6 @@ export type Node = | ClassBody | ClassDeclaration | ClassExpression - | ClassProperty | ConditionalExpression | ContinueStatement | DebuggerStatement @@ -231,6 +230,7 @@ export type Node = | ObjectPattern | Program | Property + | PropertyDefinition | RestElement | ReturnStatement | SequenceExpression @@ -245,9 +245,9 @@ export type Node = | ThisExpression | ThrowStatement | TryStatement - | TSAbstractClassProperty | TSAbstractKeyword | TSAbstractMethodDefinition + | TSAbstractPropertyDefinition | TSAnyKeyword | TSArrayType | TSAsExpression diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index dd3878e77a2f..b08161450155 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -99,7 +99,7 @@ export default util.createRule({ } return { - ClassProperty(node: TSESTree.ClassProperty): void { + PropertyDefinition(node: TSESTree.PropertyDefinition): void { if (!node.readonly || node.declare) { return; } diff --git a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts index 89671fa7567a..2e73af17354b 100644 --- a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts +++ b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts @@ -162,7 +162,7 @@ export default util.createRule({ function getUnwantedPublicAccessibilityFixer( node: | TSESTree.MethodDefinition - | TSESTree.ClassProperty + | TSESTree.PropertyDefinition | TSESTree.TSParameterProperty, ): TSESLint.ReportFixFunction { return function (fixer: TSESLint.RuleFixer): TSESLint.RuleFix { @@ -198,30 +198,36 @@ export default util.createRule({ /** * Checks if property has an accessibility modifier. - * @param classProperty The node representing a ClassProperty. + * @param propertyDefinition The node representing a PropertyDefinition. */ function checkPropertyAccessibilityModifier( - classProperty: TSESTree.ClassProperty, + propertyDefinition: TSESTree.PropertyDefinition, ): void { const nodeType = 'class property'; - const propertyName = util.getNameFromMember(classProperty, sourceCode); + const propertyName = util.getNameFromMember( + propertyDefinition, + sourceCode, + ); if ( propCheck === 'no-public' && - classProperty.accessibility === 'public' + propertyDefinition.accessibility === 'public' ) { reportIssue( 'unwantedPublicAccessibility', nodeType, - classProperty, + propertyDefinition, propertyName, - getUnwantedPublicAccessibilityFixer(classProperty), + getUnwantedPublicAccessibilityFixer(propertyDefinition), ); - } else if (propCheck === 'explicit' && !classProperty.accessibility) { + } else if ( + propCheck === 'explicit' && + !propertyDefinition.accessibility + ) { reportIssue( 'missingAccessibility', nodeType, - classProperty, + propertyDefinition, propertyName, ); } @@ -273,7 +279,7 @@ export default util.createRule({ return { TSParameterProperty: checkParameterPropertyAccessibilityModifier, - ClassProperty: checkPropertyAccessibilityModifier, + PropertyDefinition: checkPropertyAccessibilityModifier, MethodDefinition: checkMethodAccessibilityModifier, }; }, diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 81dd20d0222c..6859668bb59c 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -334,7 +334,7 @@ export default util.createRule({ } return; - case AST_NODE_TYPES.ClassProperty: + case AST_NODE_TYPES.PropertyDefinition: if (node.accessibility === 'private') { return; } diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts index 863e7d0889d0..58da2c044e10 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts @@ -113,7 +113,7 @@ const KNOWN_NODES = new Set([ AST_NODE_TYPES.ImportNamespaceSpecifier, // Class properties aren't yet supported by eslint... - AST_NODE_TYPES.ClassProperty, + AST_NODE_TYPES.PropertyDefinition, // ts keywords AST_NODE_TYPES.TSAbstractKeyword, @@ -129,7 +129,7 @@ const KNOWN_NODES = new Set([ AST_NODE_TYPES.TSNullKeyword, // ts specific nodes we want to support - AST_NODE_TYPES.TSAbstractClassProperty, + AST_NODE_TYPES.TSAbstractPropertyDefinition, AST_NODE_TYPES.TSAbstractMethodDefinition, AST_NODE_TYPES.TSArrayType, AST_NODE_TYPES.TSAsExpression, diff --git a/packages/eslint-plugin/src/rules/indent.ts b/packages/eslint-plugin/src/rules/indent.ts index 7835fd0e58d9..e057ca9886b3 100644 --- a/packages/eslint-plugin/src/rules/indent.ts +++ b/packages/eslint-plugin/src/rules/indent.ts @@ -19,7 +19,7 @@ type MessageIds = util.InferMessageIdsTypeFromRule; const KNOWN_NODES = new Set([ // Class properties aren't yet supported by eslint... - AST_NODE_TYPES.ClassProperty, + AST_NODE_TYPES.PropertyDefinition, // ts keywords AST_NODE_TYPES.TSAbstractKeyword, @@ -35,7 +35,7 @@ const KNOWN_NODES = new Set([ AST_NODE_TYPES.TSNullKeyword, // ts specific nodes we want to support - AST_NODE_TYPES.TSAbstractClassProperty, + AST_NODE_TYPES.TSAbstractPropertyDefinition, AST_NODE_TYPES.TSAbstractMethodDefinition, AST_NODE_TYPES.TSArrayType, AST_NODE_TYPES.TSAsExpression, @@ -136,7 +136,7 @@ export default util.createRule({ | TSESTree.TSEnumMember | TSESTree.TypeElement, type: - | AST_NODE_TYPES.ClassProperty + | AST_NODE_TYPES.PropertyDefinition | AST_NODE_TYPES.Property = AST_NODE_TYPES.Property, ): TSESTree.Node | null { const base = { @@ -168,7 +168,7 @@ export default util.createRule({ readonly: false, declare: false, ...base, - } as TSESTree.ClassProperty; + } as TSESTree.PropertyDefinition; } } @@ -330,8 +330,8 @@ export default util.createRule({ p => TSPropertySignatureToProperty( p, - AST_NODE_TYPES.ClassProperty, - ) as TSESTree.ClassProperty, + AST_NODE_TYPES.PropertyDefinition, + ) as TSESTree.PropertyDefinition, ), // location data diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index b913f91b871b..9f88228449b4 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -250,9 +250,9 @@ function getNodeType(node: Member): string | null { return 'call-signature'; case AST_NODE_TYPES.TSConstructSignatureDeclaration: return 'constructor'; - case AST_NODE_TYPES.TSAbstractClassProperty: + case AST_NODE_TYPES.TSAbstractPropertyDefinition: return 'field'; - case AST_NODE_TYPES.ClassProperty: + case AST_NODE_TYPES.PropertyDefinition: return node.value && functionExpressions.includes(node.value.type) ? 'method' : 'field'; @@ -278,8 +278,8 @@ function getMemberName( switch (node.type) { case AST_NODE_TYPES.TSPropertySignature: case AST_NODE_TYPES.TSMethodSignature: - case AST_NODE_TYPES.TSAbstractClassProperty: - case AST_NODE_TYPES.ClassProperty: + case AST_NODE_TYPES.TSAbstractPropertyDefinition: + case AST_NODE_TYPES.PropertyDefinition: return util.getNameFromMember(node, sourceCode); case AST_NODE_TYPES.TSAbstractMethodDefinition: case AST_NODE_TYPES.MethodDefinition: @@ -339,7 +339,7 @@ function getRank( } const abstract = - node.type === AST_NODE_TYPES.TSAbstractClassProperty || + node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition || node.type === AST_NODE_TYPES.TSAbstractMethodDefinition; const scope = diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index 91456c7ccb23..6915c841473f 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -102,8 +102,8 @@ export default util.createRule({ validator: ValidatorFunction | null, node: | TSESTree.PropertyNonComputedName - | TSESTree.ClassPropertyNonComputedName - | TSESTree.TSAbstractClassPropertyNonComputedName + | TSESTree.PropertyDefinitionNonComputedName + | TSESTree.TSAbstractPropertyDefinitionNonComputedName | TSESTree.TSPropertySignatureNonComputedName | TSESTree.MethodDefinitionNonComputedName | TSESTree.TSAbstractMethodDefinitionNonComputedName @@ -124,8 +124,8 @@ export default util.createRule({ function getMemberModifiers( node: - | TSESTree.ClassProperty - | TSESTree.TSAbstractClassProperty + | TSESTree.PropertyDefinition + | TSESTree.TSAbstractPropertyDefinition | TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition | TSESTree.TSParameterProperty, @@ -143,7 +143,7 @@ export default util.createRule({ modifiers.add(Modifiers.readonly); } if ( - node.type === AST_NODE_TYPES.TSAbstractClassProperty || + node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition || node.type === AST_NODE_TYPES.TSAbstractMethodDefinition ) { modifiers.add(Modifiers.abstract); @@ -330,10 +330,10 @@ export default util.createRule({ handleMember(validators.objectLiteralProperty, node, modifiers); }, - ':matches(ClassProperty, TSAbstractClassProperty)[computed = false][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"][value.type != "TSEmptyBodyFunctionExpression"]'( + ':matches(PropertyDefinition, TSAbstractPropertyDefinition)[computed = false][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"][value.type != "TSEmptyBodyFunctionExpression"]'( node: - | TSESTree.ClassPropertyNonComputedName - | TSESTree.TSAbstractClassPropertyNonComputedName, + | TSESTree.PropertyDefinitionNonComputedName + | TSESTree.TSAbstractPropertyDefinitionNonComputedName, ): void { const modifiers = getMemberModifiers(node); handleMember(validators.classProperty, node, modifiers); @@ -368,14 +368,14 @@ export default util.createRule({ }, [[ - ':matches(ClassProperty, TSAbstractClassProperty)[computed = false][value.type = "ArrowFunctionExpression"]', - ':matches(ClassProperty, TSAbstractClassProperty)[computed = false][value.type = "FunctionExpression"]', - ':matches(ClassProperty, TSAbstractClassProperty)[computed = false][value.type = "TSEmptyBodyFunctionExpression"]', + ':matches(PropertyDefinition, TSAbstractPropertyDefinition)[computed = false][value.type = "ArrowFunctionExpression"]', + ':matches(PropertyDefinition, TSAbstractPropertyDefinition)[computed = false][value.type = "FunctionExpression"]', + ':matches(PropertyDefinition, TSAbstractPropertyDefinition)[computed = false][value.type = "TSEmptyBodyFunctionExpression"]', ':matches(MethodDefinition, TSAbstractMethodDefinition)[computed = false][kind = "method"]', ].join(', ')]( node: - | TSESTree.ClassPropertyNonComputedName - | TSESTree.TSAbstractClassPropertyNonComputedName + | TSESTree.PropertyDefinitionNonComputedName + | TSESTree.TSAbstractPropertyDefinitionNonComputedName | TSESTree.MethodDefinitionNonComputedName | TSESTree.TSAbstractMethodDefinitionNonComputedName, ): void { diff --git a/packages/eslint-plugin/src/rules/no-extra-semi.ts b/packages/eslint-plugin/src/rules/no-extra-semi.ts index 5ed7372b08cc..4ae5cb5c396b 100644 --- a/packages/eslint-plugin/src/rules/no-extra-semi.ts +++ b/packages/eslint-plugin/src/rules/no-extra-semi.ts @@ -26,13 +26,15 @@ export default util.createRule({ return { ...rules, - ClassProperty(node): void { + 'TSAbstractMethodDefinition, TSAbstractPropertyDefinition'( + node: never, + ): void { if (rules.MethodDefinition) { // for ESLint <= v7 - rules.MethodDefinition(node as never); + rules.MethodDefinition(node); } else { // for ESLint v8 - rules['MethodDefinition, PropertyDefinition']?.(node as never); + rules['MethodDefinition, PropertyDefinition']?.(node); } }, }; diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 055dc595299a..d704b472b78c 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -195,7 +195,7 @@ export default util.createRule({ node: | TSESTree.VariableDeclarator | TSESTree.Parameter - | TSESTree.ClassProperty, + | TSESTree.PropertyDefinition, typeNode: TSESTree.TSTypeAnnotation | undefined, initNode: TSESTree.Expression | null | undefined, ): void { @@ -253,7 +253,9 @@ export default util.createRule({ }); } - function inferrablePropertyVisitor(node: TSESTree.ClassProperty): void { + function inferrablePropertyVisitor( + node: TSESTree.PropertyDefinition, + ): void { // We ignore `readonly` because of Microsoft/TypeScript#14416 // Essentially a readonly property without a type // will result in its value being the type, leading to @@ -269,7 +271,7 @@ export default util.createRule({ FunctionExpression: inferrableParameterVisitor, FunctionDeclaration: inferrableParameterVisitor, ArrowFunctionExpression: inferrableParameterVisitor, - ClassProperty: inferrablePropertyVisitor, + PropertyDefinition: inferrablePropertyVisitor, }; }, }); diff --git a/packages/eslint-plugin/src/rules/no-invalid-this.ts b/packages/eslint-plugin/src/rules/no-invalid-this.ts index 9b8a4ac8d8af..1cd1be027b94 100644 --- a/packages/eslint-plugin/src/rules/no-invalid-this.ts +++ b/packages/eslint-plugin/src/rules/no-invalid-this.ts @@ -53,10 +53,10 @@ export default createRule({ return { ...rules, - ClassProperty(): void { + PropertyDefinition(): void { thisIsValidStack.push(true); }, - 'ClassProperty:exit'(): void { + 'PropertyDefinition:exit'(): void { thisIsValidStack.pop(); }, FunctionDeclaration(node: TSESTree.FunctionDeclaration): void { diff --git a/packages/eslint-plugin/src/rules/no-invalid-void-type.ts b/packages/eslint-plugin/src/rules/no-invalid-void-type.ts index 4902071ffe34..ff612253777b 100644 --- a/packages/eslint-plugin/src/rules/no-invalid-void-type.ts +++ b/packages/eslint-plugin/src/rules/no-invalid-void-type.ts @@ -68,7 +68,7 @@ export default util.createRule<[Options], MessageIds>({ const invalidGrandParents: AST_NODE_TYPES[] = [ AST_NODE_TYPES.TSPropertySignature, AST_NODE_TYPES.CallExpression, - AST_NODE_TYPES.ClassProperty, + AST_NODE_TYPES.PropertyDefinition, AST_NODE_TYPES.Identifier, ]; const validUnionMembers: AST_NODE_TYPES[] = [ diff --git a/packages/eslint-plugin/src/rules/no-magic-numbers.ts b/packages/eslint-plugin/src/rules/no-magic-numbers.ts index a434a06dcf9a..e63c80686c3d 100644 --- a/packages/eslint-plugin/src/rules/no-magic-numbers.ts +++ b/packages/eslint-plugin/src/rules/no-magic-numbers.ts @@ -77,7 +77,7 @@ export default util.createRule({ // Check if the node is a readonly class property if ( typeof node.value === 'number' && - isParentTSReadonlyClassProperty(node) + isParentTSReadonlyPropertyDefinition(node) ) { if (options.ignoreReadonlyClassProperties) { return; @@ -211,10 +211,10 @@ function isTSNumericLiteralType(node: TSESTree.Node): boolean { * @returns true if the node parent is a readonly class property * @private */ -function isParentTSReadonlyClassProperty(node: TSESTree.Literal): boolean { +function isParentTSReadonlyPropertyDefinition(node: TSESTree.Literal): boolean { const parent = getLiteralParent(node); - if (parent?.type === AST_NODE_TYPES.ClassProperty && parent.readonly) { + if (parent?.type === AST_NODE_TYPES.PropertyDefinition && parent.readonly) { return true; } diff --git a/packages/eslint-plugin/src/rules/no-unsafe-assignment.ts b/packages/eslint-plugin/src/rules/no-unsafe-assignment.ts index b14f43fe27f9..cb4b8e7c636a 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-assignment.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-assignment.ts @@ -336,7 +336,9 @@ export default util.createRule({ checkObjectDestructureHelper(node.id, init); } }, - 'ClassProperty[value != null]'(node: TSESTree.ClassProperty): void { + 'PropertyDefinition[value != null]'( + node: TSESTree.PropertyDefinition, + ): void { checkAssignment( node.key, node.value!, diff --git a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts index 5e2fbc52d83f..b3e0686dba5b 100644 --- a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts +++ b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts @@ -160,7 +160,9 @@ export default createRule({ 'ClassBody > MethodDefinition'(node: TSESTree.MethodDefinition): void { checkFunction(node.value, node.parent!.parent as ClassLikeDeclaration); }, - 'ClassBody > ClassProperty'(node: TSESTree.ClassProperty): void { + 'ClassBody > PropertyDefinition'( + node: TSESTree.PropertyDefinition, + ): void { if ( !( node.value?.type === AST_NODE_TYPES.FunctionExpression || diff --git a/packages/eslint-plugin/src/rules/quotes.ts b/packages/eslint-plugin/src/rules/quotes.ts index e03556af63dd..a22782cc08ea 100644 --- a/packages/eslint-plugin/src/rules/quotes.ts +++ b/packages/eslint-plugin/src/rules/quotes.ts @@ -53,8 +53,8 @@ export default util.createRule({ case AST_NODE_TYPES.TSEnumMember: return node === parent.id; - case AST_NODE_TYPES.TSAbstractClassProperty: - case AST_NODE_TYPES.ClassProperty: + case AST_NODE_TYPES.TSAbstractPropertyDefinition: + case AST_NODE_TYPES.PropertyDefinition: return node === parent.key; default: diff --git a/packages/eslint-plugin/src/rules/semi.ts b/packages/eslint-plugin/src/rules/semi.ts index 40e631482aaf..647d2c21b3eb 100644 --- a/packages/eslint-plugin/src/rules/semi.ts +++ b/packages/eslint-plugin/src/rules/semi.ts @@ -51,8 +51,8 @@ export default util.createRule({ AST_NODE_TYPES.TSPropertySignature, */ const nodesToCheck = [ - AST_NODE_TYPES.ClassProperty, - AST_NODE_TYPES.TSAbstractClassProperty, + AST_NODE_TYPES.PropertyDefinition, + AST_NODE_TYPES.TSAbstractPropertyDefinition, AST_NODE_TYPES.TSAbstractMethodDefinition, AST_NODE_TYPES.TSDeclareFunction, AST_NODE_TYPES.TSExportAssignment, diff --git a/packages/eslint-plugin/src/rules/space-infix-ops.ts b/packages/eslint-plugin/src/rules/space-infix-ops.ts index 80294cd26718..fddeaac6fb6f 100644 --- a/packages/eslint-plugin/src/rules/space-infix-ops.ts +++ b/packages/eslint-plugin/src/rules/space-infix-ops.ts @@ -122,8 +122,8 @@ export default util.createRule({ * Check if it has an assignment char and report if it's faulty * @param node The node to report */ - function checkForClassPropertyAssignmentSpace( - node: TSESTree.ClassProperty, + function checkForPropertyDefinitionAssignmentSpace( + node: TSESTree.PropertyDefinition, ): void { const leftNode = sourceCode.getTokenByRangeStart( node.typeAnnotation?.range[0] ?? node.range[0], @@ -179,7 +179,7 @@ export default util.createRule({ return { ...rules, TSEnumMember: checkForEnumAssignmentSpace, - ClassProperty: checkForClassPropertyAssignmentSpace, + PropertyDefinition: checkForPropertyDefinitionAssignmentSpace, TSTypeAliasDeclaration: checkForTypeAliasAssignment, TSUnionType: checkForTypeAnnotationSpace, TSIntersectionType: checkForTypeAnnotationSpace, diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index 35655a9a803d..eaca1be209e9 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -159,7 +159,7 @@ export default util.createRule<[Options], MessageIds>({ checkParameters(node.params); } }, - ClassProperty(node): void { + PropertyDefinition(node): void { if (node.value && isVariableDeclarationIgnoreFunction(node.value)) { return; } diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index c05d6c6dab35..53c12ab5b0b1 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -32,10 +32,12 @@ function isVariableDeclaratorWithTypeAnnotation( * public x: Foo = ... * ``` */ -function isClassPropertyWithTypeAnnotation( +function isPropertyDefinitionWithTypeAnnotation( node: TSESTree.Node, -): node is TSESTree.ClassProperty { - return node.type === AST_NODE_TYPES.ClassProperty && !!node.typeAnnotation; +): node is TSESTree.PropertyDefinition { + return ( + node.type === AST_NODE_TYPES.PropertyDefinition && !!node.typeAnnotation + ); } /** @@ -81,7 +83,7 @@ function isPropertyOfObjectWithType( return ( isTypeAssertion(parent) || - isClassPropertyWithTypeAnnotation(parent) || + isPropertyDefinitionWithTypeAnnotation(parent) || isVariableDeclaratorWithTypeAnnotation(parent) || isFunctionArgument(parent) || isPropertyOfObjectWithType(parent) @@ -194,7 +196,7 @@ function isTypedFunctionExpression( return ( isTypeAssertion(parent) || isVariableDeclaratorWithTypeAnnotation(parent) || - isClassPropertyWithTypeAnnotation(parent) || + isPropertyDefinitionWithTypeAnnotation(parent) || isPropertyOfObjectWithType(parent) || isFunctionArgument(parent, node) || isConstructorArgument(parent) @@ -219,7 +221,7 @@ function isValidFunctionExpressionReturnType( parent.type !== AST_NODE_TYPES.VariableDeclarator && parent.type !== AST_NODE_TYPES.MethodDefinition && parent.type !== AST_NODE_TYPES.ExportDefaultDeclaration && - parent.type !== AST_NODE_TYPES.ClassProperty + parent.type !== AST_NODE_TYPES.PropertyDefinition ) { return true; } diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index e7e0991ba6a0..748a5cec6a6b 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -66,15 +66,15 @@ function getNameFromIndexSignature(node: TSESTree.TSIndexSignature): string { /** * Gets a string name representation of the name of the given MethodDefinition - * or ClassProperty node, with handling for computed property names. + * or PropertyDefinition node, with handling for computed property names. */ function getNameFromMember( member: | TSESTree.MethodDefinition | TSESTree.TSMethodSignature | TSESTree.TSAbstractMethodDefinition - | TSESTree.ClassProperty - | TSESTree.TSAbstractClassProperty + | TSESTree.PropertyDefinition + | TSESTree.TSAbstractPropertyDefinition | TSESTree.Property | TSESTree.TSPropertySignature, sourceCode: TSESLint.SourceCode, diff --git a/packages/eslint-plugin/tests/rules/indent/indent.test.ts b/packages/eslint-plugin/tests/rules/indent/indent.test.ts index f5c87316c171..3d6b58725f8b 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent.test.ts @@ -39,7 +39,7 @@ abstract class Foo { ], }, { - node: AST_NODE_TYPES.TSAbstractClassProperty, + node: AST_NODE_TYPES.TSAbstractPropertyDefinition, code: [ ` class Foo { diff --git a/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts b/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts index db3ca1735325..4d2d870e7e50 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts @@ -32,7 +32,6 @@ ruleTester.run('no-extra-semi', rule, { }, { code: 'for(a of b);', - parserOptions: { ecmaVersion: 6 }, }, { code: 'if(true);', @@ -50,23 +49,18 @@ ruleTester.run('no-extra-semi', rule, { // Class body. { code: 'class A { }', - parserOptions: { ecmaVersion: 6 }, }, { code: 'var A = class { };', - parserOptions: { ecmaVersion: 6 }, }, { code: 'class A { a() { this; } }', - parserOptions: { ecmaVersion: 6 }, }, { code: 'var A = class { a() { this; } };', - parserOptions: { ecmaVersion: 6 }, }, { code: 'class A { } a;', - parserOptions: { ecmaVersion: 6 }, }, // modules @@ -159,7 +153,6 @@ export class Foo { { code: 'for(a of b);;', output: 'for(a of b);', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -228,7 +221,6 @@ export class Foo { { code: 'class A { ; }', output: 'class A { }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -239,7 +231,6 @@ export class Foo { { code: 'class A { /*a*/; }', output: 'class A { /*a*/ }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -250,7 +241,6 @@ export class Foo { { code: 'class A { ; a() {} }', output: 'class A { a() {} }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -261,7 +251,6 @@ export class Foo { { code: 'class A { a() {}; }', output: 'class A { a() {} }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -272,7 +261,6 @@ export class Foo { { code: 'class A { a() {}; b() {} }', output: 'class A { a() {} b() {} }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -283,7 +271,6 @@ export class Foo { { code: 'class A {; a() {}; b() {}; }', output: 'class A { a() {} b() {} }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -302,7 +289,6 @@ export class Foo { { code: 'class A { a() {}; get b() {} }', output: 'class A { a() {} get b() {} }', - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -321,7 +307,6 @@ class Foo { public foo: number = 0; } `, - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -342,7 +327,6 @@ class Foo { public baz: number = 1; } `, - parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'unexpected', @@ -361,5 +345,88 @@ class Foo { }, ], }, + + // abstract prop/method + { + code: ` +class Foo { + abstract foo: number;; abstract bar: number;; + abstract baz: number;; +} + `, + output: ` +class Foo { + abstract foo: number; abstract bar: number; + abstract baz: number; +} + `, + errors: [ + { + messageId: 'unexpected', + line: 3, + column: 24, + }, + { + messageId: 'unexpected', + line: 3, + column: 47, + }, + { + messageId: 'unexpected', + line: 4, + column: 24, + }, + ], + }, + { + code: ` +class Foo { + abstract foo();; abstract bar();; + abstract baz();; + abstract foo(): void;; abstract bar(): void;; + abstract baz(): void;; +} + `, + output: ` +class Foo { + abstract foo(); abstract bar(); + abstract baz(); + abstract foo(): void; abstract bar(): void; + abstract baz(): void; +} + `, + errors: [ + { + messageId: 'unexpected', + line: 3, + column: 18, + }, + { + messageId: 'unexpected', + line: 3, + column: 35, + }, + { + messageId: 'unexpected', + line: 4, + column: 18, + }, + { + messageId: 'unexpected', + line: 5, + column: 24, + }, + { + messageId: 'unexpected', + line: 5, + column: 47, + }, + { + messageId: 'unexpected', + line: 6, + column: 24, + }, + ], + }, ], }); diff --git a/packages/eslint-plugin/tests/rules/quotes.test.ts b/packages/eslint-plugin/tests/rules/quotes.test.ts index 0d2549d7e101..003d4fd19d7e 100644 --- a/packages/eslint-plugin/tests/rules/quotes.test.ts +++ b/packages/eslint-plugin/tests/rules/quotes.test.ts @@ -423,7 +423,7 @@ interface Foo { options: ['backtick'], }, - // ClassProperty + // PropertyDefinition { code: ` class Foo { @@ -451,7 +451,7 @@ class Foo { options: ['backtick'], }, - // TSAbstractClassProperty + // TSAbstractPropertyDefinition { code: ` abstract class Foo { @@ -951,7 +951,7 @@ interface Foo { options: ['single'], }, - // ClassProperty + // PropertyDefinition { code: ` class Foo { @@ -1043,7 +1043,7 @@ class Foo { options: ['backtick'], }, - // TSAbstractClassProperty + // TSAbstractPropertyDefinition { code: ` abstract class Foo { diff --git a/packages/experimental-utils/src/ast-utils/predicates.ts b/packages/experimental-utils/src/ast-utils/predicates.ts index 919820d7653d..3859de3921af 100644 --- a/packages/experimental-utils/src/ast-utils/predicates.ts +++ b/packages/experimental-utils/src/ast-utils/predicates.ts @@ -151,10 +151,10 @@ function isClassOrTypeElement( return [ // ClassElement - AST_NODE_TYPES.ClassProperty, + AST_NODE_TYPES.PropertyDefinition, AST_NODE_TYPES.FunctionExpression, AST_NODE_TYPES.MethodDefinition, - AST_NODE_TYPES.TSAbstractClassProperty, + AST_NODE_TYPES.TSAbstractPropertyDefinition, AST_NODE_TYPES.TSAbstractMethodDefinition, AST_NODE_TYPES.TSEmptyBodyFunctionExpression, AST_NODE_TYPES.TSIndexSignature, diff --git a/packages/experimental-utils/src/ts-eslint/Rule.ts b/packages/experimental-utils/src/ts-eslint/Rule.ts index 9605d72ded4c..5ca14c96086c 100644 --- a/packages/experimental-utils/src/ts-eslint/Rule.ts +++ b/packages/experimental-utils/src/ts-eslint/Rule.ts @@ -259,8 +259,8 @@ interface RuleListener { ArrayExpression?: RuleFunction; ArrayPattern?: RuleFunction; ArrowFunctionExpression?: RuleFunction; - AssignmentPattern?: RuleFunction; AssignmentExpression?: RuleFunction; + AssignmentPattern?: RuleFunction; AwaitExpression?: RuleFunction; BigIntLiteral?: RuleFunction; BinaryExpression?: RuleFunction; @@ -272,7 +272,6 @@ interface RuleListener { ClassBody?: RuleFunction; ClassDeclaration?: RuleFunction; ClassExpression?: RuleFunction; - ClassProperty?: RuleFunction; ConditionalExpression?: RuleFunction; ContinueStatement?: RuleFunction; DebuggerStatement?: RuleFunction; @@ -321,6 +320,7 @@ interface RuleListener { ObjectPattern?: RuleFunction; Program?: RuleFunction; Property?: RuleFunction; + PropertyDefinition?: RuleFunction; RestElement?: RuleFunction; ReturnStatement?: RuleFunction; SequenceExpression?: RuleFunction; @@ -334,9 +334,9 @@ interface RuleListener { ThisExpression?: RuleFunction; ThrowStatement?: RuleFunction; TryStatement?: RuleFunction; - TSAbstractClassProperty?: RuleFunction; TSAbstractKeyword?: RuleFunction; TSAbstractMethodDefinition?: RuleFunction; + TSAbstractPropertyDefinition?: RuleFunction; TSAnyKeyword?: RuleFunction; TSArrayType?: RuleFunction; TSAsExpression?: RuleFunction; @@ -348,8 +348,8 @@ interface RuleListener { TSConditionalType?: RuleFunction; TSConstructorType?: RuleFunction; TSConstructSignatureDeclaration?: RuleFunction; - TSDeclareKeyword?: RuleFunction; TSDeclareFunction?: RuleFunction; + TSDeclareKeyword?: RuleFunction; TSEmptyBodyFunctionExpression?: RuleFunction; TSEnumDeclaration?: RuleFunction; TSEnumMember?: RuleFunction; diff --git a/packages/scope-manager/src/ScopeManager.ts b/packages/scope-manager/src/ScopeManager.ts index e4c9b4d3cdc4..3f360710cb0b 100644 --- a/packages/scope-manager/src/ScopeManager.ts +++ b/packages/scope-manager/src/ScopeManager.ts @@ -19,6 +19,7 @@ import { TypeScope, WithScope, } from './scope'; +import { ClassFieldInitializerScope } from './scope/ClassFieldInitializerScope'; import { Variable } from './variable'; @@ -168,6 +169,15 @@ class ScopeManager { return this.nestScope(new ClassScope(this, this.currentScope, node)); } + public nestClassFieldInitializerScope( + node: ClassFieldInitializerScope['block'], + ): ClassFieldInitializerScope { + assert(this.currentScope); + return this.nestScope( + new ClassFieldInitializerScope(this, this.currentScope, node), + ); + } + public nestConditionalTypeScope( node: ConditionalTypeScope['block'], ): ConditionalTypeScope { diff --git a/packages/scope-manager/src/referencer/ClassVisitor.ts b/packages/scope-manager/src/referencer/ClassVisitor.ts index 497c3a92ca83..d33e362ea41a 100644 --- a/packages/scope-manager/src/referencer/ClassVisitor.ts +++ b/packages/scope-manager/src/referencer/ClassVisitor.ts @@ -80,8 +80,8 @@ class ClassVisitor extends Visitor { this.#referencer.close(node); } - protected visitClassProperty( - node: TSESTree.TSAbstractClassProperty | TSESTree.ClassProperty, + protected visitPropertyDefinition( + node: TSESTree.TSAbstractPropertyDefinition | TSESTree.PropertyDefinition, ): void { this.visitProperty(node); /** @@ -229,16 +229,26 @@ class ClassVisitor extends Visitor { protected visitProperty( node: - | TSESTree.ClassProperty - | TSESTree.TSAbstractClassProperty + | TSESTree.PropertyDefinition + | TSESTree.TSAbstractPropertyDefinition | TSESTree.TSAbstractMethodDefinition, ): void { if (node.computed) { this.#referencer.visit(node.key); } - if (node.type !== AST_NODE_TYPES.TSAbstractClassProperty) { + if (node.value) { + if (node.type === AST_NODE_TYPES.PropertyDefinition) { + this.#referencer.scopeManager.nestClassFieldInitializerScope( + node.value, + ); + } + this.#referencer.visit(node.value); + + if (node.type === AST_NODE_TYPES.PropertyDefinition) { + this.#referencer.close(node.value); + } } if ('decorators' in node) { @@ -320,18 +330,18 @@ class ClassVisitor extends Visitor { this.visitChildren(node); } - protected ClassProperty(node: TSESTree.ClassProperty): void { - this.visitClassProperty(node); + protected PropertyDefinition(node: TSESTree.PropertyDefinition): void { + this.visitPropertyDefinition(node); } protected MethodDefinition(node: TSESTree.MethodDefinition): void { this.visitMethod(node); } - protected TSAbstractClassProperty( - node: TSESTree.TSAbstractClassProperty, + protected TSAbstractPropertyDefinition( + node: TSESTree.TSAbstractPropertyDefinition, ): void { - this.visitClassProperty(node); + this.visitPropertyDefinition(node); } protected TSAbstractMethodDefinition( diff --git a/packages/scope-manager/src/scope/ClassFieldInitializerScope.ts b/packages/scope-manager/src/scope/ClassFieldInitializerScope.ts new file mode 100644 index 000000000000..b176fb2060fc --- /dev/null +++ b/packages/scope-manager/src/scope/ClassFieldInitializerScope.ts @@ -0,0 +1,28 @@ +import { TSESTree } from '@typescript-eslint/types'; +import { Scope } from './Scope'; +import { ScopeBase } from './ScopeBase'; +import { ScopeType } from './ScopeType'; +import { ScopeManager } from '../ScopeManager'; + +class ClassFieldInitializerScope extends ScopeBase< + ScopeType.classFieldInitializer, + // the value expression itself is the block + TSESTree.Expression, + Scope +> { + constructor( + scopeManager: ScopeManager, + upperScope: ClassFieldInitializerScope['upper'], + block: ClassFieldInitializerScope['block'], + ) { + super( + scopeManager, + ScopeType.classFieldInitializer, + upperScope, + block, + false, + ); + } +} + +export { ClassFieldInitializerScope }; diff --git a/packages/scope-manager/src/scope/Scope.ts b/packages/scope-manager/src/scope/Scope.ts index 7df3f941629c..966e52a9456a 100644 --- a/packages/scope-manager/src/scope/Scope.ts +++ b/packages/scope-manager/src/scope/Scope.ts @@ -1,5 +1,6 @@ import { BlockScope } from './BlockScope'; import { CatchScope } from './CatchScope'; +import { ClassFieldInitializerScope } from './ClassFieldInitializerScope'; import { ClassScope } from './ClassScope'; import { ConditionalTypeScope } from './ConditionalTypeScope'; import { ForScope } from './ForScope'; @@ -19,6 +20,7 @@ type Scope = | BlockScope | CatchScope | ClassScope + | ClassFieldInitializerScope | ConditionalTypeScope | ForScope | FunctionExpressionNameScope diff --git a/packages/scope-manager/src/scope/ScopeBase.ts b/packages/scope-manager/src/scope/ScopeBase.ts index c06a7002c48b..34c9a338b5b1 100644 --- a/packages/scope-manager/src/scope/ScopeBase.ts +++ b/packages/scope-manager/src/scope/ScopeBase.ts @@ -125,8 +125,9 @@ const generator = createIdGenerator(); type VariableScope = GlobalScope | FunctionScope | ModuleScope | TSModuleScope; const VARIABLE_SCOPE_TYPES = new Set([ - ScopeType.global, + ScopeType.classFieldInitializer, ScopeType.function, + ScopeType.global, ScopeType.module, ScopeType.tsModule, ]); diff --git a/packages/scope-manager/src/scope/ScopeType.ts b/packages/scope-manager/src/scope/ScopeType.ts index 23d70fedbe80..8a63ffbdea7f 100644 --- a/packages/scope-manager/src/scope/ScopeType.ts +++ b/packages/scope-manager/src/scope/ScopeType.ts @@ -2,6 +2,7 @@ enum ScopeType { block = 'block', catch = 'catch', class = 'class', + classFieldInitializer = 'class-field-initializer', conditionalType = 'conditionalType', for = 'for', function = 'function', diff --git a/packages/scope-manager/src/scope/index.ts b/packages/scope-manager/src/scope/index.ts index 8e4683dbe68a..c94cc2982e1f 100644 --- a/packages/scope-manager/src/scope/index.ts +++ b/packages/scope-manager/src/scope/index.ts @@ -1,5 +1,6 @@ export * from './BlockScope'; export * from './CatchScope'; +export * from './ClassFieldInitializerScope'; export * from './ClassScope'; export * from './ConditionalTypeScope'; export * from './ForScope'; diff --git a/packages/scope-manager/tests/eslint-scope/class-fields.test.ts b/packages/scope-manager/tests/eslint-scope/class-fields.test.ts new file mode 100644 index 000000000000..541b46a2fff2 --- /dev/null +++ b/packages/scope-manager/tests/eslint-scope/class-fields.test.ts @@ -0,0 +1,101 @@ +import { + expectToBeClassFieldInitializerScope, + expectToBeIdentifier, +} from '../util'; +import { + expectToBeClassScope, + expectToBeGlobalScope, + parseAndAnalyze, +} from '../util'; + +describe('Class fields', () => { + it('class C { f = g }', () => { + const { scopeManager } = parseAndAnalyze('class C { f = g }'); + + const globalScope = scopeManager.scopes[0]; + expectToBeGlobalScope(globalScope); + + const classScope = globalScope.childScopes[0]; + // should create a class scope + expectToBeClassScope(classScope); + // The class scope has no references + expect(classScope.references).toHaveLength(0); + // The class scope has only the variable 'C'; it doesn't have the field name 'f'. + expect(classScope.variables).toHaveLength(1); + expect(classScope.variables[0].name).toBe('C'); + + const classFieldInitializerScope = classScope.childScopes[0]; + // The class scope has a class-field-initializer scope. + expectToBeClassFieldInitializerScope(classFieldInitializerScope); + // The class-field-initializer scope's block is the node of the field initializer. + expectToBeIdentifier(classFieldInitializerScope.block); + expect(classFieldInitializerScope.block.name).toBe('g'); + // The class-field-initializer scope's variableScope is itself. + expect(classFieldInitializerScope.variableScope).toBe( + classFieldInitializerScope, + ); + // The class-field-initializer scope has only the reference 'g'. + expect(classFieldInitializerScope.references).toHaveLength(1); + expect(classFieldInitializerScope.references[0].identifier.name).toBe('g'); + // The class-field-initializer scope has no variables. + expect(classFieldInitializerScope.variables).toHaveLength(0); + }); + + describe('class C { f }', () => { + const { scopeManager } = parseAndAnalyze('class C { f }'); + + const globalScope = scopeManager.scopes[0]; + expectToBeGlobalScope(globalScope); + + const classScope = globalScope.childScopes[0]; + // should create a class scope + expectToBeClassScope(classScope); + // The class scope has no references + expect(classScope.references).toHaveLength(0); + // The class scope has no child scopes; fields that don't have initializers don't create any class-field-initializer scopes. + expect(classScope.childScopes).toHaveLength(0); + // The class scope has only the variable 'C'; it doesn't have the field name 'f'. + expect(classScope.variables).toHaveLength(1); + expect(classScope.variables[0].name).toBe('C'); + }); + + describe('class C { [fname] }', () => { + const { scopeManager } = parseAndAnalyze('class C { [fname] }'); + + const globalScope = scopeManager.scopes[0]; + expectToBeGlobalScope(globalScope); + + const classScope = globalScope.childScopes[0]; + // should create a class scope + expectToBeClassScope(classScope); + // The class scope has only the reference `fname`. + expect(classScope.references).toHaveLength(1); + // The class scope has no child scopes; fields that don't have initializers don't create any class-field-initializer scopes. + expect(classScope.childScopes).toHaveLength(0); + }); + + describe('class C { [fname] = value }', () => { + const { scopeManager } = parseAndAnalyze('class C { [fname] = value }'); + + const globalScope = scopeManager.scopes[0]; + expectToBeGlobalScope(globalScope); + + const classScope = globalScope.childScopes[0]; + // should create a class scope + expectToBeClassScope(classScope); + // The class scope has only the reference `fname`; it doesn't have the reference 'value'. + expect(classScope.references).toHaveLength(1); + expect(classScope.references[0].identifier.name).toBe('fname'); + + const classFieldInitializerScope = classScope.childScopes[0]; + // The class scope has a class-field-initializer scope. + expectToBeClassFieldInitializerScope(classFieldInitializerScope); + // The class-field-initializer scope has the reference 'value'. + expect(classFieldInitializerScope.references).toHaveLength(1); + expect(classFieldInitializerScope.references[0].identifier.name).toBe( + 'value', + ); + // The class-field-initializer scope has no variables. + expect(classFieldInitializerScope.variables).toHaveLength(0); + }); +}); diff --git a/packages/scope-manager/tests/fixtures/class/declaration/computed-member.ts.shot b/packages/scope-manager/tests/fixtures/class/declaration/computed-member.ts.shot index fa221e5e540d..87bcc27ae85a 100644 --- a/packages/scope-manager/tests/fixtures/class/declaration/computed-member.ts.shot +++ b/packages/scope-manager/tests/fixtures/class/declaration/computed-member.ts.shot @@ -137,8 +137,17 @@ ScopeManager { Variable$5, ], }, - FunctionScope$3 { - block: FunctionExpression$7, + ClassFieldInitializerScope$3 { + block: Literal$7, + isStrict: true, + references: Array [], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], + }, + FunctionScope$4 { + block: FunctionExpression$8, isStrict: true, references: Array [], set: Map { diff --git a/packages/scope-manager/tests/fixtures/class/declaration/properties.ts b/packages/scope-manager/tests/fixtures/class/declaration/properties.ts index c047395435ad..e75d9def3e96 100644 --- a/packages/scope-manager/tests/fixtures/class/declaration/properties.ts +++ b/packages/scope-manager/tests/fixtures/class/declaration/properties.ts @@ -1,5 +1,7 @@ +const x = 1; class A { - prop = 1; + prop1 = 1; + prop2 = x; } -const unresolved = prop; +const unresolved = prop1; diff --git a/packages/scope-manager/tests/fixtures/class/declaration/properties.ts.shot b/packages/scope-manager/tests/fixtures/class/declaration/properties.ts.shot index 3592b97d80a5..180bf6246fb7 100644 --- a/packages/scope-manager/tests/fixtures/class/declaration/properties.ts.shot +++ b/packages/scope-manager/tests/fixtures/class/declaration/properties.ts.shot @@ -6,9 +6,40 @@ ScopeManager { ImplicitGlobalConstTypeVariable, Variable$2 { defs: Array [ - ClassNameDefinition$1 { + VariableDefinition$1 { + name: Identifier<"x">, + node: VariableDeclarator$1, + }, + ], + name: "x", + references: Array [ + Reference$1 { + identifier: Identifier<"x">, + init: true, + isRead: false, + isTypeReference: false, + isValueReference: true, + isWrite: true, + resolved: Variable$2, + writeExpr: Literal$2, + }, + Reference$2 { + identifier: Identifier<"x">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$2, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [ + ClassNameDefinition$2 { name: Identifier<"A">, - node: ClassDeclaration$1, + node: ClassDeclaration$3, }, ], name: "A", @@ -16,11 +47,11 @@ ScopeManager { isValueVariable: true, isTypeVariable: true, }, - Variable$3 { + Variable$4 { defs: Array [ - ClassNameDefinition$2 { + ClassNameDefinition$3 { name: Identifier<"A">, - node: ClassDeclaration$1, + node: ClassDeclaration$3, }, ], name: "A", @@ -28,24 +59,24 @@ ScopeManager { isValueVariable: true, isTypeVariable: true, }, - Variable$4 { + Variable$5 { defs: Array [ - VariableDefinition$3 { + VariableDefinition$4 { name: Identifier<"unresolved">, - node: VariableDeclarator$2, + node: VariableDeclarator$4, }, ], name: "unresolved", references: Array [ - Reference$1 { + Reference$3 { identifier: Identifier<"unresolved">, init: true, isRead: false, isTypeReference: false, isValueReference: true, isWrite: true, - resolved: Variable$4, - writeExpr: Identifier<"prop">, + resolved: Variable$5, + writeExpr: Identifier<"prop1">, }, ], isValueVariable: true, @@ -54,12 +85,13 @@ ScopeManager { ], scopes: Array [ GlobalScope$1 { - block: Program$3, + block: Program$5, isStrict: false, references: Array [ Reference$1, - Reference$2 { - identifier: Identifier<"prop">, + Reference$3, + Reference$4 { + identifier: Identifier<"prop1">, isRead: true, isTypeReference: false, isValueReference: true, @@ -69,29 +101,51 @@ ScopeManager { ], set: Map { "const" => ImplicitGlobalConstTypeVariable, - "A" => Variable$2, - "unresolved" => Variable$4, + "x" => Variable$2, + "A" => Variable$3, + "unresolved" => Variable$5, }, type: "global", upper: null, variables: Array [ ImplicitGlobalConstTypeVariable, Variable$2, - Variable$4, + Variable$3, + Variable$5, ], }, ClassScope$2 { - block: ClassDeclaration$1, + block: ClassDeclaration$3, isStrict: true, references: Array [], set: Map { - "A" => Variable$3, + "A" => Variable$4, }, type: "class", upper: GlobalScope$1, variables: Array [ - Variable$3, + Variable$4, + ], + }, + ClassFieldInitializerScope$3 { + block: Literal$6, + isStrict: true, + references: Array [], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], + }, + ClassFieldInitializerScope$4 { + block: Identifier<"x">, + isStrict: true, + references: Array [ + Reference$2, ], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], }, ], } diff --git a/packages/scope-manager/tests/fixtures/class/expression/computed-member.ts.shot b/packages/scope-manager/tests/fixtures/class/expression/computed-member.ts.shot index 25e96169fc75..f9080b0c6ee2 100644 --- a/packages/scope-manager/tests/fixtures/class/expression/computed-member.ts.shot +++ b/packages/scope-manager/tests/fixtures/class/expression/computed-member.ts.shot @@ -133,8 +133,17 @@ ScopeManager { upper: GlobalScope$1, variables: Array [], }, - FunctionScope$3 { - block: FunctionExpression$8, + ClassFieldInitializerScope$3 { + block: Literal$8, + isStrict: true, + references: Array [], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], + }, + FunctionScope$4 { + block: FunctionExpression$9, isStrict: true, references: Array [], set: Map { diff --git a/packages/scope-manager/tests/fixtures/class/expression/properties.ts b/packages/scope-manager/tests/fixtures/class/expression/properties.ts index 49b1efe6297e..508292413246 100644 --- a/packages/scope-manager/tests/fixtures/class/expression/properties.ts +++ b/packages/scope-manager/tests/fixtures/class/expression/properties.ts @@ -1,5 +1,7 @@ +const x = 1; const A = class { - prop = 1; + prop1 = 1; + prop2 = x; }; const unresolved = prop; diff --git a/packages/scope-manager/tests/fixtures/class/expression/properties.ts.shot b/packages/scope-manager/tests/fixtures/class/expression/properties.ts.shot index 261a24a50ecc..b5f489d2502b 100644 --- a/packages/scope-manager/tests/fixtures/class/expression/properties.ts.shot +++ b/packages/scope-manager/tests/fixtures/class/expression/properties.ts.shot @@ -7,21 +7,29 @@ ScopeManager { Variable$2 { defs: Array [ VariableDefinition$1 { - name: Identifier<"A">, + name: Identifier<"x">, node: VariableDeclarator$1, }, ], - name: "A", + name: "x", references: Array [ Reference$1 { - identifier: Identifier<"A">, + identifier: Identifier<"x">, init: true, isRead: false, isTypeReference: false, isValueReference: true, isWrite: true, resolved: Variable$2, - writeExpr: ClassExpression$2, + writeExpr: Literal$2, + }, + Reference$3 { + identifier: Identifier<"x">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$2, }, ], isValueVariable: true, @@ -30,20 +38,43 @@ ScopeManager { Variable$3 { defs: Array [ VariableDefinition$2 { - name: Identifier<"unresolved">, + name: Identifier<"A">, node: VariableDeclarator$3, }, ], - name: "unresolved", + name: "A", references: Array [ Reference$2 { - identifier: Identifier<"unresolved">, + identifier: Identifier<"A">, init: true, isRead: false, isTypeReference: false, isValueReference: true, isWrite: true, resolved: Variable$3, + writeExpr: ClassExpression$4, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$4 { + defs: Array [ + VariableDefinition$3 { + name: Identifier<"unresolved">, + node: VariableDeclarator$5, + }, + ], + name: "unresolved", + references: Array [ + Reference$4 { + identifier: Identifier<"unresolved">, + init: true, + isRead: false, + isTypeReference: false, + isValueReference: true, + isWrite: true, + resolved: Variable$4, writeExpr: Identifier<"prop">, }, ], @@ -53,12 +84,13 @@ ScopeManager { ], scopes: Array [ GlobalScope$1 { - block: Program$4, + block: Program$6, isStrict: false, references: Array [ Reference$1, Reference$2, - Reference$3 { + Reference$4, + Reference$5 { identifier: Identifier<"prop">, isRead: true, isTypeReference: false, @@ -69,8 +101,9 @@ ScopeManager { ], set: Map { "const" => ImplicitGlobalConstTypeVariable, - "A" => Variable$2, - "unresolved" => Variable$3, + "x" => Variable$2, + "A" => Variable$3, + "unresolved" => Variable$4, }, type: "global", upper: null, @@ -78,10 +111,11 @@ ScopeManager { ImplicitGlobalConstTypeVariable, Variable$2, Variable$3, + Variable$4, ], }, ClassScope$2 { - block: ClassExpression$2, + block: ClassExpression$4, isStrict: true, references: Array [], set: Map {}, @@ -89,6 +123,26 @@ ScopeManager { upper: GlobalScope$1, variables: Array [], }, + ClassFieldInitializerScope$3 { + block: Literal$7, + isStrict: true, + references: Array [], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], + }, + ClassFieldInitializerScope$4 { + block: Identifier<"x">, + isStrict: true, + references: Array [ + Reference$3, + ], + set: Map {}, + type: "class-field-initializer", + upper: ClassScope$2, + variables: Array [], + }, ], } `; diff --git a/packages/scope-manager/tests/util/expect.ts b/packages/scope-manager/tests/util/expect.ts index 0057af9f85c2..5609a923598b 100644 --- a/packages/scope-manager/tests/util/expect.ts +++ b/packages/scope-manager/tests/util/expect.ts @@ -14,6 +14,7 @@ import { BlockScope, CatchScope, ClassScope, + ClassFieldInitializerScope, ForScope, FunctionExpressionNameScope, FunctionScope, @@ -35,6 +36,11 @@ function expectToBeBlockScope(scope: Scope): asserts scope is BlockScope { function expectToBeCatchScope(scope: Scope): asserts scope is CatchScope { expect(scope.type).toBe(ScopeType.catch); } +function expectToBeClassFieldInitializerScope( + scope: Scope, +): asserts scope is ClassFieldInitializerScope { + expect(scope.type).toBe(ScopeType.classFieldInitializer); +} function expectToBeClassScope(scope: Scope): asserts scope is ClassScope { expect(scope.type).toBe(ScopeType.class); } @@ -65,6 +71,7 @@ function expectToBeWithScope(scope: Scope): asserts scope is WithScope { export { expectToBeBlockScope, expectToBeCatchScope, + expectToBeClassFieldInitializerScope, expectToBeClassScope, expectToBeForScope, expectToBeFunctionExpressionNameScope, diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index da5ba78fbf86..0846087759f3 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1103,11 +1103,11 @@ export class Converter { case SyntaxKind.PropertyDeclaration: { const isAbstract = hasModifier(SyntaxKind.AbstractKeyword, node); const result = this.createNode< - TSESTree.TSAbstractClassProperty | TSESTree.ClassProperty + TSESTree.TSAbstractPropertyDefinition | TSESTree.PropertyDefinition >(node, { type: isAbstract - ? AST_NODE_TYPES.TSAbstractClassProperty - : AST_NODE_TYPES.ClassProperty, + ? AST_NODE_TYPES.TSAbstractPropertyDefinition + : AST_NODE_TYPES.PropertyDefinition, key: this.convertChild(node.name), value: isAbstract ? null : this.convertChild(node.initializer), computed: isComputedProperty(node.name), diff --git a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts index 8faaef068d54..07b2bb5db318 100644 --- a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts @@ -28,7 +28,7 @@ export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.ClassBody]: ts.ClassDeclaration | ts.ClassExpression; [AST_NODE_TYPES.ClassDeclaration]: ts.ClassDeclaration; [AST_NODE_TYPES.ClassExpression]: ts.ClassExpression; - [AST_NODE_TYPES.ClassProperty]: ts.PropertyDeclaration; + [AST_NODE_TYPES.PropertyDefinition]: ts.PropertyDeclaration; [AST_NODE_TYPES.ConditionalExpression]: ts.ConditionalExpression; [AST_NODE_TYPES.ContinueStatement]: ts.ContinueStatement; [AST_NODE_TYPES.DebuggerStatement]: ts.DebuggerStatement; @@ -150,7 +150,7 @@ export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.ThisExpression]: ts.ThisExpression | ts.KeywordTypeNode; [AST_NODE_TYPES.ThrowStatement]: ts.ThrowStatement; [AST_NODE_TYPES.TryStatement]: ts.TryStatement; - [AST_NODE_TYPES.TSAbstractClassProperty]: ts.PropertyDeclaration; + [AST_NODE_TYPES.TSAbstractPropertyDefinition]: ts.PropertyDeclaration; [AST_NODE_TYPES.TSAbstractMethodDefinition]: | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration diff --git a/packages/typescript-estree/tests/ast-alignment/parse.ts b/packages/typescript-estree/tests/ast-alignment/parse.ts index 0cf1898a321e..7d340278649e 100644 --- a/packages/typescript-estree/tests/ast-alignment/parse.ts +++ b/packages/typescript-estree/tests/ast-alignment/parse.ts @@ -24,10 +24,15 @@ function createError( function parseWithBabelParser(text: string, jsx = true): File { const babel = require('@babel/parser') as typeof babelParser; const plugins: ParserPlugin[] = [ - 'classProperties', + [ + 'estree', + { + // @ts-expect-error -- this isn't exposed in the types yet + classFeatures: true, + }, + ], 'decorators-legacy', 'classStaticBlock', - 'estree', 'typescript', ]; if (jsx) { diff --git a/packages/typescript-estree/tests/ast-alignment/utils.ts b/packages/typescript-estree/tests/ast-alignment/utils.ts index 938752ce9baa..3cc6f4de0161 100644 --- a/packages/typescript-estree/tests/ast-alignment/utils.ts +++ b/packages/typescript-estree/tests/ast-alignment/utils.ts @@ -168,13 +168,15 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any { node.override = false; } }, - ClassProperty(node) { + PropertyDefinition(node) { + // babel does not + // node.type = AST_NODE_TYPES.PropertyDefinition; /** * Babel: ClassProperty + abstract: true * ts-estree: TSAbstractClassProperty */ if (node.abstract) { - node.type = AST_NODE_TYPES.TSAbstractClassProperty; + node.type = AST_NODE_TYPES.TSAbstractPropertyDefinition; delete node.abstract; node.value = null; } diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semanticInfo.test.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semanticInfo.test.ts.snap index 0fe1c5a0c57e..ce899d6636cb 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semanticInfo.test.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semanticInfo.test.ts.snap @@ -1292,7 +1292,7 @@ Object { 56, ], "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/lib/semanticInfo.test.ts b/packages/typescript-estree/tests/lib/semanticInfo.test.ts index ccce1f6e4c8d..c003d8d00466 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.test.ts @@ -161,7 +161,7 @@ describe('semanticInfo', () => { const computedPropertyString = ( (parseResult.ast.body[1] as TSESTree.ClassDeclaration).body - .body[0] as TSESTree.ClassProperty + .body[0] as TSESTree.PropertyDefinition ).key; const tsComputedPropertyString = parseResult.services.esTreeNodeToTSNodeMap.get(computedPropertyString); diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-properties.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-properties.src.ts.shot index ffbc6466b98a..34ca54e31bb1 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-properties.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-properties.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "value": null, }, Object { @@ -86,7 +86,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-readonly-property.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-readonly-property.src.ts.shot index 61229f3a6121..66cc1ade4909 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-readonly-property.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-abstract-readonly-property.src.ts.shot @@ -46,7 +46,7 @@ Object { ], "readonly": true, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-declare-properties.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-declare-properties.src.ts.shot index d6ebb066f30e..2ba070b7a0bc 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-declare-properties.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-declare-properties.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -120,7 +120,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -196,7 +196,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -271,7 +271,7 @@ Object { ], "readonly": true, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -347,7 +347,7 @@ Object { ], "readonly": true, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-override-property.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-override-property.src.ts.shot index 005003a72ef8..d22f508108d0 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-override-property.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-class-with-override-property.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "TSAbstractClassProperty", + "type": "TSAbstractPropertyDefinition", "value": null, }, ], diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-static-blocks.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-static-blocks.src.ts.shot index e370e9e24ea1..761783ccd1d6 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-static-blocks.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-static-blocks.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-accessibility-modifiers.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-accessibility-modifiers.src.ts.shot index 67d8ef501e3b..72b6e487ad10 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-accessibility-modifiers.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-accessibility-modifiers.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -121,7 +121,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-declare-properties.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-declare-properties.src.ts.shot index 5de10062c2d5..d42896e1914c 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-declare-properties.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-declare-properties.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -120,7 +120,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -195,7 +195,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -270,7 +270,7 @@ Object { ], "readonly": true, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -346,7 +346,7 @@ Object { ], "readonly": true, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -422,7 +422,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -498,7 +498,7 @@ Object { ], "readonly": true, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-definite-assignment.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-definite-assignment.src.ts.shot index 00d59df785b5..8c358d12d205 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-definite-assignment.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-definite-assignment.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-computed-property.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-computed-property.src.ts.shot index 31128d9a73bb..13e6452c8591 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-computed-property.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-computed-property.src.ts.shot @@ -47,7 +47,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-properties.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-properties.src.ts.shot index 3905565b51e0..048f000b9e2f 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-properties.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-properties.src.ts.shot @@ -193,7 +193,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -235,7 +235,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -312,7 +312,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -388,7 +388,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -431,7 +431,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -474,7 +474,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -516,7 +516,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -593,7 +593,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -670,7 +670,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-property-undefined.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-property-undefined.src.ts.shot index e3f8ea37bde3..c5fd8a1fd1aa 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-property-undefined.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-optional-property-undefined.src.ts.shot @@ -46,7 +46,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-override-property.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-override-property.src.ts.shot index 30a584f5f6da..49a33895f9d4 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-override-property.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-override-property.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-function.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-function.src.ts.shot index fbf79a0e8592..1705e340f45f 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-function.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-function.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { @@ -228,7 +228,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-values.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-values.src.ts.shot index e9ca7ddbc2ac..a2238f57f0db 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-values.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-property-values.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { @@ -103,7 +103,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { @@ -161,7 +161,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "elements": Array [], "loc": Object { @@ -219,7 +219,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { @@ -278,7 +278,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "arguments": Array [ Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-readonly-property.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-readonly-property.src.ts.shot index 23d24a88952e..c3fc1acb2af1 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-readonly-property.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/class-with-readonly-property.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": true, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/object-with-escaped-properties.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/object-with-escaped-properties.src.ts.shot index 8ef720aba18d..4373bd4bb671 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/object-with-escaped-properties.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/object-with-escaped-properties.src.ts.shot @@ -351,7 +351,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-method.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-method.src.ts.shot index d2ee9293ca2e..8f61a28350af 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-method.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-method.src.ts.shot @@ -195,7 +195,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "async": false, "body": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-method.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-method.src.ts.shot index e9af3cb75909..abfccafd6245 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-method.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-method.src.ts.shot @@ -228,7 +228,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "async": false, "body": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-method.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-method.src.ts.shot index f97f275b6905..9d4fe4c33ca7 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-method.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-method.src.ts.shot @@ -280,7 +280,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "async": false, "body": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/type-reference-comments.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/type-reference-comments.src.ts.shot index 4e24a2d0e02a..50ce8ec40fd5 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/type-reference-comments.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/type-reference-comments.src.ts.shot @@ -44,7 +44,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-instance-member.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-instance-member.src.ts.shot index 792ec79028f0..2dec783b8b49 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-instance-member.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-instance-member.src.ts.shot @@ -100,7 +100,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -197,7 +197,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": Object { "arguments": Array [], "callee": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-static-member.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-static-member.src.ts.shot index 616d39ed3670..53043405565c 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-static-member.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-factory-static-member.src.ts.shot @@ -120,7 +120,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -237,7 +237,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, ], diff --git a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-instance-member.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-instance-member.src.ts.shot index 7ba24aa3ef5d..95ccdf95a30a 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-instance-member.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-instance-member.src.ts.shot @@ -81,7 +81,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -159,7 +159,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, ], diff --git a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-static-member.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-static-member.src.ts.shot index 9e4706080cd7..60c9860bfd0b 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-static-member.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/decorators/property-decorators/property-decorator-static-member.src.ts.shot @@ -81,7 +81,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, Object { @@ -159,7 +159,7 @@ Object { ], "readonly": undefined, "static": true, - "type": "ClassProperty", + "type": "PropertyDefinition", "value": null, }, ], diff --git a/packages/typescript-estree/tests/snapshots/typescript/types/this-type-expanded.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/types/this-type-expanded.src.ts.shot index d936454024a7..5543bc1a3a01 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/types/this-type-expanded.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/types/this-type-expanded.src.ts.shot @@ -45,7 +45,7 @@ Object { ], "readonly": undefined, "static": false, - "type": "ClassProperty", + "type": "PropertyDefinition", "typeAnnotation": Object { "loc": Object { "end": Object { diff --git a/packages/visitor-keys/src/visitor-keys.ts b/packages/visitor-keys/src/visitor-keys.ts index 8bc422a0d056..8fba10df938d 100644 --- a/packages/visitor-keys/src/visitor-keys.ts +++ b/packages/visitor-keys/src/visitor-keys.ts @@ -48,6 +48,7 @@ const additionalKeys: AdditionalKeys = { MethodDefinition: ['decorators', 'key', 'value'], NewExpression: ['callee', 'typeParameters', 'arguments'], ObjectPattern: ['decorators', 'properties', 'typeAnnotation'], + PropertyDefinition: ['decorators', 'key', 'typeAnnotation', 'value'], RestElement: ['decorators', 'argument', 'typeAnnotation'], TaggedTemplateExpression: ['tag', 'typeParameters', 'quasi'], StaticBlock: [], @@ -59,13 +60,12 @@ const additionalKeys: AdditionalKeys = { JSXSpreadChild: ['expression'], // Additional Nodes. - ClassProperty: ['decorators', 'key', 'typeAnnotation', 'value'], Decorator: ['expression'], // TS-prefixed nodes - TSAbstractClassProperty: ['decorators', 'key', 'typeAnnotation'], TSAbstractKeyword: [], TSAbstractMethodDefinition: ['key', 'value'], + TSAbstractPropertyDefinition: ['decorators', 'key', 'typeAnnotation'], TSAnyKeyword: [], TSArrayType: ['elementType'], TSAsExpression: ['expression', 'typeAnnotation'],