11import type { NodePath } from "@babel/core" ;
2- import type { AssignmentExpression , CallExpression , ClassAccessorProperty , ClassDeclaration , ClassMethod , ClassPrivateMethod , ClassPrivateProperty , ClassProperty , Expression , ExpressionStatement , MemberExpression , ThisExpression , TSDeclareMethod } from "@babel/types" ;
2+ import type { AssignmentExpression , CallExpression , ClassAccessorProperty , ClassDeclaration , ClassMethod , ClassPrivateMethod , ClassPrivateProperty , ClassProperty , Expression , ExpressionStatement , MemberExpression , ThisExpression , TSDeclareMethod , TSType } from "@babel/types" ;
33import { getOr , isClassAccessorProperty , isClassMethodLike , isClassMethodOrDecl , isClassPropertyLike , isNamedClassElement , isStaticBlock , memberName , memberRefName , nonNullPath } from "../utils.js" ;
44import { AnalysisError } from "./error.js" ;
55
@@ -11,19 +11,27 @@ export type ThisFields = {
1111export type ThisFieldSite = {
1212 type : "class_field" ;
1313 path : NodePath < ClassProperty | ClassPrivateProperty | ClassMethod | ClassPrivateMethod | ClassAccessorProperty | TSDeclareMethod | AssignmentExpression > ;
14- hasType : boolean ;
14+ typing : FieldTyping | undefined ;
1515 init : FieldInit | undefined ;
1616 hasWrite : undefined ;
1717 hasSideEffect : boolean ;
1818} | {
1919 type : "expr" ;
2020 path : NodePath < MemberExpression > ;
21- hasType : undefined ;
21+ typing : undefined ;
2222 init : undefined ;
2323 hasWrite : boolean ;
2424 hasSideEffect : undefined ;
2525} ;
2626
27+ export type FieldTyping = {
28+ type : "type_value" ;
29+ valueTypePath : NodePath < TSType > ;
30+ } | {
31+ type : "type_method" ;
32+ methodDeclPath : NodePath < TSDeclareMethod > ;
33+ }
34+
2735export type FieldInit = {
2836 type : "init_value" ;
2937 valuePath : NodePath < Expression >
@@ -35,7 +43,7 @@ export type FieldInit = {
3543export type StaticFieldSite = {
3644 type : "class_field" ;
3745 path : NodePath < ClassProperty | ClassPrivateProperty | ClassMethod | ClassPrivateMethod | ClassAccessorProperty | TSDeclareMethod | AssignmentExpression > ;
38- hasType : boolean ;
46+ typing : FieldTyping | undefined ;
3947 init : FieldInit | undefined ;
4048 hasWrite : undefined ;
4149 hasSideEffect : boolean ;
@@ -59,10 +67,17 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
5967 const field = isStatic ? getStaticField ( name ) : getThisField ( name ) ;
6068 if ( isClassPropertyLike ( itemPath ) ) {
6169 const valuePath = nonNullPath < Expression > ( itemPath . get ( "value" ) ) ;
70+ const typeAnnotation = itemPath . get ( "typeAnnotation" ) ;
71+ const typeAnnotation_ = typeAnnotation . isTSTypeAnnotation ( ) ? typeAnnotation : undefined ;
6272 field . push ( {
6373 type : "class_field" ,
6474 path : itemPath ,
65- hasType : ! ! itemPath . node . typeAnnotation ,
75+ typing : typeAnnotation_
76+ ? {
77+ type : "type_value" ,
78+ valueTypePath : typeAnnotation_ . get ( "typeAnnotation" ) ,
79+ }
80+ : undefined ,
6681 init : valuePath ? { type : "init_value" , valuePath } : undefined ,
6782 hasWrite : undefined ,
6883 hasSideEffect : ! ! itemPath . node . value && estimateSideEffect ( itemPath . node . value ) ,
@@ -76,7 +91,12 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
7691 field . push ( {
7792 type : "class_field" ,
7893 path : itemPath ,
79- hasType : itemPath . isTSDeclareMethod ( ) ,
94+ typing : itemPath . isTSDeclareMethod ( )
95+ ? {
96+ type : "type_method" ,
97+ methodDeclPath : itemPath ,
98+ }
99+ : undefined ,
80100 init : isClassMethodLike ( itemPath )
81101 ? { type : "init_method" , methodPath : itemPath }
82102 : undefined ,
@@ -171,7 +191,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
171191 field . push ( {
172192 type : "class_field" ,
173193 path : exprPath ,
174- hasType : false ,
194+ typing : undefined ,
175195 init : {
176196 type : "init_value" ,
177197 valuePath : exprPath . get ( "right" ) ,
@@ -216,7 +236,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
216236 field . push ( {
217237 type : "expr" ,
218238 path : thisMemberPath ,
219- hasType : undefined ,
239+ typing : undefined ,
220240 init : undefined ,
221241 hasWrite,
222242 hasSideEffect : undefined
@@ -235,7 +255,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
235255 if ( numInits > 1 ) {
236256 throw new AnalysisError ( `${ name } is initialized more than once` ) ;
237257 }
238- const numTypes = fieldSites . reduce ( ( n , site ) => n + Number ( ! ! site . hasType ) , 0 ) ;
258+ const numTypes = fieldSites . reduce ( ( n , site ) => n + Number ( ! ! site . typing ) , 0 ) ;
239259 if ( numTypes > 1 ) {
240260 throw new AnalysisError ( `${ name } is declared more than once` ) ;
241261 }
@@ -248,7 +268,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
248268 if ( numInits > 1 ) {
249269 throw new AnalysisError ( `static ${ name } is initialized more than once` ) ;
250270 }
251- const numTypes = fieldSites . reduce ( ( n , site ) => n + Number ( ! ! site . hasType ) , 0 ) ;
271+ const numTypes = fieldSites . reduce ( ( n , site ) => n + Number ( ! ! site . typing ) , 0 ) ;
252272 if ( numTypes > 1 ) {
253273 throw new AnalysisError ( `static ${ name } is declared more than once` ) ;
254274 }
0 commit comments