-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): incorrectly interpreting some HostBinding names (#40233)
Currently when analyzing the metadata of a directive, we bundle together the bindings from `host` and the `HostBinding` and `HostListener` together. This can become a problem later on in the compilation pipeline, because we try to evaluate the value of the binding, causing something like `@HostBinding('class.foo') public true = 1;` to be treated the same as `host: {'[class.foo]': 'true'}`. While looking into the issue, I noticed another one that is closely related: we weren't treating quoted property names correctly. E.g. `@HostBinding('class.foo') public "foo-bar" = 1;` was being interpreted as `classProp('foo', ctx.foo - ctx.bar)` due to the same issue where property names were being evaluated. These changes resolve both of the issues by treating all `HostBinding` instance as if they're reading the property from `this`. E.g. the `@HostBinding('class.foo') public true = 1;` from above is now being treated as `host: {'[class.foo]': 'this.true'}` which further down the pipeline becomes `classProp('foo', ctx.true)`. This doesn't have any payload size implications for existing code, because we've always been prefixing implicit property reads with `ctx.`. If the property doesn't have an identifier that can be read using dotted access, we convert it to a quoted one (e.g. `classProp('foo', ctx['is-foo']))`. Fixes #40220. Fixes #40230. Fixes #18698. PR Close #40233
- Loading branch information
Showing
14 changed files
with
272 additions
and
17 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
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
11 changes: 11 additions & 0 deletions
11
...iance/test_cases/r3_view_compiler_bindings/host_bindings/host_bindings_primitive_names.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,11 @@ | ||
|
||
HostBindingDir.ɵdir = $r3$.ɵɵdefineDirective({ | ||
type: HostBindingDir, | ||
selectors: [["", "hostBindingDir", ""]], | ||
hostVars: 10, | ||
hostBindings: function HostBindingDir_HostBindings(rf, ctx) { | ||
if (rf & 2) { | ||
$r3$.ɵɵclassProp("a", true)("b", false)("c", ctx.true)("d", ctx.false)("e", ctx.other); | ||
} | ||
} | ||
}); |
18 changes: 18 additions & 0 deletions
18
...iance/test_cases/r3_view_compiler_bindings/host_bindings/host_bindings_primitive_names.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 {Directive, HostBinding, NgModule} from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[hostBindingDir]', | ||
host: { | ||
'[class.a]': 'true', | ||
'[class.b]': 'false', | ||
} | ||
}) | ||
export class HostBindingDir { | ||
@HostBinding('class.c') true: any; | ||
@HostBinding('class.d') false: any; | ||
@HostBinding('class.e') other: any; | ||
} | ||
|
||
@NgModule({declarations: [HostBindingDir]}) | ||
export class MyModule { | ||
} |
11 changes: 11 additions & 0 deletions
11
...mpliance/test_cases/r3_view_compiler_bindings/host_bindings/host_bindings_quoted_names.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,11 @@ | ||
|
||
HostBindingDir.ɵdir = $r3$.ɵɵdefineDirective({ | ||
type: HostBindingDir, | ||
selectors: [["", "hostBindingDir", ""]], | ||
hostVars: 6, | ||
hostBindings: function HostBindingDir_HostBindings(rf, ctx) { | ||
if (rf & 2) { | ||
$r3$.ɵɵclassProp("a", ctx["is-a"])("b", ctx["is-\"b\""])("c", ctx["\"is-c\""]); | ||
} | ||
} | ||
}); |
12 changes: 12 additions & 0 deletions
12
...mpliance/test_cases/r3_view_compiler_bindings/host_bindings/host_bindings_quoted_names.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,12 @@ | ||
import {Directive, HostBinding, NgModule} from '@angular/core'; | ||
|
||
@Directive({selector: '[hostBindingDir]'}) | ||
export class HostBindingDir { | ||
@HostBinding('class.a') 'is-a': any; | ||
@HostBinding('class.b') 'is-"b"': any; | ||
@HostBinding('class.c') '"is-c"': any; | ||
} | ||
|
||
@NgModule({declarations: [HostBindingDir]}) | ||
export class MyModule { | ||
} |
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
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
Oops, something went wrong.