Skip to content

feat: align class property with estree stage3 class-features #3077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,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 @@ -200,10 +200,10 @@ export default util.createRule<Options, MessageIds>({

/**
* Checks if property has an accessibility modifier.
* @param classProperty The node representing a ClassProperty.
* @param classProperty The node representing a PropertyDefinition.
*/
function checkPropertyAccessibilityModifier(
classProperty: TSESTree.ClassProperty,
classProperty: TSESTree.PropertyDefinition,
): void {
const nodeType = 'class property';

Expand Down Expand Up @@ -275,7 +275,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 @@ -335,8 +335,8 @@ export default util.createRule<Options, MessageIds>({
}
return;

case AST_NODE_TYPES.ClassProperty:
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
if (node.accessibility === 'private') {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,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 @@ -130,7 +130,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 @@ -17,7 +17,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 @@ -33,7 +33,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 @@ -138,7 +138,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 @@ -170,7 +170,7 @@ export default util.createRule<Options, MessageIds>({
readonly: false,
declare: false,
...base,
} as TSESTree.ClassProperty;
} as TSESTree.PropertyDefinition;
}
}

Expand Down Expand Up @@ -333,8 +333,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 @@ -179,8 +179,8 @@ function getNodeType(node: Member): string | null {
return 'method';
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'constructor';
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.PropertyDefinition:
return node.value && functionExpressions.includes(node.value.type)
? 'method'
: 'field';
Expand All @@ -206,8 +206,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 @@ -265,7 +265,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
26 changes: 13 additions & 13 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export default util.createRule<Options, MessageIds>({
validator: ValidatorFunction | null,
node:
| TSESTree.PropertyNonComputedName
| TSESTree.ClassPropertyNonComputedName
| TSESTree.TSAbstractClassPropertyNonComputedName
| TSESTree.PropertyDefinitionNonComputedName
| TSESTree.TSAbstractPropertyDefinitionNonComputedName
| TSESTree.TSPropertySignatureNonComputedName
| TSESTree.MethodDefinitionNonComputedName
| TSESTree.TSAbstractMethodDefinitionNonComputedName
Expand All @@ -125,8 +125,8 @@ export default util.createRule<Options, MessageIds>({

function getMemberModifiers(
node:
| TSESTree.ClassProperty
| TSESTree.TSAbstractClassProperty
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.MethodDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSParameterProperty,
Expand All @@ -144,7 +144,7 @@ export default util.createRule<Options, MessageIds>({
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);
Expand Down Expand Up @@ -331,10 +331,10 @@ export default util.createRule<Options, MessageIds>({
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);
Expand Down Expand Up @@ -369,14 +369,14 @@ export default util.createRule<Options, MessageIds>({
},

[[
':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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-extra-semi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default util.createRule<Options, MessageIds>({

return {
...rules,
ClassProperty(node): void {
PropertyDefinition(node): void {
rules.MethodDefinition(node as never);
},
};
Expand Down
8 changes: 5 additions & 3 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default util.createRule<Options, MessageIds>({
node:
| TSESTree.VariableDeclarator
| TSESTree.Parameter
| TSESTree.ClassProperty,
| TSESTree.PropertyDefinition,
typeNode: TSESTree.TSTypeAnnotation | undefined,
initNode: TSESTree.Expression | null | undefined,
): void {
Expand Down Expand Up @@ -252,7 +252,9 @@ export default util.createRule<Options, MessageIds>({
});
}

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
Expand All @@ -268,7 +270,7 @@ export default util.createRule<Options, MessageIds>({
FunctionExpression: inferrableParameterVisitor,
FunctionDeclaration: inferrableParameterVisitor,
ArrowFunctionExpression: inferrableParameterVisitor,
ClassProperty: inferrablePropertyVisitor,
PropertyDefinition: inferrablePropertyVisitor,
};
},
});
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-invalid-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export default createRule<Options, MessageIds>({

return {
...rules,
ClassProperty(): void {
PropertyDefinition(): void {
thisIsValidStack.push(true);
},
'ClassProperty:exit'(): void {
'PropertyDefinition:exit'(): void {
thisIsValidStack.pop();
},
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-invalid-void-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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[] = [
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-magic-numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function isTSNumericLiteralType(node: TSESTree.Node): boolean {
function isParentTSReadonlyClassProperty(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;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/no-unsafe-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,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!,
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export default util.createRule<Options, MessageIds>({
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:
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/semi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default util.createRule<Options, MessageIds>({
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,
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default util.createRule<[Options], MessageIds>({
checkParameters(node.params);
}
},
ClassProperty(node): void {
PropertyDefinition(node): void {
if (node.value && isVariableDeclarationIgnoreFunction(node.value)) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function isVariableDeclaratorWithTypeAnnotation(
*/
function isClassPropertyWithTypeAnnotation(
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
);
}

/**
Expand Down Expand Up @@ -276,7 +278,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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/rules/indent/indent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class Foo {
],
},
{
node: AST_NODE_TYPES.TSAbstractClassProperty,
node: AST_NODE_TYPES.TSAbstractPropertyDefinition,
code: [
`
class Foo {
Expand Down
Loading