Repository navigation
feat: add additional template source location metadata #68564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /*! | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * 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'; | ||
|
|
||
| const testFiles = loadStandardTestFiles({fakeCommon: true}); | ||
|
|
||
| runInEachFileSystem(() => { | ||
| describe('attachSourcecodeLoc phase', () => { | ||
| let env!: NgtscTestEnvironment; | ||
|
|
||
| beforeEach(() => { | ||
| setEnableTemplateSourceLocations(true); | ||
| env = NgtscTestEnvironment.setup(testFiles); | ||
| env.tsconfig(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| setEnableTemplateSourceLocations(false); | ||
| }); | ||
|
|
||
| it('should attach the sourcecode location as a data attribute', () => { | ||
| env.write( | ||
| `test.ts`, | ||
| ` | ||
| import {Component} from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| template: \`<div><span>Hello</span></div>\`, | ||
| }) | ||
| class Comp {} | ||
| `, | ||
| ); | ||
| env.driveMain(); | ||
| const content = env.getContents('test.js'); | ||
|
|
||
| // The div | ||
| expect(content).toContain(`["data-sourcecode-loc", "test.ts;l=5-5;c=24-53"]`); | ||
| // The span | ||
| expect(content).toContain(`["data-sourcecode-loc", "test.ts;l=5-5;c=29-47"]`); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import {SecurityContext} from '../../../../core'; | ||
| import * as o from '../../../../output/output_ast'; | ||
| import * as ir from '../../ir'; | ||
| import type {ComponentCompilationJob} from '../compilation'; | ||
|
|
||
| /** | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't we reuse the existing
We only have one user of this internally so we can work with them to account for any changes we make to the formatting.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh cool, I wasn't sure that there weren't more users. I'm coordinating with that user for them to suppor data-sourcecode-loc. Follow along at https://b.corp.google.com/510157286 |
||
| * Inserts the data-sourcecode-loc attribute onto elements in templates. | ||
| * | ||
| * This is similar to the ./attach_source_locations phase, but this phase | ||
| * attaches more precise source locations, and it emits them directly into | ||
| * the HTML in a data attribute. | ||
| * | ||
| * This protocol is more useful for framework-agnostic tooling. | ||
| */ | ||
| export function attachSourcecodeLoc(job: ComponentCompilationJob): void { | ||
| if (!job.enableDebugLocations || job.relativeTemplatePath === null) { | ||
| return; | ||
| } | ||
|
|
||
| const fileName = job.relativeTemplatePath; | ||
| for (const unit of job.units) { | ||
| for (const op of unit.create) { | ||
| if (op.kind === ir.OpKind.ElementStart || op.kind === ir.OpKind.Element) { | ||
| const startLine = op.wholeSourceSpan.start.line + 1; | ||
| const startCol = op.wholeSourceSpan.start.col + 1; | ||
| const endLine = op.wholeSourceSpan.end.line + 1; | ||
| const endCol = op.wholeSourceSpan.end.col + 1; | ||
|
|
||
| const locString = `${fileName};l=${startLine}-${endLine};c=${startCol}-${endCol}`; | ||
|
|
||
| const extractedAttributeOp = ir.createExtractedAttributeOp( | ||
| op.xref, | ||
| ir.BindingKind.Attribute, | ||
| null, | ||
| 'data-sourcecode-loc', | ||
| o.literal(locString), | ||
| null, | ||
| null, | ||
| SecurityContext.NONE, | ||
| ); | ||
| unit.create.push(extractedAttributeOp); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did these additional arguments show up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's because there are additional attributes on the elements now, because of the new data-sourcecode-loc attributes being added when the flag that controls this feature is enabled