angular/packages/compiler-cli
Kristiyan Kostadinov 9cc0cbed0c refactor(compiler): generate if block instructions (#51380)
Adds the logic to generate the instructions for `if` blocks. There are two primary use cases we need to account for:

A conditional that doesn't use the `as` parameter of the `if` block. To support it we generate a nested ternary expression that evaluates to the index of the template whose condition is truthy. If the block doesn't have an `else` branch, we pass in a special `-1` value which means that no view will be rendered.

Example with an `else`:
```ts
// {#if expr}
//   ...
//   {:else if otherExpr} ...
//   {:else} ...
// {/if}

if (rf & 1) {
  ɵɵtemplate(0, App_Conditional_0_Template, 0, 0);
  ɵɵtemplate(1, App_Conditional_1_Template, 0, 0);
  ɵɵtemplate(2, App_Conditional_2_Template, 0, 0);
}
if (rf & 2) {
  ɵɵconditional(0, ctx.expr ? 0 : ctx.otherExpr ? 1 : 2);
}
```

Example without an `else`:
```ts
// {#if expr}
//   ...
//   {:else if otherExpr} ...
// {/if}

if (rf & 1) {
  ɵɵtemplate(0, App_Conditional_0_Template, 0, 0);
  ɵɵtemplate(1, App_Conditional_1_Template, 0, 0);
}
if (rf & 2) {
  ɵɵconditional(0, ctx.expr ? 0 : ctx.otherExpr ? 1 : -1);
}
```

If a conditional captures it's value in an alias (e.g. `{#if expr; as foo}`) we need to assign the value to a temporary variable before passing it along to `conditional`.

```ts
// {#if expr; as alias}...{/if}
if (rf & 1) {
  ɵɵtemplate(0, App_Conditional_0_Template, 1, 0);
}
if (rf & 2) {
  let App_contFlowTmp;
  ɵɵconditional(0, (App_contFlowTmp = ctx.expr) ? 0 : -1, App_contFlowTmp);
}
```

PR Close #51380
2023-08-18 10:01:02 -07:00
..
integrationtest refactor(compiler-cli): remove unused integration tests. (#49862) 2023-04-17 14:57:02 +00:00
linker refactor(compiler): compute the list of dependencies for defer blocks (#51162) 2023-08-01 11:50:05 -07:00
ngcc refactor(compiler-cli): add back ngcc as a no-op with a warning (#50045) 2023-04-28 18:18:40 +02:00
private refactor(compiler-cli): remove unused class decorator downlevel code (#49351) 2023-03-08 17:59:12 +00:00
src feat(core): support TypeScript 5.2 (#51334) 2023-08-18 07:55:16 -07:00
test refactor(compiler): generate if block instructions (#51380) 2023-08-18 10:01:02 -07:00
BUILD.bazel refactor(compiler-cli): drop tsickle code paths (#50602) 2023-08-17 10:23:49 -07:00
esbuild.config.js refactor: remove __ESM_IMPORT_META_URL__ workaround now that we can use ESM (#48521) 2022-12-19 19:50:41 +00:00
index.ts refactor(compiler-cli): Export the interface PluginCompilerHost for 1p use. (#48874) 2023-02-02 09:44:18 -08:00
package.json feat(core): support TypeScript 5.2 (#51334) 2023-08-18 07:55:16 -07:00
tsconfig-build.json refactor(compiler-cli): dismantle View Engine implementation of ngc (#44269) 2021-12-01 10:36:30 -08:00
tsconfig.json build: update tsconfigs to use ES2020 as target and module (#43431) 2021-10-01 18:28:42 +00:00