feat(compiler): add support for shorthand property declarations in templates (#42421)

Adds support for shorthand property declarations inside Angular templates. E.g. doing `{foo, bar}` instead of `{foo: foo, bar: bar}`.

Fixes #10277.

PR Close #42421
This commit is contained in:
Kristiyan Kostadinov
2021-06-19 08:07:20 +02:00
committed by Dylan Hunn
parent 699a8b43cb
commit cc672f05bf
17 changed files with 390 additions and 10 deletions
@@ -870,3 +870,54 @@ export declare class MyModule {
static ɵinj: i0.ɵɵInjectorDeclaration<MyModule>;
}
/****************************************************************************************************
* PARTIAL FILE: shorthand_property_declaration.js
****************************************************************************************************/
import { Component, NgModule } from '@angular/core';
import * as i0 from "@angular/core";
export class MyComponent {
constructor() {
this.a = 1;
this.c = 3;
}
_handleClick(_value) { }
}
MyComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
MyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", type: MyComponent, selector: "ng-component", ngImport: i0, template: `
<div (click)="_handleClick({a, b: 2, c})"></div>
`, isInline: true });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyComponent, decorators: [{
type: Component,
args: [{
template: `
<div (click)="_handleClick({a, b: 2, c})"></div>
`
}]
}] });
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: [MyComponent] });
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: [MyComponent] }]
}] });
/****************************************************************************************************
* PARTIAL FILE: shorthand_property_declaration.d.ts
****************************************************************************************************/
import * as i0 from "@angular/core";
export declare class MyComponent {
a: number;
c: number;
_handleClick(_value: any): void;
static ɵfac: i0.ɵɵFactoryDeclaration<MyComponent, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MyComponent, "ng-component", never, {}, {}, never, never>;
}
export declare class MyModule {
static ɵfac: i0.ɵɵFactoryDeclaration<MyModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<MyModule, [typeof MyComponent], never, never>;
static ɵinj: i0.ɵɵInjectorDeclaration<MyModule>;
}
@@ -269,6 +269,23 @@
"failureMessage": "Incorrect template"
}
]
},
{
"description": "should handle shorthand property declarations in templates",
"inputFiles": [
"shorthand_property_declaration.ts"
],
"expectations": [
{
"files": [
{
"expected": "shorthand_property_declaration_template.js",
"generated": "shorthand_property_declaration.js"
}
],
"failureMessage": "Incorrect template"
}
]
}
]
}
@@ -0,0 +1,16 @@
import {Component, NgModule} from '@angular/core';
@Component({
template: `
<div (click)="_handleClick({a, b: 2, c})"></div>
`
})
export class MyComponent {
a = 1;
c = 3;
_handleClick(_value: any) {}
}
@NgModule({declarations: [MyComponent]})
export class MyModule {
}
@@ -0,0 +1,13 @@
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
i0.ɵɵlistener("click", function MyComponent_Template_div_click_0_listener() {
return ctx._handleClick({
a: ctx.a,
b: 2,
c: ctx.c
});
});
}
}