@@ -13,21 +13,26 @@ import { AnalysisError } from "./error.js";
1313 */
1414export type ClassFieldsAnalysis = {
1515 /** Access to instance fields (`this.foo`), indexed by their names. */
16- instanceFields : Map < string , InstanceFieldSite [ ] > ;
16+ instanceFields : Map < string , ClassFieldSite [ ] > ;
1717 /** Access to static fields (`C.foo`, where `C` is the class), indexed by their names. */
18- staticFields : Map < string , StaticFieldSite [ ] > ;
18+ staticFields : Map < string , ClassFieldSite [ ] > ;
1919} ;
2020
2121/**
22- * A place where the instance field is declared or used.
22+ * A place where the class field is declared or used.
2323 */
24- export type InstanceFieldSite = {
24+ export type ClassFieldSite =
25+ | ClassFieldDeclSite
26+ | ClassFieldExprSite ;
27+
28+ export type ClassFieldDeclSite = {
2529 type : "decl" ;
2630 /**
2731 * Declaration. One of:
2832 *
2933 * - Class element (methods, fields, etc.)
30- * - Assignment to `this` in the constructor
34+ * - Assignment to `this` in the constructor (instance case)
35+ * - Assignment to `this` in a static initialization block (static case)
3136 */
3237 path : NodePath < ClassProperty | ClassPrivateProperty | ClassMethod | ClassPrivateMethod | ClassAccessorProperty | TSDeclareMethod | AssignmentExpression > ;
3338 /**
@@ -45,7 +50,9 @@ export type InstanceFieldSite = {
4550 * true if the initializer has a side effect.
4651 */
4752 hasSideEffect : boolean ;
48- } | {
53+ } ;
54+
55+ export type ClassFieldExprSite = {
4956 type : "expr" ;
5057 /**
5158 * The node that accesses the field (both read and write)
@@ -86,45 +93,16 @@ export type FieldInit = {
8693 methodPath : NodePath < ClassMethod | ClassPrivateMethod > ;
8794} ;
8895
89- /**
90- * A place where the static field is declared or used.
91- */
92- export type StaticFieldSite = {
93- type : "decl" ;
94- /**
95- * Declaration. One of:
96- *
97- * - Class element (methods, fields, etc.)
98- * - Assignment to `this` in a static initialization block
99- */
100- path : NodePath < ClassProperty | ClassPrivateProperty | ClassMethod | ClassPrivateMethod | ClassAccessorProperty | TSDeclareMethod | AssignmentExpression > ;
101- /**
102- * Type annotation, if any.
103- *
104- * Param/return annotations attached to function-like implementations are ignored.
105- */
106- typing : FieldTyping | undefined ;
107- /**
108- * Initializing expression, if any.
109- */
110- init : FieldInit | undefined ;
111- hasWrite : undefined ;
112- /**
113- * true if the initializer has a side effect.
114- */
115- hasSideEffect : boolean ;
116- } ;
117-
11896/**
11997 * Collect declarations and uses of the following:
12098 *
12199 * - Instance fields ... `this.foo`
122100 * - Static fields ... `C.foo`, where `C` is the class
123101 */
124102export function analyzeClassFields ( path : NodePath < ClassDeclaration > ) : ClassFieldsAnalysis {
125- const instanceFields = new Map < string , InstanceFieldSite [ ] > ( ) ;
103+ const instanceFields = new Map < string , ClassFieldSite [ ] > ( ) ;
126104 const getInstanceField = ( name : string ) => getOr ( instanceFields , name , ( ) => [ ] ) ;
127- const staticFields = new Map < string , StaticFieldSite [ ] > ( ) ;
105+ const staticFields = new Map < string , ClassFieldSite [ ] > ( ) ;
128106 const getStaticField = ( name : string ) => getOr ( staticFields , name , ( ) => [ ] ) ;
129107 let constructor : NodePath < ClassMethod > | undefined = undefined ;
130108 const bodies : NodePath [ ] = [ ] ;
0 commit comments