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

@ -18,9 +18,7 @@ export const CATCH_ERROR_VAR = o.variable('error', null, null);
export const CATCH_STACK_VAR = o.variable('stack', null, null);
export interface OutputEmitter {
emitStatements(
srcFilePath: string, genFilePath: string, stmts: o.Statement[],
preamble?: string|null): string;
emitStatements(genFilePath: string, stmts: o.Statement[], preamble?: string|null): string;
}
class _EmittedLine {
@ -96,8 +94,7 @@ export class EmitterVisitorContext {
.join('\n');
}
toSourceMapGenerator(sourceFilePath: string, genFilePath: string, startsAtLine: number = 0):
SourceMapGenerator {
toSourceMapGenerator(genFilePath: string, startsAtLine: number = 0): SourceMapGenerator {
const map = new SourceMapGenerator(genFilePath);
let firstOffsetMapped = false;
@ -106,7 +103,7 @@ export class EmitterVisitorContext {
// Add a single space so that tools won't try to load the file from disk.
// Note: We are using virtual urls like `ng:///`, so we have to
// provide a content here.
map.addSource(sourceFilePath, ' ').addMapping(0, sourceFilePath, 0, 0);
map.addSource(genFilePath, ' ').addMapping(0, genFilePath, 0, 0);
firstOffsetMapped = true;
}
};

View File

@ -15,9 +15,7 @@ import {AbstractJsEmitterVisitor} from './abstract_js_emitter';
import * as o from './output_ast';
export class JavaScriptEmitter implements OutputEmitter {
emitStatements(
srcFilePath: string, genFilePath: string, stmts: o.Statement[],
preamble: string = ''): string {
emitStatements(genFilePath: string, stmts: o.Statement[], preamble: string = ''): string {
const converter = new JsEmitterVisitor();
const ctx = EmitterVisitorContext.createRoot();
converter.visitAllStatements(stmts, ctx);
@ -30,8 +28,7 @@ export class JavaScriptEmitter implements OutputEmitter {
`uire('${importedModuleName}');`);
});
const sm =
ctx.toSourceMapGenerator(srcFilePath, genFilePath, preambleLines.length).toJsComment();
const sm = ctx.toSourceMapGenerator(genFilePath, preambleLines.length).toJsComment();
const lines = [...preambleLines, ctx.toSource(), sm];
if (sm) {
// always add a newline at the end, as some tools have bugs without it.

View File

@ -31,7 +31,7 @@ function evalExpression(
// We don't want to hard code this fact, so we auto detect it via an empty function first.
const emptyFn = new Function(...fnArgNames.concat('return null;')).toString();
const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, sourceUrl, headerLines).toJsComment()}`;
fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
}
return new Function(...fnArgNames.concat(fnBody))(...fnArgValues);
}

View File

@ -39,7 +39,7 @@ export type ReferenceFilter = (reference: o.ExternalReference) => boolean;
export class TypeScriptEmitter implements OutputEmitter {
emitStatementsAndContext(
srcFilePath: string, genFilePath: string, stmts: o.Statement[], preamble: string = '',
genFilePath: string, stmts: o.Statement[], preamble: string = '',
emitSourceMaps: boolean = true,
referenceFilter?: ReferenceFilter): {sourceText: string, context: EmitterVisitorContext} {
const converter = new _TsEmitterVisitor(referenceFilter);
@ -63,7 +63,7 @@ export class TypeScriptEmitter implements OutputEmitter {
});
const sm = emitSourceMaps ?
ctx.toSourceMapGenerator(srcFilePath, genFilePath, preambleLines.length).toJsComment() :
ctx.toSourceMapGenerator(genFilePath, preambleLines.length).toJsComment() :
'';
const lines = [...preambleLines, ctx.toSource(), sm];
if (sm) {
@ -74,9 +74,8 @@ export class TypeScriptEmitter implements OutputEmitter {
return {sourceText: lines.join('\n'), context: ctx};
}
emitStatements(
srcFilePath: string, genFilePath: string, stmts: o.Statement[], preamble: string = '') {
return this.emitStatementsAndContext(srcFilePath, genFilePath, stmts, preamble).sourceText;
emitStatements(genFilePath: string, stmts: o.Statement[], preamble: string = '') {
return this.emitStatementsAndContext(genFilePath, stmts, preamble).sourceText;
}
}