Commit graph

1783 commits

Author SHA1 Message Date
Payam Valadkhan
39ddd884e8 fix(compiler-cli): show specific error for unresolved @HostListener's event name in local compilation mode (#54230)
Currently the error is a generic error "selector must be a string ...". This commit makes the error more specific to local compilation and adds some action items.

PR Close #54230
2024-02-06 21:33:29 +00:00
Payam Valadkhan
5d633240fd fix(compiler-cli): show the correct message for the error LOCAL_COMPILATION_UNRESOLVED_CONST when an unresolved symbol used for @Component.styles (#54230)
Currently the correct error message is shown only if @Component.styles is an array with some unresolved element. This change supports the new case of string type for the @Component.styles field.

PR Close #54230
2024-02-06 21:33:29 +00:00
Payam Valadkhan
6c8b09468a fix(compiler-cli): highlight the unresolved element in the @Component.styles array for the error LOCAL_COMPILATION_UNRESOLVED_CONST (#54230)
Currently the whole array is highlighted.

PR Close #54230
2024-02-06 21:33:29 +00:00
Payam Valadkhan
acc98a17d9 refactor(compiler-cli): add a helper to generate error LOCAL_COMPILATION_UNRESOLVED_CONST in common scenarios (#54230)
A helper `validateLocalCompilationUnresolvedConst` is added to encapsulate a common pattern which leads to the error `LOCAL_COMPILATION_UNRESOLVED_CONST`.

PR Close #54230
2024-02-06 21:33:29 +00:00
Payam Valadkhan
a3de5ba865 refactor(compiler-cli): remove the trailing error message for LOCAL_COMPILATION_UNRESOLVED_CONST (#54230)
The trailing error message comes from tracing the chain of DymaicValue which leads to a mostly useless error that highlights the same symbol as the original message and emits the error message "Unknown reference". This error message is removed in the favour of the original message which suffices.

PR Close #54230
2024-02-06 21:33:29 +00:00
Payam Valadkhan
df74ed94e2 refactor(compiler-cli): upgrade error codes and messages for unresolved symbol errors in local compilation mode (#54230)
A single error code is created to unify the common error pattern in local compilation mode where an imported const cannot be resolved, but needs to be resolved. This mainly happens for Angular decorator fields such as @Component.template.

The error messages are also upgraded to be more centered around this unifying theme.

PR Close #54230
2024-02-06 21:33:29 +00:00
Paul Gschwendtner
f8c02b69b9 refactor(compiler-cli): restrict read option values for signal-based queries (#54257)
The `read` option for queries can rely on lexical variables inside the
class. These constructs are fine from a technical perspective in
TypeScript, but in practice, when the component/directive definition is
being created, the read value is extracted into the definition,
**outside** of the class. This breaks `this` references.

To fix this, we are restricting the `read` option to literal values.
Similar to `descendants`. Literal references are in practice constructs
like:

 - `read: bla.X`
 - `read: X`

 where `bla` or `X` is never a `ThisKeywoord`- hence fixing the issue
 and also simplifying the patterns for easier single file compilation.

PR Close #54257
2024-02-06 16:04:10 +00:00
Paul Gschwendtner
902481b6ec refactor(compiler-cli): add JIT transform for signal-based queries (#54257)
This commit adds a JIT transform for signal-based queries, so that
queries are working as expected in JIT environments like `ng test` where
decorator metadata is needed as a prerequisite for the component
definition creation.

This is similar to the JIT transforms for signal inputs etc.

PR Close #54257
2024-02-06 16:04:10 +00:00
Paul Gschwendtner
351426853d refactor(compiler-cli): extract common JIT transform helper into transform API (#54257)
Extracts common JIT transform helper into the transform API, so that
those helpers can be re-used for output, model, queries and inputs.

PR Close #54257
2024-02-06 16:04:10 +00:00
Paul Gschwendtner
1d077fca10 refactor(compiler-cli): expose query function detection logic for JIT transform (#54257)
This commit exposes the query function detection logic so that it can
be used in the JIT transform later on.

PR Close #54257
2024-02-06 16:04:10 +00:00
Jessica Janiuk
e46c08170f Revert "fix(compiler-cli): consider the case of duplicate Angular decorators in local compilation diagnostics (#54139)" (#54264)
This reverts commit 4b1d948b36.

PR Close #54264
2024-02-05 18:18:47 +00:00
Jessica Janiuk
bd43aa39a8 Revert "fix(compiler-cli): allow custom/duplicate decorators for @Injectable classes in local compilation mode (#54139)" (#54264)
This reverts commit a592904c69.

PR Close #54264
2024-02-05 18:18:47 +00:00
Jessica Janiuk
a2af706855 Revert "fix(compiler-cli): forbid custom/duplicate decorator when option forbidOrphanComponents is set (#54139)" (#54264)
This reverts commit 96bcf4fb12.

PR Close #54264
2024-02-05 18:18:47 +00:00
Paul Gschwendtner
b285b1402c refactor(compiler-cli): introduce JIT transform for new output() initializer API (#54217)
Similar to `input()`, initializer-based `output()`'s need to be
transformed in JIT to be annotated with an `@Output()` decorator.

This is necessary so that Angular can statically collect all defined
outputs without instantiating the class (which would not be possible
upon directive definition computation).

This commit introduces a transform next to the input transform that
automatically runs with the Angular CLI and `ng test`.

PR Close #54217
2024-02-05 15:08:35 +00:00
Paul Gschwendtner
55040fb80b test: add type check diagnostic tests for output() API (#54217)
Adds type check diagnostic tests for the `output()` API. This
is necessary because we are maintaining a separate emitter
for `output()` as with the current design (still discussed - but this is
a starting foundation).

Note: `OutputEmitter` currently does not publicly expose `.subscribe`,
while the testing infrastructure exposes it for now. That is because we
are still discussing this before making changes in the TCB to account
for the case where `.subscribe` might be `@internal` ultimately.

PR Close #54217
2024-02-05 15:08:35 +00:00
Paul Gschwendtner
0e9e401ca1 test: generalize type check scenario testing helpers (#54217)
Generalizes the type check table scenario testing infrastructure
so that it can also be used for testing outputs in a table-scheme
without a lot of TS code repetition.

PR Close #54217
2024-02-05 15:08:34 +00:00
Paul Gschwendtner
65de61ba0d test: add compliance output tests for output() (#54217)
Adds compliance output tests for `output()` to verify that
we are emitting proper full compilation output, as well as proper
partial compilation output that can be linked to match the full output.

PR Close #54217
2024-02-05 15:08:34 +00:00
Paul Gschwendtner
f7da77b90d refactor(compiler-cli): detect output() function (#54217)
As we are introducing the new `output()` function as an inituive
alternative to `@Output()` that matches with signal-based inputs,
this commit prepares the compiler to detect such initializer-based
outputs.

PR Close #54217
2024-02-05 15:08:34 +00:00
Payam Valadkhan
96bcf4fb12 fix(compiler-cli): forbid custom/duplicate decorator when option forbidOrphanComponents is set (#54139)
The deps tracker which is responsible to track orphan components does not work for classes mutated by custom decorator. Some work needed to make this happen (tracked in b/320536434). As a result, with option `forbidOrphanComponents` being true the deps tracker will falsely report any component as orphan if it or its NgModule have custom/duplicate decorators. So it is unsafe to use this option in the presence of custom/duplicate decorator and we disable it until it is made compatible. Note that applying custom/duplicate decorators to `@Injectable` classes is ok since these classes never make it into the deps tracker. So we excempt them.

PR Close #54139
2024-02-05 15:07:40 +00:00
Payam Valadkhan
a592904c69 fix(compiler-cli): allow custom/duplicate decorators for @Injectable classes in local compilation mode (#54139)
Custom/duplicate decorators break the deps tracker in local mode. But deps tracker only deals with non-injectable classes. So applying custom/duplicate decorators to `@Injectable` only classes does not disturb deps tracker and local compilation in general. There are also ~ 100 such cases in g3 which cannot be cleaned up.

PR Close #54139
2024-02-05 15:07:40 +00:00
Payam Valadkhan
4b1d948b36 fix(compiler-cli): consider the case of duplicate Angular decorators in local compilation diagnostics (#54139)
For cases like this:

```
@Component({...})
@Component({...})
export class SomeComp {
}
```

The `DecoratorHandler.detect` apparantly matches only one of the `@Component` decorator, leaving the other undetected which will be transformed by TS decorator helper and that breaks local compilation runtimes. But the error message only mentioned "custom" decorator, while in this case it is a "duplicate Angular" decorator. The respective error message is updated thus.

PR Close #54139
2024-02-05 15:07:40 +00:00
Paul Gschwendtner
d74ee6e343 refactor(compiler-cli): group initializer-API based transforms into single transform (#54200)
Instead of maintaining individual transforms for `input`, `output`,
`model` etc. we are grouping them directly and the first one matching,
will execute.

This reduces needed traversal through AST and also makes it a little
more clean to write new initializer API metadata transforms.

Note: The Angular JIT transform is now also moving from `tooling.ts`
directly into `/transformers` for more local placement of transformer
logic.

PR Close #54200
2024-02-01 15:58:50 +00:00
Kristiyan Kostadinov
d89d5230a7 refactor(compiler-cli): allow signal input transform to be reused (#54200)
Splits up the signal input transform so that it can be reused for other signal-based properties.

PR Close #54200
2024-02-01 15:58:50 +00:00
Kristiyan Kostadinov
95dcf5fafa fix(compiler-cli): handle default imports in defer blocks (#53695)
Fixes that `@defer` blocks weren't recognizing default imports and generating the proper code for them. Default symbols need to be accessed through the `default` property in the `import` statement, rather than by their name.

PR Close #53695
2024-02-01 15:58:21 +00:00
Payam Valadkhan
64fa5715c6 fix(compiler-cli): generating extra imports in local compilation mode when cycle is introduced (#53543)
At the moment the extra import generation in local compilation mode fails if these extra imports produce a cycle. To handle this, the cycle handling strategy is updated for local compilation, and following the behaviour in the full compilation mode, the compiler does not generate extra import if it leads to cycle and instead leave things to the runtime.

PR Close #53543
2024-01-30 15:05:43 +00:00
Payam Valadkhan
7e861c640e feat(compiler-cli): generate extra imports for component local dependencies in local mode (#53543)
With option `generateExtraImportsInLocalMode` set, in local mode the compiler generates extra imports for each component local dependencies. Here local dependencies means all component's dependencies within the same compilation unit.

To achieve this, the compiler performs a "local version" of its regular static analysis to find each component's deps, and these deps are used to generate extra side effect imports.

PR Close #53543
2024-01-30 15:05:43 +00:00
Payam Valadkhan
f6e9dbbe73 refactor(compiler-cli): run resolve phase fully for components in local mode when generateExtraImportsInLocalMode is set (#53543)
In this commit the resolve method for components is run fully when the option `generateExtraImportsInLocalMode` is set. This is because we need local component depedencies in order to generate extra imports causing by them. This requires cutting some resolve phase logics that are unnecessary in local mode, such as diagnostics.

PR Close #53543
2024-01-30 15:05:43 +00:00
Payam Valadkhan
6a136d1ab3 refactor(compiler-cli): run register phase in local compilation mode (#53543)
When option `generateExtraImportsInLocalMode` is set, we need to compute component local depednecies in order to generate extra imports related to them. At the same time running the register phase in general is harmless in local compilation. So we run it anyway.

PR Close #53543
2024-01-30 15:05:43 +00:00
Payam Valadkhan
3263df23f2 feat(compiler-cli): generate global imports in local compilation mode (#53543)
With option `generateExtraImportsInLocalMode` set in local compilation mode, the compiler generates extra side effect imports using this rule: any external module from which an identifier is imported into an NgModule will be added as side effect import to every file in the compilation unit. To illustrate this better assume the compilation unit has source files `a.ts` and `b.ts`, and:

```
// a.ts
import {SomeExternalStuff} from 'path/to/some_where';
import {SomeExternalStuff2} from 'path/to/some_where2';

...

@NgModule({imports: [SomeExternalStuff]})

```

then the extra import `import "path/to/some_where"` will be added to both `a.js` and `b.js`. Note that this is not the case for `import "path/to/some_where2"` though, since the symbol `SomeExternalStuff2` is not imported into any NgModule.

The math behind this mechanism is, in local compilation mode we cannot resolve component external dependencies fully. For example if a component in `a.ts` uses an external component defined in an external file `some_external_comp.ts` then we can generate the import to this file in `a.js`. Instead, we want to generate an import to a file that "gurantees" that `a.js` is placed after `some_external_comp.js` in the bundle. Now since the component in  `some_external_comp.ts` is used in `a.ts`, then there must be a chain of imports starting from the NgModule that declares the component in `a.ts` to the component in `some_external_comp.ts`. This chain means some file in the same compilation unit as `a.ts` should import some external NgModule which includes `some_external_comp.ts` in its transitive closure and import it to some NgModule. So by adding this import to `a.js` we ensure that the bundling will have the right order.

PR Close #53543
2024-01-30 15:05:43 +00:00
Payam Valadkhan
8c36f2d6f1 refactor(compiler-cli): ability to generate side effect imports (#53543)
As the first step,  the import manager's `generateSideEffectImport` method is implemented to enable it to store info for side effect imports. Next, the helper `addImports` is modified to be able to generate correct statement for side effect imports.

These changes will be tested in the subsequent commits when these tools are used to generate an actual extra import for the generated file.

PR Close #53543
2024-01-30 15:05:42 +00:00
Payam Valadkhan
fc55de1416 refactor(compiler-cli): incorporate LocalCompilationExtraImportsTracker in the compilation workflow (#53543)
This commit includes a skeleton of how the tool `LocalCompilationExtraImportsTracker` is used in the overall compilation workflow end-to-end.

First of all, a new option `generateExtraImportsInLocalMode` is added, whose presence will make `LocalCompilationExtraImportsTracker` part of the compilation process. When this option is set an instance of `LocalCompilationExtraImportsTracker` is created within the NgCompiler. Then it is passed to the Ivy transformer and plumbed all the way down and the extra imports registered in it are added to the `ImportManager` instances before the imports are added from `ImportManager` to the generated file. This required adding a new method `generateSideEffectImport` to the `ImportManager`, which is an empty method and will be implemented in the subsequent commits.

This commit expected to make no change in the compilation behavior as the methods are not implemented yet.

PR Close #53543
2024-01-30 15:05:42 +00:00
Payam Valadkhan
ac3c484bb1 refactor(compiler-cli): add API for extra import tracker (#53543)
The tracker is responsible for registering the extra imports during the analysis and resolve compiler phases, and later to be used by the transformer to get a list of extra imports to be generated for each source file.

This commit only contains the API, and the actual implementation for each method will be done in subsequent commits where an application of that method is available and so tests can be written for the implementation.

PR Close #53543
2024-01-30 15:05:42 +00:00
Dylan Hunn
7b4d275f49 fix(compiler): Fix the template pipeline option (#54148)
The `useTemplatePipeline` option was not properly threaded through the component handler.

PR Close #54148
2024-01-29 21:12:32 +00:00
Payam Valadkhan
0970129e20 fix(compiler-cli): show proper error for custom decorators in local compilation mode (#53983)
At the moment local compilation mode does not support custom decorators, and it leads to unhandled errors. In this change a compile time diagnostic is produced in local mode for custom decorators. This is a temporary solution since there are few custom decorators are in use in g3. Custom decorators will be eventually supported in local mode.

PR Close #53983
2024-01-26 19:11:34 +00:00
Paul Gschwendtner
fe4343cf13 refactor(compiler): extract call and construct signatures of interfaces (#54053)
This adds initial support for extracting and rendering call and construct
signatures of classes, like within the new `InputFunction` for signal
inputs.

For now, signatures are a rare occasion and represented as class member
entries. In the future we might consider exposing this via its own entry
type, and field on the class/interface entry.

PR Close #54053
2024-01-26 19:10:56 +00:00
Paul Gschwendtner
58b8a232d6 fix(compiler-cli): support jumping to definitions of signal-based inputs (#54053)
This fixes the definitions for signal-based inputs in the language
service and type checking symbol builder.

Signal inputs emit a slightly different output. The output works well
for comppletion and was designed to affect language service minimally.
Turns out there is a small adjustment needed for the definition symbols.

PR Close #54053
2024-01-26 19:10:56 +00:00
Paul Gschwendtner
b78042f3a5 refactor(core): separate InputSignal and InputSignalWithTransform (#54053)
This commit separates `InputSignal` for input signals with transforms.
The reason being that most of the time, signal inputs are not using
transforms and the generics are rather confusing.

Especially for users with inferred types displayed in their IDEs, the
input signal types are seemingly complex, even if no transform is used.

For this reason, we are introducing a new type called
`InputSignalWithTransform`. This type will be used for inputs with
transforms, while non-transform inputs just use `InputSignal`.

A notable fact is that `InputSignal` extends `InputSignalWithTransform`,
with the "identity transform". i.e. there is no transform. This allows
us to share the code for input signals. In practice, we don't expect
users to pass around `InputSignal`'s anyway.

PR Close #54053
2024-01-26 19:10:56 +00:00
Kristiyan Kostadinov
93188cb439 refactor(compiler): distinguish two-way bindings in the AST (#54065)
During the template parsing stage two-way bindings are split up into a property and event binding. All the downstream code treats these binding the same as their one-way equivalents. For some future work we'll have to distinguish between the two so these changes update the `BoundElementProperty.type` and `ParsedEvent.type` to include a `TwoWay` type. All existing call-sites have been updated to treat `TwoWay` the same as `Property`/`Regular`, but more specialized logic will be added in the future.

PR Close #54065
2024-01-25 16:31:57 +00:00
Andrew Scott
eddf5dae5e fix(compiler): Update type check block to fix control flow source mappings (#53980)
The source mappings and types for various pieces of the control flow
were not quite right and prevented the language service from providing
accurate information.

fixes https://github.com/angular/vscode-ng-language-service/issues/1988

PR Close #53980
2024-01-24 18:37:58 -05:00
Dylan Hunn
47e6e84101 feat(compiler): Add a TSConfig option useTemplatePipeline (#54057)
The Template Pipeline is a brand new backend for the Angular compiler, replacing `TemplateDefinitionBuilder`. It generates the Ivy instructions corresponding to an input template (or host binding). The Template Pipeline has an all-new design based on an intermediate representation compiled over many phases, which will allow us to experiment with compiler changes more easily in the future.

With this commit, the template pipeline can now be enabled in any project via the `useTemplatePipeline` TSConfig option. However, it is still disabled by default.

PR Close #54057
2024-01-24 18:36:23 -05:00
Paul Gschwendtner
17a47c4c54 refactor(compiler-cli): additional diagnostics for signal-based queries (#54019)
This commit introduces three additional diagnostics for queries:

- If a query (either using decorator or signal-based) is declared on a
  static class member, a diagnostic is raised.
- If a signal-based query is mixed with a query decorator, a diagnostic
  is raised. Similar to signal inputs.
- If a singal-based query is also declared in the directive/component
  class decorator metadata, a diagnostic is raised.

PR Close #54019
2024-01-24 16:13:31 +01:00
Paul Gschwendtner
a7017a0471 refactor(core): improve safety of input/query initializer API detection (#54019)
Due to some refactorings, we were only checking the function name
and whether it originates from an import. We should also verify the
module. This seems like logic we lost in the refactorings.

PR Close #54019
2024-01-24 16:13:31 +01:00
Andrew Kushnir
aeaec8629c refactor(compiler-cli): rephrase an error message related to @defer and local compilation (#54030)
This commit updates the error message to cover cases when imported symbols have eager references inside of a file.

PR Close #54030
2024-01-24 10:08:50 +01:00
Paul Gschwendtner
d4c84a97e8 refactor(compiler-cli): generate partial compilation output for signal-based queries (#53978)
This commit ensures that libraries can use signal-based queries, and the
partial compilation output will capture their metadata.

The linker is updated to support parsing this.

Two notes:

1. Older linker versions are not capable of parsing this, so the minimum
   version for signal-based queries is adjusted when such are used.
2. We only emit `isSignal` metadata for queries when signal queries are
   used. This enables libraries to continue supporting older linker
   versions, if signal-based queries are not used.

PR Close #53978
2024-01-23 10:24:36 +01:00
Paul Gschwendtner
df828e9947 test: add compiler integration test for signal-based queries (#53978)
Adds a compiler integration test for recognizing signal-based queries,
and emitting the expected output. Concrete output will be verified via
the compliance tests.

PR Close #53978
2024-01-23 10:24:36 +01:00
Paul Gschwendtner
8f936a4111 refactor(compiler-cli): recognize signal-based queries in class initializers (#53978)
This commit uses the initializer API recognition that we built for
signal-based inputs, and teaches the compiler to recognize class members
that refer to `viewChild`, `viewChildren`, `contentChild` or
`contentChildren`. Those will declare signal-based view or content queries.

PR Close #53978
2024-01-23 10:24:36 +01:00
Paul Gschwendtner
7ed3927082 refactor(compiler-cli): expose helper for recognizing initializer APIs (#53978)
The new `input` API is recognized using class member initializers.
We want to support similar APIs for queries, using e.g. `viewChild`
or `viewChild.required`.

This commit extracts the input recognition API and makes it reusable,
so that the same logic can be used to detect queries on a class member.

Additional changes:
  - replacing `coreModule` with the simpler `isCore` parameter. This is
    more readable.
  - support for detecting a list of API names on a single class member.
    This allows us to detect possible query functions on the same class
    member without having to check X times. We simply check for the
    initializer API pattern and check if one API function name matches.

PR Close #53978
2024-01-23 10:24:36 +01:00
Payam Valadkhan
3e1384048e feat(compiler-cli): support host directives for local compilation mode (#53877)
At the moment local compilation breaks for host directives because the current logic relies on global static analysis. This change creates a local version by cutting the diagnostics and copying the directive identifier as it is to the generated code without attempting to statically resolve it.

PR Close #53877
2024-01-22 14:44:24 +01:00
Payam Valadkhan
b774e22d8e feat(compiler-cli): make it configurable to generate alias reexports (#53937)
At the moment when unified host is selected (through option `_useHostForImportGeneration`) the compiler always generates alias reexports. Such reexports are mainly generated to satisfy strict dependency condition for generated files. Such condition is no longer the case for G3. At the same time, these alias reexports make it impossible to mix locally compiled targets with globally compiled targets. More precisely, a globally compiled target may not be able to consume a locally compiled target as its dependency since the former may import from the alias reexports which do not exist in the latter due to local compilation mode. So, to make global-local compilation interop possible, it is required to be able to turn off alias reexport generation.

PR Close #53937
2024-01-22 14:20:26 +01:00
cexbrayat
a7f8d9dc08 refactor(compiler-cli): interpolatedSignalNotInvoked diagnostic for input signals (#53883)
This updates the extended diagnotic to handle input signals as well.

PR Close #53883
2024-01-17 11:36:43 -08:00
Andrew Kushnir
b0e0f0014e refactor(compiler): extra diagnostics for @defer in local compilation mode (#53899)
This commit adds extra logic to produce a diagnostic in case `@Component.deferredImports` contain types from imports that also bring eager symbols. This would result in retaining a regular import and generating a dynamic import, which would not allow to defer-load dependencies.

PR Close #53899
2024-01-17 10:46:15 -08:00
Andrew Kushnir
08233b8374 refactor(compiler): enable register and resolve phases for local compilation (#53901)
This commit update the logic to enable `register` and `resolve` phases for local compilation. Those phases will be useful for local compilation in certain cases (will be used in followup PRs).

PR Close #53901
2024-01-17 09:03:55 -08:00
Paul Gschwendtner
760b1f3d0b fix(compiler-cli): do not throw fatal error if extended type check fails (#53896)
Currently when the extended type check fails due to an import reference
that cannot be generated, the fatal diagnostic is not caught and
not properly exposed as a `ts.Diagnostic` that can be gracefully
handled. This is inconsistent to non-extended type checking diagnostics.

This is problematic because Angular CLI applications currently fail in
obscure ways because:

- the CLI does not expect `getDiagnosticsForFile` to actually throw
  runtime errors.
- the CLI does not seem to properly print these errors given the
  parallel workers and build excection, and those errors are
  especially hard to debug because there is no `stack` for
  `FatalDiagnosticError`'s.

Example: `MyDir` is not exported and the type check block cannot reference it.

PR Close #53896
2024-01-16 09:36:36 -08:00
Andrew Kushnir
df685ed7c5 refactor(compiler-cli): add checks to prevent same deps in imports and deferredImports (#53591)
This commit updates the typechecker logic to prevent same deps in `imports` and `deferredImports`.

PR Close #53591
2024-01-10 15:28:58 -08:00
Andrew Kushnir
a2aa23d8b5 refactor(compiler): add support for internal deferredImports field (#53591)
This commit updates the logic to add support for internal `deferredImports` field in compiler.

PR Close #53591
2024-01-10 15:28:58 -08:00
Andrew Kushnir
b780d39612 refactor(compiler-cli): update DeferredSymbolTracker to use explicit deps config (#53591)
This commit updates the `DeferredSymbolTracker` class to take info account the `onlyExplicitDeferDependencyImports` flag. The `DeferredSymbolTracker` class also exposes a new API to register import declarations as explicitly deferred, which will be used in followup commits.

PR Close #53591
2024-01-10 15:28:58 -08:00
Andrew Kushnir
d28cf00df6 refactor(compiler-cli): add an internal config to enforce explicit deps in @defer for local compilation mode (#53591)
This commit adds an internal config option to enforce explicit deps in `@defer` for local compilation mode.

PR Close #53591
2024-01-10 15:28:58 -08:00
Andrew Kushnir
7d1d604916 refactor(compiler-cli): store full Pipe metadata in typechecking code (#53591)
This commit updates typechecker to store full Pipe metadata in internal data strctures, so that this information is available to more places in the code, which will be updated in a followup commit.

PR Close #53591
2024-01-10 15:28:58 -08:00
Andrew Kushnir
869ebc54e9 refactor(compiler-cli): extract various logic into separate functions (#53591)
This commit updates a few places to extract the logic into a separate functions which will be reused in a few places in followup commits.

PR Close #53591
2024-01-10 15:28:58 -08:00
Paul Gschwendtner
b2066d4922 refactor(compiler-cli): detect input functions without partial evaluation (#53872)
This allows us to ensure signal inputs and a potential JIT transform
remain single file compilation compatible. The consequences are that
options need to be statically analyzable more strictly, compared to
loosened restrictions with static interpretation where e.g. `alias`
can be defined through a shared variable.

PR Close #53872
2024-01-10 12:33:31 -08:00
Paul Gschwendtner
39ea8b356e refactor(compiler-cli): drop dependency on external modules for input recognition (#53808)
Improves the recognition of the `input`/`input.required` functions to
not depend on external module resolution. This is useful for local
compilation and for transforms operating on a single file/ isolated
module.

PR Close #53808
2024-01-10 12:21:06 +00:00
Paul Gschwendtner
e934f3a581 refactor(compiler-cli): add diagnostic for inputs declared on static members (#53808)
Currently when someone declares a signal or non-signal input on a static
class member, the compiler will not yield any diagnostic. We can detect
these mistakes and report a diagnostic to help our users.

PR Close #53808
2024-01-10 12:21:06 +00:00
Paul Gschwendtner
40b0467d3e refactor(compiler-cli): diagnostics ensuring signal inputs are declared properly (#53808)
This commit addds two diagnostics for two scenarios where signal inputs
are declared incorrectly:

- a signal input is also annotated with `@Input` in the TypeScript
  sources.
- a signal input is also declared in the `inputs` option of
  `@Directive/`@Component`.

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
81379ef52c refactor(compiler-cli): follow-up typo fixes for typecheck utilities (#53808)
Follow-up for the inital signal inputs PR, fixing a typo that was made.

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
eba017f8c7 refactor(compiler-cli): add transform to support signal input in JIT (#53808)
This commit adds a transform for supporting input signals in JIT
environments. The transform will be wired up for Angular CLI
applications automatically. An integration test verifies that this fixes
unit testing with signal inputs.

The transform basically will take the signal input metadata and
transform it into `@Input` decorators that can provide static
information to the Angular JIT runtime when the directive/component
definition is compiled.

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
b51f386ac7 refactor(compiler-cli): expose import manager import insertion helper (#53808)
This commit does two things:

- exposes `addImports` so that it can be used by transforms that we are
  adding to the compiler. e.g. the signal input to `@Input` transform.

- `updates `addImports` to support/use the transform context factory.
  This will allow us to write proper transforms using `addImports`. Also
  leverages this in the Ivy JS/DTS transforms.

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
4c5366829b refactor(compiler-cli): expose helper for parsing signal input class members (#53808)
Moves the signal input class member extraction logic into the dedicated
input function file. This makes the code for signal inputs more
self-contained.

This commit then re-exposes the function as part of `ngtsc/annotations`
so that it can be used later for a transform that will take the signal
input metadata and translate it into a `@Input` decorator. This allows
us to remove code duplication and guarantees consistency/correctness

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
82a1ce0f32 test(compiler-cli): add additional diagnostic test for generic inline constructors (#53808)
We recently landed a commit to introduce support for generic type
checking of signal inputs. For that we had to implement logic that
will generate imports for inline type constructors. This required
changes to the context logic and `TypeCtorOp` file-level op.

This commit ensures that everything is working as expected, specifically
in cases where an inline type ctor is generated and imports would be
needed to unwrap the class members for `InputSignal`.

PR Close #53808
2024-01-10 12:21:05 +00:00
Paul Gschwendtner
d7147507e4 refactor(core): properly emit input flags using bitwise or (#53808)
As part of testing we did accidentally use `bitwiseAnd` for the input
flags, given we started without an extra flag for `HasTransform`.

This commit teaches the compiler to support emitting bitwise OR
and uses it when combining input flags, fully re-enabling transforms
for signal components after the new flag mechanism was introduced in
previous commits.

PR Close #53808
2024-01-10 12:21:04 +00:00
Paul Gschwendtner
3eaa006c6f test(compiler-cli): additional type-check transform tests for signal inputs (#53571)
This commit adds additional type-check transform tests for signal
inputs. These tests verify some of the problems with covariance,
contravariance and bivariance that we were suspecting to be problematic
if we would assign `InputSignal`'s directly to the type constructors.

PR Close #53571
2024-01-04 12:07:12 -08:00
Payam Valadkhan
1a6eaa0fea fix(compiler-cli): input transform in local compilation mode (#53645)
Currently compiling input transform in local mode breaks, since compiler does static analysis for the transform function, and this cannot be done in local mode if the function is imported from another compilation unit. In this fix the static analysis is ditched in local mode.

PR Close #53645
2024-01-03 10:29:34 -08:00
cexbrayat
33b5707ee9 fix(compiler-cli): interpolatedSignalNotInvoked diagnostic (#53585)
The diagnostic was catching the following case:

```ts
name = signal('Angular');
```

but not the following ones:

```ts
name = signal('Angular').asReadonly();
name = computed(() => 'Angular');
name!: Signal<string>
```

This was not catched in the tests because the type of `Signal` is different than the one actually used in core.
It turns out the real type forces the diagnostic to check both the `symbol.tsType.symbol` and the `symbol.tsType.aliasSymbol`.

PR Close #53585
2024-01-03 10:19:54 -08:00
Kristiyan Kostadinov
e5f02052cb fix(compiler): ignore empty switch blocks (#53776)
Adds some code to the compiler so that it ignores empty `@switch` blocks instead of trying to generate code for them.

Fixes #53773.

PR Close #53776
2024-01-03 10:15:17 -08:00
Dylan Hunn
cafc3b0081 refactor(compiler): Drop the explicit this. in most explicit receivers (#53594)
Consider a case when an explicit `this` read is inside a template with a context that also provides the variable name being read:

```
<ng-template let-a>{{this.a}}</ng-template>
```

Clearly, `this.a` should refer to the class property `a`. However, in today's Angular, `this.a` will refer to `let-a` on the template context.

Amazingly, both TemplateDefinitionBuilder and the Typecheck block have the same bug, and are consistent with each other! This is because `ImplicitReceiver` extends `ThisReceiver` in the parser AST, which is an insane gotcha.

In this commit, I patch the template pipeline to emulate this behavior as well.

To actually fix this nastiness, we have to:
- Update `ingest.ts` in the Template Pipeline (see the corresponding comment)
- Check `type_check_block.ts` in the Typecheck block code (see the corresponding comment)
- Turn off legacy TemplateDefinitionBuilder
- Fix g3, and release in a major version

PR Close #53594
2023-12-19 13:46:11 -08:00
Kristiyan Kostadinov
df8a825910 fix(compiler): project empty block root node (#53620)
Expands the workaround from #52414 to allow for the root nodes of `@empty` blocks to be content projected.

Fixes #53570.

PR Close #53620
2023-12-19 08:37:33 -08:00
Paul Gschwendtner
4866fce320 refactor(compiler-cli): capture signal inputs in semantic graph for directives (#53521)
Whenever an input of a directive changes, the semantic symbol should
reflect this change for the type check API. This is important because
signal inputs require special output in the type checking blocks- hence
we need to ensure that such type checking blocks are re-generated
properly.

Test verify that incremental type-checking builds work as expected now.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
e6db288ffd refactor(compiler-cli): ensure proper TS incremental program re-use w/ signal inputs (#53521)
Whenever a signal input is captured in a type check block, we will
insert an import. This will change the import graph so that the full
TypeScript program cannot be structurally re-used.

We can fix this trivially by ensuring the import graph remains stable,
by always generating an import to e.g. `@angular/core`. This fixes the
issue nicely for type-check block files. A test verifies this.

For inline code, such as TCB inline or the type constructors inline,
this fix is not applicable because we would change user-input source files,
adding new edges that would not exist for subsequent builds- causing the
program to be not re-used completely. One idea was to rely on the
existing edge that can be assumed to exist for directive code files.
This is true technically, but in practice TS does not deduplicate
imports- so our new namespace import when referencing our symbols will
invalidate the re-use. We will address this in a follow-up. There are a
couple of options, such as working with the TS team, updating the
existing edge, or inlining our helpers as well.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
e1acc6086d test(compiler-cli): set up fake_core implementation of input (#53521)
This will allow us to write unit tests for the new input function,
allowing us eventually to test type checking, emitted code etc.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
abdc7e4578 feat(compiler-cli): support type-checking for generic signal inputs (#53521)
This commit adds the last remaining piece for signal input
type-checking. Bound values to signal inputs are already checked
properly at this point, but inference of generic directive/component
types through their inputs is not implemented.

This commit fixes this. To achieve this, there are a couple of potential
solutions. The generics of a directive are inferred based on input
value expressions using a so-called type constructor. The constructor
looks something like this:

```
const _ctor = <T>(v: Pick<Dir<T>, 'input1', 'input2'>) => Dir<T>;

_ctor({input1: expr1, input2: expr2});
```

This works very well for non-signal inputs where the class member is
directly holding the input values. For signal inputs, this does NOT
work because the class member will actually hold the `InputSignal`
instance. There are a couple of solutions to this:

1. Calling `_ctor` with an `InputSignal<typeof value>`
2. Converting the `_ctor` input signal fields to their write types
   (unwrapping the input signals).

We've decided to go with the second option as TypeScript is very
sensitive with assignments and its checks. i.e. co-variance,
contravariance or bivariance. Semantically it makes more sense to unwrap
the input signal "write type" directly and "assign to it". This is safer
and conceptually also easier to follow. A type constructor continues to
only receive the "expresison values". This simplifies code as well.

It's worth noting that the unwrapping as per option 2 also comes at a
cost. We need to be able to generate imports in type constructors. This
was not possible until the previous commit because inline type constructors
did not have an associated type-check block `Environment` and we were
missing access to expression translation and correct import generation.

Overall, solution 2 is now implemented as works as expected. This commit
adds additional unit tests to ensure this.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
b18122730d refactor(compiler-cli): support emitting references in type constructor code (#53521)
For signal inputs we are looking at generating additional code inside
type constructors. This code is planned to reference an external type
from `@angular/core` to unwrap `InputSignal`'s class fields.

The existing `Environment` class contains helpers for emitting such
references / and translating them from the output AST. We extract
this logic into a superclass for only emitting references. A similar
type already existed to avoid circular dependencies- but now we have
actual use-cases to populate this as a base class.

This allows us to create more-suitable minimal emit environments
when we e.g. generate type constructors inline- which are not
part of any type check block. The existing `Environment` class is scoped
to type check blocks and therefore was not suitable.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
5f7db018eb refactor(compiler-cli): finalize transform support for signal input transforms (#53521)
Signal inputs do not need coercion members for their transforms. That is
because the `InputSignal` type- which is accessible in the class member-
already holds the type of potential "write values". This eliminates the
need for coercion members which were simply used to somehow capture this
write type (especially when libraries are consumed and only `.d.ts` is
available).

We can simplify this, and also significantlky loosen restrictions
of transform functions- given that we can fully rely on TypeScript for
inferring the type. There is no requirement in being able to
"transplant" the type into different places- hence also allowing
supporting transform functions with generics, or overloads.

In a follow-up commit, once more parts are place, there will be some
compliance tests to ensure these new "loosend restrictions".

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
0ef03a532b test(compiler-cli): initial type-check tests for signal inputs and infrastructure (#53521)
This commit ensures that the type-check diagnostic testing
infrastructure is prepared to validate signal inputs. i.e. providing the
necessary "mocks" in the fake "d.ts" of `@angular/core`.

The commit then sets up a Golang-style table driven testing environment
that allows us to validate/verify signal input type-checking in a
readable way.

With this infrastructure set up, this commit defines an initial set
of unit tests for type checking of input signals.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
8908fcd809 refactor(compiler-cli): initial type-checking for signal inputs (#53521)
This commit introduces the initial type-checking for signal inputs.
To enable type-checking od signal inputs, there are a couple of tricks
needed. It's not trivial as it would look like at first glance.

Initial attempts could have been to generate additional statements in
type-checking blocks for signal inputs to simply call a method like
`InputSignal#applyNewValue`. This would seem natural, as it would match
what will happen at runtime, but this would break the language-service
auto completion in a highly subtle way. Consider the case where multiple
directives match the same input. Consider the directives have some
overlap in accepted input values, but they also have distinct diverging
values, like:

```ts
class DirA {
  value = input<'apple'|'shared'>();
}

class DirB {
  value = input<'orange'|'shared'>();
}
```

In such cases, auto completion for the binding expression should suggest
the following values: `apple`, `shared`, `orange` and `undefined`.

The language service achieves this by getting completions in the
type-check block where the user expression would live. This BREAKS if
we'd have multiple places where the expression from the user is used.

Two different places, or more, surface additional problems with
diagnostic collection. Previously diagnostics would surface the union
type of allowed values, but with multiple places, we'd have to work with
potentially 1+ diagnostics. This is non-ideal.

Another important consideration is test coverage. It might sound
problematic to consider the existing test infrastructure as relevant,
but in practice, we have thousands of diagnostic type check block tests
that would greatly benefit if the general emit structure would still
match conceptually. This is another bonus argument on why changing the
way inputs are applied is probably an option we should consider as a
last resort.

Ultimately, there is a good solution where we unwrap directive signal
inputs, based on metadata, and access a brand type field on the
`InputSignal`. This ensures auto-completion continues to work as is, and
also the structure of type check blocks doesn't change conceptually. In
future commits we also need to handle type-inference for generic signal
inputs.

Note: Another alternative considered, in terms of using metadata or not.
We could have type helpers to unwrap signal inputs using type helpers
like: `T extends InputSignal<any, WriteT> ? WriteT : T`. This would
allow us to drop the input signal metadata dependency, but in reality,
this has a few issues:

- users might have `@Input`'s passing around `InputSignal`'s. This is
  unlikely, but shows that the solution would not be fully correct.
- we need the metadata regardless, as we plan on accessing it at runtime
  as well, to distinguish between signal inputs and normal inputs when
  applying new values. This was not clear when this option was
  considered initially.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
3e0e0b42fa refactor(compiler): emit signal input info in d.ts and generate partial compilation output (#53521)
This commit captures the metadata on whether an input is signal based or
not, in the `.d.ts` of directives and components. This exposes this
information to consumers of the directives. This is needed because
libraries may use signal inputs, and we need to know whether bound
inputs to this library are signal-based or not- so that we can generate
proper type-checking code (account for `InputSignal` or not).

Additionally, this commit introduces a new structure for the partial
compilation output of directive inputs. With the current emit, inputs
are captured in a data structure that is equivalent to the internal data
structure passed to `defineDirective` (the full compilation output).
This worked fine as we only captured a few strings, but in ends up
being a bad practice because partial compilation output should NOT
capture internal data structures that might be specific to a certian
Angular core version. Instead, we introduce a new "future proof"
structure that:

- can hold additional metadata in backwards-compatible ways, like
  `isSignal` or `isRequired`.
- can be parsed trivially using the `AstHost` for the linker, instead of
  having to unwrap/parse an array structure.

The new structure is only emitted when we discover that some inputs are
signal based (or ultimately end up configuring input flags). This is
done for backwards compatibility, so that libraries without signal
inputs remain compatible with older linker versions. In the future,
this might be the only emit.

Compliance tests for this follow in future commits, when the linker
portion is also in place. This commit specialices on the code
generation. With the linker, and compliance test infrastructure fixed
(that is broken right now), we can test the full integration.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
6b67800465 refactor(compiler-cli): foundation for propagating signal input metadata (#53521)
This commit defines the initial metadata for inputs passed around in
the compiler-cli. Inputs will now capture additional metadata on whether
they are signal-based or not. This is stored on a per-input basis as
a Zone component may contain both, signal inputs or `@Input` inputs.

The metadata is later used for type-checking, for partial output
generation, or full compilation output generation.

PR Close #53521
2023-12-13 15:44:00 -08:00
Paul Gschwendtner
bbd918acd3 refactor(core): introduce signal input() function and compiler detection (#53521)
This commit introduces a function for declaring inputs in
components. The function is called `input`. It comes in two flavors:

- `input` for optional inputs with initial values
- `input.required` for required inputs

Inputs are declared as class members, like with `@Input`- except that
the class field will no longer hold the input value directly. Angular
takes control over the input field and exposes the input value as a
signal. The runtime implementation will follow in future commits.

This commit simply introduces:

- initial compiler detection to recognize such inputs in classes
- the initial signature of `input` and `input.required`.

Note: the defer size test is flawed and there is no minification- hence
this commit also needs to incorporate the new dependency graph changes.

PR Close #53521
2023-12-13 15:44:00 -08:00
Kristiyan Kostadinov
b98d8f79ed fix(compiler): handle ambient types in input transform function (#51474)
Fixes that the compiler was throwing an error if an ambient type is used inside of an input `transform` function. The problem was that the reference emitter was trying to write a reference to the ambient type's source file which isn't necessary.

Fixes #51424.

PR Close #51474
2023-12-13 09:15:17 -08:00
Kristiyan Kostadinov
9e5456912a fix(compiler-cli): generate less type checking code in for loops (#53515)
The ops for the implicit variables in `@for` loops (e.g. `$index`) are marked as being mandatory which means that they're generated even if they aren't used. These changes make them optional so they're only added when necessary.

PR Close #53515
2023-12-12 14:30:35 -08:00
Kristiyan Kostadinov
e620b3a724 fix(compiler-cli): add compiler option to disable control flow content projection diagnostic (#53311)
These changes add an option to the `extendedDiagnostics` field that allows the check from #53190 to be disabled. This is a follow-up based on a recent discussion.

PR Close #53311
2023-12-05 17:21:16 -08:00
Kristiyan Kostadinov
d7a83f9521 fix(compiler-cli): avoid conflicts with built-in global variables in for loop blocks (#53319)
Currently we generate the following TCB for a `@for` loop:

```ts
// @for (item of items; track item) {...}

for (const item of this.items) {
  var _t1 = item;
  // Do things with `_t1`
}
```

This is problematic if the item name is the same as a global variable (e.g. `document`), because when the TCB has references to that variable (e.g. `document.createElement`), it'll find the loop initializer instead of the global variable.

These changes fix the issue by generating the following instead:

```ts
for (const _t1 of this.items) {
  // Do things with `_t1`
}
```

Fixes #53293.

PR Close #53319
2023-12-04 21:45:17 -08:00
Charles Lyding
5613051a8b fix(compiler): allow TS jsDocParsingMode host option to be programmatically set again (#53292)
When the AOT compiler creates a delegated host for a provided TypeScript CompilerHost,
it delegates functionality back to the original via a series of internal method delegations.
However, unlike other members of the CompilerHost, `jsDocParsingMode` is not a method
and cannot be delegated in this way. Attempting to call bind on the property will result
in a runtime error. Instead, `jsDocParsingMode` is now delegated via get/set accessors.
Additionally, the override of `getSourceFile` now has an updated type signature to reflect
the additional of the `jsDocParsingMode` option for the method.

This is a followup to #53126 which updates the other DelegatingCompilerHost.

PR Close #53292
2023-12-01 10:35:07 -08:00
Kristiyan Kostadinov
4c1d69e288 fix(compiler-cli): add diagnostic for control flow that prevents content projection (#53190)
This is a follow-up to the fix from #52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close #53190
2023-11-28 11:18:43 +01:00
Kristiyan Kostadinov
897391ccbd refactor(compiler-cli): expose ng-content selectors and preserveWhitespaces during template type checking (#53190)
These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close #53190
2023-11-28 11:18:43 +01:00
Charles Lyding
79ff91a813 fix(compiler): allow TS jsDocParsingMode host option to be programmatically set (#53126)
When the AOT compiler creates a delegated host for a provided TypeScript CompilerHost,
it delegates functionality back to the original via a series of internal method delegations.
However, unlike other members of the CompilerHost, `jsDocParsingMode` is not a method
and cannot be delegated in this way. Attempting to call bind on the property will result
in a runtime error. Instead, `jsDocParsingMode` is now delegated via get/set accessors.
Additionally, the override of `getSourceFile` now has an updated type signature to reflect
the additional of the `jsDocParsingMode` option for the method.

PR Close #53126
2023-11-27 12:02:11 +01:00
Kristiyan Kostadinov
c7c7ea9813 fix(core): inherit host directives (#52992)
Adds support for inheriting host directives from the parent class. This is consistent with how we inherit other features like host bindings.

Fixes #51203.

PR Close #52992
2023-11-20 13:16:15 -08:00
Kristiyan Kostadinov
f63c0f6e1c Revert "fix(compiler-cli): add diagnostic for control flow that prevents content projection (#52726)" (#53012)
This reverts commit b4d022e230.

PR Close #53012
2023-11-17 11:52:02 -08:00
Kristiyan Kostadinov
715218a7dc Revert "refactor(compiler-cli): expose ng-content selectors and preserveWhitespaces during template type checking (#52726)" (#53012)
This reverts commit 4550a81bdc.

PR Close #53012
2023-11-17 11:52:01 -08:00
Kristiyan Kostadinov
b4d022e230 fix(compiler-cli): add diagnostic for control flow that prevents content projection (#52726)
This is a follow-up to the fix from #52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close #52726
2023-11-17 08:08:41 -08:00
Kristiyan Kostadinov
4550a81bdc refactor(compiler-cli): expose ng-content selectors and preserveWhitespaces during template type checking (#52726)
These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close #52726
2023-11-17 08:08:40 -08:00
Kristiyan Kostadinov
94096c6ede feat(core): support TypeScript 5.3 (#52572)
Updates the repo to support TypeScript 5.3 and resolve any issues. Fixes include:
* Updating usages of TS compiler APIs to match their new signatures.
* In TS 5.3 negative numbers are represented as `PrefixUnaryExpression` instead of `NumericExpression`. These changes update all usages to account for it since passing a negative number into the old APIs results in a runtime error.

PR Close #52572
2023-11-09 22:56:41 +00:00