Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor(compiler): remove empty string suffix from interpolati… #60066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function MyComponent_div_3_span_2_Template(rf, ctx) {
const $foo$ = $r3$.ɵɵreference(1);
const $baz$ = $r3$.ɵɵreference(5);
$r3$.ɵɵadvance();
$r3$.ɵɵtextInterpolate3("", $foo$, "-", $bar$, "-", $baz$, "");
$r3$.ɵɵtextInterpolate3("", $foo$, "-", $bar$, "-", $baz$);
}
}
function MyComponent_div_3_Template(rf, ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ MyApp.ɵcmp = /*@__PURE__*/ $r3$.ɵɵdefineComponent({
"1:", i0.ɵɵpipeBind2(2, 7, ctx.name, 1),
"2:", i0.ɵɵpipeBind3(3, 10, ctx.name, 1, 2),
"3:", i0.ɵɵpipeBind4(4, 14, ctx.name, 1, 2, 3),
"4:", i0.ɵɵpipeBindV(5, 19, $r3$.ɵɵpureFunction1(25, $c0$, ctx.name)),
""
"4:", i0.ɵɵpipeBindV(5, 19, $r3$.ɵɵpureFunction1(25, $c0$, ctx.name))
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MyApp.ɵcmp = /*@__PURE__*/ $r3$.ɵɵdefineComponent({
if (rf & 2) {
$r3$.ɵɵtextInterpolate($r3$.ɵɵpipeBind2(2, 6, $r3$.ɵɵpipeBind2(1, 3, ctx.name, ctx.size), ctx.size));
$r3$.ɵɵadvance(4);
$r3$.ɵɵtextInterpolate2("", $r3$.ɵɵpipeBindV(5, 9, $r3$.ɵɵpureFunction1(18, $c0$, ctx.name)), " ", ctx.name ? 1 : $r3$.ɵɵpipeBind1(6, 16, 2), "");
$r3$.ɵɵtextInterpolate2("", $r3$.ɵɵpipeBindV(5, 9, $r3$.ɵɵpureFunction1(18, $c0$, ctx.name)), " ", ctx.name ? 1 : $r3$.ɵɵpipeBind1(6, 16, 2));
}
},
dependencies: [MyPipe, MyPurePipe],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ template: function MyApp_Template(rf, ctx) {
// ...
if (rf & 2) {
$r3$.ɵɵadvance();
$r3$.ɵɵtextInterpolate1("Total: $", 1000000 * ctx.multiplier, "");
$r3$.ɵɵtextInterpolate1("Total: $", 1000000 * ctx.multiplier);
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("Remaining: $", 123456.789 / 2, "");
$r3$.ɵɵtextInterpolate1("Remaining: $", 123456.789 / 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function MyComponent_li_2_Template(rf, ctx) {
const $myComp$ = $r3$.ɵɵnextContext();
const $foo$ = $r3$.ɵɵreference(1);
$r3$.ɵɵadvance();
$r3$.ɵɵtextInterpolate2("", $myComp$.salutation, " ", $foo$, "");
$r3$.ɵɵtextInterpolate2("", $myComp$.salutation, " ", $foo$);
}
}
// ...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if (rf & 2) {
$r3$.ɵɵadvance();
$r3$.ɵɵtextInterpolate1("No interpolations: ", ctx.tag `hello world `, "");
$r3$.ɵɵtextInterpolate1("No interpolations: ", ctx.tag `hello world `);
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("With interpolations: ", ctx.tag `hello ${ctx.name}, it is currently ${ctx.timeOfDay}!`, "");
$r3$.ɵɵtextInterpolate1("With interpolations: ", ctx.tag `hello ${ctx.name}, it is currently ${ctx.timeOfDay}!`);
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("With pipe: ", $r3$.ɵɵpipeBind1(6, 3, ctx.tag `hello ${ctx.name}`), "");
$r3$.ɵɵtextInterpolate1("With pipe: ", $r3$.ɵɵpipeBind1(6, 3, ctx.tag `hello ${ctx.name}`));
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if (rf & 2) {
$r3$.ɵɵadvance();
$r3$.ɵɵtextInterpolate1("No interpolations: ", `hello world `, "");
$r3$.ɵɵtextInterpolate1("No interpolations: ", `hello world `);
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("With interpolations: ", `hello ${ctx.name}, it is currently ${ctx.timeOfDay}!`, "");
$r3$.ɵɵtextInterpolate1("With interpolations: ", `hello ${ctx.name}, it is currently ${ctx.timeOfDay}!`);
$r3$.ɵɵadvance(2);
$r3$.ɵɵtextInterpolate1("With pipe: ", $r3$.ɵɵpipeBind1(6, 3, `hello ${ctx.name}`), "");
$r3$.ɵɵtextInterpolate1("With pipe: ", $r3$.ɵɵpipeBind1(6, 3, `hello ${ctx.name}`));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ template: function MyApp_Template(rf, ctx) {
i0.ɵɵadvance();
i0.ɵɵtextInterpolate1("Hello, ", ctx.firstName ?? "Frodo", "!");
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Your last name is ", ctx.lastName ?? ctx.lastNameFallback ?? "unknown", "");
i0.ɵɵtextInterpolate1("Your last name is ", ctx.lastName ?? ctx.lastNameFallback ?? "unknown");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
} if (rf & 2) {
i0.ɵɵadvance();
i0.ɵɵtextInterpolate1("Safe Property: ", ctx.p == null ? null : ctx.p.a == null ? null : ctx.p.a.b == null ? null : ctx.p.a.b.c == null ? null : ctx.p.a.b.c.d, "");
i0.ɵɵtextInterpolate1("Safe Property: ", ctx.p == null ? null : ctx.p.a == null ? null : ctx.p.a.b == null ? null : ctx.p.a.b.c == null ? null : ctx.p.a.b.c.d);
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Safe Keyed: ", ctx.p == null ? null : ctx.p["a"] == null ? null : ctx.p["a"]["b"] == null ? null : ctx.p["a"]["b"]["c"] == null ? null : ctx.p["a"]["b"]["c"]["d"], "");
i0.ɵɵtextInterpolate1("Safe Keyed: ", ctx.p == null ? null : ctx.p["a"] == null ? null : ctx.p["a"]["b"] == null ? null : ctx.p["a"]["b"]["c"] == null ? null : ctx.p["a"]["b"]["c"]["d"]);
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Mixed Property: ", ctx.p == null ? null : ctx.p.a == null ? null : ctx.p.a.b.c.d == null ? null : ctx.p.a.b.c.d.e == null ? null : ctx.p.a.b.c.d.e.f == null ? null : ctx.p.a.b.c.d.e.f.g.h, "");
i0.ɵɵtextInterpolate1("Mixed Property: ", ctx.p == null ? null : ctx.p.a == null ? null : ctx.p.a.b.c.d == null ? null : ctx.p.a.b.c.d.e == null ? null : ctx.p.a.b.c.d.e.f == null ? null : ctx.p.a.b.c.d.e.f.g.h);
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Mixed Property and Keyed: ", ctx.p.a["b"].c.d == null ? null : ctx.p.a["b"].c.d["e"] == null ? null : ctx.p.a["b"].c.d["e"]["f"] == null ? null : ctx.p.a["b"].c.d["e"]["f"].g["h"]["i"] == null ? null : ctx.p.a["b"].c.d["e"]["f"].g["h"]["i"].j.k, "");
i0.ɵɵtextInterpolate1("Mixed Property and Keyed: ", ctx.p.a["b"].c.d == null ? null : ctx.p.a["b"].c.d["e"] == null ? null : ctx.p.a["b"].c.d["e"]["f"] == null ? null : ctx.p.a["b"].c.d["e"]["f"].g["h"]["i"] == null ? null : ctx.p.a["b"].c.d["e"]["f"].g["h"]["i"].j.k);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
let $tmp_3_2$;
let $tmp_3_3$;
i0.ɵɵadvance();
i0.ɵɵtextInterpolate1("Safe Property with Calls: ", ($tmp_0_0$ = ctx.p()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.a()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.b()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.c()) == null ? null : $tmp_0_0$.d(), "");
i0.ɵɵtextInterpolate1("Safe Property with Calls: ", ($tmp_0_0$ = ctx.p()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.a()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.b()) == null ? null : ($tmp_0_0$ = $tmp_0_0$.c()) == null ? null : $tmp_0_0$.d());
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Safe and Unsafe Property with Calls: ", ctx.p == null ? null : ($tmp_1_0$ = ctx.p.a()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.b().c().d()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.e()) == null ? null : $tmp_1_0$.f == null ? null : $tmp_1_0$.f.g.h == null ? null : ($tmp_1_0$ = $tmp_1_0$.f.g.h.i()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.j()) == null ? null : $tmp_1_0$.k().l, "");
i0.ɵɵtextInterpolate1("Safe and Unsafe Property with Calls: ", ctx.p == null ? null : ($tmp_1_0$ = ctx.p.a()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.b().c().d()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.e()) == null ? null : $tmp_1_0$.f == null ? null : $tmp_1_0$.f.g.h == null ? null : ($tmp_1_0$ = $tmp_1_0$.f.g.h.i()) == null ? null : ($tmp_1_0$ = $tmp_1_0$.j()) == null ? null : $tmp_1_0$.k().l);
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Nested Safe with Calls: ", ($tmp_2_0$ = ctx.f1()) == null ? null : $tmp_2_0$[($tmp_2_1$ = ctx.f2()) == null ? null : $tmp_2_1$.a] == null ? null : $tmp_2_0$[($tmp_2_1$ = $tmp_2_1$) == null ? null : $tmp_2_1$.a].b, "");
i0.ɵɵtextInterpolate1("Nested Safe with Calls: ", ($tmp_2_0$ = ctx.f1()) == null ? null : $tmp_2_0$[($tmp_2_1$ = ctx.f2()) == null ? null : $tmp_2_1$.a] == null ? null : $tmp_2_0$[($tmp_2_1$ = $tmp_2_1$) == null ? null : $tmp_2_1$.a].b);
i0.ɵɵadvance(2);
i0.ɵɵtextInterpolate1("Deep Nested Safe with Calls: ", ($tmp_3_0$ = ctx.f1()) == null ? null : $tmp_3_0$[($tmp_3_1$ = ctx.f2()) == null ? null : ($tmp_3_2$ = $tmp_3_1$.f3()) == null ? null : $tmp_3_2$[($tmp_3_3$ = ctx.f4()) == null ? null : $tmp_3_3$.f5()]] == null ? null : $tmp_3_0$[($tmp_3_1$ = $tmp_3_1$) == null ? null : ($tmp_3_2$ = $tmp_3_2$) == null ? null : $tmp_3_2$[($tmp_3_3$ = $tmp_3_3$) == null ? null : $tmp_3_3$.f5()]].f6(), "");
i0.ɵɵtextInterpolate1("Deep Nested Safe with Calls: ", ($tmp_3_0$ = ctx.f1()) == null ? null : $tmp_3_0$[($tmp_3_1$ = ctx.f2()) == null ? null : ($tmp_3_2$ = $tmp_3_1$.f3()) == null ? null : $tmp_3_2$[($tmp_3_3$ = ctx.f4()) == null ? null : $tmp_3_3$.f5()]] == null ? null : $tmp_3_0$[($tmp_3_1$ = $tmp_3_1$) == null ? null : ($tmp_3_2$ = $tmp_3_2$) == null ? null : $tmp_3_2$[($tmp_3_3$ = $tmp_3_3$) == null ? null : $tmp_3_3$.f5()]].f6());
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
template: function MyComponent_Template(rf, ctx) {
if (rf & 2) {
$r3$.ɵɵattributeInterpolate1("tabindex", "prefix-", 0 + 3, "");
$r3$.ɵɵattributeInterpolate2("aria-label", "hello-", 1 + 3, "-", 2 + 3, "");
$r3$.ɵɵattributeInterpolate1("tabindex", "prefix-", 0 + 3);
$r3$.ɵɵattributeInterpolate2("aria-label", "hello-", 1 + 3, "-", 2 + 3);
$r3$.ɵɵattribute("title", 1)("id", 2);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
template: function MyComponent_Template(rf, ctx) {
if (rf & 2) {
$r3$.ɵɵattributeInterpolate1("aria-label", "prefix-", 1 + 3, "");
$r3$.ɵɵattributeInterpolate1("aria-label", "prefix-", 1 + 3);
$r3$.ɵɵproperty("id", 2);
$r3$.ɵɵattribute("title", 1)("tabindex", 3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function MyCmp_Template(rf, ctx) {
} if (rf & 2) {
i0.ɵɵstyleProp("style1", ctx.foo);
i0.ɵɵclassProp("class1", ctx.foo);
i0.ɵɵattributeInterpolate1("attrInterp1", "interp ", ctx.foo, "");
i0.ɵɵpropertyInterpolate1("propInterp1", "interp ", ctx.foo, "");
i0.ɵɵattributeInterpolate1("attrInterp1", "interp ", ctx.foo);
i0.ɵɵpropertyInterpolate1("propInterp1", "interp ", ctx.foo);
i0.ɵɵproperty("prop1", ctx.foo);
i0.ɵɵattribute("attr1", ctx.foo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ template: function MyComponent_Template(rf, ctx) {
if (rf & 2) {
$r3$.ɵɵpropertyInterpolate("tabindex", 0 + 3);
$r3$.ɵɵpropertyInterpolate2("aria-label", "hello-", 1 + 3, "-", 2 + 3, "");
$r3$.ɵɵpropertyInterpolate2("aria-label", "hello-", 1 + 3, "-", 2 + 3);
$r3$.ɵɵproperty("title", 1)("id", 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ template:function MyComponent_Template(rf, $ctx$){
$i0$.ɵɵelement(0, "a", 0);
}
if (rf & 2) {
$i0$.ɵɵpropertyInterpolate1("title", "Hello ", $ctx$.name, "");
$i0$.ɵɵpropertyInterpolate1("title", "Hello ", $ctx$.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ template:function MyComponent_Template(rf, $ctx$){
}
if (rf & 2) {
$r3$.ɵɵadvance();
$i0$.ɵɵtextInterpolate1("Hello ", $ctx$.name, "");
$i0$.ɵɵtextInterpolate1("Hello ", $ctx$.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ template: function MyComponent_Template(rf, ctx) {
}
if (rf & 2) {
i0.ɵɵadvance(2);
i0.ɵɵpropertyInterpolate2("title", "", ctx.foo, "-", ctx.foo, "");
i0.ɵɵpropertyInterpolate2("title", "", ctx.foo, "-", ctx.foo);
i0.ɵɵadvance(2);
i0.ɵɵi18nExp(ctx.foo)(ctx.foo)(ctx.foo)(ctx.foo)(ctx.foo);
i0.ɵɵi18nApply(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ template: function MyComponent_Template(rf, $ctx$) {
$r3$.ɵɵstyleProp("bar", $r3$.ɵɵpipeBind2(2, 14, $ctx$.barExp, 3000))("baz", $r3$.ɵɵpipeBind2(3, 17, $ctx$.bazExp, 4000));
$r3$.ɵɵclassProp("foo", $r3$.ɵɵpipeBind2(4, 20, $ctx$.fooExp, 2000));
$r3$.ɵɵadvance(5);
$r3$.ɵɵtextInterpolate1(" ", $ctx$.item, "");
$r3$.ɵɵtextInterpolate1(" ", $ctx$.item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$r3$.ɵɵelement(0, "div");
}
if (rf & 2) {
$r3$.ɵɵclassMapInterpolate1("foo foo-", $ctx$.fooId, "");
$r3$.ɵɵclassMapInterpolate1("foo foo-", $ctx$.fooId);
}
}
// ...
Expand All @@ -17,7 +17,7 @@
$r3$.ɵɵelement(0, "div");
}
if (rf & 2) {
$r3$.ɵɵclassMapInterpolate2("foo foo-", $ctx$.fooId, "-", $ctx$.fooUsername, "");
$r3$.ɵɵclassMapInterpolate2("foo foo-", $ctx$.fooId, "-", $ctx$.fooUsername);
}
}
// ...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
i0.ɵɵtextInterpolate1(" ", (ctx.a == null ? null : ctx.a.b) ? 1 : 2, "")
i0.ɵɵtextInterpolate1(" ", (ctx.a == null ? null : ctx.a.b) ? 1 : 2)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ i0.ɵɵelementStart(0, "h3") // SOURCE: "/interpolation_basic.ts" "<h3>"
i0.ɵɵelementEnd() // SOURCE: "/interpolation_basic.ts" "</h3>"
i0.ɵɵtextInterpolate1("Hello ", ctx.name, "") // SOURCE: "/interpolation_basic.ts" "Hello {{ name }}"
i0.ɵɵtextInterpolate1("Hello ", ctx.name) // SOURCE: "/interpolation_basic.ts" "Hello {{ name }}"
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// TODO: Work out how to fix the broken segment for the last item in a template
.ɵɵele // SOURCE: "/interpolation_basic.ts" "</h3>'"
.ɵɵtextInterpolate1("Hello ", ctx.name, "") // SOURCE: "/interpolation_basic.ts" "Hello {{ name }}"
.ɵɵtextInterpolate1("Hello ", ctx.name) // SOURCE: "/interpolation_basic.ts" "Hello {{ name }}"
2 changes: 1 addition & 1 deletion packages/compiler-cli/test/ngtsc/template_mapping_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ runInEachFileSystem((os) => {
});
expectMapping(mappings, {
source: 'Hello {{ name }}',
generated: 'i0.ɵɵtextInterpolate1("Hello ", ctx.name, "")',
generated: 'i0.ɵɵtextInterpolate1("Hello ", ctx.name)',
sourceUrl: '../test.ts',
});
expectMapping(mappings, {
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler/src/template/pipeline/src/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ export function textInterpolate(
sourceSpan: ParseSourceSpan,
): ir.UpdateOp {
const interpolationArgs = collateInterpolationArgs(strings, expressions);

return callVariadicInstruction(TEXT_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
}

Expand All @@ -598,6 +599,7 @@ export function propertyInterpolate(
sourceSpan: ParseSourceSpan,
): ir.UpdateOp {
const interpolationArgs = collateInterpolationArgs(strings, expressions);

const extraArgs = [];
if (sanitizer !== null) {
extraArgs.push(sanitizer);
Expand All @@ -620,6 +622,7 @@ export function attributeInterpolate(
sourceSpan: ParseSourceSpan,
): ir.UpdateOp {
const interpolationArgs = collateInterpolationArgs(strings, expressions);

const extraArgs = [];
if (sanitizer !== null) {
extraArgs.push(sanitizer);
Expand All @@ -642,6 +645,7 @@ export function stylePropInterpolate(
sourceSpan: ParseSourceSpan,
): ir.UpdateOp {
const interpolationArgs = collateInterpolationArgs(strings, expressions);

const extraArgs: o.Expression[] = [];
if (unit !== null) {
extraArgs.push(o.literal(unit));
Expand Down Expand Up @@ -748,6 +752,7 @@ function collateInterpolationArgs(strings: string[], expressions: o.Expression[]
for (idx = 0; idx < expressions.length; idx++) {
interpolationArgs.push(o.literal(strings[idx]), expressions[idx]);
}

// idx points at the last string.
interpolationArgs.push(o.literal(strings[idx]));
}
Expand Down Expand Up @@ -953,7 +958,21 @@ function callVariadicInstructionExpr(
extraArgs: o.Expression[],
sourceSpan: ParseSourceSpan | null,
): o.Expression {
// mapping need to be done before potentially dropping the last interpolation argument
const n = config.mapping(interpolationArgs.length);

// In the case the interpolation instruction ends with a empty string we drop it
// And the runtime will take care of it.
const lastInterpolationArg = interpolationArgs.at(-1);
if (
extraArgs.length === 0 &&
interpolationArgs.length > 1 &&
lastInterpolationArg instanceof o.LiteralExpr &&
lastInterpolationArg.value === ''
) {
interpolationArgs.pop();
}

if (n < config.constant.length) {
// Constant calling pattern.
return o
Expand Down
Loading