Skip to content

Commit c22bb56

Browse files
committed
Refactor: rename: body -> analysis
1 parent e5ebcb9 commit c22bb56

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/analysis.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ const SPECIAL_STATIC_NAMES = new Set<string>([
3030
"getDerivedStateFromProps",
3131
]);
3232

33-
export type ComponentBody = {
33+
export type AnalysisResult = {
3434
locals: LocalManager,
3535
render: RenderAnalysis;
3636
state: StateObjAnalysis;
3737
props: PropsObjAnalysis;
3838
userDefined: UserDefinedAnalysis;
3939
};
4040

41-
export function analyzeBody(
41+
export function analyzeClass(
4242
path: NodePath<ClassDeclaration>,
4343
preanalysis: PreAnalysisResult
44-
): ComponentBody {
44+
): AnalysisResult {
4545
const locals = new LocalManager(path);
4646
const { thisFields: sites, staticFields } = analyzeThisFields(path);
4747

@@ -167,6 +167,6 @@ function analyzeOuterCapturings(classPath: NodePath<ClassDeclaration>, locals: L
167167
return capturings;
168168
}
169169

170-
export function needsProps(body: ComponentBody): boolean {
171-
return body.props.sites.length > 0;
170+
export function needsProps(analysis: AnalysisResult): boolean {
171+
return analysis.props.sites.length > 0;
172172
}

src/index.ts

Lines changed: 20 additions & 20 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, preanalyzeClass, ComponentBody, PreAnalysisResult, needsProps, LibRef } from "./analysis.js";
4+
import { AnalysisError, analyzeClass, preanalyzeClass, AnalysisResult, PreAnalysisResult, needsProps, LibRef } from "./analysis.js";
55

66
type Options = {};
77

@@ -19,8 +19,8 @@ export default function plugin(babel: typeof import("@babel/core")): PluginObj<P
1919
if (path.parentPath.isExportDefaultDeclaration()) {
2020
const declPath = path.parentPath;
2121
try {
22-
const body = analyzeBody(path, preanalysis);
23-
const { funcNode, typeNode } = transformClass(preanalysis, body, { ts }, babel);
22+
const analysis = analyzeClass(path, preanalysis);
23+
const { funcNode, typeNode } = transformClass(preanalysis, analysis, { 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, preanalysis);
51-
const { funcNode, typeNode } = transformClass(preanalysis, body, { ts }, babel);
50+
const analysis = analyzeClass(path, preanalysis);
51+
const { funcNode, typeNode } = transformClass(preanalysis, analysis, { ts }, babel);
5252
// Necessary to avoid false error regarding duplicate declaration.
5353
path.scope.removeBinding(path.node.id.name);
5454
path.replaceWith(
@@ -77,11 +77,11 @@ type TransformResult = {
7777
typeNode?: TSType | undefined;
7878
};
7979

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

84-
for (const [, prop] of body.props.props) {
84+
for (const [, prop] of analysis.props.props) {
8585
for (const alias of prop.aliases) {
8686
if (alias.localName !== prop.newAliasName!) {
8787
// Rename variables that props are bound to.
@@ -91,28 +91,28 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
9191
}
9292
}
9393
}
94-
for (const path of body.locals.removePaths) {
94+
for (const path of analysis.locals.removePaths) {
9595
path.remove();
9696
}
97-
for (const ren of body.render.renames) {
97+
for (const ren of analysis.render.renames) {
9898
// Rename local variables in the render method
9999
// to avoid unintentional variable capturing.
100100
ren.scope.rename(ren.oldName, ren.newName);
101101
}
102-
if (body.props.hasDefaults) {
103-
for (const [, prop] of body.props.props) {
102+
if (analysis.props.hasDefaults) {
103+
for (const [, prop] of analysis.props.props) {
104104
for (const site of prop.sites) {
105105
// this.props.foo -> foo
106106
site.path.replaceWith(t.identifier(prop.newAliasName!));
107107
}
108108
}
109109
} else {
110-
for (const site of body.props.sites) {
110+
for (const site of analysis.props.sites) {
111111
// this.props -> props
112112
site.path.replaceWith(site.path.node.property);
113113
}
114114
}
115-
for (const [, prop] of body.props.props) {
115+
for (const [, prop] of analysis.props.props) {
116116
if (prop.defaultValue && prop.typing) {
117117
// Make the prop optional
118118
prop.typing.node.optional = true;
@@ -141,7 +141,7 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
141141
}
142142
}
143143
}
144-
for (const [name, stateAnalysis] of body.state) {
144+
for (const [name, stateAnalysis] of analysis.state) {
145145
for (const site of stateAnalysis.sites) {
146146
if (site.type === "expr") {
147147
// this.state.foo -> foo
@@ -157,7 +157,7 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
157157
}
158158
}
159159
}
160-
for (const [, field] of body.userDefined.fields) {
160+
for (const [, field] of analysis.userDefined.fields) {
161161
if (field.type === "user_defined_function" || field.type === "user_defined_ref") {
162162
for (const site of field.sites) {
163163
if (site.type === "expr") {
@@ -181,7 +181,7 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
181181
}
182182
// Preamble is a set of statements to be added before the original render body.
183183
const preamble: Statement[] = [];
184-
const propsWithAlias = Array.from(body.props.props).filter(([, prop]) => prop.needsAlias);
184+
const propsWithAlias = Array.from(analysis.props.props).filter(([, prop]) => prop.needsAlias);
185185
if (propsWithAlias.length > 0) {
186186
// Expand this.props into variables.
187187
// E.g. const { foo, bar } = props;
@@ -204,7 +204,7 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
204204
),
205205
]));
206206
}
207-
for (const field of body.state.values()) {
207+
for (const field of analysis.state.values()) {
208208
// State declarations
209209
const call = t.callExpression(
210210
getReactImport("useState", babel, preanalysis.superClassRef),
@@ -233,7 +233,7 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
233233
)
234234
]))
235235
}
236-
for (const [, field] of body.userDefined.fields) {
236+
for (const [, field] of analysis.userDefined.fields) {
237237
if (field.type === "user_defined_function") {
238238
// Method definitions.
239239
if (field.init.type === "method") {
@@ -305,12 +305,12 @@ function transformClass(preanalysis: PreAnalysisResult, body: ComponentBody, opt
305305
))
306306
}
307307
}
308-
const bodyNode = body.render.path.node.body;
308+
const bodyNode = analysis.render.path.node.body;
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.
312312
const functionNeeded = preanalysis.isPure || !!preanalysis.typeParameters;
313-
const params = needsProps(body)
313+
const params = needsProps(analysis)
314314
? [assignTypeAnnotation(
315315
t.identifier("props"),
316316
// If the function is generic, put type annotations here instead of the `const` to be defined.

0 commit comments

Comments
 (0)