Skip to content
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
5 changes: 2 additions & 3 deletions packages/ast-spec/src/parameter/TSParameterProperty/spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { Accessibility } from '../../base/Accessibility';
import type { BaseNode } from '../../base/BaseNode';
import { Identifier } from '../../expression/spec';
import type { Decorator } from '../../special/Decorator/spec';
import type { BindingName } from '../../unions/BindingName';
import type { AssignmentPattern } from '../AssignmentPattern/spec';
import type { RestElement } from '../RestElement/spec';

export interface TSParameterProperty extends BaseNode {
type: AST_NODE_TYPES.TSParameterProperty;
accessibility: Accessibility | undefined;
decorators: Decorator[];
override: boolean;
parameter: AssignmentPattern | BindingName | RestElement;
parameter: Identifier | (AssignmentPattern & { left: Identifier });
readonly: boolean;
static: boolean;
}
41 changes: 40 additions & 1 deletion packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,46 @@ export class Converter {
expression: false, // is not present in ESTreeNode
generator: false,
id: null,
params: this.convertParameters(node.parameters),
params: node.parameters.map((param: ts.ParameterDeclaration) => {
const parameter = this.convertChild(param) as TSESTree.Parameter;
parameter.decorators = this.convertChildren(
getDecorators(param) ?? [],
);

const hasAccessModifier =
hasModifier(param, SyntaxKind.PrivateKeyword) ||
hasModifier(param, SyntaxKind.PublicKeyword) ||
hasModifier(param, SyntaxKind.ProtectedKeyword);
const hasReadonlyModifier = hasModifier(
param,
SyntaxKind.ReadonlyKeyword,
);

if (hasAccessModifier || hasReadonlyModifier) {
const isIdentifier = parameter.type === AST_NODE_TYPES.Identifier;
const isAssignmentToIdentifier =
parameter.type === AST_NODE_TYPES.AssignmentPattern &&
(parameter.left as TSESTree.Node).type ===
AST_NODE_TYPES.Identifier;

if (!isIdentifier && !isAssignmentToIdentifier) {
this.#throwError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this part move to check-modifiers.ts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cant change the project structure and the function is used any many places

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fisker is right, I think. This newly added logic duplicates what's already in convertParameters. It'd be better to add these new modifier checks to the existing area of code that checks modifiers.

param,
'Invalid TS parameter property: only `Identifier` or `AssignmentPattern` (with Identifier left-hand side) are allowed.',
);
}
return this.createNode(param, {
type: AST_NODE_TYPES.TSParameterProperty,
accessibility: getTSNodeAccessibility(param),
readonly: hasReadonlyModifier,
override: hasModifier(param, SyntaxKind.OverrideKeyword),
static: hasModifier(param, SyntaxKind.StaticKeyword),
decorators: this.convertChildren(getDecorators(param) ?? []),
parameter,
});
}
return parameter;
}),
returnType: node.type && this.convertTypeAnnotation(node.type, node),
typeParameters:
node.typeParameters &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// class Invalid1 {
// constructor(private [foo] = [1]) {}
// }
// class Invalid2 {
// constructor(private { bar } = { bar: 1 }) {}
// }
// class Invalid3 {
// constructor(private ...baz) {}
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Valid1 {
constructor(private foo: number) {}
}
class Valid2 {
constructor(private foo = 1) {}
}
class Valid3 {
constructor(public readonly bar?: string) {}
}
Loading