feat(compiler-cli): produce template diagnostics error messages (#17125)

Refactoring the compiler to use transformers moves the code generation
after type-checking which suppresses the errors TypeScript would
generate in the user code.

`TypeChecker` currently produces the same factory code that was
generated prior the switch to transfomers, getting back the same
diagnostics as before. The refactoring will allow the code to
diverge from the factory code and allow better diagnostic error
messages than was previously possible by type-checking the factories.
This commit is contained in:
Chuck Jazdzewski
2017-06-01 11:13:50 -06:00
committed by Victor Berchet
parent 1338995ee9
commit 230255f887
6 changed files with 398 additions and 7 deletions

View File

@ -61,6 +61,7 @@ export * from './ml_parser/interpolation_config';
export * from './ml_parser/tags';
export {NgModuleCompiler} from './ng_module_compiler';
export {AssertNotNull, BinaryOperator, BinaryOperatorExpr, BuiltinMethod, BuiltinVar, CastExpr, ClassStmt, CommaExpr, CommentStmt, ConditionalExpr, DeclareFunctionStmt, DeclareVarStmt, ExpressionStatement, ExpressionVisitor, ExternalExpr, ExternalReference, FunctionExpr, IfStmt, InstantiateExpr, InvokeFunctionExpr, InvokeMethodExpr, LiteralArrayExpr, LiteralExpr, LiteralMapExpr, NotExpr, ReadKeyExpr, ReadPropExpr, ReadVarExpr, ReturnStatement, StatementVisitor, ThrowStmt, TryCatchStmt, WriteKeyExpr, WritePropExpr, WriteVarExpr, StmtModifier, Statement} from './output/output_ast';
export {EmitterVisitorContext} from './output/abstract_emitter';
export * from './output/ts_emitter';
export * from './parse_util';
export * from './schema/dom_element_schema_registry';

View File

@ -35,6 +35,7 @@ export class EmitterVisitorContext {
private _lines: _EmittedLine[];
private _classes: o.ClassStmt[] = [];
private _preambleLineCount = 0;
constructor(private _indent: number) { this._lines = [new _EmittedLine(_indent)]; }
@ -155,6 +156,23 @@ export class EmitterVisitorContext {
return map;
}
setPreambleLineCount(count: number) { return this._preambleLineCount = count; }
spanOf(line: number, column: number): ParseSourceSpan|null {
const emittedLine = this._lines[line - this._preambleLineCount];
if (emittedLine) {
let columnsLeft = column - emittedLine.indent;
for (let partIndex = 0; partIndex < emittedLine.parts.length; partIndex++) {
const part = emittedLine.parts[partIndex];
if (part.length > columnsLeft) {
return emittedLine.srcSpans[partIndex];
}
columnsLeft -= part.length;
}
}
return null;
}
private get sourceLines(): _EmittedLine[] {
if (this._lines.length && this._lines[this._lines.length - 1].parts.length === 0) {
return this._lines.slice(0, -1);

View File

@ -37,9 +37,9 @@ export function debugOutputAstAsTypeScript(ast: o.Statement | o.Expression | o.T
export class TypeScriptEmitter implements OutputEmitter {
emitStatements(
srcFilePath: string, genFilePath: string, stmts: o.Statement[],
preamble: string = ''): string {
emitStatementsAndContext(
srcFilePath: string, genFilePath: string, stmts: o.Statement[], preamble: string = '',
emitSourceMaps: boolean = true): {sourceText: string, context: EmitterVisitorContext} {
const converter = new _TsEmitterVisitor();
const ctx = EmitterVisitorContext.createRoot();
@ -60,14 +60,21 @@ export class TypeScriptEmitter implements OutputEmitter {
`ort * as ${prefix} from '${importedModuleName}';`);
});
const sm =
ctx.toSourceMapGenerator(srcFilePath, genFilePath, preambleLines.length).toJsComment();
const sm = emitSourceMaps ?
ctx.toSourceMapGenerator(srcFilePath, 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.
lines.push('');
}
return lines.join('\n');
ctx.setPreambleLineCount(preambleLines.length);
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;
}
}