Commit graph

1294 commits

Author SHA1 Message Date
Jeremy Elbourn
8ef4b1d2d1 refactor(compiler): rename decorator extracted "options" to "members" (#52462)
The property name `members` makes it easier to use the same rendering
code as other constructs.

PR Close #52462
2023-10-31 14:58:03 -07:00
Kristiyan Kostadinov
eb15358479 fix(compiler): project control flow root elements into correct slot (#52414)
With the directive-based control flow users were able to conditionally project content using the `*` syntax. E.g. `<div *ngIf="expr" projectMe></div>` will be projected into `<ng-content select="[projectMe]"/>`, because the attributes and tag name from the `div` are copied to the template via the template creation instruction. With `@if` and `@for` that is not the case, because the conditional is placed *around* elements, rather than *on* them. The result is that content projection won't work in the same way if a user converts from `*ngIf` to `@if`.

These changes aim to cover the most common case by doing the same copying when a control flow node has *one and only one* root element or template node.

This approach comes with some caveats:
1. As soon as any other node is added to the root, the copying behavior won't work anymore. A diagnostic will be added to flag cases like this and to explain how to work around it.
2. If `preserveWhitespaces` is enabled, it's very likely that indentation will break this workaround, because it'll include an additional text node as the first child. We can work around it here, but in a discussion it was decided not to, because the user explicitly opted into preserving the whitespace and we would have to drop it from the generated code. The diagnostic mentioned point #1 will flag such cases to users.

Fixes #52277.

PR Close #52414
2023-10-31 14:52:30 -07:00
JoostK
581eff4ab1 fix(compiler-cli): use originally used module specifier for transform functions (#52437)
Prior to this change, the transform function would be referenced with a potentially
relative import into an external declaration file. Such imports are not portable
and should not be created in this context. This commit addresses the issue by threading
though the originally used module specifier by means of the `Reference` type.

Fixes #52324

PR Close #52437
2023-10-31 13:42:50 -07:00
JoostK
0c7accf92d fix(compiler-cli): properly emit literal types in input coercion function arguments (#52437)
This commit fixes an issue where using literal types in the arguments of an input coercion
function could result in emitting invalid output, due to an assumption that TypeScript makes
when emitting literal types. Specifically, it takes the literal's text from its containing
source file, but this breaks when the literal type node has been transplanted into a
different source file. This issue has surfaced in the type-check code generator and is
already being addressed there, so this commit moves the relevant `TypeEmitter` class
from the `typecheck` module to the `translator` module, such that it can be reused for
emitting types in the type translator.

Fixes #51672

PR Close #52437
2023-10-31 13:42:50 -07:00
Dylan Hunn
73c5d1c04a refactor(compiler): Implement the remaining defer on triggers (#52387)
The previous commits provided the scaffolding for `defer on`. In this commit, we build on that work, adding triggers for `immediate`, `timer`, `hover`, and `viewport`.

PR Close #52387
2023-10-31 12:45:18 -07:00
Dylan Hunn
79684499e4 refactor(compiler): Introduce ConstCollectedExpr, and use it in defer on expressions (#52387)
Previously, we supported a `HasConst` trait, allowing an op to be const collected automatically. However, that approach had the shortcoming that each op could only collect a single constant.

Instead, we now provide a `ConstCollectedExpr`, which collects constants at the expression level, allowing ops to have multiple collectible consts.

Then, we use this new abstraction to support the `defer on` conditions.

PR Close #52387
2023-10-31 12:45:18 -07:00
Dylan Hunn
6c507e75a6 refactor(compiler): Implement defer conditions, and change the way slots are linked (#52387)
Previously, we had an "empty shell" implementation of defer conditions, and we used separate ops to represent secondary defer blocks.

Now, we have a real scaffolding for supporting the various defer conditions, and the secondary defer block information has been refactored onto the main defer op.

Additionally, to enable this, we refactor the way that using slot indices works. Instead of having a trait that causes users of slot indices to be linked to the allocated slot, we share a single `SlotHandle` object by reference. This allows an op to use slot information for more than one Xref at a time, and eliminates a layer of indirection.

Co-authored-by: Alex Rickabaugh <alxhub@users.noreply.github.com>

PR Close #52387
2023-10-31 12:45:18 -07:00
Miles Malerba
e92d87ee08 test(compiler): Enable passing i18n tests (#52390)
Enables a handful of i18n tests that are currently skipped, but pass if
enabled. Some of them require alternate golden files because of
inconsequential differences in the cost array order.

PR Close #52390
2023-10-27 16:16:04 -07:00
Jeremy Elbourn
9291ffc418 refactor(compiler): extract api docs for inherited members (#52389)
This commit expands docs extraction for classes and interfaces to include inherited members. This relies on the type checker to get the _resolved_ members of the type so that the extractor doesn't need to reason about inheritance rules, which can get tricky (especially with regards to method overloads).

PR Close #52389
2023-10-27 12:54:14 -07:00
Jeremy Elbourn
740d46f93b refactor(compiler): extract decorator API docs (#52389)
This commit adds decorators to the extracted API docs. It makes some
very hard-coded assumptions about the pattern used to declare decorators
that's extremely specific to what the framework does today.

PR Close #52389
2023-10-27 12:54:14 -07:00
Andrea Canciani
fc9ba3978c refactor: fix a number of typos throughout the codebase (#52249)
Fix some typos such as `boostrap`, `propery` and more, both in
documentation and in code (comments, identifiers).

PR Close #52249
2023-10-25 16:51:24 -07:00
Dylan Hunn
d82d58621e refactor(compiler): Don't double-create pipes in switch cases (#52289)
Previously, we would emit *two* pipe creation instructions for each pipe in a switch case. This is because we were visiting both the transformed and raw versions of the pipe bindings.

Now, we clear the raw case expressions array after generating the transformed test expression.

Also, we introduce some new goldens, because our pipe creation order is harmlessly different.

PR Close #52289
2023-10-24 11:07:50 -07:00
Dylan Hunn
17be1a8aca refactor(compiler): Support content projection source maps (#52289)
The `projection` op should map onto the entire corresponding `ng-content`.

PR Close #52289
2023-10-24 11:07:50 -07:00
Dylan Hunn
3343ceb82d refactor(compiler): Update pipe test golden for alternative create order (#52289)
We roughly attempt to match TemplateDefinitionBuilder's pipe creation order, by placing pipe creation instructions after their target elements. However, we cannot fully emulate the "inside-out" ordering TemplateDefinitionBuilder uses when multiple pipes apply to one element, because TemplateDefinitionBuilder creates the pipes as expressions are visited, from the leaves up. Our order is perfectly adequate though.

We also add a non-compatibility-mode ordering, which just appends them to the end of the create block. This is better because it allows for more chaining opportunities.

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
0491fba523 refactor(compiler): Fix a special case involving var counting for singleton propertyInterpolate (#52289)
Singleton property interpolation instructions consume only one variable, but are still emitted as an interpolation instruction (they cannot be collapsed because `propertyInterpolate` implicitly stringifies its argument.)

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
f6cc09b0f4 refactor(compiler): The projection instruction takes an array literal (#52289)
We were incorrectly emiting a extracted constant pool index for the final argument of the projection instruction. It actually takes an array literal.

(N.B.: This means we re-create the array every time! We should probably modify the runtime to use a const index for this.)

Additionally, we alter the projection op to not extend the element op base type.

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
9f839272bd refactor(compiler): Improve the ordering of update ops (#52289)
The correct order of attributes and properties is:

1. Interpolated properties
2. Interpolated attributes
3. Non-interpolated properties
4. Non-interpolated attributes

This includes an additional nuance: singleton attribute interpolations, such as `[attr.foo]="{{bar}}"`, will be "collaped" into a simple `attribute` instruction. However, this is *not* the case for singleton property interpolations! The ordering phase must take this nuance into account to match the TemplateDefinitionBuilder order.

After the project lands, it might be nice to also collapse singleton property interpolations.

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
d55ff744e3 refactor(compiler): Order elements before other phases (#52289)
Previously, we ran the ordering phase near the end of the compilation. However, this meant that phases like slot assignment and variable offset assignment would happen first, and then the nice, monotonically-increasing orders would be scrambled by the reordering.

It's much more intelligible to order first, and then perform these assignments. However, to make this happen, some modifications to the ordering phase are required. In particular, we can no longer rely on `advance` instructions to break up orderable groups.

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
b814b97d8e refactor(compiler): Imitate TemplateDefinitionBuilder's variable offset assignment order (#52289)
Many instructions consume variable slots, which are used to persist data between update runs. For top-level instructions, the offset into the variable data array is implicitly advanced, because those instructions always run.

However, instructions in non-top-level expressions cannot be assumed to run every time, because they might be conditionally executed. Therefore, they cannot implicitly advance the offset into the variable data, and must be given an explicitly assigned variable offset.

TemplateDefinitionBuilder assigned offsets top-to-bottom for all instructions *except* pure functions. Pure functions would be assigned offsets lazily, on a second pass.

Template Pipeline can now imitate this behavior, when in compatibility mode: pure functions are assigned offsets on a second pass.

This also makes the "variadic var offsets" phase unnecessary -- the new approach is more general and correct.

PR Close #52289
2023-10-24 11:07:49 -07:00
Dylan Hunn
044df0c1a8 refactor(compiler): Use already available context in closures, instead of saving it (#52289)
Previously, inside an event listener, template pipeline would always save the context from restoring a view, e.g.

```
const restored_ctx = r0.ɵɵrestoreView(s);
```

This is usually correct! However, consider the case of a listener in the template's root view. The appropriate context will already be available via closure capture, and we can just use it (as `ctx`).

Now, the context resolution phase understands that we don't need to use the restored view's saved context if we would have access to it by closure.

Note: we also create a new golden, because the const array is in a harmlessly different order.

PR Close #52289
2023-10-24 11:07:48 -07:00
Dylan Hunn
f5dffe1c5b refactor(compiler): Empty context variable reads are $implicit (#52289)
Previously, the template pipeline did not handle "empty" reads gracefully: it would emit syntactically invalid reads of empty properties. Now we read `$implicit`.

This allows us to enable a test that relies on `$implicit`. However, we also have to create another golden, because our variable inlining is more aggressive.

PR Close #52289
2023-10-24 11:07:48 -07:00
Dylan Hunn
5d527e8c6b refactor(compiler): Update golden for pipe binding collapsing (#52289)
We currently allow elements to be collapsed around pipe creation instructions. TemplateDefinitionBuilder disallows this, but only sometimes. Collapsing in this case is actually less generated code, and it's OK to allow it.

PR Close #52289
2023-10-24 11:07:48 -07:00
Dylan Hunn
efe170fb2a refactor(compiler): Enable already-passing tests (#52289)
Some tests are already passing, but were not enabled at the time they were previously fixed.

PR Close #52289
2023-10-24 11:07:48 -07:00
Kristiyan Kostadinov
b6b5adca38 fix(compiler): account for type-only imports in defer blocks (#52343)
Fixes that `@defer` blocks didn't account for type-only imports which could cause the import to be considered as not deferrable.

PR Close #52343
2023-10-24 09:20:49 -07:00
Dylan Hunn
30e81d4469 refactor(compiler): Support track functions and context variables in @for loops for template pipeline (#52001)
The template pipeline can now generate track functions, and extract them into the constant pool (or optimize them if needed). Additionally, context variables such as `$index` can be used inside track functions and for loop bodies.

PR Close #52001
2023-10-23 13:44:29 -07:00
Dylan Hunn
c8a5a4f852 refactor(compiler): Support @for blocks in template pipeline (#52001)
Add support for `repeaterCreate` and `repeater` instructions. Correctly count decls and vars, and support primary and empty blocks.

`track` functions are not yet extracted.

PR Close #52001
2023-10-23 13:44:29 -07:00
Kristiyan Kostadinov
dc3f7cb3bf fix(compiler): template type checking not reporting diagnostics for incompatible type comparisons (#52322)
In #52110 the compiler was changed to produce `if` statements when type checking `@switch` in order to avoid a bug in the TypeScript compiler. In order to avoid duplicate diagnostics, the main `@switch` expression was ignored in each of the `@case` comparisons. This appears to have caused a regression where comparing incompatible types wasn't being reported anymore.

These changes resolve the issue by wrapping the expression in parentheses which allows the compiler to report comparison diagnostics while ignoring diagnostics in the expression itself.

Fixes #52315.

PR Close #52322
2023-10-23 09:27:23 -07:00
Payam Valadkhan
56a76d73e0 fix(compiler-cli): modify getConstructorDependencies helper to work with reflection host after the previous change (#52215)
Now the method `getConstructorDependencies` no longer needs to do any post analysis, and can rely on the reflection host's result to generate ctor params. This will automatically include invalid factories which fix the issue.

PR Close #52215
2023-10-19 09:38:30 -07:00
Miles Malerba
1cb039fa53 refactor(compiler): Wrap bare ICUs in an i18n block (#52250)
ICUs can be used outside of an i18n block. In this case the ICU should
be automatically wrapped in a new i18n block. This commit adds a new
phase to handle wrapping these bare ICUs.

PR Close #52250
2023-10-18 14:00:04 +02:00
Miles Malerba
1cd51d6d68 refactor(compiler): Resolve ICU params during i18n post-processing (#52250)
ICU params in i18n messages are now resolved in the post-processing call
rather than in the initial message creation. This matches the output
generated by TemplateDefinitionBuilder.

PR Close #52250
2023-10-18 14:00:03 +02:00
Miles Malerba
8eed992052 refactor(compiler): Add support for ingesting ICUs (#52250)
ICUs are now ingested by adding ops to both the creation and update IR.
Both of these ops are ultimately removed before reification, but they
are needed to coordinate and link data between the creation and update
ops. This is done in a new ICU extraction phase that removes both ICU
ops and adds an i18nExpr op to the update IR.

PR Close #52250
2023-10-18 14:00:03 +02:00
Jeremy Elbourn
634c529504 refactor(compiler): extract generic info for api reference (#52204)
This commit extracts the API reference info for generic parameters for
classes, methods, interfaces, and functions. It includes any constraints
and the default type if present.

PR Close #52204
2023-10-17 12:29:24 +02:00
Miles Malerba
9bc9464a54 test(compiler): Update golden partial file (#52202)
Updates the golden partial file to account for the newly added test

PR Close #52202
2023-10-17 10:13:23 +02:00
Miles Malerba
7929097820 refactor(compiler): Fix handling of structural directive on i18n element (#52202)
Placing a structural directive on an element with an `i18n` attribute
was generating too many i18n blocks. This was due to both the element
and the template generating their own i18n block. To fix the issue, we
no longer generate top-level i18n blocks for structural directive
templates.

PR Close #52202
2023-10-17 10:13:23 +02:00
Miles Malerba
197819ee5e refactor(compiler): Fix handling of sturctural directive on ng-template (#52202)
Structural directives on an ng-template (e.g. <ng-template *ngIf>) were
being assigned the wrong tag name ('ng-template' instead of null).

PR Close #52202
2023-10-17 10:13:23 +02:00
Miles Malerba
9f4927e778 refactor(compiler): Fix i18n placeholders for slef-closing elements (#52195)
Fixes handling of placeholders for self-closing tags. Self-closing tags
set a combined value for the start tag placeholder, rather than separate
values for the start and close placeholders.

This commit also enables a number of now passing tests. For some of
these tests I had create a separate golden file due to the different
ordering of the const array. In the template pipeline, i18n and
attribute const collection happen in different pahses and we therefore
get a different order than TemplateDefinitionBuilder, which collected
everything in one pass. The order should not affect the overall behavior.

PR Close #52195
2023-10-16 19:25:05 +02:00
Miles Malerba
3f2620c1f3 refactor(compiler): Fix propagation of child i18n params (#52195)
The way we were propagating params up to parent i18n ops didn't account
for the fact that a parent and child could both have a value for the
same placeholder. In order to properly merge the value for these cases,
we need to propagate the params up *before* serialization. Therefore I
removed the standalone param propagation phase and folded the logic into
the placeholder resolution phase.

PR Close #52195
2023-10-16 19:25:05 +02:00
Kristiyan Kostadinov
9d19c8e317 fix(compiler): don't allocate variable to for loop expression (#52158)
Currently the compiler allocates a variable slot to the `@for` loop expression which ends up unused since we don't store the result on the `LView`.

PR Close #52158
2023-10-11 09:12:57 -07:00
Payam Valadkhan
1eefa0c9c4 refactor(compiler-cli): include forbidOrphanComponents option in component's debug info (#52061)
A new flag added to the component's debug info to determine whether to throw runtime error (in dev mode) if component is being rendered without its NgModule. This flag is only set for non-standalone components.

PR Close #52061
2023-10-10 15:30:26 -07:00
Payam Valadkhan
e8201a5962 refactor(compiler-cli): add a compiler option to enable checking for orphan component (#52061)
Orphan component is an anti-pattern in Angular where a component is rendered while the NgModule declaring it is not installed. It is not easy to capture this scenario, specially in compile time. But it is possible to capture a special case in runtime where the component is being rendered without its NgModule even loaded into the browser. This change adds a flag in cli compiler option to enable such checking, and throwing a runtime exception if it happens. Note that such check is only done in dev mode.

Currently the check requires some generated code that is behind ngJitMode flag (i.e., call to ɵɵsetNgModuleScope), and the new flag can be set only if JIT mode is enabled (i.e., supportJitMode=true) otherwise an error will be thrown.

The orphan component is a main blocker for rolling out local compilation in g3. This option is needed for identifying and isolating such cases.

PR Close #52061
2023-10-10 15:30:26 -07:00
Kristiyan Kostadinov
5a969e06b7 build: remove Windows CI check (#52140)
Based on recent discussions, these changes remove the Windows CI check because it has been too flaky for too long. Furthermore, we've concluded that the simulated file system in the compiler tests already catches the same set of bugs as running the tests on a real Windows system.

PR Close #52140
2023-10-10 14:07:03 -07:00
Jeremy Elbourn
1934524a0c feat(compiler): add docs extraction for type aliases (#52118)
This commit adds support for extracting type alises. It currently
extracts the raw written type from the source without performing any
resolution, such as for resolving `typeof` queries, as current Angular
public APIs do not rely on this.

PR Close #52118
2023-10-10 12:40:10 -07:00
Kristiyan Kostadinov
861ce3a7c5 fix(compiler): pipes using DI not working in blocks (#52112)
Fixes that the new block syntax was generating instructions in the wrong order which meant that pipes were being declared too early. This meant that if the block is first in the template, any pipes used in it won't be able to inject things like `ChangeDetectorRef`.

These changes update the compiler and add a bunch of tests to ensure that pipes work as expected.

Fixes #52102.

PR Close #52112
2023-10-10 09:48:37 -07:00
Kristiyan Kostadinov
ac0d5dcfd6 fix(compiler): narrow the type of expressions in event listeners inside switch blocks (#52069)
Since expressions in event listener are added inside of a callback, type narrowing won't apply to them anymore. These changes add the logic to create a guard expression that will re-narrow the expression in the callback.

Fixes #52052.

PR Close #52069
2023-10-10 09:47:47 -07:00
Kristiyan Kostadinov
16ff08ec70 fix(compiler): narrow the type of expressions in event listeners inside if blocks (#52069)
Since expressions in event listener are added inside of a callback, type narrowing won't apply to them anymore. These changes add the logic to create a guard expression that will re-narrow the expression in the callback.

Fixes #52052.

PR Close #52069
2023-10-10 09:47:47 -07:00
Andrew Kushnir
2eebd47733 refactor(core): make timer-related @defer logic tree-shakable (#52042)
This commit updates `@defer` logic related to handling `after` and `minimum` parameters tree-shakable.

If `after` or `minimum` was used on a `@loading` or `@placeholder` blocks, compiler generates an extra argument for the `ɵɵdefer` instruction. This extra argument is a reference to a function that brings timer-related code.

PR Close #52042
2023-10-09 15:57:59 -07:00
Payam Valadkhan
acd468f804 refactor(compiler-cli): add debug info to components (#51919)
A new statement will be generated for components which will attach some useful debug info to them to be used in runtime error handling. Currently this only happens in full and local compilation modes.

PR Close #51919
2023-10-09 15:57:03 -07:00
Payam Valadkhan
11bb19cafc fix(compiler-cli): handle nested qualified names in ctor injection in local compilation mode (#51947)
The current implementation assumes a qualified name consists of just two identifier, e.g., Foo.Bar. However it can be more nested, like Foo.Bar.Baz.XX.YY. While such nested patterns are quite uncommon and devs mostly just use two identifier here, the TS compiler seems to throw error if we make such assumption and it broke quite a lot of targets in g3 when compiled in local mode. So here we handle this nested property of qualified names.

PR Close #51947
2023-10-09 14:34:55 -07:00
Miles Malerba
371cd58eec refactor(compiler): Fix advance instructions for i18n expressions (#51988)
The custom logic in the generate advance phase for i18n expressions did
not work in all cases. Instead we add a new phase to update the
expression's target op, and then allow the standard advance generation
code to determine the number of advance instructions needed.

Co-authored-by: Dylan Hunn <dylhunn@users.noreply.github.com>

PR Close #51988
2023-10-09 12:35:15 -07:00
Miles Malerba
d8bc6aa129 refactor(compiler): Propagate i18n blocks through child templates (#51988)
Adds a phase to the template pipeline to recursively create child i18n
blocks for ng-template views existing inside an i18n block.

PR Close #51988
2023-10-09 12:35:15 -07:00