fix(compiler): don’t use ng:// in AOT source maps, and never point to the original source file

This is important to not confuse users nor downstream tools that
consume our source maps. For generated content for which we don’t
have an original source file, we use the generated file now.

Fixes #19538
This commit is contained in:
Tobias Bosch
2017-10-04 13:37:27 -07:00
committed by Alex Rickabaugh
parent 5b5108363d
commit 01f711281c
16 changed files with 42 additions and 56 deletions

View File

@ -29,7 +29,7 @@ describe('compiler (unbundled Angular)', () => {
describe('aot source mapping', () => {
const componentPath = '/app/app.component.ts';
const ngComponentPath = 'ng:///app/app.component.ts';
const ngFactoryPath = '/app/app.component.ngfactory.ts';
let rootDir: MockDirectory;
let appDir: MockDirectory;
@ -83,7 +83,7 @@ describe('compiler (unbundled Angular)', () => {
}
describe('inline templates', () => {
const ngUrl = `${ngComponentPath}.AppComponent.html`;
const ngUrl = `${componentPath}.AppComponent.html`;
function templateDecorator(template: string) { return `template: \`${template}\`,`; }
@ -91,7 +91,7 @@ describe('compiler (unbundled Angular)', () => {
});
describe('external templates', () => {
const ngUrl = 'ng:///app/app.component.html';
const ngUrl = '/app/app.component.html';
const templateUrl = '/app/app.component.html';
function templateDecorator(template: string) {
@ -129,14 +129,14 @@ describe('compiler (unbundled Angular)', () => {
const sourceMap = extractSourceMap(genSource) !;
expect(sourceMap.file).toEqual(genFile.genFileUrl);
// the generated file contains code that is not mapped to
// the template but rather to the original source file (e.g. import statements, ...)
// Note: the generated file also contains code that is not mapped to
// the template (e.g. import statements, ...)
const templateIndex = sourceMap.sources.indexOf(ngUrl);
expect(sourceMap.sourcesContent[templateIndex]).toEqual(template);
// for the mapping to the original source file we don't store the source code
// as we want to keep whatever TypeScript / ... produced for them.
const sourceIndex = sourceMap.sources.indexOf(ngComponentPath);
const sourceIndex = sourceMap.sources.indexOf(ngFactoryPath);
expect(sourceMap.sourcesContent[sourceIndex]).toBe(' ');
});
@ -176,14 +176,14 @@ describe('compiler (unbundled Angular)', () => {
.toEqual({line: 2, column: 9, source: ngUrl});
});
it('should map non template parts to the source file', () => {
it('should map non template parts to the factory file', () => {
appDir['app.component.ts'] = createComponentSource(templateDecorator('Hello World!'));
const genFile = compileApp();
const genSource = toTypeScript(genFile);
const sourceMap = extractSourceMap(genSource) !;
expect(originalPositionFor(sourceMap, {line: 1, column: 0}))
.toEqual({line: 1, column: 0, source: ngComponentPath});
.toEqual({line: 1, column: 0, source: ngFactoryPath});
});
}
});

View File

@ -25,7 +25,7 @@ export function main() {
ctx.print(createSourceSpan(fileA, 1), 'o1');
ctx.print(createSourceSpan(fileB, 0), 'o2');
ctx.print(createSourceSpan(fileB, 1), 'o3');
const sm = ctx.toSourceMapGenerator('o.ts', 'o.js').toJSON() !;
const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !;
expect(sm.sources).toEqual([fileA.url, fileB.url]);
expect(sm.sourcesContent).toEqual([fileA.content, fileB.content]);
});
@ -43,7 +43,7 @@ export function main() {
it('should be able to shift the content', () => {
ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
const sm = ctx.toSourceMapGenerator('o.ts', 'o.js', 10).toJSON() !;
const sm = ctx.toSourceMapGenerator('o.ts', 10).toJSON() !;
expect(originalPositionFor(sm, {line: 11, column: 0})).toEqual({
line: 1,
column: 0,
@ -113,7 +113,7 @@ export function main() {
function expectMap(
ctx: EmitterVisitorContext, genLine: number, genCol: number, source: string | null = null,
srcLine: number | null = null, srcCol: number | null = null) {
const sm = ctx.toSourceMapGenerator('o.ts', 'o.js').toJSON() !;
const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !;
const genPosition = {line: genLine + 1, column: genCol};
const origPosition = originalPositionFor(sm, genPosition);
expect(origPosition.source).toEqual(source);
@ -123,7 +123,7 @@ function expectMap(
// returns the number of segments per line
function nbSegmentsPerLine(ctx: EmitterVisitorContext) {
const sm = ctx.toSourceMapGenerator('o.ts', 'o.js').toJSON() !;
const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !;
const lines = sm.mappings.split(';');
return lines.map(l => {
const m = l.match(/,/g);

View File

@ -16,7 +16,6 @@ import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler
import {extractSourceMap, originalPositionFor} from './source_map_util';
const someGenFilePath = 'somePackage/someGenFile';
const someSourceFilePath = 'somePackage/someSourceFile';
export function main() {
describe('JavaScriptEmitter', () => {
@ -27,7 +26,7 @@ export function main() {
function emitSourceMap(stmt: o.Statement | o.Statement[], preamble?: string): SourceMap {
const stmts = Array.isArray(stmt) ? stmt : [stmt];
const source = emitter.emitStatements(someSourceFilePath, someGenFilePath, stmts, preamble);
const source = emitter.emitStatements(someGenFilePath, stmts, preamble);
return extractSourceMap(source) !;
}
@ -40,7 +39,7 @@ export function main() {
const someVar = o.variable('someVar', null, sourceSpan);
const sm = emitSourceMap(someVar.toStmt(), '/* MyPreamble \n */');
expect(sm.sources).toEqual([someSourceFilePath, 'in.js']);
expect(sm.sources).toEqual([someGenFilePath, 'in.js']);
expect(sm.sourcesContent).toEqual([' ', ';;;var']);
expect(originalPositionFor(sm, {line: 3, column: 0}))
.toEqual({line: 1, column: 3, source: 'in.js'});

View File

@ -13,7 +13,6 @@ import * as o from '@angular/compiler/src/output/output_ast';
import {stripSourceMapAndNewLine} from './abstract_emitter_spec';
const someGenFilePath = 'somePackage/someGenFile';
const someSourceFilePath = 'somePackage/someSourceFile';
const anotherModuleUrl = 'somePackage/someOtherPath';
const sameModuleIdentifier = new o.ExternalReference(null, 'someLocalId', null);
@ -35,7 +34,7 @@ export function main() {
});
function emitStmt(stmt: o.Statement, preamble?: string): string {
const source = emitter.emitStatements(someSourceFilePath, someGenFilePath, [stmt], preamble);
const source = emitter.emitStatements(someGenFilePath, [stmt], preamble);
return stripSourceMapAndNewLine(source);
}

View File

@ -16,7 +16,6 @@ import {ParseSourceSpan} from '@angular/compiler/src/parse_util';
import {extractSourceMap, originalPositionFor} from './source_map_util';
const someGenFilePath = 'somePackage/someGenFile';
const someSourceFilePath = 'somePackage/someSourceFile';
export function main() {
// Not supported features of our OutputAst in TS:
@ -34,7 +33,7 @@ export function main() {
function emitSourceMap(stmt: o.Statement | o.Statement[], preamble?: string): SourceMap {
const stmts = Array.isArray(stmt) ? stmt : [stmt];
const source = emitter.emitStatements(someSourceFilePath, someGenFilePath, stmts, preamble);
const source = emitter.emitStatements(someGenFilePath, stmts, preamble);
return extractSourceMap(source) !;
}
@ -47,7 +46,7 @@ export function main() {
const someVar = o.variable('someVar', null, sourceSpan);
const sm = emitSourceMap(someVar.toStmt(), '/* MyPreamble \n */');
expect(sm.sources).toEqual([someSourceFilePath, 'in.js']);
expect(sm.sources).toEqual([someGenFilePath, 'in.js']);
expect(sm.sourcesContent).toEqual([' ', ';;;var']);
expect(originalPositionFor(sm, {line: 3, column: 0}))
.toEqual({line: 1, column: 3, source: 'in.js'});

View File

@ -13,7 +13,6 @@ import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler
import {stripSourceMapAndNewLine} from './abstract_emitter_spec';
const someGenFilePath = 'somePackage/someGenFile';
const someSourceFilePath = 'somePackage/someSourceFile';
const anotherModuleUrl = 'somePackage/someOtherPath';
const sameModuleIdentifier = new o.ExternalReference(null, 'someLocalId', null);
@ -36,7 +35,7 @@ export function main() {
function emitStmt(stmt: o.Statement | o.Statement[], preamble?: string): string {
const stmts = Array.isArray(stmt) ? stmt : [stmt];
const source = emitter.emitStatements(someSourceFilePath, someGenFilePath, stmts, preamble);
const source = emitter.emitStatements(someGenFilePath, stmts, preamble);
return stripSourceMapAndNewLine(source);
}
@ -435,7 +434,7 @@ export function main() {
new o.ReturnStatement(o.variable('someVar', null, referenceSpan), returnSpan)
])])];
const {sourceText, context} =
emitter.emitStatementsAndContext('a.ts', 'a.ts', statements, '/* some preamble /*\n\n');
emitter.emitStatementsAndContext('a.ts', statements, '/* some preamble /*\n\n');
const spanOf = (text: string, after: number = 0) => {
const location = sourceText.indexOf(text, after);
const {line, col} = calculateLineCol(sourceText, location);