3

I want to start contributing to angular, I have an idea for a feature, I want the template compiler to issue a warning if a template contains two template variables of the same name. I think I managed to get close to the source files responsible: https://github.com/angular/angular/blob/master/packages/compiler/src/view_compiler/view_compiler.ts but couldn't quite find the spot, understandably. Is there anyone here who can guide me ?

3
  • but the compiler is doing that already - Reference "#t" is defined several times (" Commented Sep 14, 2017 at 5:44
  • How embarrassing, you're right... Thanks a lot anyway, I will look at that source file and try to discover how I didn't find out about it when I looked for it. Commented Sep 14, 2017 at 7:54
  • 1
    I don't see any embarrassing in wanting to learn more and occassionaly missing something :). I sometimes do that too when working with something unfamiliar. I showed you where the logic you're asking about happens. Commented Sep 14, 2017 at 8:37

1 Answer 1

1

You need to look into the _assertNoReferenceDuplicationOnTemplate method:

  _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 compiler creates AST with one node type being ElementAST which has references property:

export class ElementAst implements TemplateAst {
  constructor(
      public name: string,
      public references: ReferenceAst[],
      ...

And this is the property that is checked in the _assertNoReferenceDuplicationOnTemplate function and if found the error is generated.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.