Skip to content

Commit 8f2c824

Browse files
dylhunnalxhub
authored andcommitted
refactor(compiler): Support $any in host bindings (angular#53478)
`$any` should be interpreted as a cast, not as a context read of a variable called `$any`. This already worked in template compilations, but the relevant phase was not enabled for host bindings. PR Close angular#53478
1 parent 6b20561 commit 8f2c824

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_bindings/host_bindings/GOLDEN_PARTIAL.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,35 @@ export declare class MyComponent {
663663
static ɵcmp: i0.ɵɵComponentDeclaration<MyComponent, "my-comp", never, {}, {}, never, never, false, never>;
664664
}
665665

666+
/****************************************************************************************************
667+
* PARTIAL FILE: host_dollar_any.js
668+
****************************************************************************************************/
669+
import { Component } from '@angular/core';
670+
import * as i0 from "@angular/core";
671+
export class HostBindingDir {
672+
}
673+
HostBindingDir.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingDir, deps: [], target: i0.ɵɵFactoryTarget.Component });
674+
HostBindingDir.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: HostBindingDir, selector: "[hostBindingDir]", host: { properties: { "style.color": "$any(\"red\")" } }, ngImport: i0, template: ``, isInline: true });
675+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingDir, decorators: [{
676+
type: Component,
677+
args: [{
678+
selector: '[hostBindingDir]',
679+
host: {
680+
'[style.color]': '$any("red")',
681+
},
682+
template: ``
683+
}]
684+
}] });
685+
686+
/****************************************************************************************************
687+
* PARTIAL FILE: host_dollar_any.d.ts
688+
****************************************************************************************************/
689+
import * as i0 from "@angular/core";
690+
export declare class HostBindingDir {
691+
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingDir, never>;
692+
static ɵcmp: i0.ɵɵComponentDeclaration<HostBindingDir, "[hostBindingDir]", never, {}, {}, never, never, false, never>;
693+
}
694+
666695
/****************************************************************************************************
667696
* PARTIAL FILE: host_bindings_primitive_names.js
668697
****************************************************************************************************/

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_bindings/host_bindings/TEST_CASES.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,20 @@
253253
}
254254
]
255255
},
256+
{
257+
"description": "should allow $any in host binding expressions",
258+
"inputFiles": [
259+
"host_dollar_any.ts"
260+
],
261+
"expectations": [
262+
{
263+
"failureMessage": "Invalid host binding code",
264+
"files": [
265+
"host_dollar_any.js"
266+
]
267+
}
268+
]
269+
},
256270
{
257271
"description": "should handle host bindings with the same name as a primitive value",
258272
"inputFiles": [
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function HostBindingDir_HostBindings(rf, ctx) {
2+
if (rf & 2) {
3+
i0.ɵɵstyleProp("color", "red");
4+
}
5+
}
6+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Component} from '@angular/core';
2+
3+
@Component({
4+
selector: '[hostBindingDir]',
5+
host: {
6+
'[style.color]': '$any("red")',
7+
},
8+
template: ``
9+
})
10+
export class HostBindingDir {
11+
}

packages/compiler/src/template/pipeline/src/emit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const phases: Phase[] = [
113113
{kind: Kind.Tmpl, fn: generateProjectionDefs},
114114
{kind: Kind.Tmpl, fn: generateVariables},
115115
{kind: Kind.Tmpl, fn: saveAndRestoreView},
116-
{kind: Kind.Tmpl, fn: deleteAnyCasts},
116+
{kind: Kind.Both, fn: deleteAnyCasts},
117117
{kind: Kind.Both, fn: resolveDollarEvent},
118118
{kind: Kind.Tmpl, fn: generateRepeaterDerivedVars},
119119
{kind: Kind.Tmpl, fn: generateTrackVariables},

0 commit comments

Comments
 (0)