Title: Refactoring of Template Expression Parsing for Stability · Issue #31898 · angular/angular · GitHub
Open Graph Title: Refactoring of Template Expression Parsing for Stability · Issue #31898 · angular/angular
X Title: Refactoring of Template Expression Parsing for Stability · Issue #31898 · angular/angular
Description: 🚀 feature request Relevant Package @angular/compiler @angular/compiler-cli Description The Angular template expression parser, while working well for Angular compilations, has several issues that make the AST it generates difficult to us...
Open Graph Description: 🚀 feature request Relevant Package @angular/compiler @angular/compiler-cli Description The Angular template expression parser, while working well for Angular compilations, has several issues that m...
X Description: 🚀 feature request Relevant Package @angular/compiler @angular/compiler-cli Description The Angular template expression parser, while working well for Angular compilations, has several issues that m...
Opengraph URL: https://github.com/angular/angular/issues/31898
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Refactoring of Template Expression Parsing for Stability","articleBody":"# 🚀 feature request\r\n\r\n### Relevant Package\r\n@angular/compiler\r\n@angular/compiler-cli \r\n\r\n### Description\r\n\r\nThe Angular template expression parser, while working well for Angular compilations, has several issues that make the AST it generates difficult to use for other tasks; notably source mapping and indexing. This is because in many cases, the expression ASTs generated by the expression parser are not representative of the raw source code the expression is defined in.\r\n\r\n[Design Doc](https://hackmd.io/@X3ECPVy-RCuVfba-pnvIpw/BkDUxaW84/%2FMA1oxh6jRXqSmZBcLfYdyw?type=book) (with examples of issues)\r\n\r\nThis issue is meant to track development of the parser to better support use cases of source mapping and indexing. An overview of a plan-of-action is available on the design doc, as are examples of the issues currently experienced with expression ASTs.\r\n\r\nWork in this domain has already been started with at least #30181 and #31391.\r\n\r\n### Presentation of issues\r\n\r\n#### Preemptive discarding of whitespace\r\n\r\nThe template parsing pipeline discards whitespace in a template before generating the Render3Ast (the combined markup and expression AST):\r\n\r\nhttps://github.com/angular/angular/blob/376ad9c3cdf5a9432500333bce4a6fc42113210a/packages/compiler/src/render3/view/template.ts#L1942-L1956\r\n\r\nThis leads to a couple issues. The primary one is that the AST is not representative of the source code unless the entire pipeline is run without preserving whitespaces. The second is codes like\r\n\r\n```html\r\n\u003cdiv>{{ ' '.length }}\u003c/div>\r\n```\r\n\r\nare folded and ultimately evaluated to\r\n\r\n```html\r\n\u003cdiv>0\u003c/div>\r\n```\r\n\r\nA proposed solution is to eventually move the whitespace removal step to after the R3Ast is already generated. This involves writing a new tree rewriter.\r\n\r\n#### Overlapping spans with `leadingTriviaChars`\r\n\r\nThis issue has been previously recorded in #30181.\r\nTemplates parsed with leading trivia characters can wind up having overlapping spans between template HTML nodes and template expressions when whitespace discarding is disabled.\r\n\r\n```html\r\n\u003cdiv> \\n {{foo}}\u003c/div>\r\n^^^^^^^^^ recorded span of opening `div`\r\n ^^^^^^^^^^^^ recorded span of node text\r\n```\r\n\r\nA proposed solution is to record the absolute span of such expressions in the source code so that there is no need to add source spans to compute expression spans in a template.\r\n\r\n#### Misleading `AstWithSource` spans\r\n\r\nConsider a template code\r\n\r\n```html\r\n\u003cdiv *ngFor=\"let _ of all\">\u003c/div>\r\n```\r\n\r\nAbbreviated for example, the AST of this template is\r\n\r\n```typescript\r\nBoundAttribute {\r\n name: 'ngForOf',\r\n value: AstWithSource {\r\n source: 'all',\r\n span: {start: 0, end: 3},\r\n ast: PropertyRead {\r\n name: 'all',\r\n span: {start: 9, end: 12},\r\n },\r\n }\r\n}\r\n```\r\n\r\nThe issue here is that `AstWithSource` records the same source the `PropertyRead` does, but in a different context. It's difficult to tell the best way to resolve this:\r\n\r\n- Should we ignore `AstWithSource` and look only at the `PropertyRead` span?\r\n - This breaks cases where `AstWithSource` is worth looking at, e.g. in most non-template-binding ASTs\r\n- Should `AstWithSource` provide the span for all of `let _ of all`?\r\n - This is not necessarily the goal of `AstWithSource` and breaks many existing consumers of `AstWithSource`.\r\n- Should `PropertyRead` have a span relative to `AstWithSource`?\r\n - This requires updating `AstWithSource`'s source span calculation and also breaks many existing consumers.\r\n\r\nThe least ambiguous resolution is to record the source span of `AstWithSource` and the `PropertyRead` relative to the entire source code, so that calculation via span summation is not needed at all.\r\n\r\n### Proposed Plan of Attack\r\n\r\nThese issues can be reconciled by several goals. These goals are ordered by increasing size of impact, not necessarily by linearity.\r\n\r\n1. Record absolute source spans and byte offsets of expressions.\r\n - Move to expression ASTs recording the absolute location of an expression in a template file, not relative to the HTML node they are contained in as is being done now.\r\n - Introduce a new `AbsoluteSourceSpan` to represent this.\r\n2. Create a new `ExpressionIdentifier` (name variable) class to augment the name field of an expression AST. This will contain more accurate information about an expression, including\r\n - Its name\r\n - Its raw/original form\r\n - Its logical/computed form\r\n3. Move AST whitespace folding to after the R3 AST has already been created.\r\n - Create a new tree rewriter that can perform this.\r\n\r\n### Related Issues\r\n\r\nhttps://github.com/angular/vscode-ng-language-service/issues/137","author":{"url":"https://github.com/ayazhafiz","@type":"Person","name":"ayazhafiz"},"datePublished":"2019-07-29T20:45:42Z","url":"https://github.com/angular/angular/issues/31898"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:930cd7eb-3f36-caf6-f1ca-ea8baefd1e57 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87EC:240CD1:12B712D:17D74EC:6B012202 |
| html-safe-nonce | cbae4b562ecbb76d497ed4bd7b96973e93f8c0f0b10fbf4f3f510b0bf2be03ce |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0VDOjI0MENEMToxMkI3MTJEOjE3RDc0RUM6NkIwMTIyMDIiLCJ2aXNpdG9yX2lkIjoiNDk1OTY1MDE3MTgwMTY1MTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 860b422342ee68e33bdebbfe6f4f07f571baa46e117a867fe652da367ee74b35 |
| hovercard-subject-tag | issue:474248311 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/31898/issue_layout |
| twitter:image | https://opengraph.githubassets.com/279abf4284fe7d9854c1b69f81565190f696683fef25e19009e358ba88f3eace/angular/angular/issues/31898 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/279abf4284fe7d9854c1b69f81565190f696683fef25e19009e358ba88f3eace/angular/angular/issues/31898 |
| og:image:alt | 🚀 feature request Relevant Package @angular/compiler @angular/compiler-cli Description The Angular template expression parser, while working well for Angular compilations, has several issues that m... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ayazhafiz |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width