feat(compiler): support number separators in templates (#42672)

As of ES2021, JavaScript allows using underscores as separators inside numbers, in order to make them more readable (e.g. `1_000_000` vs `1000000`). TypeScript has had support for separators for a while so these changes expand the template parser to handle them as well.

PR Close #42672
This commit is contained in:
Kristiyan Kostadinov
2021-06-27 12:24:51 +02:00
committed by Jessica Janiuk
parent 234b5edcc7
commit 9f5cc7c808
7 changed files with 152 additions and 4 deletions
@@ -631,3 +631,53 @@ export declare class MyModule {
static ɵinj: i0.ɵɵInjectorDeclaration<MyModule>;
}
/****************************************************************************************************
* PARTIAL FILE: number_separator.js
****************************************************************************************************/
import { Component, NgModule } from '@angular/core';
import * as i0 from "@angular/core";
export class MyApp {
constructor() {
this.multiplier = 5;
}
}
MyApp.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyApp, deps: [], target: i0.ɵɵFactoryTarget.Component });
MyApp.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", type: MyApp, selector: "my-app", ngImport: i0, template: `
<div>Total: \${{ 1_000_000 * multiplier }}</div>
<span>Remaining: \${{ 123_456.78_9 / 2 }}</span>
`, isInline: true });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyApp, decorators: [{
type: Component,
args: [{
selector: 'my-app',
template: `
<div>Total: \${{ 1_000_000 * multiplier }}</div>
<span>Remaining: \${{ 123_456.78_9 / 2 }}</span>
`
}]
}] });
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: [MyApp] });
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: [MyApp] }]
}] });
/****************************************************************************************************
* PARTIAL FILE: number_separator.d.ts
****************************************************************************************************/
import * as i0 from "@angular/core";
export declare class MyApp {
multiplier: number;
static ɵfac: i0.ɵɵFactoryDeclaration<MyApp, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MyApp, "my-app", never, {}, {}, never, never>;
}
export declare class MyModule {
static ɵfac: i0.ɵɵFactoryDeclaration<MyModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<MyModule, [typeof MyApp], never, never>;
static ɵinj: i0.ɵɵInjectorDeclaration<MyModule>;
}
@@ -272,6 +272,20 @@
]
}
]
},
{
"description": "should support number literals with separators",
"inputFiles": [
"number_separator.ts"
],
"expectations": [
{
"failureMessage": "Invalid number literal",
"files": [
"number_separator.js"
]
}
]
}
]
}
@@ -0,0 +1,9 @@
template: function MyApp_Template(rf, ctx) {
// ...
if (rf & 2) {
$r3$.ɵɵadvance(1);
$r3$.ɵɵtextInterpolate1("Total: $", 1000000 * ctx.multiplier, "");
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("Remaining: $", 123456.789 / 2, "");
}
}
@@ -0,0 +1,16 @@
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>Total: \${{ 1_000_000 * multiplier }}</div>
<span>Remaining: \${{ 123_456.78_9 / 2 }}</span>
`
})
export class MyApp {
multiplier = 5;
}
@NgModule({declarations: [MyApp]})
export class MyModule {
}