Repository navigation
feat(compiler): support safe keyed read expressions #41911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c97071
feat(compiler): support safe keyed read expressions
crisbeto 413bb6e
fixup! feat(compiler): support safe keyed read expressions
crisbeto a9ae5fb
fixup! feat(compiler): support safe keyed read expressions
crisbeto 89bb80e
fixup! feat(compiler): support safe keyed read expressions
crisbeto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...es/compiler-cli/test/compliance/test_cases/r3_view_compiler/safe_access/GOLDEN_PARTIAL.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /**************************************************************************************************** | ||
| * PARTIAL FILE: safe_keyed_read.js | ||
| ****************************************************************************************************/ | ||
| import { Component, NgModule } from '@angular/core'; | ||
| import * as i0 from "@angular/core"; | ||
| export class MyApp { | ||
| constructor() { | ||
| this.unknownNames = null; | ||
| this.knownNames = [['Frodo', 'Bilbo']]; | ||
| this.species = null; | ||
| this.keys = null; | ||
| this.speciesMap = { key: 'unknown' }; | ||
| } | ||
| } | ||
| MyApp.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyApp, deps: [], target: i0.ɵɵFactoryTarget.Component }); | ||
| MyApp.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", type: MyApp, selector: "ng-component", ngImport: i0, template: ` | ||
|
crisbeto marked this conversation as resolved.
Resolved
|
||
| <span [title]="'Your last name is ' + (unknownNames?.[0] || 'unknown')"> | ||
| Hello, {{ knownNames?.[0]?.[1] }}! | ||
| You are a Balrog: {{ species?.[0]?.[1]?.[2]?.[3]?.[4]?.[5] || 'unknown' }} | ||
| You are an Elf: {{ speciesMap?.[keys?.[0] ?? 'key'] }} | ||
| You are an Orc: {{ speciesMap?.['key'] }} | ||
| </span> | ||
| `, isInline: true }); | ||
| i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyApp, decorators: [{ | ||
| type: Component, | ||
| args: [{ | ||
| template: ` | ||
| <span [title]="'Your last name is ' + (unknownNames?.[0] || 'unknown')"> | ||
| Hello, {{ knownNames?.[0]?.[1] }}! | ||
| You are a Balrog: {{ species?.[0]?.[1]?.[2]?.[3]?.[4]?.[5] || 'unknown' }} | ||
| You are an Elf: {{ speciesMap?.[keys?.[0] ?? 'key'] }} | ||
| You are an Orc: {{ speciesMap?.['key'] }} | ||
| </span> | ||
| ` | ||
| }] | ||
| }] }); | ||
| export class MyModule { | ||
| } | ||
| MyModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); | ||
| MyModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyModule, declarations: [MyApp] }); | ||
| MyModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyModule }); | ||
| i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyModule, decorators: [{ | ||
| type: NgModule, | ||
| args: [{ declarations: [MyApp] }] | ||
| }] }); | ||
|
|
||
| /**************************************************************************************************** | ||
| * PARTIAL FILE: safe_keyed_read.d.ts | ||
| ****************************************************************************************************/ | ||
| import * as i0 from "@angular/core"; | ||
| export declare class MyApp { | ||
| unknownNames: string[] | null; | ||
| knownNames: string[][]; | ||
| species: null; | ||
| keys: null; | ||
| speciesMap: Record<string, string>; | ||
| static ɵfac: i0.ɵɵFactoryDeclaration<MyApp, never>; | ||
| static ɵcmp: i0.ɵɵComponentDeclaration<MyApp, "ng-component", never, {}, {}, never, never>; | ||
| } | ||
| export declare class MyModule { | ||
| static ɵfac: i0.ɵɵFactoryDeclaration<MyModule, never>; | ||
| static ɵmod: i0.ɵɵNgModuleDeclaration<MyModule, [typeof MyApp], never, never>; | ||
| static ɵinj: i0.ɵɵInjectorDeclaration<MyModule>; | ||
| } | ||
|
|
||
22 changes: 22 additions & 0 deletions
22
...ages/compiler-cli/test/compliance/test_cases/r3_view_compiler/safe_access/TEST_CASES.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "$schema": "../../test_case_schema.json", | ||
| "cases": [ | ||
| { | ||
| "description": "should handle safe keyed reads inside templates", | ||
| "inputFiles": [ | ||
| "safe_keyed_read.ts" | ||
| ], | ||
| "expectations": [ | ||
| { | ||
| "files": [ | ||
| { | ||
| "expected": "safe_keyed_read_template.js", | ||
| "generated": "safe_keyed_read.js" | ||
| } | ||
| ], | ||
| "failureMessage": "Incorrect template" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
23 changes: 23 additions & 0 deletions
23
...s/compiler-cli/test/compliance/test_cases/r3_view_compiler/safe_access/safe_keyed_read.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import {Component, NgModule} from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| template: ` | ||
| <span [title]="'Your last name is ' + (unknownNames?.[0] || 'unknown')"> | ||
| Hello, {{ knownNames?.[0]?.[1] }}! | ||
| You are a Balrog: {{ species?.[0]?.[1]?.[2]?.[3]?.[4]?.[5] || 'unknown' }} | ||
| You are an Elf: {{ speciesMap?.[keys?.[0] ?? 'key'] }} | ||
| You are an Orc: {{ speciesMap?.['key'] }} | ||
| </span> | ||
| ` | ||
| }) | ||
| export class MyApp { | ||
| unknownNames: string[]|null = null; | ||
| knownNames: string[][] = [['Frodo', 'Bilbo']]; | ||
| species = null; | ||
| keys = null; | ||
| speciesMap: Record<string, string> = {key: 'unknown'}; | ||
| } | ||
|
|
||
| @NgModule({declarations: [MyApp]}) | ||
| export class MyModule { | ||
| } |
13 changes: 13 additions & 0 deletions
13
...r-cli/test/compliance/test_cases/r3_view_compiler/safe_access/safe_keyed_read_template.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| template: function MyApp_Template(rf, ctx) { | ||
| if (rf & 1) { | ||
| i0.ɵɵelementStart(0, "span", 0); | ||
| i0.ɵɵtext(1); | ||
| i0.ɵɵelementEnd(); | ||
| } | ||
| if (rf & 2) { | ||
| let $tmp_0_0$; | ||
| i0.ɵɵproperty("title", "Your last name is " + ((ctx.unknownNames == null ? null : ctx.unknownNames[0]) || "unknown")); | ||
|
JoostK marked this conversation as resolved.
Resolved
|
||
| i0.ɵɵadvance(1); | ||
| i0.ɵɵtextInterpolate4(" Hello, ", ctx.knownNames == null ? null : ctx.knownNames[0] == null ? null : ctx.knownNames[0][1], "! You are a Balrog: ", (ctx.species == null ? null : ctx.species[0] == null ? null : ctx.species[0][1] == null ? null : ctx.species[0][1][2] == null ? null : ctx.species[0][1][2][3] == null ? null : ctx.species[0][1][2][3][4] == null ? null : ctx.species[0][1][2][3][4][5]) || "unknown", " You are an Elf: ", ctx.speciesMap == null ? null : ctx.speciesMap[($tmp_0_0$ = ctx.keys == null ? null : ctx.keys[0]) !== null && $tmp_0_0$ !== undefined ? $tmp_0_0$ : "key"], " You are an Orc: ", ctx.speciesMap == null ? null : ctx.speciesMap["key"], " "); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is necessary/desirable. In 2 of the 3 cases, a sub-expression is already marked with the source span of the
SafeKeyedRead. It's odd to have an outer expression that's also annotated.I think this could be refactored:
addParseSpanInfo, and mark the element access expression with the span inside theveWillInferAnyForcase.let nodeandreturnfrom inside each case instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have the full context here, but I was following the pattern from
visitSafeMethodCallandvisitSafePropertyReadright above it. Should they be refactored as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, but I think that's a bigger change that should be in a different PR. For now, I think following the pattern from
visitSafeMethodCallandvisitSafePropertyReadis correct.