Repository navigation
Commit a8f351b
authored
fix(compiler): dedupe generated i0 namespace imports against all file identifiers (#558)
* fix(compiler): dedupe generated i0 namespace imports against all file identifiers
A user binding named `i0` (const, namespace import, anything in the file's
identifier set) collided with the injected `import * as i0 from
'@angular/core'`, producing a duplicate declaration. Upstream's AOT/dts
ImportManager (`presetImportManagerForceNamespaceImports`) never reuses an
existing namespace import: it mints `i0` and renames to `i0_1`, `i0_2`, ...
until the name is free in `SourceFile.identifiers` — every identifier in
the original file, decorator positions included. The dts transform runs a
second ImportManager over the same original identifier set, so `.d.ts`
members use the same alias the JS emit picked.
- `NamespaceRegistry` now dedupes every generated alias against
`file_identifiers` (all identifier nodes in the original program), never
reuses an existing `@angular/core` import, and keeps `next_index`
consumed on collisions like ImportManager's `nextUniqueIndex++`.
- All `i0`/`ɵfac`/`ɵcmp` emit sites — components, directives, pipes,
injectables, NgModules, injectors, services, partial declarations,
HMR initializer, setClassMetadata/setClassDebugInfo — reference the
registry's alias (`core_namespace` threaded through, ~30 signatures).
- `DtsDeclaration` gains `namespace_imports` (alias → module specifier,
`@angular/core` included) so consumers can emit the imports the members
reference; `input_transform_types` prints `@angular/core` transform
types through the same alias (`i0_1.Signal` on collision).
- Jit-forced classes preserve decorator-position identifiers (member,
`declare` member, param decorators) that `__decorate`/`propDecorators`
re-emit, alongside the existing ctorParameters preservation.
- Vite plugin `utils/dts.ts` rewritten on oxc-parser (zero regexes):
class bodies located via AST, member namespace heads rewritten via
TSQualifiedName spans, members appended at end of class body like
upstream's `[...members, ...newMembers]`, imports emitted after the
last import's line (skipping multiline trailing comments), `.d.mts`/
`.d.cts` covered, canonical alias per module identity (relative
specifiers resolved against the source module), and structural
per-member idempotency so re-runs can't duplicate.
Fixes #509
* style: oxfmt dts plugin files
* fix(vite-plugin): rebase relative dts imports to the output path
Codex review on PR #558:
- P1: relative specifiers ("./dep") were emitted verbatim into the
.d.ts asset, resolving against the OUTPUT directory. Two source files
in different dirs canonicalized to i1/i1_1 but both emitted "./dep".
injectDtsDeclarations now takes outputFile and rebases relative
specifiers to the asset's directory; canonical entries store the
emitted specifier and the reuse check compares against it.
- P2: the idempotency check counted instance members — an instance
`ɵfac: string` would suppress the generated `static ɵfac`. Only
static member names now count as previously injected.
- P2: parseMembers dereferenced program.body[0] without an undefined
guard; empty/comment-only member strings are now handled explicitly.
* fix(vite-plugin): emit dts relative specifiers verbatim
Follow-up to the previous commit: rebasing to the source tree was wrong —
a published package ships dist/ only, and structure-preserving
declaration emit (the dominant mode for vite-plugin-dts /
rolldown-plugin-dts / tsdown) mirrors the source layout, so
"./dep" compiled under src/a/ correctly reaches dist/a/dep.d.ts.
Rebasing would have pointed imports at files outside the package.
Relative specifiers are still resolved against sourceFile for identity
dedup; under fully-bundled declaration output, two same-named relative
modules cannot both resolve — documented as a dts-generator limitation.
* refactor(vite-plugin): move .d.ts injection to Rust
The dts post-processing pass parsed the emitted declaration file with
the JS oxc-parser package — a runtime dependency duplicating what the
compiler already carries in Rust. injectDtsDeclarations is now a napi
export of dts_inject::inject_dts_declarations in oxc_angular_compiler
(SourceType::d_ts + oxc_ast_visit), and vite-plugin/utils/dts.ts is a
thin typed wrapper. The oxc-parser dependency is removed from
napi/angular-compiler (the e2e/compare harness keeps its own copy).
Same contract as before: verbatim specifier emission, resolved-identity
dedup, static-only idempotency, multiline-comment-aware import
insertion.
* fix(vite-plugin): keep generated imports out of same-line class bodies
Codex review on the previous commit (lines are stale — the logic now
lives in crates/oxc_angular_compiler/src/dts_inject.rs):
- P2: end_of_line_after scanned past syntax sharing the last import's
line ("import …; export declare class Foo {") and spliced the new
import inside the class body. import_insert_position now advances only
over trailing comments and the splice opens its own line, so same-line
declarations stay below the new import. Also fixes "import …; // note"
at EOF previously inserting inside the line comment.
- P2: dotted ambient namespaces (declare namespace Outer.Inner) already
recurse in the Rust port (TSNamespaceDeclarationBody nesting); added a
regression test so the finding is covered either way.
* fix(vite-plugin): infer unmapped i0 aliases as @angular/core
Codex review: when a caller omits namespaceImports, an unmapped non-ɵ
head like i0.Signal<number> was skipped — the member lands with i0
unresolvable, where the pre-canonicalization helper always emitted the
i0 import. The registry reserves i0/i0_N for @angular/core, so heads
matching that convention are now inferred as core when the map is
empty. i1+ aliases stay untouched: without the map their module is
unknowable.
* refactor(compiler): centralize the i0 / @angular/core literals
The CORE module name and CORE_ALIAS preferred namespace alias now live in
r3::identifiers (next to the other Angular runtime identifiers) and are
re-exported from crate::r3. Every real-code literal — the registry's mint
site, emitter/linker/HMR fallbacks, the host-bindings call, directive
initializer-API tables, dts_type matches, and dts_inject's core-alias
convention check — references the constants. Test-only literals are left
as-is (they assert on the string values).1 parent 3a05105 commit a8f351b
69 files changed
Lines changed: 3681 additions & 900 deletions
File tree
- crates/oxc_angular_compiler
- src
- class_debug_info
- class_metadata
- component
- directive
- factory
- hmr
- injectable
- injector
- linker
- ng_module
- output
- partial
- pipeline
- phases
- reify
- statements
- pipe
- r3
- service
- tests
- napi/angular-compiler
- src
- test
- vite-plugin
- utils
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | | - | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
164 | | - | |
165 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
166 | 172 | | |
167 | 173 | | |
168 | 174 | | |
169 | 175 | | |
170 | | - | |
| 176 | + | |
171 | 177 | | |
172 | 178 | | |
173 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
803 | 803 | | |
804 | 804 | | |
805 | 805 | | |
806 | | - | |
| 806 | + | |
807 | 807 | | |
808 | 808 | | |
809 | 809 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | | - | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
57 | | - | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| |||
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
66 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
67 | 75 | | |
68 | 76 | | |
69 | 77 | | |
| |||
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
| 86 | + | |
78 | 87 | | |
79 | 88 | | |
80 | 89 | | |
| |||
83 | 92 | | |
84 | 93 | | |
85 | 94 | | |
86 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
87 | 102 | | |
88 | 103 | | |
89 | 104 | | |
| |||
197 | 212 | | |
198 | 213 | | |
199 | 214 | | |
| 215 | + | |
200 | 216 | | |
201 | 217 | | |
202 | | - | |
| 218 | + | |
203 | 219 | | |
204 | 220 | | |
205 | 221 | | |
| |||
234 | 250 | | |
235 | 251 | | |
236 | 252 | | |
| 253 | + | |
237 | 254 | | |
238 | 255 | | |
239 | 256 | | |
240 | 257 | | |
241 | 258 | | |
242 | | - | |
| 259 | + | |
| 260 | + | |
243 | 261 | | |
244 | 262 | | |
245 | 263 | | |
| |||
255 | 273 | | |
256 | 274 | | |
257 | 275 | | |
258 | | - | |
| 276 | + | |
259 | 277 | | |
260 | 278 | | |
261 | 279 | | |
| |||
343 | 361 | | |
344 | 362 | | |
345 | 363 | | |
346 | | - | |
347 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
348 | 370 | | |
349 | 371 | | |
350 | 372 | | |
351 | 373 | | |
352 | | - | |
| 374 | + | |
353 | 375 | | |
354 | 376 | | |
355 | 377 | | |
| |||
0 commit comments