From a0be043f8164b10e1d6516c88674f8d4cd0a67f6 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Fri, 20 May 2022 14:29:56 +0000 Subject: [PATCH] reactor(compiler-cli): account for babel types change (#45967) This commit accounts for the Babel types changes. Some properties can now also be `undefined` so existing checks/assertions had to be adjusted to also capture `undefined` (along with `null`). Additionally, in preparation for a new ECMA proposal, Babel types seem to have been updated to include private names in object property keys. This is not necessarily the case for object expressions, but could be for object patterns (in the future -- when implemented). More details: https://github.com/babel/babel/pull/14304 and https://github.com/tc39/proposal-destructuring-private. PR Close #45967 --- .../linker/babel/src/ast/babel_ast_host.ts | 15 ++++++++++----- .../inline_templates/ng_for_simple_partial.js | 6 +++--- .../inline_templates/ng_for_templated_partial.js | 2 +- .../inline_templates/ng_if_simple_partial.js | 4 ++-- .../inline_templates/ng_if_templated_partial.js | 4 ++-- .../output_binding_complex_partial.js | 2 +- .../output_binding_simple_partial.js | 2 +- .../two_way_binding_longhand_partial.js | 2 +- .../two_way_binding_simple_partial.js | 2 +- 9 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts b/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts index 49ccccbb9363f..d9523601581d4 100644 --- a/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts +++ b/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts @@ -71,7 +71,8 @@ export class BabelAstHost implements AstHost { for (const property of obj.properties) { assert(property, t.isObjectProperty, 'a property assignment'); assert(property.value, t.isExpression, 'an expression'); - assert(property.key, isPropertyName, 'a property name'); + assert(property.key, isObjectExpressionPropertyName, 'a property name'); + const key = t.isIdentifier(property.key) ? property.key.name : property.key.value; result.set(`${key}`, property.value); } @@ -124,7 +125,7 @@ export class BabelAstHost implements AstHost { } getRange(node: t.Expression): Range { - if (node.loc == null || node.start === null || node.end === null) { + if (node.loc == null || node.start == null || node.end == null) { throw new FatalLinkerError( node, 'Unable to read range for node - it is missing location information.'); } @@ -156,10 +157,14 @@ function isNotSpreadElement(e: t.Expression|t.SpreadElement): e is t.Expression /** - * Return true if the expression can be considered a text based property name. + * Return true if the node can be considered a text based property name for an + * object expression. + * + * Notably in the Babel AST, object patterns (for destructuring) could be of type + * `t.PrivateName` so we need a distinction between object expressions and patterns. */ -function isPropertyName(e: t.Expression): e is t.Identifier|t.StringLiteral|t.NumericLiteral { - return t.isIdentifier(e) || t.isStringLiteral(e) || t.isNumericLiteral(e); +function isObjectExpressionPropertyName(n: t.Node): n is t.Identifier|t.StringLiteral|t.NumericLiteral { + return t.isIdentifier(n) || t.isStringLiteral(n) || t.isNumericLiteral(n); } /** diff --git a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_simple_partial.js b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_simple_partial.js index b359a1d8fa584..558f295ec552d 100644 --- a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_simple_partial.js +++ b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_simple_partial.js @@ -1,11 +1,11 @@ -.ɵɵelementStart(0, "div");\n // SOURCE: "/ng_for_simple.ts" "
'" … -.ɵɵadvance(1);\n // SOURCE: "/ng_for_simple.ts" "{{ item }}" +.ɵɵadvance(1);\n // SOURCE: "/ng_for_simple.ts" "{{ item }}" … .ɵɵtextInterpolate(item_r1);\n }\n}\n\n // SOURCE: "/ng_for_simple.ts" "{{ item }}" … diff --git a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_templated_partial.js b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_templated_partial.js index 038c0f1b2bb35..0628cf5bad4ea 100644 --- a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_templated_partial.js +++ b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_for_templated_partial.js @@ -1,4 +1,4 @@ -.ɵɵtext(0);\n }\n\n if (rf & 2) {\n const item_r1 = ctx.$implicit;\n // SOURCE: "/ng_for_templated.ts" "{{ item }}`" +.ɵɵtext(0);\n }\n\n if (rf & 2) {\n const item_r1 = ctx.$implicit;\n // SOURCE: "/ng_for_templated.ts" "{{ item }}`" … // TODO - map the bindings better // TODO: Work out how to fix the broken segment for the last item in a template diff --git a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_simple_partial.js b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_simple_partial.js index cac7eddaf0dab..288758812f35f 100644 --- a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_simple_partial.js +++ b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_simple_partial.js @@ -1,6 +1,6 @@ -.ɵɵelementStart(0, "div");\n // SOURCE: "/ng_if_simple.ts" "
'" diff --git a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_templated_partial.js b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_templated_partial.js index cb98fa4d43c12..4920bdc19b5b0 100644 --- a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_templated_partial.js +++ b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/ng_if_templated_partial.js @@ -1,8 +1,8 @@ -.ɵɵelementStart(0, "div");\n // SOURCE: "/ng_if_templated.ts" "
" +.ɵɵelementStart(0, "div");\n // SOURCE: "/ng_if_templated.ts" "
" … // TODO - map the bindings better // TODO: Work out how to fix the broken segment for the last item in a template -.ɵɵelementEnd();\n // SOURCE: "/ng_if_templated.ts" "
\n" +.ɵɵelementEnd();\n // SOURCE: "/ng_if_templated.ts" "
\n" … // TODO: the `ctx_r...` appears to be dependent upon previous tests!!! // TODO: expectMapping(mappings, { source: '{{ name }}', generated: 'i0.ɵɵtextInterpolate(ctx_r0.name)', sourceUrl: '../ng_if_templated.ts'}); diff --git a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/output_binding_complex_partial.js b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/output_binding_complex_partial.js index 770917fa4a871..c498be1e2fdf3 100644 --- a/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/output_binding_complex_partial.js +++ b/packages/compiler-cli/test/compliance/test_cases/source_mapping/inline_templates/output_binding_complex_partial.js @@ -1,6 +1,6 @@ .ɵɵelementStart(0, "button", 0) // SOURCE: "/output_binding_complex.ts" "