|
| 1 | +// Shared by the Babel plugin and native compiler tests (#3828): a name in a |
| 2 | +// TypeScript type position is erased before the code runs, so an SSR props |
| 3 | +// constructor must never be handed it as a captured value. Each case lists, |
| 4 | +// per `new _P$(…)` in output order, the bindings passed as captures; the |
| 5 | +// output is then type-stripped and executed, and must agree with the |
| 6 | +// `hoistProps: false` literal form. |
| 7 | +const babel = require("@babel/core"); |
| 8 | +const { stripTypeScriptTypes } = require("node:module"); |
| 9 | + |
| 10 | +const components = `const List = (p: any) => p.children; |
| 11 | +const Item = (p: any) => [p.v, p.w]; |
| 12 | +`; |
| 13 | + |
| 14 | +const cases = [ |
| 15 | + { |
| 16 | + name: "type parameter in a nested arrow's parameter and return annotations", |
| 17 | + filename: "arrow.tsx", |
| 18 | + source: `${components} |
| 19 | +export const C = <Row,>(props: any) => ( |
| 20 | + <List> |
| 21 | + <Item v={props.rows} w={(row: Row): Row => props.key(row)} /> |
| 22 | + </List> |
| 23 | +); |
| 24 | +const [rows, key] = C({ rows: [1], key: (r: number) => r + 1 }); |
| 25 | +__result = [rows, key(1)];`, |
| 26 | + captures: [["props"], ["props"]], |
| 27 | + result: [[1], 2] |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "type parameter of a function component in a function expression's annotations", |
| 31 | + filename: "function.tsx", |
| 32 | + source: `${components} |
| 33 | +function C<Row>(props: any) { |
| 34 | + return ( |
| 35 | + <List> |
| 36 | + <Item |
| 37 | + v={props.v} |
| 38 | + w={function (this: unknown, row: Row): Row { |
| 39 | + return props.key(row); |
| 40 | + }} |
| 41 | + /> |
| 42 | + </List> |
| 43 | + ); |
| 44 | +} |
| 45 | +const [v, w] = C({ v: 1, key: (r: number) => r * 3 }); |
| 46 | +__result = [v, w(2)];`, |
| 47 | + captures: [["props"], ["props"]], |
| 48 | + result: [1, 6] |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "as, satisfies and non-null keep the value and drop the type", |
| 52 | + filename: "assertions.tsx", |
| 53 | + source: `${components} |
| 54 | +function C<Row>(props: any) { |
| 55 | + return <Item v={(props.v as Row)!} w={props.w satisfies Row} />; |
| 56 | +} |
| 57 | +__result = C({ v: 1, w: 2 });`, |
| 58 | + captures: [["props"]], |
| 59 | + result: [1, 2] |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "generic call and constructor type arguments", |
| 63 | + filename: "type-arguments.tsx", |
| 64 | + source: `${components} |
| 65 | +const id = <T,>(v: T) => v; |
| 66 | +function C<Row>(props: any) { |
| 67 | + return <Item v={id<Row>(props.v)} w={new Map<string, Row>([["k", props.w]]).get("k")} />; |
| 68 | +} |
| 69 | +__result = C({ v: 1, w: 2 });`, |
| 70 | + captures: [["props"]], |
| 71 | + result: [1, 2] |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "typeof a local value in a type captures neither it nor a reassigned one", |
| 75 | + filename: "typeof.tsx", |
| 76 | + source: `${components} |
| 77 | +function C(props: any) { |
| 78 | + let n = props.n; |
| 79 | + n++; |
| 80 | + const m = props.m; |
| 81 | + return <Item v={props.v as typeof n} w={props.w as typeof m} />; |
| 82 | +} |
| 83 | +__result = C({ n: 1, m: 2, v: 3, w: 4 });`, |
| 84 | + captures: [["props"]], |
| 85 | + result: [3, 4] |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "local type alias, interface heritage and class implements", |
| 89 | + filename: "declarations.tsx", |
| 90 | + source: `${components} |
| 91 | +function C(props: any) { |
| 92 | + type Row = { v: number }; |
| 93 | + interface Keyed extends Row { |
| 94 | + k: number; |
| 95 | + } |
| 96 | + return ( |
| 97 | + <Item |
| 98 | + v={(() => { |
| 99 | + interface Local extends Keyed {} |
| 100 | + class K implements Keyed { |
| 101 | + v = props.v; |
| 102 | + k = 0; |
| 103 | + } |
| 104 | + const local: Local = new K(); |
| 105 | + return local.v; |
| 106 | + })()} |
| 107 | + w={props.w as Row} |
| 108 | + /> |
| 109 | + ); |
| 110 | +} |
| 111 | +__result = C({ v: 1, w: 2 });`, |
| 112 | + captures: [["props"]], |
| 113 | + result: [1, 2] |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "type-only imports", |
| 117 | + filename: "imports.tsx", |
| 118 | + source: `import type { Row } from "./types"; |
| 119 | +import { type Key } from "./types"; |
| 120 | +${components} |
| 121 | +function C(props: any) { |
| 122 | + return <Item v={props.v as Row} w={props.w as Key} />; |
| 123 | +} |
| 124 | +__result = C({ v: 1, w: 2 });`, |
| 125 | + captures: [["props"]], |
| 126 | + result: [1, 2] |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "a name that is both a type and a value captures the value use", |
| 130 | + filename: "type-and-value.tsx", |
| 131 | + source: `${components} |
| 132 | +function C(props: any) { |
| 133 | + const Row = (v: number) => v * 2; |
| 134 | + type Row = ReturnType<typeof Row>; |
| 135 | + return <Item v={Row(props.v) as Row} w={props.w as Row} />; |
| 136 | +} |
| 137 | +__result = C({ v: 1, w: 2 });`, |
| 138 | + captures: [["Row", "props"]], |
| 139 | + result: [2, 2] |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "a type declared before a same-named value captures the value use", |
| 143 | + filename: "type-then-value.tsx", |
| 144 | + source: `${components} |
| 145 | +function C(props: any) { |
| 146 | + type Row = number; |
| 147 | + const Row = (v: number): Row => v * 2; |
| 148 | + return <Item v={Row(props.v) as Row} w={props.w as Row} />; |
| 149 | +} |
| 150 | +__result = C({ v: 1, w: 2 });`, |
| 151 | + captures: [["Row", "props"]], |
| 152 | + result: [2, 2] |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "a type parameter shadowing a value captures the value use", |
| 156 | + filename: "shadowed.tsx", |
| 157 | + source: `${components} |
| 158 | +function C(props: any) { |
| 159 | + const Row = props.factor; |
| 160 | + function Inner<Row>(p: any) { |
| 161 | + return <Item v={p.v as Row} w={p.w * Row} />; |
| 162 | + } |
| 163 | + return Inner(props); |
| 164 | +} |
| 165 | +__result = C({ factor: 3, v: 1, w: 2 });`, |
| 166 | + captures: [["p", "Row"]], |
| 167 | + result: [1, 6] |
| 168 | + } |
| 169 | +]; |
| 170 | + |
| 171 | +const ctorName = /^_P\$\d*$/; |
| 172 | + |
| 173 | +/** Per `new _P$(…)` in output order, the arguments bound to `this[_m$…]`. */ |
| 174 | +function captures(code) { |
| 175 | + const ast = babel.parseSync(code, { |
| 176 | + babelrc: false, |
| 177 | + configFile: false, |
| 178 | + sourceType: "module", |
| 179 | + parserOpts: { plugins: ["typescript"] } |
| 180 | + }); |
| 181 | + const slots = new Map(); |
| 182 | + const calls = []; |
| 183 | + babel.traverse(ast, { |
| 184 | + FunctionDeclaration(path) { |
| 185 | + const { id, params, body } = path.node; |
| 186 | + if (!id || !ctorName.test(id.name)) return; |
| 187 | + const names = params.map(param => param.name); |
| 188 | + const captured = []; |
| 189 | + for (const statement of body.body) { |
| 190 | + const e = statement.expression; |
| 191 | + if ( |
| 192 | + e?.type === "AssignmentExpression" && |
| 193 | + e.left.type === "MemberExpression" && |
| 194 | + e.left.computed && |
| 195 | + e.left.object.type === "ThisExpression" && |
| 196 | + e.right.type === "Identifier" |
| 197 | + ) { |
| 198 | + captured.push(names.indexOf(e.right.name)); |
| 199 | + } |
| 200 | + } |
| 201 | + slots.set(id.name, captured); |
| 202 | + }, |
| 203 | + NewExpression(path) { |
| 204 | + const { callee } = path.node; |
| 205 | + if (callee.type === "Identifier" && ctorName.test(callee.name)) calls.push(path.node); |
| 206 | + } |
| 207 | + }); |
| 208 | + return calls.map(call => |
| 209 | + slots.get(call.callee.name).map(index => { |
| 210 | + const arg = call.arguments[index]; |
| 211 | + return arg.type === "Identifier" ? arg.name : arg.type; |
| 212 | + }) |
| 213 | + ); |
| 214 | +} |
| 215 | + |
| 216 | +const runtime = { |
| 217 | + ssr: (t, ...v) => ({ t, v }), |
| 218 | + escape: v => v, |
| 219 | + mergeProps: (...sources) => Object.assign({}, ...sources), |
| 220 | + memo: fn => fn, |
| 221 | + createComponent: (Comp, props) => Comp(props) |
| 222 | +}; |
| 223 | + |
| 224 | +/** Strips the types the way the bundler does after the JSX transform, then runs the module body. */ |
| 225 | +function run(code) { |
| 226 | + const body = stripTypeScriptTypes(code) |
| 227 | + .replace(/import \{ ([\w$]+) as ([\w$]+) \} from "r-server";/g, "const $2 = __rt.$1;") |
| 228 | + .replace(/^\s*import \{\s*\} from "[^"]+";$/gm, "") |
| 229 | + .replace(/^export /gm, ""); |
| 230 | + return new Function("__rt", `let __result;\n${body}\nreturn __result;`)(runtime); |
| 231 | +} |
| 232 | + |
| 233 | +/** `compile(source, filename, hoistProps)` returns the compiled code. */ |
| 234 | +exports.assertCase = function assertCase(testCase, compile) { |
| 235 | + const hoisted = compile(testCase.source, testCase.filename, true); |
| 236 | + expect(captures(hoisted)).toEqual(testCase.captures); |
| 237 | + expect(run(hoisted)).toEqual(testCase.result); |
| 238 | + const literal = compile(testCase.source, testCase.filename, false); |
| 239 | + expect(captures(literal)).toEqual([]); |
| 240 | + expect(run(literal)).toEqual(testCase.result); |
| 241 | +}; |
| 242 | + |
| 243 | +exports.cases = cases; |
0 commit comments