Skip to content

Commit f83feb0

Browse files
committed
Refactor: rename ThisFields as ClassFields
1 parent 646f13e commit f83feb0

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

src/analysis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NodePath } from "@babel/core";
22
import type { Scope } from "@babel/traverse";
33
import type { ClassDeclaration, ClassMethod, Identifier, JSXIdentifier, TSType, TSTypeParameterDeclaration } from "@babel/types";
44
import { AnalysisError } from "./analysis/error.js";
5-
import { analyzeThisFields } from "./analysis/this_fields.js";
5+
import { analyzeClassFields } from "./analysis/this_fields.js";
66
import { analyzeState, StateObjAnalysis } from "./analysis/state.js";
77
import { getAndDelete } from "./utils.js";
88
import { analyzeProps, PropsObjAnalysis } from "./analysis/prop.js";
@@ -49,7 +49,7 @@ export function analyzeClass(
4949
preanalysis: PreAnalysisResult
5050
): AnalysisResult {
5151
const locals = new LocalManager(path);
52-
const { thisFields: sites, staticFields } = analyzeThisFields(path);
52+
const { instanceFields: sites, staticFields } = analyzeClassFields(path);
5353

5454
const propsObjSites = getAndDelete(sites, "props") ?? [];
5555
const defaultPropsObjSites = getAndDelete(staticFields, "defaultProps") ?? [];

src/analysis/prop.ts

Lines changed: 2 additions & 2 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, ThisFieldSite } from "./this_fields.js";
7+
import { StaticFieldSite, InstanceFieldSite } from "./this_fields.js";
88
import { trackMember } from "./track_member.js";
99
import { PreAnalysisResult } from "./pre.js";
1010

@@ -55,7 +55,7 @@ export type PropAlias = {
5555
* ```
5656
*/
5757
export function analyzeProps(
58-
propsObjSites: ThisFieldSite[],
58+
propsObjSites: InstanceFieldSite[],
5959
defaultPropsObjSites: StaticFieldSite[],
6060
locals: LocalManager,
6161
preanalysis: PreAnalysisResult,

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 { ThisFieldSite } from "./this_fields.js";
7+
import type { InstanceFieldSite } from "./this_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: ThisFieldSite[],
48-
setStateSites: ThisFieldSite[],
47+
stateObjSites: InstanceFieldSite[],
48+
setStateSites: InstanceFieldSite[],
4949
locals: LocalManager,
5050
preanalysis: PreAnalysisResult,
5151
): StateObjAnalysis {

src/analysis/this_fields.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import { AnalysisError } from "./error.js";
1111
/**
1212
* Aggregated result of class field analysis.
1313
*/
14-
export type ThisFields = {
14+
export type ClassFieldsAnalysis = {
1515
/** Access to instance fields (`this.foo`), indexed by their names. */
16-
thisFields: Map<string, ThisFieldSite[]>;
16+
instanceFields: Map<string, InstanceFieldSite[]>;
1717
/** Access to static fields (`C.foo`, where `C` is the class), indexed by their names. */
1818
staticFields: Map<string, StaticFieldSite[]>;
1919
};
2020

2121
/**
2222
* A place where the instance field is declared or used.
2323
*/
24-
export type ThisFieldSite = {
24+
export type InstanceFieldSite = {
2525
type: "class_field";
2626
/**
2727
* Declaration. One of:
@@ -121,9 +121,9 @@ export type StaticFieldSite = {
121121
* - Instance fields ... `this.foo`
122122
* - Static fields ... `C.foo`, where `C` is the class
123123
*/
124-
export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields {
125-
const thisFields = new Map<string, ThisFieldSite[]>();
126-
const getThisField = (name: string) => getOr(thisFields, name, () => []);
124+
export function analyzeClassFields(path: NodePath<ClassDeclaration>): ClassFieldsAnalysis {
125+
const instanceFields = new Map<string, InstanceFieldSite[]>();
126+
const getInstanceField = (name: string) => getOr(instanceFields, name, () => []);
127127
const staticFields = new Map<string, StaticFieldSite[]>();
128128
const getStaticField = (name: string) => getOr(staticFields, name, () => []);
129129
let constructor: NodePath<ClassMethod> | undefined = undefined;
@@ -137,7 +137,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
137137
if (name == null) {
138138
throw new AnalysisError(`Unnamed class element`);
139139
}
140-
const field = isStatic ? getStaticField(name) : getThisField(name);
140+
const field = isStatic ? getStaticField(name) : getInstanceField(name);
141141
if (isClassPropertyLike(itemPath)) {
142142
// Class field.
143143
// - May have an initializer: `foo = 42;` or not: `foo;`
@@ -273,7 +273,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
273273
}
274274
// TODO: check for parameter/local variable reference
275275

276-
const field = getThisField(name)!;
276+
const field = getInstanceField(name)!;
277277
field.push({
278278
type: "class_field",
279279
path: exprPath,
@@ -305,7 +305,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
305305
throw new AnalysisError(`Unrecognized this-property reference`);
306306
}
307307

308-
const field = getThisField(name)!;
308+
const field = getInstanceField(name)!;
309309

310310
const thisMemberParentPath = thisMemberPath.parentPath;
311311
const hasWrite =
@@ -334,9 +334,9 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
334334
}
335335

336336
// Post validation
337-
for (const [name, fieldSites] of thisFields) {
337+
for (const [name, fieldSites] of instanceFields) {
338338
if (fieldSites.length === 0) {
339-
thisFields.delete(name);
339+
instanceFields.delete(name);
340340
}
341341
const numInits = fieldSites.reduce((n, site) => n + Number(!!site.init), 0);
342342
if (numInits > 1) {
@@ -349,7 +349,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
349349
}
350350
for (const [name, fieldSites] of staticFields) {
351351
if (fieldSites.length === 0) {
352-
thisFields.delete(name);
352+
instanceFields.delete(name);
353353
}
354354
const numInits = fieldSites.reduce((n, site) => n + Number(!!site.init), 0);
355355
if (numInits > 1) {
@@ -361,7 +361,7 @@ export function analyzeThisFields(path: NodePath<ClassDeclaration>): ThisFields
361361
}
362362
}
363363

364-
return { thisFields, staticFields };
364+
return { instanceFields, staticFields };
365365
}
366366

367367
function traverseThis(path: NodePath, visit: (path: NodePath<ThisExpression>) => void) {

src/analysis/user_defined.ts

Lines changed: 6 additions & 6 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 { ThisFieldSite } from "./this_fields.js";
6+
import type { InstanceFieldSite } from "./this_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: ThisFieldSite[];
52+
sites: InstanceFieldSite[];
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: ThisFieldSite[];
59+
sites: InstanceFieldSite[];
6060
};
6161
export type UserDefinedFn = {
6262
type: "user_defined_function";
6363
localName?: string | undefined;
6464
init: FnInit;
6565
typeAnnotation?: NodePath<TSType> | undefined;
66-
sites: ThisFieldSite[];
66+
sites: InstanceFieldSite[];
6767
};
6868

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

7777
export function analyzeUserDefined(
78-
thisFields: Map<string, ThisFieldSite[]>
78+
instanceFields: Map<string, InstanceFieldSite[]>
7979
): UserDefinedAnalysis {
8080
const fields = new Map<string, UserDefined>();
81-
for (const [name, fieldSites] of thisFields) {
81+
for (const [name, fieldSites] of instanceFields) {
8282
if (SPECIAL_MEMBER_NAMES.has(name)) {
8383
throw new AnalysisError(`Cannot transform ${name}`);
8484
}

0 commit comments

Comments
 (0)