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

Function sliceGuarded

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

* Slice `span` from the original source, with a `// @ts-ignore` line right before each identifier * in `guards` (`ClassMetadata.laterDeclarationReferences`) and any implicit signal `debugName` * insertions that fall inside it (mirroring ngtsc, where `signalMetadataTransform` runs on the * `Source

(
  s: MagicString | string,
  span: nga.SpanMetadata,
  guards: nga.SpanMetadata[],
  functionSpans?: nga.SpanMetadata[],
)

Source from the content-addressed store, hash-verified

2251 * https://github.com/angular/angular/blob/83622ee/packages/compiler-cli/src/ngtsc/core/src/compiler.ts#L879-L880).
2252 */
2253function sliceGuarded(
2254 s: MagicString | string,
2255 span: nga.SpanMetadata,
2256 guards: nga.SpanMetadata[],
2257 functionSpans?: nga.SpanMetadata[],
2258): string {
2259 const source = typeof s === 'string' ? s : s.original;
2260 const insertions: SourceInsertion[] = [];
2261 if (functionSpans) {
2262 for (const fnSpan of functionSpans) {
2263 if (
2264 fnSpan.start >= span.start &&
2265 fnSpan.end <= span.end &&
2266 !(fnSpan.start === span.start && fnSpan.end === span.end)
2267 ) {
2268 insertions.push({position: fnSpan.start, text: '('});
2269 insertions.push({position: fnSpan.end, text: ')'});
2270 }
2271 }
2272 }
2273 if (typeof s !== 'string') {
2274 const debugInsertions = signalDebugNameInsertionsBySource.get(s);
2275 if (debugInsertions !== undefined) {
2276 for (const ins of debugInsertions) {
2277 if (ins.position >= span.start && ins.position < span.end) {
2278 insertions.push(ins);
2279 }
2280 }
2281 }
2282 }
2283 const seenGuards = new Set<number>();
2284 for (const guard of guards) {
2285 if (guard.start >= span.start && guard.start < span.end && !seenGuards.has(guard.start)) {
2286 seenGuards.add(guard.start);
2287 insertions.push({position: guard.start, text: FORWARD_REFERENCE_GUARD});
2288 }
2289 }
2290 if (insertions.length === 0) {
2291 return source.slice(span.start, span.end);
2292 }
2293 insertions.sort((a, b) => a.position - b.position);
2294 let result = '';
2295 let cursor = span.start;
2296 for (const ins of insertions) {
2297 result += source.slice(cursor, ins.position) + ins.text;
2298 cursor = ins.position;
2299 }
2300 return result + source.slice(cursor, span.end);
2301}
2302
2303/**
2304 * `sliceGuarded` wrapped for the output AST, remembering `span` so HMR can tell which identifier

Callers 8

compileClassesFunction · 0.85
inlineDecoratorResourcesFunction · 0.85
rawSliceFunction · 0.85
buildSetClassMetadataFunction · 0.85
rawExprFromSpanFunction · 0.85
rawExprsFromSpansFunction · 0.85
buildInjectorImportsFunction · 0.85

Calls 5

getMethod · 0.65
hasMethod · 0.65
addMethod · 0.65
sliceMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected