forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler): Add failing tests about structural attr bindings
While running a g3 presubmit, I discovered two related novel failure modes: 1. Simple case: this new test uses an `ngFor` structural directive, which binds a context variable. That variable is immediately used in an attribute binding. It looks like we generate an extra attribute instruction, which might result in an invalid property read at runtime. 2. Complex case: this is another attribute binding, this time on a structural element, inside of an `ng-template`. Not sure what's going on here.
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
...nce/test_cases/r3_view_compiler_template/attr_binding_on_structural_inside_ng_template.js
This file contains 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,24 @@ | ||
function MyComponent_ng_template_0_span_0_Template(rf, ctx) { | ||
if (rf & 1) { | ||
i0.ɵɵelement(0, "span"); | ||
} if (rf & 2) { | ||
const $ctx_r2$ = i0.ɵɵnextContext(2); | ||
i0.ɵɵattribute("someAttr", $ctx_r2$.someField); | ||
} | ||
} | ||
|
||
function MyComponent_ng_template_0_Template(rf, ctx) { | ||
if (rf & 1) { | ||
i0.ɵɵtemplate(0, MyComponent_ng_template_0_span_0_Template, 1, 1, "span", 1); | ||
} if (rf & 2) { | ||
const $ctx_r0$ = i0.ɵɵnextContext(); | ||
i0.ɵɵproperty("ngIf", $ctx_r0$.someBooleanField); | ||
} | ||
} | ||
|
||
… | ||
consts: [["someLocalRef", ""], [4, "ngIf"]], template: function MyComponent_Template(rf, ctx) { | ||
if (rf & 1) { | ||
i0.ɵɵtemplate(0, MyComponent_ng_template_0_Template, 1, 1, "ng-template", null, 0, i0.ɵɵtemplateRefExtractor); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nce/test_cases/r3_view_compiler_template/attr_binding_on_structural_inside_ng_template.ts
This file contains 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,18 @@ | ||
import {Component, NgModule} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'my-component', | ||
template: ` | ||
<ng-template #someLocalRef> | ||
<span [attr.someAttr]="someField" *ngIf="someBooleanField"></span> | ||
</ng-template> | ||
`, | ||
}) | ||
export class MyComponent { | ||
someField!: any; | ||
someBooleanField!: boolean; | ||
} | ||
|
||
@NgModule({declarations: [MyComponent]}) | ||
export class MyModule { | ||
} |
18 changes: 18 additions & 0 deletions
18
...li/test/compliance/test_cases/r3_view_compiler_template/ng_for_context_in_attr_binding.js
This file contains 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,18 @@ | ||
MyComponent_div_0_Template(rf, ctx) { | ||
if (rf & 1) { | ||
i0.ɵɵelement(0, "div"); | ||
} if (rf & 2) { | ||
const $someElem_r1$ = ctx.$implicit; | ||
i0.ɵɵattribute("someInputAttr", $someElem_r1$.someAttr()); | ||
} | ||
} | ||
|
||
… | ||
consts: [[__AttributeMarker.Template__, "ngFor", "ngForOf"]], | ||
template:function MyComponent_Template(rf, ctx){ | ||
if (rf & 1) { | ||
i0.ɵɵtemplate(0, MyComponent_div_0_Template, 1, 1, "div", 0); | ||
} if (rf & 2) { | ||
i0.ɵɵproperty("ngForOf", ctx.someField.someMethod()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...li/test/compliance/test_cases/r3_view_compiler_template/ng_for_context_in_attr_binding.ts
This file contains 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,17 @@ | ||
import {Component, NgModule} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'my-component', | ||
template: ` | ||
<div *ngFor="let someElem of someField.someMethod()" | ||
[attr.someInputAttr]="someElem.someAttr()"> | ||
</div> | ||
`, | ||
}) | ||
export class MyComponent { | ||
someField!: any; | ||
} | ||
|
||
@NgModule({declarations: [MyComponent]}) | ||
export class MyModule { | ||
} |