forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler): generate debug location instruction
Adds the logic that will generate the `ɵɵattachSourceLocations` instruction. Fixes angular#42530.
- Loading branch information
Showing
13 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
packages/compiler-cli/test/ngtsc/attach_source_location_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/*! | ||
* @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 {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; | ||
import {loadStandardTestFiles} from '@angular/compiler-cli/src/ngtsc/testing'; | ||
import {NgtscTestEnvironment} from './env'; | ||
import {setEnableTemplateSourceLocations} from '@angular/compiler/src/render3/view/config'; | ||
|
||
const testFiles = loadStandardTestFiles({fakeCommon: true}); | ||
|
||
runInEachFileSystem(() => { | ||
describe('source location instruction generation', () => { | ||
let env!: NgtscTestEnvironment; | ||
|
||
beforeEach(() => { | ||
setEnableTemplateSourceLocations(true); | ||
env = NgtscTestEnvironment.setup(testFiles); | ||
env.tsconfig(); | ||
}); | ||
|
||
afterEach(() => { | ||
setEnableTemplateSourceLocations(false); | ||
}); | ||
|
||
it('should attach the source location in an inline template', () => { | ||
env.write( | ||
`test.ts`, | ||
` | ||
import {Component} from '@angular/core'; | ||
@Component({ | ||
template: \` | ||
<div><span> | ||
<strong>Hello</strong> | ||
</span></div> | ||
\`, | ||
}) | ||
class Comp {} | ||
`, | ||
); | ||
env.driveMain(); | ||
const content = env.getContents('test.js'); | ||
expect(content).toContain('ɵɵelementStart(0, "div")(1, "span")(2, "strong");'); | ||
expect(content).toContain( | ||
'ɵɵattachSourceLocations("test.ts", [[0, 114, 5, 14], [1, 119, 5, 19], [2, 142, 6, 16]]);', | ||
); | ||
}); | ||
|
||
it('should attach the source location in an external template', () => { | ||
env.write( | ||
'test.html', | ||
` | ||
<div><span> | ||
<strong>Hello</strong> | ||
</span></div> | ||
`, | ||
); | ||
|
||
env.write( | ||
`test.ts`, | ||
` | ||
import {Component} from '@angular/core'; | ||
@Component({templateUrl: './test.html'}) | ||
class Comp {} | ||
`, | ||
); | ||
env.driveMain(); | ||
const content = env.getContents('test.js'); | ||
expect(content).toContain('ɵɵelementStart(0, "div")(1, "span")(2, "strong");'); | ||
expect(content).toContain( | ||
'ɵɵattachSourceLocations("test.html", [[0, 9, 1, 8], [1, 14, 1, 13], [2, 31, 2, 10]]);', | ||
); | ||
}); | ||
|
||
it('should attach the source location to structural directives', () => { | ||
env.write( | ||
`test.ts`, | ||
` | ||
import {Component} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
@Component({ | ||
imports: [CommonModule], | ||
template: \` | ||
<div *ngIf="true"> | ||
<span></span> | ||
</div> | ||
\`, | ||
}) | ||
class Comp {} | ||
`, | ||
); | ||
env.driveMain(); | ||
const content = env.getContents('test.js'); | ||
expect(content).toContain('ɵɵtemplate(0,'); | ||
expect(content).toContain('ɵɵelementStart(0, "div");'); | ||
expect(content).toContain('ɵɵelement(1, "span");'); | ||
expect(content).toContain( | ||
'ɵɵattachSourceLocations("test.ts", [[0, 207, 7, 14], [1, 242, 8, 16]]);', | ||
); | ||
}); | ||
|
||
it('should not attach the source location to ng-container', () => { | ||
env.write( | ||
`test.ts`, | ||
` | ||
import {Component} from '@angular/core'; | ||
@Component({ | ||
template: \` | ||
<ng-container> | ||
<div> | ||
<ng-container> | ||
<span></span> | ||
</ng-container> | ||
</div> | ||
</ng-container> | ||
\`, | ||
}) | ||
class Comp {} | ||
`, | ||
); | ||
env.driveMain(); | ||
const content = env.getContents('test.js'); | ||
expect(content).toContain('ɵɵelementContainerStart(0);'); | ||
expect(content).toContain('ɵɵelementStart(1, "div");'); | ||
expect(content).toContain('ɵɵelementContainerStart(2);'); | ||
expect(content).toContain('ɵɵelement(3, "span");'); | ||
expect(content).toContain( | ||
'ɵɵattachSourceLocations("test.ts", [[1, 145, 6, 16], [3, 204, 8, 20]]);', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*! | ||
* @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 | ||
*/ | ||
|
||
/** | ||
* Whether to produce instructions that will attach the source location to each DOM node. | ||
* | ||
* !!!Important!!! at the time of writing this flag isn't exposed externally, but internal debug | ||
* tools enable it via a local change. Any modifications to this flag need to update the | ||
* internal tooling as well. | ||
*/ | ||
export let ENABLE_TEMPLATE_SOURCE_LOCATIONS = false; | ||
|
||
/** | ||
* Utility function to set the `ENABLE_TEMPLATE_SOURCE_LOCATIONS` flag. | ||
* Intended to be used **only** inside unit tests. | ||
*/ | ||
export function setEnableTemplateSourceLocations(value: boolean): void { | ||
ENABLE_TEMPLATE_SOURCE_LOCATIONS = value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/compiler/src/template/pipeline/src/phases/attach_source_locations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @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 * as ir from '../../ir'; | ||
import type {ComponentCompilationJob} from '../compilation'; | ||
|
||
/** | ||
* Locates all of the elements defined in a creation block and outputs an op | ||
* that will expose their definition location in the DOM. | ||
*/ | ||
export function attachSourceLocations(job: ComponentCompilationJob): void { | ||
if (!job.enableDebugLocations || job.relativeTemplatePath === null) { | ||
return; | ||
} | ||
|
||
for (const unit of job.units) { | ||
const locations: ir.ElementSourceLocation[] = []; | ||
|
||
for (const op of unit.create) { | ||
if (op.kind === ir.OpKind.ElementStart || op.kind === ir.OpKind.Element) { | ||
const start = op.startSourceSpan.start; | ||
locations.push({ | ||
targetSlot: op.handle, | ||
offset: start.offset, | ||
line: start.line, | ||
column: start.col, | ||
}); | ||
} | ||
} | ||
|
||
if (locations.length > 0) { | ||
unit.create.push(ir.createSourceLocationOp(job.relativeTemplatePath, locations)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters