Skip to content

feat(compiler): record absolute span of template expressions in parser - #31897

Closed
ayazhafiz wants to merge 5 commits into
angular:masterfrom
ayazhafiz:feat/raw-value
Closed

ayazhafiz wants to merge 5 commits into
angular:masterfrom
ayazhafiz:feat/raw-value

Conversation

@ayazhafiz

@ayazhafiz ayazhafiz commented Jul 29, 2019 •

Copy link
Copy Markdown
Contributor

Currently, the spans of expressions are recorded only relative to the
template node that they reside in, not their source file.

Introduce a sourceSpan property on expression ASTs that records the
location of an expression relative to the entire source code file that
it is in. This may allow for reducing duplication of effort in
ngtsc/typecheck/src/diagnostics later on as well.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #31898

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@ayazhafiz ayazhafiz added feature Label used to distinguish feature request from other issues target: major This PR is targeted for the next major release area: compiler Issues related to `ngc`, Angular's template compiler labels Jul 29, 2019
@ayazhafiz
ayazhafiz requested a review from a team July 29, 2019 20:33
@ayazhafiz ayazhafiz self-assigned this Jul 29, 2019
@ngbot ngbot Bot modified the milestone: needsTriage Jul 29, 2019
@ayazhafiz
ayazhafiz requested review from a team, AndrewKushnir, alxhub, kara and kyliau July 29, 2019 20:47

@kyliau kyliau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for language service

@alxhub
alxhub requested a review from petebacondarwin July 30, 2019 23:44
@ayazhafiz

Copy link
Copy Markdown
Contributor Author

One open question is whether the absolute span should apply to an AST's entire logical span or just the AST name. For example, the AST span of a safe property read currently covers the object being read from as well:

obj?.prop
^^^^^^^^^ SafePropertyRead span
     ^^^^ potential alternate span

What is an example of a Quote AST in a template? I can't seem to create one that is validly parsed.

@JoostK

JoostK commented Jul 31, 2019

Copy link
Copy Markdown
Member

One open question is whether the absolute span should apply to an AST's entire logical span or just the AST name. For example, the AST span of a safe property read currently covers the object being read from as well:

obj?.prop
^^^^^^^^^ SafePropertyRead span
     ^^^^ potential alternate span

Yes, this is an interesting question. The TypeScript AST prevents this problem by having prop being a ts.Identifier with its own span, and the parent, SafePropertyRead would then have the full span. I'm afraid we can't easily change the AST as there's many dependencies :( Alternatively, we could include an additional propSpan on SafePropertyRead with the downsides that it becomes quite cumbersome. This is the approach I took recently where I added valueSpan for things like property binding nodes.

What is an example of a Quote AST in a template? I can't seem to create one that is validly parsed.

I didn't know this one, but it seems like a hook to allow external languages to be used inside Angular templates, like so:

<input [value]="sql:select id from persons"/>

I don't know if this has ever been used as such, Quote itself does not seem to be dealt with in any way.

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally approve - nice work @ayazhafiz!
I have a couple of nits and questions that I would like to see dealt with.

Regarding the PropertyRead span... what is the downside of leaving it as the full expression (i.e. abc.foo)? If there is no obvious use case for distinguishing them, then I would leave it as it is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there much point in this ASTWithSource type now, given that AST nodes will include the sourceSpan already?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, but many consumers still rely on it. We can look into removing it in a future PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to get rid of this as well, but agree it should be part of followup work 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is called a lot of times with the same start number (and possibly when there is the same this.inputIndex value. If that is the case then we are creating unnecessary duplicate AbsoluteSourceSpan objects. Can we check all the calls to sourceSpan() to make sure that they are actually needed and can't be cached in variables - which they already are occasionally.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Using an object instead of a map for performance; let me know if you'd prefer more idiomatic usage.

I'll perform a follow up PR and clean up assignments of both span and sourceSpan after this is merged.

@ayazhafiz

ayazhafiz commented Jul 31, 2019 •

Copy link
Copy Markdown
Contributor Author

@JoostK

I'm afraid we can't easily change the AST as there's many dependencies :( Alternatively, we could include an additional propSpan on SafePropertyRead with the downsides that it becomes quite cumbersome.

Sorry for being vague in my comment; what I mean to say is that this new sourceSpan field could function in a manner that records only the span of the AST name. This has some benefits, including potentially making error messages much more granular.

One other issue I've noticed is that nested expressions with trailing whitespace will be given a span that includes the trailing whitespace:

{{ bool ? 1 : 0 }}
   ^^^^^ span of `bool`

We may want to make absolute spans ignore this too to remove ambiguity.

@petebacondarwin

Regarding the PropertyRead span... what is the downside of leaving it as the full expression (i.e. abc.foo)? If there is no obvious use case for distinguishing them, then I would leave it as it is.

There isn't a downside, but the benefits are what I mentioned in the paragraph above. I think it'll work fine either way though.

@JoostK

I didn't know this one, but it seems like a hook to allow external languages to be used inside Angular templates, like so:

<input [value]="sql:select id from persons"/>

👍 thanks, this is what I was looking for!

@ayazhafiz
ayazhafiz requested a review from JoostK July 31, 2019 18:13

@JoostK JoostK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a plan for getting rid of ParseSpan on AST nodes? With the introduction of sourceSpan I don't see why they should stay around.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to get rid of this as well, but agree it should be part of followup work 👍

@ayazhafiz

Copy link
Copy Markdown
Contributor Author

Is there a plan for getting rid of ParseSpan on AST nodes? With the introduction of sourceSpan I don't see why they should stay around.

Good idea. We will have to think about this because there are many consumers of Ast#span (maybe even externally?), but it should definitely be possible.

Currently, the spans of expressions are recorded only relative to the
template node that they reside in, not their source file.

Introduce a `sourceSpan` property on expression ASTs that records the
location of an expression relative to the entire source code file that
it is in. This may allow for reducing duplication of effort in
ngtsc/typecheck/src/diagnostics later on as well.

Child of angular#31898

@mhevery mhevery left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global Approval

@kyliau kyliau added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 14, 2019
@ngbot ngbot Bot added the action: merge The PR is ready for merge by the caretaker label Oct 14, 2019
@mhevery mhevery closed this in b04488d Oct 14, 2019
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Nov 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler cla: yes feature Label used to distinguish feature request from other issues merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants