diff --git a/goldens/public-api/compiler-cli/compiler_options.api.md b/goldens/public-api/compiler-cli/compiler_options.api.md index 84f01a5a265..e560e1f7e7f 100644 --- a/goldens/public-api/compiler-cli/compiler_options.api.md +++ b/goldens/public-api/compiler-cli/compiler_options.api.md @@ -7,6 +7,7 @@ // @public export interface BazelAndG3Options { annotateForClosureCompiler?: boolean; + enableTemplateSourceLocations?: boolean; _experimentalAllowEmitDeclarationOnly?: boolean; generateDeepReexports?: boolean; generateExtraImportsInLocalMode?: boolean; diff --git a/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts b/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts index cb78ec15134..2b86a517c68 100644 --- a/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts +++ b/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts @@ -255,6 +255,7 @@ export class PartialComponentLinkerVersion1< declarations, hasDirectiveDependencies: !baseMeta.isStandalone || hasDirectiveDependencies, foreignImports: null, + enableTemplateSourceLocations: false, }; } diff --git a/packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts b/packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts index 8ca2b49ec7b..13c9baa4047 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts @@ -291,6 +291,7 @@ export class ComponentDecoratorHandler implements DecoratorHandler< private readonly enableSelectorless: boolean, private readonly emitDeclarationOnly: boolean, private readonly legacyOptionalChaining: boolean, + private readonly enableTemplateSourceLocations: boolean, ) { this.extractTemplateOptions = { enableI18nLegacyMessageIdFormat: this.enableI18nLegacyMessageIdFormat, @@ -1037,6 +1038,7 @@ export class ComponentDecoratorHandler implements DecoratorHandler< rawImports: rawImports !== null ? new o.WrappedNodeExpr(rawImports) : undefined, relativeTemplatePath, foreignImports: null, + enableTemplateSourceLocations: this.enableTemplateSourceLocations, }, typeCheckMeta: extractDirectiveTypeCheckMeta(node, inputs, this.reflector), classMetadata: this.includeClassMetadata diff --git a/packages/compiler-cli/src/ngtsc/annotations/component/test/component_spec.ts b/packages/compiler-cli/src/ngtsc/annotations/component/test/component_spec.ts index 3d43ac0fe15..33f024c4402 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/component/test/component_spec.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/component/test/component_spec.ts @@ -164,6 +164,7 @@ function setup( /* enableSelectorless */ false, /* emitDeclarationOnly */ false, /* enableInlineStyles */ true, + /* enableTemplateSourceLocations */ false, ); return {reflectionHost, handler, resourceLoader, metaRegistry}; } diff --git a/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts b/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts index 340178ca2c5..f811c4fa3ba 100644 --- a/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts +++ b/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts @@ -340,11 +340,18 @@ export interface BazelAndG3Options { _experimentalAllowEmitDeclarationOnly?: boolean; /** - * Whether to follow the Javascript optional chaining specs: returning `undefined` instead of `null` for null-safe navigation operations. + * Whether to follow the Javascript optional chaining specs: returning `undefined` instead of + * `null` for null-safe navigation operations. * * Defaults to `false`. */ legacyOptionalChaining?: boolean; + + /** + * Whether to generate additional code that adds the source location + * of elements to the DOM as an attribute. + */ + enableTemplateSourceLocations?: boolean; } /** diff --git a/packages/compiler-cli/src/ngtsc/core/src/compiler.ts b/packages/compiler-cli/src/ngtsc/core/src/compiler.ts index fed0edec07b..4bee79965b1 100644 --- a/packages/compiler-cli/src/ngtsc/core/src/compiler.ts +++ b/packages/compiler-cli/src/ngtsc/core/src/compiler.ts @@ -397,6 +397,7 @@ export class NgCompiler { private readonly implicitStandaloneValue: boolean; private readonly enableSelectorless: boolean; private readonly emitDeclarationOnly: boolean; + private readonly enableTemplateSourceLocations: boolean; /** * `NgCompiler` can be reused for multiple compilations (for resource-only changes), and each @@ -472,6 +473,7 @@ export class NgCompiler { this.angularCoreVersion === null || coreVersionSupportsFeature(this.angularCoreVersion, '>= 18.1.0'); this.enableSelectorless = options['_enableSelectorless'] ?? false; + this.enableTemplateSourceLocations = options['enableTemplateSourceLocations'] ?? false; this.emitDeclarationOnly = !!options.emitDeclarationOnly && !!options._experimentalAllowEmitDeclarationOnly; // Standalone by default is enabled since v19. We need to toggle it here, @@ -1549,6 +1551,7 @@ export class NgCompiler { this.enableSelectorless, this.emitDeclarationOnly, this.options.legacyOptionalChaining ?? LEGACY_OPTIONAL_CHAINING_DEFAULT, + this.enableTemplateSourceLocations, ), // TODO(alxhub): understand why the cast here is necessary (something to do with `null` diff --git a/packages/compiler-cli/test/ngtsc/attach_source_location_spec.ts b/packages/compiler-cli/test/ngtsc/attach_source_location_spec.ts index 98b2f54bb09..f0fd8b126d3 100644 --- a/packages/compiler-cli/test/ngtsc/attach_source_location_spec.ts +++ b/packages/compiler-cli/test/ngtsc/attach_source_location_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import {setEnableTemplateSourceLocations} from '@angular/compiler'; import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing'; import {loadStandardTestFiles} from '../../src/ngtsc/testing'; import {NgtscTestEnvironment} from './env'; @@ -18,16 +17,33 @@ runInEachFileSystem(() => { let env!: NgtscTestEnvironment; beforeEach(() => { - setEnableTemplateSourceLocations(true); env = NgtscTestEnvironment.setup(testFiles); - env.tsconfig(); }); - afterEach(() => { - setEnableTemplateSourceLocations(false); + it('should not attach template source locations by default', () => { + env.tsconfig(); + env.write( + `test.ts`, + ` + import {Component} from '@angular/core'; + + @Component({ + template: \` +