Commit graph

2723 commits

Author SHA1 Message Date
Kristiyan Kostadinov
fa72384ec5 refactor(compiler): introduce AST for outputting arrow functions (#51436)
Extends the compiler to add support for generating arrow functions in the output AST. This will be required for the `for` control flow block and we can potentially leverage it in other places to reduces the amount of generated code.

PR Close #51436
2023-08-23 14:45:33 -07:00
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
Kristiyan Kostadinov
0c4c773fca refactor(compiler): generate switch block instructions (#51380)
Adds the logic to generate the instructions for `switch` instructions. For the following block:

```html
{#switch value()}
  {:case 0} case 0
  {:case 1} case 1
  {:case 2} case 2
  {:default} default
{/switch}
```

The compiler will produce the following output:

```ts
function App_Template(rf, ctx) {
  if (rf & 1) {
    ɵɵtemplate(0, App_Case_0_Template, 1, 0);
    ɵɵtemplate(1, App_Case_1_Template, 1, 0);
    ɵɵtemplate(2, App_Case_2_Template, 1, 0);
    ɵɵtemplate(3, App_Case_3_Template, 1, 0);
  }
  if (rf & 2) {
    let App_contFlowTmp;
    ɵɵconditional(0, (App_contFlowTmp = ctx.value()) === 0 ? 0 : App_contFlowTmp === 1 ? 1 : App_contFlowTmp === 2 ? 2 : 3);
  }
}
```

PR Close #51380
2023-08-18 10:01:02 -07:00
JoostK
5bd9fbd2c3 fix(compiler-cli): enforce a minimum version to be used when a library uses input transform (#51413)
Angular 16.1 introduced the input transform feature, requiring the partial compilation output to be extended
with a reference to the input transform function. This has resulted in a subtle breaking change, where older
versions of the Angular linker can no longer consume libraries that have started to use this feature.

We do try to support using a 16.1 library from an Angular 16.0 application, but if a library actually
adopts a new feature then this is no longer possible. In such cases, it is desirable to report a message
telling the user that their version of the Angular compiler is too old, as determined by the `"minVersion"`
property that is present in each partial declaration. This version would still indicate that the declaration
required at least Angular 14.0 to be compiled, but this is not accurate once input transforms are being
used. Consequently, this error would not be reported, causing a less informative error once the input transform
was being observed.

Fixes #51411

PR Close #51413
2023-08-18 07:58:53 -07:00
Kristiyan Kostadinov
9cc52b9b85 feat(core): support TypeScript 5.2 (#51334)
Updates the project to support TypeScript 5.2.

PR Close #51334
2023-08-18 07:55:16 -07:00
Payam Valadkhan
0b901a814b refactor(compiler-cli): better error messages when external strings used for template and styles in local compilation mode (#51338)
In local compilation mode it is not possible to use an imported string for component's template or styles as it cannot be resolved statically in compile time. There are some such use cases in g3 and potentially devs might incorporate such pattern. At the moment such pattern will cause the local compilation fail with generic error messages (e.g., so and so at position 1 is not a reference, etc). This change makes specific error messages with helpful hints for such cases. These new error messages can help devs to quickly resolve the issue as well as make it possible to identify existing issues in g3.

PR Close #51338
2023-08-17 14:02:52 -07:00
Paul Gschwendtner
552ea77854 refactor(compiler-cli): drop tsickle code paths (#50602)
`tsickle` is not used in any code paths in 3P and we can remove
this complexity. The `tsickle` npm package has not been released
in a while and we are risking breakages with e.g. future TypeScript
versions.

Note that the `ng_module` rule was updated to not emit through
tsickle at all. The tsickle in 1P is done directly by `tsc_wrapped`
and our code path in `compiler-cli` is not needed at all.

PR Close #50602
2023-08-17 10:23:49 -07:00
Andrew Kushnir
bcc3c43fca refactor(core): update TestBed to handle async component metadata (#51182)
This commit updates TestBed to wait for async component metadata resolution before compiling components.
Async metadata is added by the compiler in case a component uses defer blocks, which contain deferrable
symbols.

PR Close #51182
2023-08-15 11:32:09 -07:00
Andrew Kushnir
c41a1950fd refactor(compiler): apply component metadata asynchronously when defer blocks are present (#51182)
This commit updates compiler logic to generate the `setClassMetadataAsync` calls for components that used defer blocks. The `setClassMetadataAsync` function loads deferrable dependencies and invokes the `setClassMetadata` synchronously once everything is loaded. This change is needed to avoid eager references to deferrable symbols in component metadata in generated code.

PR Close #51182
2023-08-15 11:32:09 -07:00
Kristiyan Kostadinov
422d0d5ca3 refactor(compiler): handle deferred when trigger with a pipe (#51368)
Fixes that we weren't processing `when` conditions correctly which led to a compilation error when a pipe is used inside the expression.

PR Close #51368
2023-08-15 10:03:59 -07:00
Kristiyan Kostadinov
5212b47bbf refactor(compiler): introduce defer trigger instructions (#51315)
Adds the logic for generating the instructions for the various deferred triggers.

PR Close #51315
2023-08-11 06:55:13 -07:00
Kristiyan Kostadinov
79f9d49fad refactor(compiler): introduce defer block instructions (#51315)
Adds the logic for generating `{#defer}`, `{:placeholder}`, `{:loading}` and `{:error}` block instructions in the compiler.

PR Close #51315
2023-08-11 06:55:13 -07:00
Kristiyan Kostadinov
36b180ade4 refactor(compiler): implement conditional block AST (#51299)
Adds the AST for `if`, `else if` and `else` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Kristiyan Kostadinov
4424920f0b refactor(compiler): implement for block AST (#51299)
Adds the AST for `for` and `empty` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Kristiyan Kostadinov
31c6c5e944 refactor(compiler): implement switch block AST (#51299)
Adds the AST for `switch`, `case` and `default` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Payam Valadkhan
1eda1bdfcc refactor(compiler-cli): ctor dependencies in local compilation mode (#51089)
Ctor deps are added to the fctory function for all the angular classes: NgModule, Component, Pipe, Directive and Injectable

PR Close #51089
2023-08-08 13:58:48 -07:00
Payam Valadkhan
827e10ae0e refactor(compiler): add a factory for component dependencies in local compilation mode (#51089)
A factory generator function called "i0.ɵɵgetComponentDepsFactory" is added to generate a factory function for component dependencies. This function will use the deps tracker to calculate the component's dependencies.

For standalone components the component imports (if exists) will be passed to this function. Alternatively this function can grab the imports directly from the decorate, but such extraaction needs some runtime logic which overlapps with what the trait compiler is doing. So better to pass the imports directly to this function at compile time.

PR Close #51089
2023-08-08 13:58:48 -07:00
Payam Valadkhan
e26080be0b refactor(compiler-cli): ng module injector compilation in local mode (#51089)
In local mode the compiler combines the raw imports and exports and pass them to the injector definition as the imports field. It is not possible to filter out ng modules at compile time though, and it will be done in runtime.

Unit tests also added, and since that was the first time adding tests for local compilation some tweaks had to be made in order to disable diagnostics in local compilation mode in order for tests to run (such situation is also the case in real compilation where we ignore all teh diagnostics basically)

PR Close #51089
2023-08-08 13:58:48 -07:00
Andrew Kushnir
6f506cdff0 refactor(compiler): drop regular imports when symbols can be defer-loaded (#51171)
This commit updates the logic to drop regular imports when all symbols that it brings can be defer-loaded.
The change ensures that there is no mix of regular and dynamic imports present in a source file.

PR Close #51171
2023-08-04 11:28:07 -04:00
Kristiyan Kostadinov
d11548f2ef refactor(compiler): store deferred triggers as a map (#51262)
Stores the `deferred` block triggers as a map instead of an array, because triggers can't be duplicated and because having to search through an array will be inconvenient later on.

I've also added a `DeferredBlock.visitAll` method to deduplicate the logic from the various visitor implementations.

PR Close #51262
2023-08-04 11:27:39 -04:00
Miles Malerba
74974f80e0 refactor(compiler): Parse extracted class attributes (#51258)
Adds logic to parse extracted class attributes into separate
ExtractedAttributeOps per class in the attribute.

PR Close #51258
2023-08-03 14:34:53 -04:00
Miles Malerba
2d52d5e62c refactor(compiler): support safe function calls (#51100)
Adds support for safe function calls in template pipeline compiler

PR Close #51100
2023-08-01 13:45:34 -07:00
Miles Malerba
42caeeef21 refactor(compiler): reuse temp vars when possible (#51100)
Updates the template pipeline's temporary variables phase to reuse
temporary variables within an expression. The algorithm implemented here
reuses variables more aggressively than TemplateDefinitionBuilder. This
change in behavior is acceptable, as it is unlikely to cause any
failures, and implementing the exact behavior observed in
TemplateDefinitionBuilder would be difficult.

PR Close #51100
2023-08-01 13:45:34 -07:00
Miles Malerba
b8ec182a2a test(compiler): allow alternate expected file for template pipeline (#51100)
In some cases it is not feasible to have the template pipeline produce
the exact same compiled output as the TemplateDefinitionBuilder. This
commit adds support to the testing infrastructure to have different
expected output files for each. This option should be used sparingly, as
we want the output to be as close as possible.

PR Close #51100
2023-08-01 13:45:34 -07:00
Andrew Kushnir
efb486e8bc refactor(compiler): handle defer blocks in TemplateDefinitionBuilder (#51162)
Updates the TemplateDefinitionBuilder class to generate the `defer` instruction for `{#defer}` blocks. Also generates dependency function that would be invoked at runtime (with dynamic imports inside).

PR Close #51162
2023-08-01 11:50:05 -07:00
Andrew Kushnir
08992a5f2f refactor(compiler): compute the list of dependencies for defer blocks (#51162)
This commit brings the logic to calculate teh set of dependencies for each defer block. For each dependency we also identify whether it can be defer-loaded or not.

PR Close #51162
2023-08-01 11:50:05 -07:00
Andrew Kushnir
f5116e739e refactor(compiler): extracting helper function and types to the top level (#51162)
This is a minor refactoring of the ComponentHandler class logic to extract helper function and types to the top level for simplicity and reuse across other functions of the class.

PR Close #51162
2023-08-01 11:50:05 -07:00
Andrew Kushnir
256e6826bc refactor(compiler): add DeferredSymbolTracker class to keep track of symbol usages (#51162)
This commit adds a new class called `DeferredSymbolTracker` to keep track of all usages of a particular symbol within a source file and allow to detect whether a symbol can be defer loaded (i.e. if there are any references to a symbol).

PR Close #51162
2023-08-01 11:50:04 -07:00
Miles Malerba
c1052cf7a7 refactor(compiler): add support for sanitizing properties and attributes (#51156)
Sets sanitizer functions when attempting to set sensitive properties and attributes

PR Close #51156
2023-07-28 15:11:51 -07:00
Miles Malerba
09bf32724f refactor(compiler): add support for animation listeners (#50975)
Adds support for binding animation listeners, e.g.

PR Close #50975
2023-07-28 14:40:13 -07:00
Miles Malerba
7dd99cd843 refactor(compiler): add support for animation properties (#50975)
Adds support for binding animation properties, e.g.

PR Close #50975
2023-07-28 14:40:13 -07:00
Dylan Hunn
50613117bd refactor(compiler): Ingest host attribute bindings in template pipeline (#51188)
Host property bindings beginning with `attr.` should have `Attribute` binding kind, and result in an `attribute` instruction.

This should really be handled in the parser in the future.

PR Close #51188
2023-07-28 11:47:57 -07:00
Dylan Hunn
a42c91e78b refactor(compiler): Generate temporaries in host bindings for template pipeline (#51188)
Add the ability to name and resolve generated temporary variables in host bindings, using the existing phase.

PR Close #51188
2023-07-28 11:47:57 -07:00
Dylan Hunn
72a59bdc4d refactor(compiler): Support svg and math namespace instructions in template pipeline (#51188)
Templates may contain special `svg` and `math` elements, as well as logical descendants of those elements (e.g. `svg` may contain `g`). These will be parsed with a special colon-prefixed *namespace identifier*, such as `:svg:svg`, or `:svg:g`, or `:math:infinity`.

The template pipeline now considers these namespace prefixes, and stores them specially on the Element and Template data structures, ultimately generating the appropriate runtime instructions to change namespaces when needed.

PR Close #51188
2023-07-28 11:47:57 -07:00
Dylan Hunn
c4b289ab8f refactor(compiler): Allow chaining of listeners in template pipeline (#51188)
Calls to the listener instruction can be chained.

PR Close #51188
2023-07-28 11:47:57 -07:00
Dylan Hunn
8f50385041 refactor(compiler): support ngNonBindable in the template pipeline (#51188)
When a container-like element has the `ngNonBindable` special attribute, bindings are disabled for it and its descendants. This requires emitting the `disableBindings` and `enableBindings` instructions when nested content exists.

PR Close #51188
2023-07-28 11:47:57 -07:00
Dylan Hunn
833cb8ef69 refactor(compiler): Enable additional tests for the template pipeline (#50899)
A number of tests were previously disabled, but are now passing with the latest changes.

PR Close #50899
2023-07-27 15:08:05 -07:00
Dylan Hunn
5a0ecdb58b refactor(compiler): Allow host binding functions to specialize bindings (#50899)
Interestingly, host bindings are parsed quite differently from template functions. For example, bindings such as `[style.foo]: 3px` would be parsed into a value, unit, and type when bound to a template, but will not be parsed as such when used in a host binding.

In this commit, we remedy this shortcoming by adding support for bindings in host binding functions to the template pipeline. In particular, we create a phase to process these bindings, and transform them into the correct output binding kind.

Additionally, we fix some other minor bugs and omissions.

Finally, we enable compilation of host bindings with the template pipeline, which requires us to turn off a number of failing tests.

PR Close #50899
2023-07-27 15:08:05 -07:00
Dylan Hunn
31ff476060 refactor(compiler): Introduce source maps in the template pipeline. (#50899)
Begin producing source maps for the template pipeline, for a couple fundamental kinds of instructions, including elements, templates, properties, text, and interpolations.

PR Close #50899
2023-07-27 15:08:04 -07:00
Dylan Hunn
8a6a72e7cb refactor(compiler): Fix $event in listeners for template pipeline (#50899)
Previously, `$event` was interpreted as a lexical read on the enclosing context. Now, a new pass converts such reads into simple output AST reads of `$event`, so they are not processed by the context resolution or naming phases. Additionally, the same pass sets a field on the enclosing listener op, so that the reify phase does not have to search for reads of `$event`.

PR Close #50899
2023-07-27 15:08:04 -07:00
Dylan Hunn
c6010f0610 refactor(compiler): Support $any in template pipeline (#50899)
`$any(...)` casts should be dropped, except when they are an explicit call on `this.$any(...)`. Fix a bug in which we were transforming `ThisReceiver` into an implicit receiver.

PR Close #50899
2023-07-27 15:08:04 -07:00
Jessica Janiuk
386cb2f18b Revert "docs: added the alt attribute in the img tag (#51102)" (#51107)
This reverts commit 3b248c59c6.

PR Close #51107
2023-07-19 18:48:59 +00:00
alkavats1
3b248c59c6 docs: added the alt attribute in the img tag (#51102)
PR Close #51102
2023-07-19 17:39:44 +00:00
Andrew Kushnir
24bf133eb6 refactor(compiler): add support for dynamic imports in the output AST (#51087)
This commit updates the output AST (and related visitors) to support dynamic imports. This functionality will be used later to generate the output for defer blocks.

PR Close #51087
2023-07-19 16:54:42 +00:00
Kristiyan Kostadinov
7410d6847b refactor(compiler): add compiler flag to enable deferred blocks for testing (#51079)
Adds a new compiler option that will allow `defer` (and other) blocks to be enabled when writing unit tests.

PR Close #51079
2023-07-18 17:05:29 +00:00
Kristiyan Kostadinov
9e61616ffe refactor(compiler): introduce deferred block AST (#51050)
Adds the logic to create `defer`-specific AST nodes from the generic HTML `BlockGroup` and `Block`. The logic for parsing the triggers will be in the next commit.

PR Close #51050
2023-07-17 21:05:47 +00:00
Miles Malerba
afd2fd8681 refactor(compiler): ensure correct ordering of properties and attributes (#50805)
Ensures that all property and attribute ops are ordered consistently
regardless of the order they appear in the template. This ensures
correct precedence (e.g. `[style.color]="'#000'"` awlays wins out over
`[style]="{color: '#fff'}"`)

PR Close #50805
2023-07-17 17:03:57 +00:00
Miles Malerba
03e6dc3f28 refactor(compiler): normalize style prop binding names (#50805)
Normalizes style property names in bindings by converting them to
kebab-case and stripping off `!important`

PR Close #50805
2023-07-17 17:03:57 +00:00
Miles Malerba
c27d6b6b37 build: enable newly passing style tests for the template compiler (#50805)
Enables style tests that are now passing in the template compiler thanks
to recent fixes

PR Close #50805
2023-07-17 17:03:57 +00:00
Miles Malerba
2f7072dc3f refactor(compiler): add support for class property bindings (#50805)
Adds support for bindings of the form `[class.some-class]="isActive"`

PR Close #50805
2023-07-17 17:03:57 +00:00