MCPcopy Create free account
hub / github.com/angular/angular / buildInjectorImports

Function buildInjectorImports

packages/compiler-cli/preprocessor/src/processor.ts:3273–3305  ·  view source on GitHub ↗

* Builds the OPTIMIZE-mode `ɵinj.imports` expression list: resolved references with the * `injectorImportRawSpans` elements spliced in verbatim — ngtsc keeps a top-level `imports` * element as written whenever it carries a `ModuleWithProviders` or none of its references are * filtered out, which

(
  ngModule: nga.NgModuleMetadata,
  content?: MagicString | string,
  guards: nga.SpanMetadata[] = [],
)

Source from the content-addressed store, hash-verified

3271 * only applies when a module has no imports/exports to re-print.
3272 */
3273function buildInjectorImports(
3274 ngModule: nga.NgModuleMetadata,
3275 content?: MagicString | string,
3276 guards: nga.SpanMetadata[] = [],
3277): o.Expression[] {
3278 const {imports, injectorImports, exports} = ngModule;
3279 const importExprs: o.Expression[] = (
3280 injectorImports || (imports || []).concat(exports || [])
3281 ).map((d: nga.ReferenceMetadata) => refToExpression(d));
3282
3283 // Raw indices were computed against `injectorImports`; if that failed to lower and we
3284 // fell back to `imports`/`exports` above, they are meaningless — skip splicing.
3285 if (ngModule.injectorImportRawSpans && injectorImports && content) {
3286 const raws = [...ngModule.injectorImportRawSpans].sort((a, b) => a.index - b.index);
3287 for (const raw of raws) {
3288 // `raw.index` was computed against the flattened kept-import count in
3289 // `optimize_ng_module`; if it exceeds the references we actually received, the two
3290 // models desynced — fail loudly rather than emit the expression in the wrong place.
3291 if (raw.index > importExprs.length) {
3292 throw new Error(
3293 `ɵinj.imports raw index ${raw.index} exceeds resolved import count ` +
3294 `${importExprs.length}; flattened-count model desynced from wire references`,
3295 );
3296 }
3297 importExprs.splice(
3298 raw.index,
3299 0,
3300 new o.WrappedNodeExpr(new RawSource(sliceGuarded(content, raw.span, guards))),
3301 );
3302 }
3303 }
3304 return importExprs;
3305}
3306
3307/**
3308 * Reports NG8014 when an `import` declaration used by `@Component.deferredImports` cannot be

Callers 1

injectorImportsExprsFunction · 0.85

Calls 3

refToExpressionFunction · 0.85
sliceGuardedFunction · 0.85
mapMethod · 0.80

Tested by

no test coverage detected