Skip to content

Commit 6a346a1

Browse files
committed
Refactor: rename head -> preanalysis
1 parent c4f6e48 commit 6a346a1

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

src/analysis.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { getAndDelete } from "./utils.js";
88
import { analyzeProps, PropsObjAnalysis } from "./analysis/prop.js";
99
import { LocalManager, RemovableNode } from "./analysis/local.js";
1010
import { analyzeUserDefined, UserDefinedAnalysis } from "./analysis/user_defined.js";
11-
import { ComponentHead } from "./analysis/head.js";
11+
import { PreAnalysisResult } from "./analysis/head.js";
1212

1313
export { AnalysisError } from "./analysis/error.js";
1414

1515
export type { LibRef } from "./analysis/lib.js";
1616
export type {
17-
ComponentHead
17+
PreAnalysisResult
1818
} from "./analysis/head.js";
19-
export { analyzeHead } from "./analysis/head.js";
19+
export { preanalyzeClass } from "./analysis/head.js";
2020
export type { LocalManager } from "./analysis/local.js";
2121
export type { StateObjAnalysis } from "./analysis/state.js";
2222
export type { PropsObjAnalysis } from "./analysis/prop.js";
@@ -40,7 +40,7 @@ export type ComponentBody = {
4040

4141
export function analyzeBody(
4242
path: NodePath<ClassDeclaration>,
43-
head: ComponentHead
43+
preanalysis: PreAnalysisResult
4444
): ComponentBody {
4545
const locals = new LocalManager(path);
4646
const { thisFields: sites, staticFields } = analyzeThisFields(path);
@@ -50,7 +50,7 @@ export function analyzeBody(
5050

5151
const stateObjSites = getAndDelete(sites, "state") ?? [];
5252
const setStateSites = getAndDelete(sites, "setState") ?? [];
53-
const states = analyzeState(stateObjSites, setStateSites, locals, head);
53+
const states = analyzeState(stateObjSites, setStateSites, locals, preanalysis);
5454

5555
const renderSites = getAndDelete(sites, "render") ?? [];
5656

@@ -78,7 +78,7 @@ export function analyzeBody(
7878
if (!renderPath) {
7979
throw new AnalysisError(`Missing render method`);
8080
}
81-
const props = analyzeProps(propsObjSites, defaultPropsObjSites, locals, head);
81+
const props = analyzeProps(propsObjSites, defaultPropsObjSites, locals, preanalysis);
8282
for (const [name, propAnalysis] of props.props) {
8383
if (propAnalysis.needsAlias) {
8484
propAnalysis.newAliasName = locals.newLocal(

src/analysis/head.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { BlockStatement, ClassDeclaration, Identifier, Program, TSInterface
33
import { memberName, nonNullPath } from "../utils.js";
44
import { analyzeLibRef, isReactRef, LibRef } from "./lib.js";
55

6-
export type ComponentHead = {
6+
export type PreAnalysisResult = {
77
name?: Identifier | undefined;
88
typeParameters?: NodePath<TSTypeParameterDeclaration> | undefined;
99
superClassRef: LibRef;
@@ -13,7 +13,7 @@ export type ComponentHead = {
1313
states: Map<string, NodePath<TSPropertySignature | TSMethodSignature>>;
1414
};
1515

16-
export function analyzeHead(path: NodePath<ClassDeclaration>): ComponentHead | undefined {
16+
export function preanalyzeClass(path: NodePath<ClassDeclaration>): PreAnalysisResult | undefined {
1717
if (path.node.leadingComments?.some((comment) => /react-declassify-disable/.test(comment.value))) {
1818
// Explicitly disabled
1919
return;

src/analysis/prop.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AnalysisError } from "./error.js";
66
import type { LocalManager } from "./local.js";
77
import { StaticFieldSite, ThisFieldSite } from "./this_fields.js";
88
import { trackMember } from "./track_member.js";
9-
import { ComponentHead } from "./head.js";
9+
import { PreAnalysisResult } from "./head.js";
1010

1111
export type PropsObjAnalysis = {
1212
hasDefaults: boolean;
@@ -58,7 +58,7 @@ export function analyzeProps(
5858
propsObjSites: ThisFieldSite[],
5959
defaultPropsObjSites: StaticFieldSite[],
6060
locals: LocalManager,
61-
head: ComponentHead,
61+
preanalysis: PreAnalysisResult,
6262
): PropsObjAnalysis {
6363
const defaultProps = analyzeDefaultProps(defaultPropsObjSites);
6464
const newObjSites: PropsObjSite[] = [];
@@ -93,7 +93,7 @@ export function analyzeProps(
9393
}
9494
}
9595
}
96-
for (const [name, propTyping] of head.propsEach) {
96+
for (const [name, propTyping] of preanalysis.propsEach) {
9797
getProp(name).typing = propTyping;
9898
}
9999
if (defaultProps) {

src/analysis/state.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NodePath } from "@babel/core";
22
import type { CallExpression, Expression, Identifier, ObjectProperty, RestElement, TSType } from "@babel/types";
33
import { getOr, memberName } from "../utils.js";
44
import { AnalysisError } from "./error.js";
5-
import { ComponentHead } from "./head.js";
5+
import { PreAnalysisResult } from "./head.js";
66
import type { LocalManager } from "./local.js";
77
import type { ThisFieldSite } from "./this_fields.js";
88
import { trackMember } from "./track_member.js";
@@ -47,7 +47,7 @@ export function analyzeState(
4747
stateObjSites: ThisFieldSite[],
4848
setStateSites: ThisFieldSite[],
4949
locals: LocalManager,
50-
head: ComponentHead,
50+
preanalysis: PreAnalysisResult,
5151
): StateObjAnalysis {
5252
const states = new Map<string, StateAnalysis>();
5353
const getState = (name: string) => getOr(states, name, () => ({
@@ -151,7 +151,7 @@ export function analyzeState(
151151
throw new AnalysisError(`Non-analyzable setState`);
152152
}
153153
}
154-
for (const [name, stateType] of head.states) {
154+
for (const [name, stateType] of preanalysis.states) {
155155
const state = getState(name);
156156
if (stateType.isTSPropertySignature()) {
157157
const annot = stateType.get("typeAnnotation");

src/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ArrowFunctionExpression, ClassMethod, ClassPrivateMethod, Expression, FunctionDeclaration, FunctionExpression, Identifier, ImportDeclaration, MemberExpression, ObjectMethod, Pattern, RestElement, Statement, TSEntityName, TSType, TSTypeAnnotation, TSTypeParameterDeclaration, VariableDeclaration } from "@babel/types";
22
import type { NodePath, PluginObj, PluginPass } from "@babel/core";
33
import { assignReturnType, assignTypeAnnotation, assignTypeArguments, assignTypeParameters, importName, isTS, nonNullPath } from "./utils.js";
4-
import { AnalysisError, analyzeBody, analyzeHead, ComponentBody, ComponentHead, needsProps, LibRef } from "./analysis.js";
4+
import { AnalysisError, analyzeBody, preanalyzeClass, ComponentBody, PreAnalysisResult, needsProps, LibRef } from "./analysis.js";
55

66
type Options = {};
77

@@ -12,15 +12,15 @@ export default function plugin(babel: typeof import("@babel/core")): PluginObj<P
1212
visitor: {
1313
ClassDeclaration(path, state) {
1414
const ts = isTS(state);
15-
const head = analyzeHead(path);
16-
if (!head) {
15+
const preanalysis = preanalyzeClass(path);
16+
if (!preanalysis) {
1717
return;
1818
}
1919
if (path.parentPath.isExportDefaultDeclaration()) {
2020
const declPath = path.parentPath;
2121
try {
22-
const body = analyzeBody(path, head);
23-
const { funcNode, typeNode } = transformClass(head, body, { ts }, babel);
22+
const body = analyzeBody(path, preanalysis);
23+
const { funcNode, typeNode } = transformClass(preanalysis, body, { ts }, babel);
2424
if (path.node.id) {
2525
// Necessary to avoid false error regarding duplicate declaration.
2626
path.scope.removeBinding(path.node.id.name);
@@ -47,8 +47,8 @@ export default function plugin(babel: typeof import("@babel/core")): PluginObj<P
4747
}
4848
} else {
4949
try {
50-
const body = analyzeBody(path, head);
51-
const { funcNode, typeNode } = transformClass(head, body, { ts }, babel);
50+
const body = analyzeBody(path, preanalysis);
51+
const { funcNode, typeNode } = transformClass(preanalysis, body, { ts }, babel);
5252
// Necessary to avoid false error regarding duplicate declaration.
5353
path.scope.removeBinding(path.node.id.name);
5454
path.replaceWith(
@@ -77,7 +77,7 @@ type TransformResult = {
7777
typeNode?: TSType | undefined;
7878
};
7979

80-
function transformClass(head: ComponentHead, body: ComponentBody, options: { ts: boolean }, babel: typeof import("@babel/core")): TransformResult {
80+
function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, options: { ts: boolean }, babel: typeof import("@babel/core")): TransformResult {
8181
const { types: t } = babel;
8282
const { ts } = options;
8383

@@ -207,7 +207,7 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
207207
for (const field of body.state.values()) {
208208
// State declarations
209209
const call = t.callExpression(
210-
getReactImport("useState", babel, head.superClassRef),
210+
getReactImport("useState", babel, preanalysis.superClassRef),
211211
field.init ? [field.init.valuePath.node] : []
212212
);
213213
preamble.push(t.variableDeclaration("const", [
@@ -266,7 +266,7 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
266266
} else if (field.type === "user_defined_ref") {
267267
// const foo = useRef(null);
268268
const call = t.callExpression(
269-
getReactImport("useRef", babel, head.superClassRef),
269+
getReactImport("useRef", babel, preanalysis.superClassRef),
270270
[t.nullLiteral()]
271271
);
272272
preamble.push(t.variableDeclaration(
@@ -286,7 +286,7 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
286286
} else if (field.type === "user_defined_direct_ref") {
287287
// const foo = useRef(init);
288288
const call = t.callExpression(
289-
getReactImport("useRef", babel, head.superClassRef),
289+
getReactImport("useRef", babel, preanalysis.superClassRef),
290290
[field.init.node]
291291
);
292292
preamble.push(t.variableDeclaration(
@@ -309,26 +309,26 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
309309
bodyNode.body.splice(0, 0, ...preamble);
310310
// recast is not smart enough to correctly pretty-print type parameters for arrow functions.
311311
// so we fall back to functions when type parameters are present.
312-
const functionNeeded = head.isPure || !!head.typeParameters;
312+
const functionNeeded = preanalysis.isPure || !!preanalysis.typeParameters;
313313
const params = needsProps(body)
314314
? [assignTypeAnnotation(
315315
t.identifier("props"),
316316
// If the function is generic, put type annotations here instead of the `const` to be defined.
317317
// TODO: take children into account, while being careful about difference between `@types/react` v17 and v18
318-
head.typeParameters
319-
? head.props
320-
? t.tsTypeAnnotation(head.props.node)
318+
preanalysis.typeParameters
319+
? preanalysis.props
320+
? t.tsTypeAnnotation(preanalysis.props.node)
321321
: undefined
322322
: undefined
323323
)]
324324
: [];
325325
// If the function is generic, put type annotations here instead of the `const` to be defined.
326-
const returnType = head.typeParameters
326+
const returnType = preanalysis.typeParameters
327327
// Construct `React.ReactElement | null`
328328
? t.tsTypeAnnotation(
329329
t.tsUnionType([
330330
t.tsTypeReference(
331-
toTSEntity(getReactImport("ReactElement", babel, head.superClassRef), babel)
331+
toTSEntity(getReactImport("ReactElement", babel, preanalysis.superClassRef), babel)
332332
),
333333
t.tsNullKeyword(),
334334
])
@@ -338,7 +338,7 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
338338
assignReturnType(
339339
functionNeeded
340340
? t.functionExpression(
341-
head.name ? t.cloneNode(head.name) : undefined,
341+
preanalysis.name ? t.cloneNode(preanalysis.name) : undefined,
342342
params,
343343
bodyNode
344344
)
@@ -348,20 +348,20 @@ function transformClass(head: ComponentHead, body: ComponentBody, options: { ts:
348348
),
349349
returnType
350350
),
351-
head.typeParameters?.node
351+
preanalysis.typeParameters?.node
352352
);
353353
return {
354-
funcNode: head.isPure
354+
funcNode: preanalysis.isPure
355355
? t.callExpression(
356-
getReactImport("memo", babel, head.superClassRef),
356+
getReactImport("memo", babel, preanalysis.superClassRef),
357357
[funcNode]
358358
)
359359
: funcNode,
360-
typeNode: ts && !head.typeParameters
360+
typeNode: ts && !preanalysis.typeParameters
361361
? t.tsTypeReference(
362-
toTSEntity(getReactImport("FC", babel, head.superClassRef), babel),
363-
head.props
364-
? t.tsTypeParameterInstantiation([head.props.node])
362+
toTSEntity(getReactImport("FC", babel, preanalysis.superClassRef), babel),
363+
preanalysis.props
364+
? t.tsTypeParameterInstantiation([preanalysis.props.node])
365365
: null
366366
)
367367
: undefined,

0 commit comments

Comments
 (0)