8000 fixup! fix(compiler): `this.a` should always refer to class property `a` · angular/angular@f7ba14f · GitHub
[go: up one dir, main page]

Skip to content

Commit f7ba14f

Browse files
committed
fixup! fix(compiler): this.a should always refer to class property a
1 parent 0c8817e commit f7ba14f

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_template/GOLDEN_PARTIAL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,13 +1249,13 @@ import * as i0 from "@angular/core";
12491249
export class MyComponent {
12501250
}
12511251
MyComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
1252-
MyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: MyComponent, isStandalone: true, selector: "my-component", ngImport: i0, template: '<ng-template let-a [ngIf]="true">{{this.a}}</ng-template>', isInline: true });
1252+
MyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: MyComponent, isStandalone: true, selector: "my-component", ngImport: i0, template: '<ng-template let-a [ngIf]="true">{{a}}</ng-template>', isInline: true });
12531253
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyComponent, decorators: [{
12541254
type: Component,
12551255
args: [{
12561256
selector: 'my-component',
12571257
standalone: true,
1258-
template: '<ng-template let-a [ngIf]="true">{{this.a}}</ng-template>',
1258+
template: '<ng-template let-a [ngIf]="true">{{a}}</ng-template>',
12591259
}]
12601260
}] });
12611261

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_template/ng_template_implicit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ MyComponent_ng_template_0_Template(rf, ctx) {
22
if (rf & 1) {
33
i0.ɵɵtext(0);
44
} if (rf & 2) {
5-
const $a_r1$ = i0.ɵɵnextContext();
5+
const $a_r1$ = ctx.$implicit;
66
i0.ɵɵtextInterpolate($a_r1$);
77
}
88
}
@@ -15,4 +15,4 @@ function MyComponent_Template(rf, ctx) {
1515
} if (rf & 2) {
1616
i0.ɵɵproperty("ngIf", true);
1717
}
18-
}
18+
}

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_template/ng_template_implicit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Component} from '@angular/core';
33
@Component({
44
selector: 'my-component',
55
standalone: true,
6-
template: '<ng-template let-a [ngIf]="true">{{this.a}}</ng-template>',
6+
template: '<ng-template let-a [ngIf]="true">{{a}}</ng-template>',
77
})
88
export class MyComponent {
99
p1!: any;

packages/compiler/src/render3/view/t2_binder.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -922,21 +922,13 @@ class TemplateBinder extends RecursiveAstVisitor implements Visitor {
922922
private maybeMap(ast: PropertyRead | SafePropertyRead | PropertyWrite, name: string): void {
923923
// If the receiver of the expression isn't the `ImplicitReceiver`, this isn't the root of an
924924
// `AST` expression that maps to a `Variable` or `Reference`.
925-
if (!(ast.receiver instanceof ImplicitReceiver)) {
925+
if (!(ast.receiver instanceof ImplicitReceiver) || ast.receiver instanceof ThisReceiver) {
926926
return;
927927
}
928928

929929
// Check whether the name exists in the current scope. If so, map it. Otherwise, the name is
930930
// probably a property on the top-level component context.
931931
const target = this.scope.lookup(name);
932-
933-
// It's not allowed to read template entities via `this`, however it previously worked by
934-
// accident 628C (see #55115). Since `@let` declarations are new, we can fix it from the beginning,
935-
// whereas pre-existing template entities will be fixed in #55115.
936-
if (target instanceof LetDeclaration && ast.receiver instanceof ThisReceiver) {
937-
return;
938-
}
939-
940932
if (target !== null) {
941933
this.bindings.set(ast, target);
942934
}

packages/compiler/test/render3/view/binding_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,35 @@ describe('t2 binding', () => {
373373
expect((target as a.LetDeclaration)?.name).toBe('value');
374374
});
375375

376+
it('should not resolve a `this` access to a template reference', () => {
377+
const template = parseTemplate(
378+
`
379+
<input #value>
380+
{{this.value}}
381+
`,
382+
'',
383+
);
384+
const binder = new R3TargetBinder(new SelectorMatcher<DirectiveMeta[]>());
385+
const res = binder.bind({template: template.nodes});
386+
const interpolationWrapper = (template.nodes[1] as a.BoundText).value as e.ASTWithSource;
387+
const propertyRead = (interpolationWrapper.ast as e.Interpolation).expressions[0];
388+
const target = res.getExpressionTarget(propertyRead);
389+
390+
expect(target).toBe(null);
391+
});
392+
393+
it('should not resolve a `this` access to a template variable', () => {
394+
const template = parseTemplate(`<ng-template let-value>{{this.value}}</ng-template>`, '');
395+
const binder = new R3TargetBinder(new SelectorMatcher<DirectiveMeta[]>());
396+
const res = binder.bind({template: template.nodes});
397+
const templateNode = template.nodes[0] as a.Template;
398+
const interpolationWrapper = (templateNode.children[0] as a.BoundText).value as e.ASTWithSource;
399+
const propertyRead = (interpolationWrapper.ast as e.Interpolation).expressions[0];
400+
const target = res.getExpressionTarget(propertyRead);
401+
402+
expect(target).toBe(null);
403+
});
404+
376405
it('should not resolve a `this` access to a `@let` declaration', () => {
377406
const template = parseTemplate(
378407
`

packages/core/test/acceptance/embedded_views_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('embedded views', () => {
4444
});
4545

4646
it('should resolve template input variables through the implicit receiver', () => {
47-
@Component({template: `<ng-template let-a [ngIf]="true">{{this.a}}</ng-template>`})
47+
@Component({template: `<ng-template let-a [ngIf]="true">{{a}}</ng-template>`})
4848
class TestCmp {}
4949

5050
TestBed.configureTestingModule({declarations: [TestCmp]});

0 commit comments

Comments
 (0)
0