1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//! S4a T3: the four ES-module declaration visits. A further `impl<'bt, 'sc,
//! 'sm, 'ad> SemanticResolver<'bt, 'sc, 'sm, 'ad>` block, split out of
//! `resolver/mod.rs` the same way `identifiers.rs` (S1 T4),
//! `declarations.rs` (S1 T5), `expressions.rs` (S1 T6), `functions.rs`
//! (S1 T7), `statements.rs` (S2 T1), `classes.rs` (S2 T4) and `calls.rs`
//! (S2 T6) were — see `identifiers.rs`'s module doc for why a child module
//! sees `mod.rs`'s private fields and helpers.
//!
//! Ports `SemanticResolver::visit(ImportDeclarationNode *)`
//! (SemanticResolver.cpp:874-890),
//! `visit(ExportNamedDeclarationNode *)` (cpp:1524-1531),
//! `visit(ExportDefaultDeclarationNode *)` (cpp:1533-1561) and
//! `visit(ExportAllDeclarationNode *)` (cpp:1563-1568).
//!
//! ## No CommonJS-module support in this port
//!
//! All four visits are gated on `astContext_.getUseCJSModules()` — the
//! `-commonjs` driver flag. This port does not implement `-commonjs`
//! anywhere (neither the flag, nor `Context::useCJSModules`, nor the
//! `$SHBuiltin` module protocol `calls.rs:310-341` still panics on), so
//! `getUseCJSModules()` is CONSTANTLY FALSE here and every `!useCJSModules`
//! sub-condition folds away to `true`. The Rust gates are therefore
//! `self.compile()` alone (or nothing at all, for the import visit — see
//! below), each marked `// S4b: && !use_cjs_modules` at the site so that
//! whoever adds CJS support has the exact condition to restore.
//!
//! ## One bug-for-bug quirk, preserved and flagged
//!
//! 1. **The import error is NOT `compile_`-gated** (cpp:876-879), while all
//! three export errors ARE (cpp:1525, 1534, 1564). So under the
//! `compile = false` entry point (`resolveASTForParser`,
//! `resolve::resolve_ast_for_parser`) an `import` still errors and an
//! `export` does not — an asymmetry with no stated rationale in the C++.
//! Pinned from BOTH sides by the parser-entry corpus:
//! `module-imports.js` (errors under `compile = false`) and
//! `compile-false-basics.js` (does not).
//!
//! Two further quirks this port used to pin have since been FIXED upstream
//! and the fixes mirrored here: rewrite #4 dropping the `async` flag
//! (`6b59daf0d`) and `ExportAllDeclaration` spelling its error "CommonJS
//! module mode" while the other two say plain "module mode" (`f90a83146`).
//!
//! ## Rewrite #4: `export default function () {}` (spec §3.4)
//!
//! `visit(ExportDefaultDeclarationNode *)` mutates its own child in place
//! (`node->_declaration = funcExpr;`, cpp:1557) and only then runs
//! `visitESTreeChildren` over the mutated node. Structural fields are
//! immutable in this port, so — exactly like rewrite #1 (the arrow's
//! expression body, `functions.rs`) and rewrite #3 (`$SHBuiltin.prop(...)`,
//! `calls.rs`) — the rewrite instead FUNCTIONALLY REBUILDS both nodes
//! through the generated `builder`: a fresh `FunctionExpression` carrying
//! the declaration's seven structural children, then a fresh
//! `ExportDefaultDeclaration` pointing at it. The children walk then runs on
//! the REBUILT export node, which is what C++'s runs on too, and the visit
//! reports `Changed` even when that walk itself reports `Unchanged`
//! (`export default function () {}` — nothing below the rewrite changes),
//! the same tail `calls::visit_call_expression` needs for rewrite #3.
//!
//! No decorate-before-recurse exception here (`resolver/mod.rs`'s module
//! doc): the visit writes no `Cell` of its own. `strictness` (cpp:1553) is a
//! `Cell` COPY off the declaration made while building the replacement,
//! before anything recurses — and it is `Strictness::NotSet` at that point
//! either way, since `visitFunctionLike` is what eventually sets it, on the
//! rebuilt node.
//!
//! `-commonjs` is where this rewrite is actually observable in a dump (it is
//! what makes `test/AST/es6/export-default-function.js`'s
//! `-dump-transformed-ast -commonjs` print `"type": "FunctionExpression"`),
//! so **S4b owns the `-commonjs` corpus pinning of this rewrite**. Without
//! `-commonjs` the enclosing `export` is an error and `hermesc` never dumps;
//! what the S4a corpus can pin is the parser-entry side
//! (`compile-false-basics.js` — where `compile_` is false, so the rewrite
//! deliberately does NOT fire and the dump shows a `FunctionDeclaration`)
//! plus the unit test
//! `export_default_anonymous_function_is_rewritten_to_an_expression` in
//! `tests/resolver.rs`, which reaches into the returned tree.
//!
//! ## Backref fixup: `FunctionInfo::imports` (spec §3.4 (a))
//!
//! `visit(ImportDeclarationNode *)` pushes the node onto
//! `curFunctionInfo()->imports` (cpp:887) before descending into it. This is
//! the second of the two sema records that keep a `NodeRc` to an INTERIOR
//! node (`LexicalScope::hoisted_functions` is the first, discharged by
//! `functions::visit_function_declaration`), and it takes the same fixup for
//! the same reason: the children walk can rebuild the `ImportDeclaration`,
//! stranding the recorded `NodeRc` on a node no longer in the returned tree.
//! `visit_import_declaration` therefore remembers WHICH `FunctionInfo`'s
//! list it pushed into and at WHICH index (a `Vec::push`, so `len() - 1`;
//! nothing ever removes an entry) and patches that slot when — and only
//! when — the walk returns `Changed`. With that, `resolver/mod.rs`'s
//! §3.4 (a) obligation is fully discharged for both records.
//!
//! The differential cannot catch a missed fixup: `imports` is dump-blind
//! everywhere (`SemContextDumper.cpp` never mentions it, and neither does
//! this port's `dump_context.rs`) — it exists for `ESTreeIRGen`'s CommonJS
//! module lowering, which S4b owns. The unit tests
//! `import_declarations_are_recorded_on_the_function_info` and
//! `import_backref_is_untouched_without_a_rebuild` in `tests/resolver.rs`
//! are what pin it, by comparing the recorded nodes' identity against the
//! `ImportDeclaration`s in the tree the resolver returned.
//!
//! The `Changed` branch of the fixup is DEFENSIVE, not currently
//! exercisable: an `ImportDeclaration`'s children are specifiers (whose own
//! children are `Identifier` leaves), a `StringLiteral` source and
//! `ImportAttribute`s (literal key/value pairs) — nothing this resolver
//! rewrites or folds, so its walk can only report `Unchanged` today. It is
//! written anyway because the obligation is structural rather than
//! shape-specific, and because the alternative (omitting it and relying on
//! a shape argument that a later phase can invalidate silently) is exactly
//! the failure mode §3.4 (a) exists to prevent. What the first test above
//! DOES pin is the neighboring, real case: an ancestor rebuild (a fold
//! elsewhere in the `Program`) leaves the recorded `NodeRc`s pointing at
//! nodes that are still in the returned tree.
use ;
use ;
use TransformResult;
use copy_location_from;
use SemanticResolver;