我想开始贡献角度,我对某个功能有所了解,我希望模板编译器在模板包含两个同名模板变量时发出警告。我想我设法接近负责的源文件:https://github.com/angular/angular/blob/master/packages/compiler/src/view_compiler/view_compiler.ts但是找不到这个地方,这是可以理解的。这里有人可以指导我吗?
答案 0 :(得分:1)
您需要查看_assertNoReferenceDuplicationOnTemplate方法:
_assertNoReferenceDuplicationOnTemplate(result: TemplateAst[], errors): void {
const existingReferences: string[] = [];
result.filter(element => !!(<any>element).references)
.forEach(element => (<any>element).references.forEach((reference: ReferenceAst) => {
const name = reference.name;
if (existingReferences.indexOf(name) < 0) {
existingReferences.push(name);
} else {
const error = new TemplateParseError(
`Reference "#${name}" is defined several times`, reference.sourceSpan,
ParseErrorLevel.ERROR);
errors.push(error);
}
}));
}
Angular编译器创建AST,其中一个节点类型为ElementAST,其references
属性为:{/ p>
export class ElementAst implements TemplateAst {
constructor(
public name: string,
public references: ReferenceAst[],
...
这是在_assertNoReferenceDuplicationOnTemplate
函数中检查的属性,如果发现错误,则生成。