Description
Angular/Typescript teams discussed this in person.
Angular 2 includes a template parser, and an expression language which may be used in places in the template.
We would like to translate the template to a TypeScript buffer, and pass the buffer to the language services for things like producing semantic errors, requesting intellisense, and the other usual editor features.
However, the template has a backing class (actually a virtual class per @tbosch ) where the fields may be referenced by template expressions. In our canonical example,
@Component({selector: 'greet', template: 'Hello {{name}}!'})
class Greet {
name: string;
constructor() {
this.name = 'World';
}
}
The template could be represented in TypeScript with the expression
`Hello ${this.name}`
In order to get intellisense or errors for that expression, we need to evaluate it in a place where this.name
is defined. The obvious way to do this is for us to pass a "detached buffer", meaning a range of the SourceFile which lives outside the file content. It could look like (just a wild stab at possible syntax):
__template_eval(): string { return `Hello ${this.name}`; }
as if this code existed inside the Greet
class.
Also we would expect any ranges in the result to give the pos
and width
locations relative to the buffer we passed, not the Greet
class.