Skip to content

Commit 1192332

Browse files
committed
Merge InstanceFieldSite and StaticFieldSite
1 parent 575d05f commit 1192332

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

src/analysis/class_fields.ts

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ import { AnalysisError } from "./error.js";
1313
*/
1414
export 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
*/
124102
export 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[] = [];

src/analysis/prop.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Expression, MemberExpression, TSMethodSignature, TSPropertySignatu
44
import { getOr, memberName } from "../utils.js";
55
import { AnalysisError } from "./error.js";
66
import type { LocalManager } from "./local.js";
7-
import { StaticFieldSite, InstanceFieldSite } from "./class_fields.js";
7+
import { ClassFieldSite } from "./class_fields.js";
88
import { trackMember } from "./track_member.js";
99
import { PreAnalysisResult } from "./pre.js";
1010

@@ -55,8 +55,8 @@ export type PropAlias = {
5555
* ```
5656
*/
5757
export function analyzeProps(
58-
propsObjSites: InstanceFieldSite[],
59-
defaultPropsObjSites: StaticFieldSite[],
58+
propsObjSites: ClassFieldSite[],
59+
defaultPropsObjSites: ClassFieldSite[],
6060
locals: LocalManager,
6161
preanalysis: PreAnalysisResult,
6262
): PropsObjAnalysis {
@@ -114,7 +114,7 @@ export function analyzeProps(
114114
}
115115

116116
function analyzeDefaultProps(
117-
defaultPropsSites: StaticFieldSite[],
117+
defaultPropsSites: ClassFieldSite[],
118118
): Map<string, NodePath<Expression>> | undefined {
119119
for (const site of defaultPropsSites) {
120120
if (!site.init) {

src/analysis/state.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getOr, memberName } from "../utils.js";
44
import { AnalysisError } from "./error.js";
55
import { PreAnalysisResult } from "./pre.js";
66
import type { LocalManager } from "./local.js";
7-
import type { InstanceFieldSite } from "./class_fields.js";
7+
import type { ClassFieldSite } from "./class_fields.js";
88
import { trackMember } from "./track_member.js";
99

1010
export type StateObjAnalysis = Map<string, StateAnalysis>;
@@ -44,8 +44,8 @@ export type StateTypeAnnotation = {
4444
};
4545

4646
export function analyzeState(
47-
stateObjSites: InstanceFieldSite[],
48-
setStateSites: InstanceFieldSite[],
47+
stateObjSites: ClassFieldSite[],
48+
setStateSites: ClassFieldSite[],
4949
locals: LocalManager,
5050
preanalysis: PreAnalysisResult,
5151
): StateObjAnalysis {

src/analysis/user_defined.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ArrowFunctionExpression, ClassMethod, ClassPrivateMethod, Expression, F
33
import { isClassMethodLike, nonNullPath } from "../utils.js";
44
import { AnalysisError } from "./error.js";
55
import { analyzeLibRef, isReactRef } from "./lib.js";
6-
import type { InstanceFieldSite } from "./class_fields.js";
6+
import type { ClassFieldSite } from "./class_fields.js";
77

88
const SPECIAL_MEMBER_NAMES = new Set<string>([
99
// Special variables
@@ -49,21 +49,21 @@ export type UserDefinedRef = {
4949
type: "user_defined_ref";
5050
localName?: string | undefined;
5151
typeAnnotation?: NodePath<TSType> | undefined;
52-
sites: InstanceFieldSite[];
52+
sites: ClassFieldSite[];
5353
};
5454
export type UserDefinedDirectRef = {
5555
type: "user_defined_direct_ref";
5656
localName?: string | undefined;
5757
init: NodePath<Expression>;
5858
typeAnnotation?: NodePath<TSType> | undefined;
59-
sites: InstanceFieldSite[];
59+
sites: ClassFieldSite[];
6060
};
6161
export type UserDefinedFn = {
6262
type: "user_defined_function";
6363
localName?: string | undefined;
6464
init: FnInit;
6565
typeAnnotation?: NodePath<TSType> | undefined;
66-
sites: InstanceFieldSite[];
66+
sites: ClassFieldSite[];
6767
};
6868

6969
export type FnInit = {
@@ -75,7 +75,7 @@ export type FnInit = {
7575
};
7676

7777
export function analyzeUserDefined(
78-
instanceFields: Map<string, InstanceFieldSite[]>
78+
instanceFields: Map<string, ClassFieldSite[]>
7979
): UserDefinedAnalysis {
8080
const fields = new Map<string, UserDefined>();
8181
for (const [name, fieldSites] of instanceFields) {

0 commit comments

Comments
 (0)