|
| 1 | +import type { ClassMethod } from "@babel/types"; |
| 2 | +import type { NodePath } from "@babel/traverse"; |
| 3 | +import type { ClassFieldAnalysis } from "./class_fields.js"; |
| 4 | +import { AnalysisError } from "./error.js"; |
| 5 | +import type { UserDefinedAnalysis } from "./user_defined.js"; |
| 6 | + |
| 7 | +export type EffectAnalysis = { |
| 8 | + cdmPath: NodePath<ClassMethod> | undefined; |
| 9 | + cduPath: NodePath<ClassMethod> | undefined; |
| 10 | + cwuPath: NodePath<ClassMethod> | undefined; |
| 11 | + isMountedLocalName?: string | undefined; |
| 12 | + cleanupLocalName?: string | undefined; |
| 13 | +}; |
| 14 | +export function analyzeEffects( |
| 15 | + componentDidMount: ClassFieldAnalysis, |
| 16 | + componentDidUpdate: ClassFieldAnalysis, |
| 17 | + componentWillUnmount: ClassFieldAnalysis, |
| 18 | + userDefined: UserDefinedAnalysis, |
| 19 | +): EffectAnalysis { |
| 20 | + const cdmInit = componentDidMount.sites.find((site) => site.init); |
| 21 | + const cduInit = componentDidUpdate.sites.find((site) => site.init); |
| 22 | + const cwuInit = componentWillUnmount.sites.find((site) => site.init); |
| 23 | + if (componentDidMount.sites.some((site) => !site.init)) { |
| 24 | + throw new AnalysisError("Do not use componentDidMount by yourself"); |
| 25 | + } |
| 26 | + if (componentDidUpdate.sites.some((site) => !site.init)) { |
| 27 | + throw new AnalysisError("Do not use componentDidUpdate by yourself"); |
| 28 | + } |
| 29 | + if (componentWillUnmount.sites.some((site) => !site.init)) { |
| 30 | + throw new AnalysisError("Do not use componentWillUnmount by yourself"); |
| 31 | + } |
| 32 | + let cdmPath: NodePath<ClassMethod> | undefined = undefined; |
| 33 | + let cduPath: NodePath<ClassMethod> | undefined = undefined; |
| 34 | + let cwuPath: NodePath<ClassMethod> | undefined = undefined; |
| 35 | + if (cdmInit) { |
| 36 | + if (!cdmInit.path.isClassMethod()) { |
| 37 | + throw new AnalysisError("Not a class method: componentDidMount"); |
| 38 | + } |
| 39 | + if (cdmInit.path.node.params.length > 0) { |
| 40 | + throw new AnalysisError("Invalid parameter of componentDidMount"); |
| 41 | + } |
| 42 | + cdmPath = cdmInit.path; |
| 43 | + } |
| 44 | + if (cduInit) { |
| 45 | + if (!cduInit.path.isClassMethod()) { |
| 46 | + throw new AnalysisError("Not a class method: componentDidUpdate"); |
| 47 | + } |
| 48 | + if (cduInit.path.node.params.length > 0) { |
| 49 | + throw new AnalysisError("Not supported: componentDidUpdate parameters"); |
| 50 | + } |
| 51 | + cduPath = cduInit.path; |
| 52 | + } |
| 53 | + if (cwuInit) { |
| 54 | + if (!cwuInit.path.isClassMethod()) { |
| 55 | + throw new AnalysisError("Not a class method: componentWillUnmount"); |
| 56 | + } |
| 57 | + if (cwuInit.path.node.params.length > 0) { |
| 58 | + throw new AnalysisError("Invalid parameter of componentWillUnmount"); |
| 59 | + } |
| 60 | + cwuPath = cwuInit.path; |
| 61 | + } |
| 62 | + |
| 63 | + for (const [name, field] of userDefined.fields) { |
| 64 | + if ( |
| 65 | + field.type === "user_defined_function" |
| 66 | + && field.sites.some((site) => |
| 67 | + site.type === "expr" |
| 68 | + && site.owner === "componentWillUnmount" |
| 69 | + && !site.path.parentPath.isCallExpression() |
| 70 | + ) |
| 71 | + ) { |
| 72 | + // A user-defined function is used without immediately calling in componentWillUnmount. |
| 73 | + // This is likely the following idiom: |
| 74 | + // |
| 75 | + // ```js |
| 76 | + // onMouseOver = () => { |
| 77 | + // ... |
| 78 | + // } |
| 79 | + // componentDidMount() { |
| 80 | + // this.div.addEventListener("mouseover", this.onMouseOver); |
| 81 | + // } |
| 82 | + // componentWillUnmount() { |
| 83 | + // this.div.removeEventListener("mouseover", this.onMouseOver); |
| 84 | + // } |
| 85 | + // ``` |
| 86 | + // |
| 87 | + // It may break in our "raw effect" transformation |
| 88 | + // because function identity may change over time. |
| 89 | + // |
| 90 | + // We will implement a separate paths for the patterns above, |
| 91 | + // but for now we just error out to avoid risks. |
| 92 | + |
| 93 | + throw new AnalysisError(`Possible event unregistration of ${name} in componentWillUnmount`); |
| 94 | + } |
| 95 | + } |
| 96 | + return { |
| 97 | + cdmPath, |
| 98 | + cduPath, |
| 99 | + cwuPath, |
| 100 | + }; |
| 101 | +} |
0 commit comments