Skip to content

Commit

Permalink
feat: align class property representation with ESTree (#3806)
Browse files Browse the repository at this point in the history
Half of #3430
Closes #3077
  • Loading branch information
bradzacher committed Oct 11, 2021
1 parent 5823524 commit 22fa5c0
Show file tree
Hide file tree
Showing 81 changed files with 651 additions and 268 deletions.
4 changes: 2 additions & 2 deletions packages/ast-spec/src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum AST_NODE_TYPES {
ClassBody = 'ClassBody',
ClassDeclaration = 'ClassDeclaration',
ClassExpression = 'ClassExpression',
ClassProperty = 'ClassProperty',
ConditionalExpression = 'ConditionalExpression',
ContinueStatement = 'ContinueStatement',
DebuggerStatement = 'DebuggerStatement',
Expand Down Expand Up @@ -64,6 +63,7 @@ export enum AST_NODE_TYPES {
ObjectPattern = 'ObjectPattern',
Program = 'Program',
Property = 'Property',
PropertyDefinition = 'PropertyDefinition',
RestElement = 'RestElement',
ReturnStatement = 'ReturnStatement',
SequenceExpression = 'SequenceExpression',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
19 changes: 0 additions & 19 deletions packages/ast-spec/src/element/ClassProperty/spec.ts

This file was deleted.

19 changes: 19 additions & 0 deletions packages/ast-spec/src/element/PropertyDefinition/spec.ts
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 0 additions & 21 deletions packages/ast-spec/src/element/TSAbstractClassProperty/spec.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/ast-spec/src/element/TSAbstractPropertyDefinition/spec.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions packages/ast-spec/src/element/spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
8 changes: 4 additions & 4 deletions packages/ast-spec/src/unions/ClassElement.ts
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 4 additions & 4 deletions packages/ast-spec/src/unions/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -181,7 +181,6 @@ export type Node =
| ClassBody
| ClassDeclaration
| ClassExpression
| ClassProperty
| ConditionalExpression
| ContinueStatement
| DebuggerStatement
Expand Down Expand Up @@ -231,6 +230,7 @@ export type Node =
| ObjectPattern
| Program
| Property
| PropertyDefinition
| RestElement
| ReturnStatement
| SequenceExpression
Expand All @@ -245,9 +245,9 @@ export type Node =
| ThisExpression
| ThrowStatement
| TryStatement
| TSAbstractClassProperty
| TSAbstractKeyword
| TSAbstractMethodDefinition
| TSAbstractPropertyDefinition
| TSAnyKeyword
| TSArrayType
| TSAsExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default util.createRule<Options, MessageIds>({
}

return {
ClassProperty(node: TSESTree.ClassProperty): void {
PropertyDefinition(node: TSESTree.PropertyDefinition): void {
if (!node.readonly || node.declare) {
return;
}
Expand Down
26 changes: 16 additions & 10 deletions packages/eslint-plugin/src/rules/explicit-member-accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default util.createRule<Options, MessageIds>({
function getUnwantedPublicAccessibilityFixer(
node:
| TSESTree.MethodDefinition
| TSESTree.ClassProperty
| TSESTree.PropertyDefinition
| TSESTree.TSParameterProperty,
): TSESLint.ReportFixFunction {
return function (fixer: TSESLint.RuleFixer): TSESLint.RuleFix {
Expand Down Expand Up @@ -198,30 +198,36 @@ export default util.createRule<Options, MessageIds>({

/**
* 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,
);
}
Expand Down Expand Up @@ -273,7 +279,7 @@ export default util.createRule<Options, MessageIds>({

return {
TSParameterProperty: checkParameterPropertyAccessibilityModifier,
ClassProperty: checkPropertyAccessibilityModifier,
PropertyDefinition: checkPropertyAccessibilityModifier,
MethodDefinition: checkMethodAccessibilityModifier,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export default util.createRule<Options, MessageIds>({
}
return;

case AST_NODE_TYPES.ClassProperty:
case AST_NODE_TYPES.PropertyDefinition:
if (node.accessibility === 'private') {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin/src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;

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,
Expand All @@ -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,
Expand Down Expand Up @@ -136,7 +136,7 @@ export default util.createRule<Options, MessageIds>({
| 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 = {
Expand Down Expand Up @@ -168,7 +168,7 @@ export default util.createRule<Options, MessageIds>({
readonly: false,
declare: false,
...base,
} as TSESTree.ClassProperty;
} as TSESTree.PropertyDefinition;
}
}

Expand Down Expand Up @@ -330,8 +330,8 @@ export default util.createRule<Options, MessageIds>({
p =>
TSPropertySignatureToProperty(
p,
AST_NODE_TYPES.ClassProperty,
) as TSESTree.ClassProperty,
AST_NODE_TYPES.PropertyDefinition,
) as TSESTree.PropertyDefinition,
),

// location data
Expand Down
10 changes: 5 additions & 5 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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:
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 22fa5c0

Please sign in to comment.