Skip to content

Commit 7e43b0f

Browse files
committed
Implement def for ClassAccessorProperty.
1 parent 911fc17 commit 7e43b0f

File tree

6 files changed

+111
-37
lines changed

6 files changed

+111
-37
lines changed

src/def/babel-core.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export default function (fork: Fork) {
220220
def("ClassPrivateProperty"),
221221
def("ClassMethod"),
222222
def("ClassPrivateMethod"),
223+
def("ClassAccessorProperty"),
223224
def("StaticBlock"),
224225
);
225226

@@ -239,19 +240,40 @@ export default function (fork: Fork) {
239240
.build("key", "params", "body", "kind", "computed", "static")
240241
.field("key", def("PrivateName"));
241242

243+
def("ClassAccessorProperty")
244+
.bases("Declaration")
245+
.build("key", "value", "decorators", "computed", "static")
246+
.field("key", or(
247+
def("Literal"),
248+
def("Identifier"),
249+
def("PrivateName"),
250+
// Only when .computed is true (TODO enforce this)
251+
def("Expression"),
252+
))
253+
.field("value", def("Expression"));
254+
242255
["ClassMethod",
243256
"ClassPrivateMethod",
244257
].forEach(typeName => {
245258
def(typeName)
246259
.field("kind", or("get", "set", "method", "constructor"), () => "method")
247260
.field("body", def("BlockStatement"))
248-
.field("computed", Boolean, defaults["false"])
249-
.field("static", or(Boolean, null), defaults["null"])
250-
.field("abstract", or(Boolean, null), defaults["null"])
261+
// For backwards compatibility only. Expect accessibility instead (see below).
251262
.field("access", or("public", "private", "protected", null), defaults["null"])
263+
});
264+
265+
["ClassMethod",
266+
"ClassPrivateMethod",
267+
"ClassAccessorProperty",
268+
].forEach(typeName => {
269+
def(typeName)
270+
.field("computed", Boolean, defaults["false"])
271+
.field("static", Boolean, defaults["false"])
272+
.field("abstract", Boolean, defaults["false"])
252273
.field("accessibility", or("public", "private", "protected", null), defaults["null"])
253274
.field("decorators", or([def("Decorator")], null), defaults["null"])
254-
.field("optional", or(Boolean, null), defaults["null"]);
275+
.field("optional", Boolean, defaults["false"])
276+
.field("readonly", Boolean, defaults["false"]);
255277
});
256278

257279
var ObjectPatternProperty = or(

src/def/typescript.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,10 @@ export default function (fork: Fork) {
498498
def("ClassProperty")
499499
.field("access", // Not "accessibility"?
500500
or("public", "private", "protected", void 0),
501-
defaults["undefined"])
501+
defaults["undefined"]);
502+
503+
def("ClassAccessorProperty")
504+
.bases("Declaration", "TSHasOptionalTypeAnnotation");
502505

503506
// Defined already in es6 and babel-core.
504507
def("ClassBody")
@@ -508,6 +511,7 @@ export default function (fork: Fork) {
508511
def("ClassPropertyDefinition"),
509512
def("ClassProperty"),
510513
def("ClassPrivateProperty"),
514+
def("ClassAccessorProperty"),
511515
def("ClassMethod"),
512516
def("ClassPrivateMethod"),
513517
def("StaticBlock"),

src/gen/builders.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -901,11 +901,11 @@ export interface StaticBlockBuilder {
901901

902902
export interface ClassBodyBuilder {
903903
(
904-
body: (K.MethodDefinitionKind | K.VariableDeclaratorKind | K.ClassPropertyDefinitionKind | K.ClassPropertyKind | K.ClassPrivatePropertyKind | K.ClassMethodKind | K.ClassPrivateMethodKind | K.StaticBlockKind | K.TSDeclareMethodKind | K.TSCallSignatureDeclarationKind | K.TSConstructSignatureDeclarationKind | K.TSIndexSignatureKind | K.TSMethodSignatureKind | K.TSPropertySignatureKind)[]
904+
body: (K.MethodDefinitionKind | K.VariableDeclaratorKind | K.ClassPropertyDefinitionKind | K.ClassPropertyKind | K.ClassPrivatePropertyKind | K.ClassAccessorPropertyKind | K.ClassMethodKind | K.ClassPrivateMethodKind | K.StaticBlockKind | K.TSDeclareMethodKind | K.TSCallSignatureDeclarationKind | K.TSConstructSignatureDeclarationKind | K.TSIndexSignatureKind | K.TSMethodSignatureKind | K.TSPropertySignatureKind)[]
905905
): namedTypes.ClassBody;
906906
from(
907907
params: {
908-
body: (K.MethodDefinitionKind | K.VariableDeclaratorKind | K.ClassPropertyDefinitionKind | K.ClassPropertyKind | K.ClassPrivatePropertyKind | K.ClassMethodKind | K.ClassPrivateMethodKind | K.StaticBlockKind | K.TSDeclareMethodKind | K.TSCallSignatureDeclarationKind | K.TSConstructSignatureDeclarationKind | K.TSIndexSignatureKind | K.TSMethodSignatureKind | K.TSPropertySignatureKind)[];
908+
body: (K.MethodDefinitionKind | K.VariableDeclaratorKind | K.ClassPropertyDefinitionKind | K.ClassPropertyKind | K.ClassPrivatePropertyKind | K.ClassAccessorPropertyKind | K.ClassMethodKind | K.ClassPrivateMethodKind | K.StaticBlockKind | K.TSDeclareMethodKind | K.TSCallSignatureDeclarationKind | K.TSConstructSignatureDeclarationKind | K.TSIndexSignatureKind | K.TSMethodSignatureKind | K.TSPropertySignatureKind)[];
909909
comments?: K.CommentKind[] | null;
910910
loc?: K.SourceLocationKind | null;
911911
}
@@ -2757,11 +2757,11 @@ export interface ClassMethodBuilder {
27572757
params: K.PatternKind[],
27582758
body: K.BlockStatementKind,
27592759
computed?: boolean,
2760-
staticParam?: boolean | null
2760+
staticParam?: boolean
27612761
): namedTypes.ClassMethod;
27622762
from(
27632763
params: {
2764-
abstract?: boolean | null;
2764+
abstract?: boolean;
27652765
access?: "public" | "private" | "protected" | null;
27662766
accessibility?: "public" | "private" | "protected" | null;
27672767
async?: boolean;
@@ -2776,12 +2776,13 @@ export interface ClassMethodBuilder {
27762776
key: K.LiteralKind | K.IdentifierKind | K.ExpressionKind;
27772777
kind?: "get" | "set" | "method" | "constructor";
27782778
loc?: K.SourceLocationKind | null;
2779-
optional?: boolean | null;
2779+
optional?: boolean;
27802780
params: K.PatternKind[];
27812781
predicate?: K.FlowPredicateKind | null;
2782+
readonly?: boolean;
27822783
rest?: K.IdentifierKind | null;
27832784
returnType?: K.TypeAnnotationKind | K.TSTypeAnnotationKind | null;
2784-
static?: boolean | null;
2785+
static?: boolean;
27852786
typeParameters?: K.TypeParameterDeclarationKind | K.TSTypeParameterDeclarationKind | null;
27862787
}
27872788
): namedTypes.ClassMethod;
@@ -2794,11 +2795,11 @@ export interface ClassPrivateMethodBuilder {
27942795
body: K.BlockStatementKind,
27952796
kind?: "get" | "set" | "method" | "constructor",
27962797
computed?: boolean,
2797-
staticParam?: boolean | null
2798+
staticParam?: boolean
27982799
): namedTypes.ClassPrivateMethod;
27992800
from(
28002801
params: {
2801-
abstract?: boolean | null;
2802+
abstract?: boolean;
28022803
access?: "public" | "private" | "protected" | null;
28032804
accessibility?: "public" | "private" | "protected" | null;
28042805
async?: boolean;
@@ -2813,17 +2814,44 @@ export interface ClassPrivateMethodBuilder {
28132814
key: K.PrivateNameKind;
28142815
kind?: "get" | "set" | "method" | "constructor";
28152816
loc?: K.SourceLocationKind | null;
2816-
optional?: boolean | null;
2817+
optional?: boolean;
28172818
params: K.PatternKind[];
28182819
predicate?: K.FlowPredicateKind | null;
2820+
readonly?: boolean;
28192821
rest?: K.IdentifierKind | null;
28202822
returnType?: K.TypeAnnotationKind | K.TSTypeAnnotationKind | null;
2821-
static?: boolean | null;
2823+
static?: boolean;
28222824
typeParameters?: K.TypeParameterDeclarationKind | K.TSTypeParameterDeclarationKind | null;
28232825
}
28242826
): namedTypes.ClassPrivateMethod;
28252827
}
28262828

2829+
export interface ClassAccessorPropertyBuilder {
2830+
(
2831+
key: K.LiteralKind | K.IdentifierKind | K.PrivateNameKind | K.ExpressionKind,
2832+
value: K.ExpressionKind,
2833+
decorators?: K.DecoratorKind[] | null,
2834+
computed?: boolean,
2835+
staticParam?: boolean
2836+
): namedTypes.ClassAccessorProperty;
2837+
from(
2838+
params: {
2839+
abstract?: boolean;
2840+
accessibility?: "public" | "private" | "protected" | null;
2841+
comments?: K.CommentKind[] | null;
2842+
computed?: boolean;
2843+
decorators?: K.DecoratorKind[] | null;
2844+
key: K.LiteralKind | K.IdentifierKind | K.PrivateNameKind | K.ExpressionKind;
2845+
loc?: K.SourceLocationKind | null;
2846+
optional?: boolean;
2847+
readonly?: boolean;
2848+
static?: boolean;
2849+
typeAnnotation?: K.TSTypeAnnotationKind | null;
2850+
value: K.ExpressionKind;
2851+
}
2852+
): namedTypes.ClassAccessorProperty;
2853+
}
2854+
28272855
export interface RestPropertyBuilder {
28282856
(argument: K.ExpressionKind): namedTypes.RestProperty;
28292857
from(
@@ -3915,6 +3943,7 @@ export interface builders {
39153943
regExpLiteral: RegExpLiteralBuilder;
39163944
classMethod: ClassMethodBuilder;
39173945
classPrivateMethod: ClassPrivateMethodBuilder;
3946+
classAccessorProperty: ClassAccessorPropertyBuilder;
39183947
restProperty: RestPropertyBuilder;
39193948
forAwaitStatement: ForAwaitStatementBuilder;
39203949
import: ImportBuilder;

0 commit comments

Comments
 (0)