@tsrx/vue 0.4.5 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/transform.js +500 -33
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Vue compiler built on @tsrx/core",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
|
-
"version": "0.4.
|
|
6
|
+
"version": "0.4.6",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"esrap": "^2.4.2",
|
|
43
43
|
"is-reference": "^3.0.3",
|
|
44
44
|
"zimmerframe": "^1.1.2",
|
|
45
|
-
"@tsrx/core": "0.6.
|
|
45
|
+
"@tsrx/core": "0.6.6",
|
|
46
46
|
"@tsrx/vue-runtime": "0.2.3"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
package/src/transform.js
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
NORMALIZE_SPREAD_PROPS_INTERNAL_NAME,
|
|
33
33
|
setLocation,
|
|
34
34
|
SUSPENSE_INTERNAL_NAME,
|
|
35
|
+
switchStatementToJsxChild,
|
|
35
36
|
TSRX_ERRORS,
|
|
36
37
|
} from '@tsrx/core';
|
|
37
38
|
|
|
@@ -63,6 +64,25 @@ const VAPOR_FOR_INTERNAL_NAME = '__VaporFor';
|
|
|
63
64
|
const FOR_ITEMS_INTERNAL_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'for_items';
|
|
64
65
|
/** The `computed` that walks the source of a `VaporFor` loop with `@empty`. */
|
|
65
66
|
const VAPOR_ITEMS_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'vapor_items';
|
|
67
|
+
/** The `computed` that picks the case of a `@switch` row, numbered per row. */
|
|
68
|
+
const SWITCH_CASE_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'case';
|
|
69
|
+
/** What a `@switch` row's `computed` caught. */
|
|
70
|
+
const SWITCH_ERROR_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'error';
|
|
71
|
+
/** The value a `@switch` row's `computed` held before it runs again. */
|
|
72
|
+
const SWITCH_PREVIOUS_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'previous';
|
|
73
|
+
/**
|
|
74
|
+
* How many `@switch` rows each compile has lowered, so a nested one's
|
|
75
|
+
* `computed` can't capture an outer one's name.
|
|
76
|
+
* @type {WeakMap<TransformContext, number>}
|
|
77
|
+
*/
|
|
78
|
+
const switch_counts = new WeakMap();
|
|
79
|
+
/**
|
|
80
|
+
* The metadata flag on a `@switch`'s shared lowering, which survives the
|
|
81
|
+
* clones a `VaporFor` row goes through.
|
|
82
|
+
*/
|
|
83
|
+
const VAPOR_SWITCH_METADATA = 'vapor_switch';
|
|
84
|
+
/** The most cases a `@switch` row turns into a chain of `createIf`s. */
|
|
85
|
+
const MAX_SWITCH_ROW_CASES = 64;
|
|
66
86
|
|
|
67
87
|
const DEFINE_VAPOR_COMPONENT = 'defineVaporComponent';
|
|
68
88
|
|
|
@@ -147,6 +167,9 @@ const vue_platform = {
|
|
|
147
167
|
},
|
|
148
168
|
renderForOf: (node, loop_params, body_statements, ctx, get_empty_statements) =>
|
|
149
169
|
render_for_of_as_vapor_for(node, loop_params, body_statements, ctx, get_empty_statements),
|
|
170
|
+
controlFlow: {
|
|
171
|
+
switchStatement: render_switch_statement,
|
|
172
|
+
},
|
|
150
173
|
createPendingBoundary(try_content, fallback_content, ctx) {
|
|
151
174
|
return create_vapor_pending_boundary(try_content, fallback_content, ctx);
|
|
152
175
|
},
|
|
@@ -1486,29 +1509,6 @@ function read_children_from_default_slot(fn, name, ctx) {
|
|
|
1486
1509
|
return fn;
|
|
1487
1510
|
}
|
|
1488
1511
|
|
|
1489
|
-
/**
|
|
1490
|
-
* @param {AST.Node} node
|
|
1491
|
-
* @param {import('zimmerframe').Context<AST.Node, ChildrenReadState>} context
|
|
1492
|
-
*/
|
|
1493
|
-
const shadow_head = (node, { state, next }) => {
|
|
1494
|
-
/** @type {AST.Node[]} */
|
|
1495
|
-
const bindings = [];
|
|
1496
|
-
if (node.type === 'CatchClause' && node.param) bindings.push(node.param);
|
|
1497
|
-
const head =
|
|
1498
|
-
node.type === 'ForStatement'
|
|
1499
|
-
? node.init
|
|
1500
|
-
: node.type === 'ForInStatement' || node.type === 'ForOfStatement'
|
|
1501
|
-
? node.left
|
|
1502
|
-
: null;
|
|
1503
|
-
if (head?.type === 'VariableDeclaration') {
|
|
1504
|
-
for (const declarator of head.declarations) bindings.push(declarator.id);
|
|
1505
|
-
}
|
|
1506
|
-
if (bindings.length === 0) return next();
|
|
1507
|
-
const shadowed_names = new Set(state.shadowed_names);
|
|
1508
|
-
for (const binding of bindings) collect_pattern_names(binding, shadowed_names);
|
|
1509
|
-
return next({ ...state, shadowed_names });
|
|
1510
|
-
};
|
|
1511
|
-
|
|
1512
1512
|
const body = /** @type {AST.BlockStatement | AST.Expression} */ (
|
|
1513
1513
|
walk(/** @type {AST.Node} */ (fn.body), state, {
|
|
1514
1514
|
ChainExpression(node, { path, state, next }) {
|
|
@@ -1558,10 +1558,14 @@ function read_children_from_default_slot(fn, name, ctx) {
|
|
|
1558
1558
|
FunctionDeclaration: rewrite_function_shadowed_refs,
|
|
1559
1559
|
FunctionExpression: rewrite_function_shadowed_refs,
|
|
1560
1560
|
ArrowFunctionExpression: rewrite_function_shadowed_refs,
|
|
1561
|
-
CatchClause:
|
|
1562
|
-
ForStatement:
|
|
1563
|
-
ForInStatement:
|
|
1564
|
-
ForOfStatement:
|
|
1561
|
+
CatchClause: rewrite_head_shadowed_refs,
|
|
1562
|
+
ForStatement: rewrite_head_shadowed_refs,
|
|
1563
|
+
ForInStatement: rewrite_head_shadowed_refs,
|
|
1564
|
+
ForOfStatement: rewrite_head_shadowed_refs,
|
|
1565
|
+
SwitchStatement: rewrite_switch_shadowed_refs,
|
|
1566
|
+
ClassExpression: rewrite_class_shadowed_refs,
|
|
1567
|
+
StaticBlock: rewrite_static_block_shadowed_refs,
|
|
1568
|
+
TSEnumDeclaration: rewrite_enum_shadowed_refs,
|
|
1565
1569
|
BlockStatement(node, context) {
|
|
1566
1570
|
// The component body declares the destructured `children` binding;
|
|
1567
1571
|
// it cannot redeclare the props parameter or its bindings.
|
|
@@ -1870,7 +1874,10 @@ function render_for_of_as_vapor_for(
|
|
|
1870
1874
|
(!key_expression ||
|
|
1871
1875
|
suspends(node) ||
|
|
1872
1876
|
destructuring_runs_code(loop_params[0]) ||
|
|
1873
|
-
!is_vapor_for_row(rendered)
|
|
1877
|
+
!is_vapor_for_row(rendered) ||
|
|
1878
|
+
// A `@switch` row with `@empty` stays where #1378 left it, which
|
|
1879
|
+
// renders on the server.
|
|
1880
|
+
is_lowered_switch(rendered))
|
|
1874
1881
|
) {
|
|
1875
1882
|
// A default in the head's pattern that reads the index can't be read in
|
|
1876
1883
|
// the parameters, where the index comes second. The shared lowering
|
|
@@ -1881,7 +1888,7 @@ function render_for_of_as_vapor_for(
|
|
|
1881
1888
|
return render_for_of_as_map_iterable(node, loop_params, rendered, transform_context);
|
|
1882
1889
|
}
|
|
1883
1890
|
|
|
1884
|
-
const row = to_vapor_for_row(rendered);
|
|
1891
|
+
const row = to_vapor_for_row(rendered, transform_context);
|
|
1885
1892
|
const slot = key_expression
|
|
1886
1893
|
? create_keyed_vapor_for_slot(loop_params, row, transform_context)
|
|
1887
1894
|
: { params: loop_params, body: row, expression: true };
|
|
@@ -1889,6 +1896,12 @@ function render_for_of_as_vapor_for(
|
|
|
1889
1896
|
return null;
|
|
1890
1897
|
}
|
|
1891
1898
|
|
|
1899
|
+
if (is_lowered_switch(rendered) && lowered_switch_statement(rendered).cases.length > 0) {
|
|
1900
|
+
// The name `switch_in_fragment` gave the row's `computed`.
|
|
1901
|
+
switch_counts.set(transform_context, (switch_counts.get(transform_context) ?? 0) + 1);
|
|
1902
|
+
transform_context.needs_vapor_computed = true;
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1892
1905
|
transform_context.needs_vapor_for = true;
|
|
1893
1906
|
transform_context.needs_for_items = true;
|
|
1894
1907
|
|
|
@@ -2029,6 +2042,7 @@ function destructuring_runs_code(pattern) {
|
|
|
2029
2042
|
*/
|
|
2030
2043
|
function is_vapor_for_row(node) {
|
|
2031
2044
|
return (
|
|
2045
|
+
is_lowered_switch(node) ||
|
|
2032
2046
|
node.type === 'JSXElement' ||
|
|
2033
2047
|
node.type === 'JSXFragment' ||
|
|
2034
2048
|
node.type === 'ConditionalExpression' ||
|
|
@@ -2056,12 +2070,17 @@ function yields(node) {
|
|
|
2056
2070
|
* The row a `VaporFor` slot returns. The slot runs once per row, so a ternary
|
|
2057
2071
|
* it returns would keep the branch it picked first: a row that picks a branch,
|
|
2058
2072
|
* or can skip itself, is a fragment around the choice, which vue-jsx-vapor
|
|
2059
|
-
* makes a `createIf` that follows its condition
|
|
2073
|
+
* makes a `createIf` that follows its condition, and a `@switch` row is a
|
|
2074
|
+
* fragment around its cases (`switch_in_fragment`).
|
|
2060
2075
|
*
|
|
2061
2076
|
* @param {AST.Expression} node
|
|
2077
|
+
* @param {TransformContext} transform_context
|
|
2062
2078
|
* @returns {AST.Expression}
|
|
2063
2079
|
*/
|
|
2064
|
-
function to_vapor_for_row(node) {
|
|
2080
|
+
function to_vapor_for_row(node, transform_context) {
|
|
2081
|
+
if (is_lowered_switch(node)) {
|
|
2082
|
+
return switch_in_fragment(/** @type {AST.CallExpression} */ (node), transform_context);
|
|
2083
|
+
}
|
|
2065
2084
|
if (
|
|
2066
2085
|
node.type === 'ConditionalExpression' ||
|
|
2067
2086
|
(node.type === 'LogicalExpression' && node.operator === '&&')
|
|
@@ -2104,6 +2123,267 @@ function to_vapor_branch(node) {
|
|
|
2104
2123
|
return node;
|
|
2105
2124
|
}
|
|
2106
2125
|
|
|
2126
|
+
/**
|
|
2127
|
+
* `@switch` on Vue: the shared lowering, an IIFE around a `switch`, marked so
|
|
2128
|
+
* a `VaporFor` row that is a `@switch` can render through
|
|
2129
|
+
* `switch_in_fragment`. A row slot runs once, so the shared IIFE would keep
|
|
2130
|
+
* the case it picked first and return `null` when no case matches, which
|
|
2131
|
+
* Vapor can't mount. Elsewhere vue-jsx-vapor evaluates the IIFE in an effect,
|
|
2132
|
+
* which follows the value.
|
|
2133
|
+
*
|
|
2134
|
+
* Not marked, so a row keeps the IIFE: the type-only output; a `@switch` that
|
|
2135
|
+
* awaits or yields; one whose case has setup statements or renders anything
|
|
2136
|
+
* but an element, a fragment or a choice of them (a nested `@switch`, a loop
|
|
2137
|
+
* through `map_iterable`), which as a branch of its own would render again,
|
|
2138
|
+
* components and all, whenever what it reads changes, such as the row's
|
|
2139
|
+
* item; and one with more than `MAX_SWITCH_ROW_CASES` cases, whose chain of
|
|
2140
|
+
* choices would nest as deep.
|
|
2141
|
+
*
|
|
2142
|
+
* @param {AST.SwitchStatement} node
|
|
2143
|
+
* @param {TransformContext} transform_context
|
|
2144
|
+
* @returns {ESTreeJSX.JSXRenderNode}
|
|
2145
|
+
*/
|
|
2146
|
+
function render_switch_statement(node, transform_context) {
|
|
2147
|
+
const container = switchStatementToJsxChild(node, transform_context);
|
|
2148
|
+
const statement =
|
|
2149
|
+
container.type === 'JSXExpressionContainer' &&
|
|
2150
|
+
container.expression.type === 'CallExpression' &&
|
|
2151
|
+
container.expression.callee.type === 'ArrowFunctionExpression' &&
|
|
2152
|
+
container.expression.callee.body.type === 'BlockStatement'
|
|
2153
|
+
? container.expression.callee.body.body[0]
|
|
2154
|
+
: null;
|
|
2155
|
+
if (
|
|
2156
|
+
!transform_context.typeOnly &&
|
|
2157
|
+
statement?.type === 'SwitchStatement' &&
|
|
2158
|
+
statement.cases.length <= MAX_SWITCH_ROW_CASES &&
|
|
2159
|
+
statement.cases.every(
|
|
2160
|
+
(switch_case) =>
|
|
2161
|
+
switch_case.consequent.length === 1 &&
|
|
2162
|
+
switch_case.consequent[0].type === 'ReturnStatement' &&
|
|
2163
|
+
renders_once(switch_case.consequent[0].argument),
|
|
2164
|
+
) &&
|
|
2165
|
+
!suspends(node) &&
|
|
2166
|
+
container.type === 'JSXExpressionContainer'
|
|
2167
|
+
) {
|
|
2168
|
+
/** @type {Record<string, unknown>} */ (
|
|
2169
|
+
/** @type {unknown} */ (container.expression.metadata ??= { path: [] })
|
|
2170
|
+
)[VAPOR_SWITCH_METADATA] = true;
|
|
2171
|
+
}
|
|
2172
|
+
return container;
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
/**
|
|
2176
|
+
* Whether a case's output, as a `createIf` branch, renders once while the
|
|
2177
|
+
* branch stays: an element, a fragment, nothing, or a choice of them, which
|
|
2178
|
+
* vue-jsx-vapor makes `createIf`s too. Anything else is evaluated in an
|
|
2179
|
+
* effect of its own, which renders it again when what it reads changes.
|
|
2180
|
+
*
|
|
2181
|
+
* @param {AST.Expression | null | undefined} node
|
|
2182
|
+
* @returns {boolean}
|
|
2183
|
+
*/
|
|
2184
|
+
function renders_once(node) {
|
|
2185
|
+
if (!node) return true;
|
|
2186
|
+
switch (node.type) {
|
|
2187
|
+
case 'JSXElement':
|
|
2188
|
+
case 'JSXFragment':
|
|
2189
|
+
return true;
|
|
2190
|
+
case 'Literal':
|
|
2191
|
+
return node.value === null;
|
|
2192
|
+
case 'ConditionalExpression':
|
|
2193
|
+
return renders_once(node.consequent) && renders_once(node.alternate);
|
|
2194
|
+
case 'LogicalExpression':
|
|
2195
|
+
return node.operator === '&&' && renders_once(node.right);
|
|
2196
|
+
default:
|
|
2197
|
+
return false;
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
/**
|
|
2202
|
+
* Whether `node` is a `@switch` that `render_switch_statement` marked.
|
|
2203
|
+
*
|
|
2204
|
+
* @param {AST.Node} node
|
|
2205
|
+
* @returns {boolean}
|
|
2206
|
+
*/
|
|
2207
|
+
function is_lowered_switch(node) {
|
|
2208
|
+
return (
|
|
2209
|
+
node.type === 'CallExpression' &&
|
|
2210
|
+
!!(
|
|
2211
|
+
/** @type {Record<string, unknown> | undefined} */ (/** @type {unknown} */ (node.metadata))?.[
|
|
2212
|
+
VAPOR_SWITCH_METADATA
|
|
2213
|
+
]
|
|
2214
|
+
)
|
|
2215
|
+
);
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
/**
|
|
2219
|
+
* The `switch` in the IIFE of a `@switch` that `render_switch_statement`
|
|
2220
|
+
* marked.
|
|
2221
|
+
*
|
|
2222
|
+
* @param {AST.Expression} node
|
|
2223
|
+
* @returns {AST.SwitchStatement}
|
|
2224
|
+
*/
|
|
2225
|
+
function lowered_switch_statement(node) {
|
|
2226
|
+
return /** @type {AST.SwitchStatement} */ (
|
|
2227
|
+
/** @type {AST.BlockStatement} */ (
|
|
2228
|
+
/** @type {AST.ArrowFunctionExpression} */ (/** @type {AST.CallExpression} */ (node).callee)
|
|
2229
|
+
.body
|
|
2230
|
+
).body[0]
|
|
2231
|
+
);
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
/**
|
|
2235
|
+
* A `VaporFor` row that is a `@switch`. Its slot runs once, so a `computed`
|
|
2236
|
+
* runs the `switch` for the index of the case it picks, and the row is a
|
|
2237
|
+
* fragment around the case chosen by that index, which vue-jsx-vapor makes
|
|
2238
|
+
* `createIf`s: the row follows its value, keeps its case mounted while the
|
|
2239
|
+
* index stays the same, and renders an empty fragment when no case matches,
|
|
2240
|
+
* since a row can't be `null`. The `computed` is made once per row, so the
|
|
2241
|
+
* `switch` runs once for each change it reads.
|
|
2242
|
+
*
|
|
2243
|
+
* @param {AST.CallExpression} node
|
|
2244
|
+
* @param {TransformContext} transform_context
|
|
2245
|
+
* @returns {AST.Expression}
|
|
2246
|
+
*/
|
|
2247
|
+
function switch_in_fragment(node, transform_context) {
|
|
2248
|
+
const statement = lowered_switch_statement(node);
|
|
2249
|
+
// Without cases there is nothing to choose: the fragment runs the
|
|
2250
|
+
// discriminant in an effect, as the `switch` ran it elsewhere.
|
|
2251
|
+
if (statement.cases.length === 0) {
|
|
2252
|
+
return /** @type {AST.Expression} */ (
|
|
2253
|
+
/** @type {unknown} */ (
|
|
2254
|
+
b.jsx_fragment([
|
|
2255
|
+
to_jsx_expression_container(
|
|
2256
|
+
b.call(b.arrow([], b.block([statement, b.return(b.literal(null))]))),
|
|
2257
|
+
),
|
|
2258
|
+
])
|
|
2259
|
+
)
|
|
2260
|
+
);
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
// Named with the next number, which `render_for_of_as_vapor_for` takes
|
|
2264
|
+
// only if the loop renders through `VaporFor`.
|
|
2265
|
+
const case_ref = create_generated_identifier(
|
|
2266
|
+
`${SWITCH_CASE_NAME}_${(switch_counts.get(transform_context) ?? 0) + 1}`,
|
|
2267
|
+
);
|
|
2268
|
+
const error_ref = create_generated_identifier(SWITCH_ERROR_NAME);
|
|
2269
|
+
const previous_ref = create_generated_identifier(SWITCH_PREVIOUS_NAME);
|
|
2270
|
+
/** The index the `computed` holds, or the last one it picked while it holds a throw. */
|
|
2271
|
+
const held_index = () =>
|
|
2272
|
+
b.conditional(
|
|
2273
|
+
b.binary(
|
|
2274
|
+
'===',
|
|
2275
|
+
b.unary('typeof', create_value_member_expression(case_ref)),
|
|
2276
|
+
b.literal('object'),
|
|
2277
|
+
),
|
|
2278
|
+
b.member(create_value_member_expression(case_ref), 'index'),
|
|
2279
|
+
create_value_member_expression(case_ref),
|
|
2280
|
+
);
|
|
2281
|
+
|
|
2282
|
+
/** @type {AST.Expression} */
|
|
2283
|
+
let chain = b.jsx_fragment();
|
|
2284
|
+
for (let index = statement.cases.length - 1; index >= 0; index--) {
|
|
2285
|
+
const output = /** @type {AST.ReturnStatement} */ (statement.cases[index].consequent[0]);
|
|
2286
|
+
chain = b.conditional(
|
|
2287
|
+
b.binary(
|
|
2288
|
+
'===',
|
|
2289
|
+
// The first choice throws what the `switch` threw, in its
|
|
2290
|
+
// `createIf`'s effect, which keeps following the `computed`;
|
|
2291
|
+
// the others keep the case picked before the throw.
|
|
2292
|
+
index === 0
|
|
2293
|
+
? b.conditional(
|
|
2294
|
+
b.binary(
|
|
2295
|
+
'===',
|
|
2296
|
+
b.unary('typeof', create_value_member_expression(case_ref)),
|
|
2297
|
+
b.literal('object'),
|
|
2298
|
+
),
|
|
2299
|
+
b.call(
|
|
2300
|
+
b.arrow(
|
|
2301
|
+
[],
|
|
2302
|
+
b.block([
|
|
2303
|
+
/** @type {AST.ThrowStatement} */ ({
|
|
2304
|
+
type: 'ThrowStatement',
|
|
2305
|
+
argument: b.member(create_value_member_expression(case_ref), 'error'),
|
|
2306
|
+
metadata: { path: [] },
|
|
2307
|
+
}),
|
|
2308
|
+
]),
|
|
2309
|
+
),
|
|
2310
|
+
),
|
|
2311
|
+
create_value_member_expression(case_ref),
|
|
2312
|
+
)
|
|
2313
|
+
: held_index(),
|
|
2314
|
+
b.literal(index),
|
|
2315
|
+
),
|
|
2316
|
+
to_vapor_branch(output.argument ?? b.literal(null)),
|
|
2317
|
+
chain,
|
|
2318
|
+
);
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
return b.call(
|
|
2322
|
+
b.arrow(
|
|
2323
|
+
[],
|
|
2324
|
+
b.block([
|
|
2325
|
+
// Vue stops running an effect that read a `computed` whose getter
|
|
2326
|
+
// threw, so the `computed` returns what the `switch` threw, with
|
|
2327
|
+
// the index it held before.
|
|
2328
|
+
b.const(
|
|
2329
|
+
case_ref,
|
|
2330
|
+
b.call(
|
|
2331
|
+
VUE_COMPUTED_INTERNAL_NAME,
|
|
2332
|
+
b.arrow(
|
|
2333
|
+
[previous_ref],
|
|
2334
|
+
b.block([
|
|
2335
|
+
b.try(
|
|
2336
|
+
b.block([
|
|
2337
|
+
setLocation(
|
|
2338
|
+
b.switch(
|
|
2339
|
+
statement.discriminant,
|
|
2340
|
+
statement.cases.map((switch_case, index) =>
|
|
2341
|
+
setLocation(
|
|
2342
|
+
b.switch_case(switch_case.test, [b.return(b.literal(index))]),
|
|
2343
|
+
has_location(switch_case) ? switch_case : undefined,
|
|
2344
|
+
),
|
|
2345
|
+
),
|
|
2346
|
+
),
|
|
2347
|
+
has_location(statement) ? statement : undefined,
|
|
2348
|
+
),
|
|
2349
|
+
b.return(b.literal(-1)),
|
|
2350
|
+
]),
|
|
2351
|
+
b.catch_clause(
|
|
2352
|
+
error_ref,
|
|
2353
|
+
null,
|
|
2354
|
+
b.block([
|
|
2355
|
+
b.return(
|
|
2356
|
+
b.object([
|
|
2357
|
+
b.prop('init', b.id('error'), clone_identifier(error_ref)),
|
|
2358
|
+
b.prop(
|
|
2359
|
+
'init',
|
|
2360
|
+
b.id('index'),
|
|
2361
|
+
b.conditional(
|
|
2362
|
+
b.binary(
|
|
2363
|
+
'===',
|
|
2364
|
+
b.unary('typeof', clone_identifier(previous_ref)),
|
|
2365
|
+
b.literal('object'),
|
|
2366
|
+
),
|
|
2367
|
+
b.member(clone_identifier(previous_ref), 'index'),
|
|
2368
|
+
b.logical('??', clone_identifier(previous_ref), b.literal(-1)),
|
|
2369
|
+
),
|
|
2370
|
+
),
|
|
2371
|
+
]),
|
|
2372
|
+
),
|
|
2373
|
+
]),
|
|
2374
|
+
),
|
|
2375
|
+
null,
|
|
2376
|
+
),
|
|
2377
|
+
]),
|
|
2378
|
+
),
|
|
2379
|
+
),
|
|
2380
|
+
),
|
|
2381
|
+
b.return(b.jsx_fragment([to_jsx_expression_container(chain)])),
|
|
2382
|
+
]),
|
|
2383
|
+
),
|
|
2384
|
+
);
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2107
2387
|
/**
|
|
2108
2388
|
* @param {AST.ForOfStatement} node
|
|
2109
2389
|
* @param {AST.Pattern[]} loop_params
|
|
@@ -2443,6 +2723,14 @@ function rewrite_vapor_for_keyed_slot_refs(node, loop_params, replacements = new
|
|
|
2443
2723
|
FunctionExpression: rewrite_function_shadowed_refs,
|
|
2444
2724
|
ArrowFunctionExpression: rewrite_function_shadowed_refs,
|
|
2445
2725
|
BlockStatement: rewrite_block_shadowed_refs,
|
|
2726
|
+
StaticBlock: rewrite_static_block_shadowed_refs,
|
|
2727
|
+
CatchClause: rewrite_head_shadowed_refs,
|
|
2728
|
+
ForStatement: rewrite_head_shadowed_refs,
|
|
2729
|
+
ForInStatement: rewrite_head_shadowed_refs,
|
|
2730
|
+
ForOfStatement: rewrite_head_shadowed_refs,
|
|
2731
|
+
SwitchStatement: rewrite_switch_shadowed_refs,
|
|
2732
|
+
ClassExpression: rewrite_class_shadowed_refs,
|
|
2733
|
+
TSEnumDeclaration: rewrite_enum_shadowed_refs,
|
|
2446
2734
|
},
|
|
2447
2735
|
)
|
|
2448
2736
|
);
|
|
@@ -2554,7 +2842,7 @@ function collect_pattern_replacements(pattern, source, replacements) {
|
|
|
2554
2842
|
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
2555
2843
|
* @returns {AST.Node | void}
|
|
2556
2844
|
*/
|
|
2557
|
-
function rewrite_function_shadowed_refs(node, { state, next }) {
|
|
2845
|
+
function rewrite_function_shadowed_refs(node, { state, next, visit }) {
|
|
2558
2846
|
const shadowed_names = new Set(state.shadowed_names);
|
|
2559
2847
|
if (node.type !== 'ArrowFunctionExpression' && node.id) {
|
|
2560
2848
|
collect_pattern_names(node.id, shadowed_names);
|
|
@@ -2563,7 +2851,59 @@ function rewrite_function_shadowed_refs(node, { state, next }) {
|
|
|
2563
2851
|
collect_pattern_names(param, shadowed_names);
|
|
2564
2852
|
}
|
|
2565
2853
|
collect_function_var_names(node.body, shadowed_names);
|
|
2566
|
-
|
|
2854
|
+
const inner = { ...state, shadowed_names };
|
|
2855
|
+
|
|
2856
|
+
// A parameter's decorators run in the enclosing scope, before the
|
|
2857
|
+
// parameters exist: they are walked with the outer names and put back.
|
|
2858
|
+
/** @typedef {AST.Node & { decorators?: AST.Node[] }} DecoratedNode */
|
|
2859
|
+
/** @param {AST.Node} param */
|
|
2860
|
+
const decorated = (param) =>
|
|
2861
|
+
/** @type {DecoratedNode} */ (
|
|
2862
|
+
param.type === 'TSParameterProperty'
|
|
2863
|
+
? /** @type {{ parameter: AST.Node }} */ (/** @type {unknown} */ (param)).parameter
|
|
2864
|
+
: param
|
|
2865
|
+
);
|
|
2866
|
+
/** @type {Map<number, AST.Node[]>} */
|
|
2867
|
+
const decorators = new Map();
|
|
2868
|
+
const params = (node.params || []).map((param, index) => {
|
|
2869
|
+
const target = decorated(/** @type {AST.Node} */ (param));
|
|
2870
|
+
if (!target.decorators?.length) return param;
|
|
2871
|
+
decorators.set(
|
|
2872
|
+
index,
|
|
2873
|
+
target.decorators.map((decorator) => /** @type {AST.Node} */ (visit(decorator, state))),
|
|
2874
|
+
);
|
|
2875
|
+
return with_decorators(/** @type {AST.Node} */ (param), []);
|
|
2876
|
+
});
|
|
2877
|
+
if (decorators.size === 0) return next(inner);
|
|
2878
|
+
|
|
2879
|
+
const walked = /** @type {AST.Function} */ (
|
|
2880
|
+
visit(/** @type {AST.Node} */ ({ ...node, params }), inner)
|
|
2881
|
+
);
|
|
2882
|
+
return /** @type {AST.Node} */ ({
|
|
2883
|
+
...walked,
|
|
2884
|
+
params: walked.params.map((param, index) => {
|
|
2885
|
+
const outer = decorators.get(index);
|
|
2886
|
+
return outer ? with_decorators(/** @type {AST.Node} */ (param), outer) : param;
|
|
2887
|
+
}),
|
|
2888
|
+
});
|
|
2889
|
+
}
|
|
2890
|
+
|
|
2891
|
+
/**
|
|
2892
|
+
* `param`, a parameter or a constructor's parameter property, with its
|
|
2893
|
+
* decorators replaced.
|
|
2894
|
+
*
|
|
2895
|
+
* @param {AST.Node} param
|
|
2896
|
+
* @param {AST.Node[]} decorators
|
|
2897
|
+
* @returns {AST.Pattern}
|
|
2898
|
+
*/
|
|
2899
|
+
function with_decorators(param, decorators) {
|
|
2900
|
+
if (param.type === 'TSParameterProperty') {
|
|
2901
|
+
const property = /** @type {{ parameter: AST.Node }} */ (/** @type {unknown} */ (param));
|
|
2902
|
+
return /** @type {AST.Pattern} */ (
|
|
2903
|
+
/** @type {unknown} */ ({ ...param, parameter: { ...property.parameter, decorators } })
|
|
2904
|
+
);
|
|
2905
|
+
}
|
|
2906
|
+
return /** @type {AST.Pattern} */ (/** @type {unknown} */ ({ ...param, decorators }));
|
|
2567
2907
|
}
|
|
2568
2908
|
|
|
2569
2909
|
/**
|
|
@@ -2578,6 +2918,116 @@ function rewrite_block_shadowed_refs(node, { state, next }) {
|
|
|
2578
2918
|
return next({ ...state, shadowed_names });
|
|
2579
2919
|
}
|
|
2580
2920
|
|
|
2921
|
+
/**
|
|
2922
|
+
* A class `static` block is a function body: its `var`s are its own too.
|
|
2923
|
+
*
|
|
2924
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
2925
|
+
* @param {AST.StaticBlock} node
|
|
2926
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
2927
|
+
* @returns {AST.Node | void}
|
|
2928
|
+
*/
|
|
2929
|
+
function rewrite_static_block_shadowed_refs(node, { state, next }) {
|
|
2930
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
2931
|
+
collect_block_lexical_names(node.body, shadowed_names);
|
|
2932
|
+
collect_function_var_names(node.body, shadowed_names);
|
|
2933
|
+
return next({ ...state, shadowed_names });
|
|
2934
|
+
}
|
|
2935
|
+
|
|
2936
|
+
/**
|
|
2937
|
+
* The names a `catch` clause's parameter or a `for` head declares, which
|
|
2938
|
+
* shadow the outer ones in the whole statement. A `let` or `const` head's
|
|
2939
|
+
* names are in their temporal dead zone in the loop's own expressions, the
|
|
2940
|
+
* right-hand side of `for (const row of row.children)` included, which reads
|
|
2941
|
+
* the inner `row` and throws, so those stay as written too.
|
|
2942
|
+
*
|
|
2943
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
2944
|
+
* @param {AST.CatchClause | AST.ForStatement | AST.ForInStatement | AST.ForOfStatement} node
|
|
2945
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
2946
|
+
* @returns {AST.Node | void}
|
|
2947
|
+
*/
|
|
2948
|
+
function rewrite_head_shadowed_refs(node, { state, next }) {
|
|
2949
|
+
/** @type {AST.Node[]} */
|
|
2950
|
+
const bindings = [];
|
|
2951
|
+
if (node.type === 'CatchClause' && node.param) bindings.push(node.param);
|
|
2952
|
+
const head =
|
|
2953
|
+
node.type === 'ForStatement'
|
|
2954
|
+
? node.init
|
|
2955
|
+
: node.type === 'ForInStatement' || node.type === 'ForOfStatement'
|
|
2956
|
+
? node.left
|
|
2957
|
+
: null;
|
|
2958
|
+
if (head?.type === 'VariableDeclaration') {
|
|
2959
|
+
for (const declarator of head.declarations) bindings.push(declarator.id);
|
|
2960
|
+
}
|
|
2961
|
+
if (bindings.length === 0) return next();
|
|
2962
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
2963
|
+
for (const binding of bindings) collect_pattern_names(binding, shadowed_names);
|
|
2964
|
+
return next({ ...state, shadowed_names });
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
/**
|
|
2968
|
+
* A `switch`'s cases share one block, which its discriminant is read outside of.
|
|
2969
|
+
*
|
|
2970
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
2971
|
+
* @param {AST.SwitchStatement} node
|
|
2972
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
2973
|
+
* @returns {AST.SwitchStatement}
|
|
2974
|
+
*/
|
|
2975
|
+
function rewrite_switch_shadowed_refs(node, { state, visit }) {
|
|
2976
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
2977
|
+
collect_block_lexical_names(
|
|
2978
|
+
node.cases.flatMap((switch_case) => switch_case.consequent),
|
|
2979
|
+
shadowed_names,
|
|
2980
|
+
);
|
|
2981
|
+
const inner = { ...state, shadowed_names };
|
|
2982
|
+
return {
|
|
2983
|
+
...node,
|
|
2984
|
+
discriminant: /** @type {AST.Expression} */ (visit(node.discriminant, state)),
|
|
2985
|
+
cases: node.cases.map(
|
|
2986
|
+
(switch_case) => /** @type {AST.SwitchCase} */ (visit(switch_case, inner)),
|
|
2987
|
+
),
|
|
2988
|
+
};
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2991
|
+
/**
|
|
2992
|
+
* An `enum`'s members are bound inside it: a member's initializer reads an
|
|
2993
|
+
* earlier member by its name. (Members of another declaration of the same
|
|
2994
|
+
* `enum` are in scope too; those aren't collected.)
|
|
2995
|
+
*
|
|
2996
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
2997
|
+
* @param {AST.Node} node A `TSEnumDeclaration`.
|
|
2998
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
2999
|
+
* @returns {AST.Node | void}
|
|
3000
|
+
*/
|
|
3001
|
+
function rewrite_enum_shadowed_refs(node, { state, next }) {
|
|
3002
|
+
const members =
|
|
3003
|
+
/** @type {{ body?: { members?: { id: AST.Node }[] }, members?: { id: AST.Node }[] }} */ (
|
|
3004
|
+
/** @type {unknown} */ (node)
|
|
3005
|
+
);
|
|
3006
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
3007
|
+
for (const member of members.body?.members ?? members.members ?? []) {
|
|
3008
|
+
const id = /** @type {AST.Node} */ (member.id);
|
|
3009
|
+
if (id.type === 'Identifier') shadowed_names.add(id.name);
|
|
3010
|
+
// A quoted member (`"row" = 1`) is read by its name too.
|
|
3011
|
+
else if (id.type === 'Literal' && typeof id.value === 'string') shadowed_names.add(id.value);
|
|
3012
|
+
}
|
|
3013
|
+
return next({ ...state, shadowed_names });
|
|
3014
|
+
}
|
|
3015
|
+
|
|
3016
|
+
/**
|
|
3017
|
+
* A class expression's name is bound inside it, its heritage included.
|
|
3018
|
+
*
|
|
3019
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
3020
|
+
* @param {AST.ClassExpression} node
|
|
3021
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
3022
|
+
* @returns {AST.Node | void}
|
|
3023
|
+
*/
|
|
3024
|
+
function rewrite_class_shadowed_refs(node, { state, next }) {
|
|
3025
|
+
if (!node.id) return next();
|
|
3026
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
3027
|
+
collect_pattern_names(node.id, shadowed_names);
|
|
3028
|
+
return next({ ...state, shadowed_names });
|
|
3029
|
+
}
|
|
3030
|
+
|
|
2581
3031
|
/**
|
|
2582
3032
|
* @param {AST.Statement[]} statements
|
|
2583
3033
|
* @param {Set<string>} names
|
|
@@ -2597,6 +3047,16 @@ function collect_block_lexical_names(statements, names) {
|
|
|
2597
3047
|
statement.id
|
|
2598
3048
|
) {
|
|
2599
3049
|
collect_pattern_names(statement.id, names);
|
|
3050
|
+
continue;
|
|
3051
|
+
}
|
|
3052
|
+
|
|
3053
|
+
// An `enum` or a namespace, which the statement types leave out.
|
|
3054
|
+
const declaration = /** @type {AST.Node} */ (statement);
|
|
3055
|
+
if (
|
|
3056
|
+
(declaration.type === 'TSEnumDeclaration' || declaration.type === 'TSModuleDeclaration') &&
|
|
3057
|
+
declaration.id.type === 'Identifier'
|
|
3058
|
+
) {
|
|
3059
|
+
collect_pattern_names(declaration.id, names);
|
|
2600
3060
|
}
|
|
2601
3061
|
}
|
|
2602
3062
|
}
|
|
@@ -2683,6 +3143,13 @@ function collect_pattern_names(node, names) {
|
|
|
2683
3143
|
case 'Property':
|
|
2684
3144
|
collect_pattern_names(node.value, names);
|
|
2685
3145
|
break;
|
|
3146
|
+
// A constructor's parameter property (`constructor(public row)`).
|
|
3147
|
+
case 'TSParameterProperty':
|
|
3148
|
+
collect_pattern_names(
|
|
3149
|
+
/** @type {AST.Node} */ (/** @type {unknown} */ (node.parameter)),
|
|
3150
|
+
names,
|
|
3151
|
+
);
|
|
3152
|
+
break;
|
|
2686
3153
|
}
|
|
2687
3154
|
}
|
|
2688
3155
|
|