11import type { NodePath } from "@babel/core" ;
22import type { Scope } from "@babel/traverse" ;
3- import type { ClassDeclaration , ClassMethod , Identifier , JSXIdentifier , TSType , TSTypeParameterDeclaration } from "@babel/types" ;
3+ import type {
4+ ClassDeclaration ,
5+ ClassMethod ,
6+ Identifier ,
7+ JSXIdentifier ,
8+ TSType ,
9+ TSTypeParameterDeclaration ,
10+ } from "@babel/types" ;
411import { AnalysisError } from "./analysis/error.js" ;
512import { BindThisSite , analyzeClassFields } from "./analysis/class_fields.js" ;
613import { analyzeState , StateObjAnalysis } from "./analysis/state.js" ;
714import { getAndDelete } from "./utils.js" ;
815import { analyzeProps , needAlias , PropsObjAnalysis } from "./analysis/prop.js" ;
916import { LocalManager , RemovableNode } from "./analysis/local.js" ;
10- import { analyzeUserDefined , postAnalyzeCallbackDependencies , UserDefinedAnalysis } from "./analysis/user_defined.js" ;
17+ import {
18+ analyzeUserDefined ,
19+ postAnalyzeCallbackDependencies ,
20+ UserDefinedAnalysis ,
21+ } from "./analysis/user_defined.js" ;
1122import type { PreAnalysisResult } from "./analysis/pre.js" ;
1223import type { LibRef } from "./analysis/lib.js" ;
1324import { EffectAnalysis , analyzeEffects } from "./analysis/effect.js" ;
1425
1526export { AnalysisError } from "./analysis/error.js" ;
1627
1728export type { LibRef } from "./analysis/lib.js" ;
18- export type {
19- PreAnalysisResult
20- } from "./analysis/pre.js" ;
29+ export type { PreAnalysisResult } from "./analysis/pre.js" ;
2130export { preanalyzeClass } from "./analysis/pre.js" ;
2231export type { LocalManager } from "./analysis/local.js" ;
23- export type { StateObjAnalysis , SetStateSite , SetStateFieldSite } from "./analysis/state.js" ;
32+ export type {
33+ StateObjAnalysis ,
34+ SetStateSite ,
35+ SetStateFieldSite ,
36+ } from "./analysis/state.js" ;
2437export { needAlias } from "./analysis/prop.js" ;
2538export type { PropsObjAnalysis } from "./analysis/prop.js" ;
2639
@@ -53,18 +66,36 @@ export function analyzeClass(
5366 preanalysis : PreAnalysisResult
5467) : AnalysisResult {
5568 const locals = new LocalManager ( path ) ;
56- const { instanceFields : sites , staticFields, bindThisSites } = analyzeClassFields ( path ) ;
69+ const {
70+ instanceFields : sites ,
71+ staticFields,
72+ bindThisSites,
73+ } = analyzeClassFields ( path ) ;
5774
5875 const propsObjAnalysis = getAndDelete ( sites , "props" ) ?? { sites : [ ] } ;
59- const defaultPropsObjAnalysis = getAndDelete ( staticFields , "defaultProps" ) ?? { sites : [ ] } ;
76+ const defaultPropsObjAnalysis = getAndDelete (
77+ staticFields ,
78+ "defaultProps"
79+ ) ?? { sites : [ ] } ;
6080
6181 const stateObjAnalysis = getAndDelete ( sites , "state" ) ?? { sites : [ ] } ;
6282 const setStateAnalysis = getAndDelete ( sites , "setState" ) ?? { sites : [ ] } ;
63- const states = analyzeState ( stateObjAnalysis , setStateAnalysis , locals , preanalysis ) ;
83+ const states = analyzeState (
84+ stateObjAnalysis ,
85+ setStateAnalysis ,
86+ locals ,
87+ preanalysis
88+ ) ;
6489
65- const componentDidMount = getAndDelete ( sites , "componentDidMount" ) ?? { sites : [ ] } ;
66- const componentDidUpdate = getAndDelete ( sites , "componentDidUpdate" ) ?? { sites : [ ] } ;
67- const componentWillUnmount = getAndDelete ( sites , "componentWillUnmount" ) ?? { sites : [ ] } ;
90+ const componentDidMount = getAndDelete ( sites , "componentDidMount" ) ?? {
91+ sites : [ ] ,
92+ } ;
93+ const componentDidUpdate = getAndDelete ( sites , "componentDidUpdate" ) ?? {
94+ sites : [ ] ,
95+ } ;
96+ const componentWillUnmount = getAndDelete ( sites , "componentWillUnmount" ) ?? {
97+ sites : [ ] ,
98+ } ;
6899
69100 const renderAnalysis = getAndDelete ( sites , "render" ) ?? { sites : [ ] } ;
70101
@@ -92,13 +123,13 @@ export function analyzeClass(
92123 if ( ! renderPath ) {
93124 throw new AnalysisError ( `Missing render method` ) ;
94125 }
95- const props = analyzeProps ( propsObjAnalysis , defaultPropsObjAnalysis , locals , preanalysis ) ;
96- postAnalyzeCallbackDependencies (
97- userDefined ,
98- props ,
99- states ,
100- sites ,
126+ const props = analyzeProps (
127+ propsObjAnalysis ,
128+ defaultPropsObjAnalysis ,
129+ locals ,
130+ preanalysis
101131 ) ;
132+ postAnalyzeCallbackDependencies ( userDefined , props , states , sites ) ;
102133
103134 for ( const [ name , propAnalysis ] of props . props ) {
104135 if ( needAlias ( propAnalysis ) ) {
@@ -113,19 +144,25 @@ export function analyzeClass(
113144 componentDidMount ,
114145 componentDidUpdate ,
115146 componentWillUnmount ,
116- userDefined ,
147+ userDefined
117148 ) ;
118149
119150 const render = analyzeRender ( renderPath , locals ) ;
120151
121152 for ( const [ name , stateAnalysis ] of states . states . entries ( ) ) {
122153 const bindingPaths = stateAnalysis . sites . map ( ( site ) => site . path ) ;
123154 stateAnalysis . localName = locals . newLocal ( name , bindingPaths ) ;
124- stateAnalysis . localSetterName = locals . newLocal ( `set${ name . replace ( / ^ [ a - z ] / , ( s ) => s . toUpperCase ( ) ) } ` , bindingPaths ) ;
155+ stateAnalysis . localSetterName = locals . newLocal (
156+ `set${ name . replace ( / ^ [ a - z ] / , ( s ) => s . toUpperCase ( ) ) } ` ,
157+ bindingPaths
158+ ) ;
125159 }
126160
127161 for ( const [ name , field ] of userDefined . fields ) {
128- field . localName = locals . newLocal ( name , field . sites . map ( ( site ) => site . path ) ) ;
162+ field . localName = locals . newLocal (
163+ name ,
164+ field . sites . map ( ( site ) => site . path )
165+ ) ;
129166 }
130167
131168 if ( effects . cdmPath || effects . cduPath || effects . cwuPath ) {
@@ -157,20 +194,18 @@ export type RenderAnalysis = {
157194} ;
158195
159196export type LocalRename = {
160- scope : Scope ,
197+ scope : Scope ;
161198 oldName : string ;
162199 newName : string ;
163200} ;
164201
165202function analyzeRender (
166203 path : NodePath < ClassMethod > ,
167- locals : LocalManager ,
204+ locals : LocalManager
168205) : RenderAnalysis {
169206 const renames : LocalRename [ ] = [ ] ;
170207 for ( const [ name , binding ] of Object . entries ( path . scope . bindings ) ) {
171- if (
172- locals . allRemovePaths . has ( binding . path as NodePath < RemovableNode > )
173- ) {
208+ if ( locals . allRemovePaths . has ( binding . path as NodePath < RemovableNode > ) ) {
174209 // Already handled as an alias
175210 continue ;
176211 }
@@ -184,10 +219,13 @@ function analyzeRender(
184219 return { path, renames } ;
185220}
186221
187- function analyzeOuterCapturings ( classPath : NodePath < ClassDeclaration > , locals : LocalManager ) : Set < string > {
222+ function analyzeOuterCapturings (
223+ classPath : NodePath < ClassDeclaration > ,
224+ locals : LocalManager
225+ ) : Set < string > {
188226 const capturings = new Set < string > ( ) ;
189227 function visitIdent ( path : NodePath < Identifier | JSXIdentifier > ) {
190- path . getOuterBindingIdentifiers
228+ path . getOuterBindingIdentifiers ;
191229 const binding = path . scope . getBinding ( path . node . name ) ;
192230 if ( ! binding || binding . path . isAncestor ( classPath ) ) {
193231 capturings . add ( path . node . name ) ;
@@ -204,7 +242,7 @@ function analyzeOuterCapturings(classPath: NodePath<ClassDeclaration>, locals: L
204242 if ( path . isReferencedIdentifier ( ) ) {
205243 visitIdent ( path ) ;
206244 }
207- }
245+ } ,
208246 } ) ;
209247 return capturings ;
210248}
0 commit comments