Commit graph

2053 commits

Author SHA1 Message Date
Paul Gschwendtner
810b0a7e5c refactor: add explicit types for exports relying on inferred call return type (#61312)
As part of the Bazel toolchain migration we noticed that implicit types
generated by the TypeScript compiler sometimes end up referencing types
from other packages (i.e. cross-package imports).

These imports currently work just because the Bazel `ts_library` and
`ng_module` rules automatically inserted a `<amd-module
name="@angular/x" />` into `.d.ts` of packages. This helped TS figure
out how to import a given file. Notably this is custom logic that is not
occuring in vanilla TS or Angular compilations—so we will drop this
magic as part of the toolchain cleanup!

To improve code quality and keep the existing behavior working, we are
doing the following:

- adding a lint rule that reduces the risk of such imports breaking. The
  failure scenario without the rule is that API goldens show unexpected
  diffs, and types might be duplicated in a different package!

- keeping the `<amd-module` headers, but we manually insert them into
  the package entry-points. This should ensure we don't regress
  anywhere; while we also improved general safety around this above.

Long-term, isolated declarations or a lint rule from eslint-typescript
can make this even more robust.

PR Close #61312
2025-05-13 22:45:18 +00:00
Kristiyan Kostadinov
109e49c31e refactor(compiler-cli): produce template symbols for selectorless nodes (#61240)
Updates the template type checker to produce symbols for selectorless nodes. This is necessary for integration into the language service.

PR Close #61240
2025-05-12 15:31:45 -07:00
Kristiyan Kostadinov
c439d6938d fix(compiler-cli): symbol builder duplicating host directives (#61240)
The template symbol builder works by finding the variables referring to template AST nodes with specific offsets and resolving them to directives. Afterwards it goes through the directives and resolves their host directives.

The problem is that host directives are added with the exact same offsets as their host which means they get added once initially and again when resolving host directives.

These changes resolve the issue by de-duplicating them.

PR Close #61240
2025-05-12 15:31:44 -07:00
Charles Lyding
f03ff5acf9 fix(compiler-cli): avoid fatal diagnostics for invalid module schemas (#61220)
In the event of an invalid `schemas` field for an Angular module, an
empty schema array will now be used instead of a fatal error occurring.
A build will still fail in this case with the error reported as a
diagnostic. However, for the language service, this allows the module
to exist in the compiler registry and prevents cascading diagnostics
within an IDE due to "missing" modules/components. The originating
schema related errors will still be reported in the IDE.

PR Close #61220
2025-05-12 15:28:45 -07:00
Paul Gschwendtner
10bc80a788 build: migrate compiler-cli/src/ngtsc to ts_project (#61181)
Migrates all of `compiler-cli/src/ngtsc` to `ts_project`. This change
was generated using Gemini.

PR Close #61181
2025-05-09 15:59:46 +00:00
Paul Gschwendtner
32c4792667 build: migrate compiler-cli/src/ngtsc/docs to ts_project (#61181)
Migrates `compiler-cli/src/ngtsc/docs` to `ts_project`.

PR Close #61181
2025-05-09 15:59:46 +00:00
Kristiyan Kostadinov
0558575d0a refactor(compiler-cli): do not resolve selectorless references from variables (#61158)
Based on some recent discussions, these changes remove the logic that resolves selectorless references from variables. It also updates the wording so it's clearer where selectorless references are supported.

PR Close #61158
2025-05-08 07:11:37 +02:00
Kristiyan Kostadinov
b973c5b52f refactor(compiler-cli): support selectorless in the template indexer (#61158)
Handles the new selectorless nodes when indexing a template.

PR Close #61158
2025-05-08 07:11:37 +02:00
Kristiyan Kostadinov
814e6b07ac refactor(compiler): detect directly referenced pipes during parsing (#61158)
Moves the logic to detect directly referenced pipes into the compiler so that we don't have to do it ad-hoc.

PR Close #61158
2025-05-08 07:11:37 +02:00
Kristiyan Kostadinov
bb863ee0db refactor(compiler): consolidate combined recursive visitors (#61158)
We have several cases where we need a visitor that traverses both the template and expression ASTs fully. Currently we're re-implementing the visitor each time which means that we need to update multiple visitors every time something changes.

These changes add a single base class that we can reuse to simplify such cases in the future.

PR Close #61158
2025-05-08 07:11:36 +02:00
Kristiyan Kostadinov
7d2a6b3864 refactor(compiler-cli): defer selectorless dependencies (#61100)
Fixes that selectorless dependencies weren't being deferred correctly.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
707a70ea0b refactor(compiler-cli): properly emit references to selectorless pipes (#61100)
Fixes that we weren't emitting references to selectorless pipes, because we were checking the name of the pipe, rather than the local name of the symbol.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
4a1f5db517 refactor(compiler-cli): assert that selectorless references are standalone (#61100)
Adds some logic to assert that the directives referenced in a selectorless way are all standalone.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
d5a68d8a22 refactor(compiler-cli): wire up selectorless behavior (#61100)
These changes connect the dependency analysis data from the previous commits with the template type checker which allows us to fully type check a selectorless component.

Also includes tests for all of the new selectorless behaviors that have been introduced so far.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
e15dc2c3b5 refactor(compiler-cli): add scope for selectorless components (#61100)
Adds the new `SelectorlessComponentScopeReader` that will be used for selectorless components.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
7118dac442 refactor(compiler-cli): add selectorless-related analysis to components (#61100)
Adds the logic to determine whether a component is selectorless and to track which symbols are used in the template.

PR Close #61100
2025-05-05 14:38:12 -07:00
Kristiyan Kostadinov
adaecba95f refactor(compiler-cli): split up large method (#61018)
The `ComponentHandler.resolve` method is ~500 lines and is a bit hard to follow due to some very long `if` statements. These changes split the functionality across several smaller methods to make it easier to manage.

PR Close #61018
2025-04-29 21:06:10 +00:00
Kristiyan Kostadinov
abdb6e2945 refactor(compiler): allow binder to be created without matcher (#61018)
Currently to create an `R3TargetBinder`, we have to pass some sort of directive matcher, however we also have a couple of use cases where we use the binder to do analysis that's unrelated to directives (e.g. resolving the `@defer` blocks). In these cases having to create a dummy matcher adds some slight overhead and makes the code harder to reason about since it looks like directive matching may be happening.

These changes update the `R3TargetBinder` to allow for `null` to be passed as the directive matcher.

PR Close #61018
2025-04-29 21:06:10 +00:00
Kristiyan Kostadinov
8488279659 refactor(compiler): simplify tracking of directives (#61018)
An earlier commit that introduced tracking of selectorless directives to the template binder made it so `getDirectivesOfNode` returns _all_ of the matched directives while a new method called `getOwnedDirectives` would return only the ones brought in by the specific node.

In hindsight, this is likely to cause bugs in the future, because it's unclear whether to reach for `getDirectivesOfNode` or `getOwnedDirectives`. These changes remove `getOwnedDirectives` and make it so in selectorless `getDirectivesOfNode` accepts the directive AST node itself.

Another goal of this refactor is that the TCB shouldn't have to check the `selectorlessEnabled` config option.

PR Close #61018
2025-04-29 21:06:10 +00:00
Kevin Brey
c889382a20 feat(compiler-cli): detect missing structural directive imports (#59443)
Adds a new diagnostic that ensures that a standalone component using custom structural directives in a template has the necessary imports for those directives.

Fixes #37322

PR Close #59443
2025-04-29 09:43:12 -07:00
Kristiyan Kostadinov
148f4670fe refactor(compiler-cli): generate type checking code for selectorless (#60977)
Adds the logic that will generate type checking code in the TCB for the selectorless AST nodes.

Note that we're still missing the logic that determines which symbols are available in the template and exposes them to the template binder. That will come in a future change.

PR Close #60977
2025-04-24 13:02:39 -07:00
Kristiyan Kostadinov
4b0f733cb4 refactor(compiler): add more information to template binder (#60977)
Updates the template binder to include information about directives owned by a specific component/directive node and the names of template symbols that don't exist. These will be used when generating the type check block.

PR Close #60977
2025-04-24 13:02:39 -07:00
Kristiyan Kostadinov
26cba0804f refactor(compiler-cli): pass in tag name to schema checker (#60977)
Updates the `DomSchemaChecker` to require the tag name as a string, rather than the entire DOM node. This makes selectorless a bit easier to intergrate.

PR Close #60977
2025-04-24 13:02:39 -07:00
Kristiyan Kostadinov
e89ebf3dc7 refactor(compiler-cli): add infrastructure for new diagnostics (#60977)
We need a couple of custom diagnostics for selectorless. These changes add the infrastructure so they can be reported.

PR Close #60977
2025-04-24 13:02:39 -07:00
Kristiyan Kostadinov
bc9a067ef4 refactor(compiler-cli): add flag to enable selectorless (#60977)
Adds a private flag that we can use to enable selectorless as it's being developed.

PR Close #60977
2025-04-24 13:02:39 -07:00
Matthieu Riegler
1323ab1ce5 refactor(compiler-cli): Extract type predicates from type guards. (#60934)
Previously they were extracted as `boolean`.

PR Close #60934
2025-04-23 09:56:44 +02:00
Kristiyan Kostadinov
3d86b3ade7 refactor(compiler): account for selectorless in template binder (#60952)
Updates the template binder to account for the new selectorless AST nodes. This is a prerequisite to supporting template type checking of the new syntax.

PR Close #60952
2025-04-23 09:55:54 +02:00
Kristiyan Kostadinov
6cce056d41 refactor(compiler): allow different kinds of directive matchers to be passed to binder (#60952)
Updates the target binder to allow either a selector-based or selectorless matcher to be passed in. This will allow us to skip some of the overhead when matching directives to nodes.

PR Close #60952
2025-04-23 09:55:54 +02:00
Matthieu Riegler
c990265473 docs(docs-infra): Improve doc support for pipes. (#60926)
This commits adds the pipe usage and the impure pipe mention.

PR Close #60926
2025-04-23 08:46:11 +02:00
Matthieu Riegler
1b8e7ab9fe feat(compiler): support the in keyword in Binary expression (#58432)
This commit adds the support for the `in` keyword as a relational operator, with the same precedence as the other relational operators (<,>, <=, >=)

BREAKING CHANGE: 'in' in an expression now refers to the operator

PR Close #58432
2025-04-22 21:44:12 +02:00
Matthieu Riegler
f88c747e29 refactor(compiler-cli): Don't extract constructors with no parameters (#60928)
This will prevent usesless paramters in the docs, ex: https://angular.dev/api/core/ApplicationInitStatus#constructor

This improves #60302

PR Close #60928
2025-04-22 11:59:44 +02:00
Paul Gschwendtner
abe96914f5 build: remove unnecessary feature detection that breaks with shared chunks (#60825)
Since APF can contain shared chunks, where e.g. `eetemplate` lives, the
`coreHasSymbol` check is no longer reliable. Right now it even prevents
the version range check (that is reliably working) from running because
we detect a `index.d.ts` file but simply don't find the requested
symbol in there (we don't expand exports via type checker).

PR Close #60825
2025-04-10 14:19:52 +00:00
Kristiyan Kostadinov
8744c9a165 fix(compiler-cli): ensure HMR works with different output module type (#60797)
Currently when we transpile the HMR update module, we use the project's compiler options verbatim. This appears to break down with some module types, whereas we have to use a native export.

These changes override the compiler options to ensure that the user's options don't end up breaking HMR.

Fixes #60795.

PR Close #60797
2025-04-09 11:42:55 -07:00
Enea Jahollari
7a971766dc feat(compiler): add extended diagnostic for uninvoked track function on @for blocks (#60495)
The compiler will warn devs when they haven't invoked the track function passed to track in @for blocks

PR Close #60495
2025-04-08 10:10:33 -07:00
Kristiyan Kostadinov
92c4123a74 refactor(compiler): integrate new AST nodes into visitors (#60724)
Updates the various visitors to add placeholders for the new AST nodes.

PR Close #60724
2025-04-04 11:28:47 -07:00
Andrew Kushnir
4f458a825d refactor(compiler-cli): convert scripts within packages/compiler-cli to relative imports (#60625)
This commit updates scripts within `packages/compiler-cli` to relative imports as a prep work to the upcoming infra updates.

PR Close #60625
2025-04-01 11:57:53 +00:00
Jessica Janiuk
005ad65b1f fix(core): prevent omission of deferred pipes in full compilation (#60571)
This prevents a bug where pipes would be excluded from defer dependency generation.

PR Close #60571
2025-03-31 13:11:12 +00:00
Andrew Scott
5948cd03c5 fix(compiler-cli): Produce fatal diagnostic on duplicate decorated properties (#60376)
This prevents the compilation and language service from crashing.

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

PR Close #60376
2025-03-27 20:26:42 +00:00
Kristiyan Kostadinov
1e6faad479 fix(compiler-cli): correctly parse event name in HostListener (#60561)
Fixes that we weren't accounting for targeted events when creating the AST for `@HostListener`-decorated members.

PR Close #60561
2025-03-26 20:46:45 -07:00
Kristiyan Kostadinov
7e03af898e fix(compiler-cli): set correct target when type checking events (#60561)
Currently the TCB generation code doesn't handle targeted events (e.g. `(document:click)`) which ends up binding to the current element and can have type inference implications. These changes take the event's `target` into account.

PR Close #60561
2025-03-26 20:46:45 -07:00
Paul Gschwendtner
9f18c7cc74 fix(compiler-cli): support relative imports to symbols outside rootDir (#60555)
By default, the compiler-cli uses the relative import strategy when
there is no `rootDir` or `rootDirs`. This is expected as everything is
assumed to be somehow reachable through relative imports.

With `rootDirs` that allow for a "virtual file system"-like environment,
the compiler is not necessarily able to always construct proper relative
imports. The compiler includes the `LogicalProjectStrategy` for this
reason. This strategy is able to respect `rootDirs` to construct
relative paths when possible.

This logic currently accidentally triggers when there is a `rootDir`
set. This option is not to be confused with the virtual directory
option called `rootDirs`. The compiler currently confuses this and
accidentally enters this mode when there is just a `rootDir`— breaking
in monorepos that imports can point outside the `rootDir` to e.g. other
compilation unit's `.d.ts` (which is valid; just not `.ts` sources can
live outside the root dir).

This is necessary for our Bazel toolchain migration.

PR Close #60555
2025-03-26 20:45:55 -07:00
Miles Malerba
55e9f1ffa2 refactor(compiler-cli): remove unnecessary paren logic for ternaries (#60263)
Removes logic that was explicitly adding parentheses around ternaries
used as the condition of another ternary. Instead we can just rely on
Typescript to add the parentheses if they are needed to make the code
match the structure of the AST.

Also added a note pointing to the issue that currently prevents us from
removing similar logic pertaining to nullish coalescing

PR Close #60263
2025-03-21 14:34:35 -07:00
Paul Gschwendtner
e0ff321433 build: prepare testing infrastructure for code splitting of core package (#60487)
When we switch to relative imports, shared `.d.ts` chunks can be
generated.

We need to also pull these into our mock virtual FS testing
environments. Notably this does not cause a test slow-down because we
are talking about very few extra `.d.ts` chunk files. In our experiments
before, with no dts bundling, we saw test time increase from e.g.
20seconds to 100seconds. The 20s are still the same locally!

In addition, since code for definitions can now reside in shared `.d.ts`
chunks, the language service tests need to be adjusted in cases where
they assert for code definition locations in `@angular/core`. A new
helper prepares for more code to be moved into arbitrary `.d.ts` files;
we should simply assert the definition comes out of
`node_modules/@angular/core`.

PR Close #60487
2025-03-20 12:30:57 -07:00
Kristiyan Kostadinov
29eded6457 fix(compiler-cli): report more accurate diagnostic for invalid import (#60455)
Currently when an incorrect value is in the `imports` array, we highlight the entire array which can be very noisy for large arrays. This comes up semi-regularly (at least for me) when an import is missing.

These changes add some logic that reports a more accurate diagnostic location for the most common case where the `imports` array is static. Non-static arrays will fall back to the current behavior.

PR Close #60455
2025-03-19 19:06:38 +01:00
Kristiyan Kostadinov
2d51a203dc fix(compiler-cli): wrong event name for host listener decorators (#60460)
Fixes that when generating the type checking code `@HostListener`, we were using the method name instead of the event name.

PR Close #60460
2025-03-19 19:05:44 +01:00
Miles Malerba
bec1610da2 feat(compiler-cli): add extended diagnostic for invalid nullish coalescing (#60279)
The semantics for nullish coalescing (`??`) in Javascript/Typescript
differ slightly from the semantics in Angular templates. Mixing nullish
coalescing with logical and/or without parentheses is an error in
Javascript. This PR adds an extended diagnostic that can be enabled to
treat it as an error in Angular templates as well.

PR Close #60279
2025-03-19 10:21:48 +01:00
Kristiyan Kostadinov
bf2b0253bc refactor(compiler-cli): move typeCheckHostBindings option (#60267)
Moves the `typeCheckHostBindings` option into `StrictTemplateOptions` and renames the latter.

PR Close #60267
2025-03-17 14:28:41 +01:00
Kristiyan Kostadinov
1971e57a45 feat(compiler-cli): support type checking of host bindings (#60267)
Historically Angular's type checking only extended to templates, however host bindings can contain expressions as well which can have type checking issues of their own. These changes expand the type checking infrastructure to cover the `host` object literal, `@HostBinding` decorators and `@HostListener` with full language service support coming in future commits.

Note that initially the new functionality is disabled by default and has to be enabled using the `typeCheckHostBindings` compiler flag.

PR Close #60267
2025-03-17 14:28:41 +01:00
Kristiyan Kostadinov
eb4c22e6d9 refactor(compiler-cli): enable type checking of directive classes (#60267)
Updates the directive handler to enable type checking of directives. Currently this only applies to checking of host bindings.

PR Close #60267
2025-03-17 14:28:40 +01:00
Kristiyan Kostadinov
84b3721351 refactor(compiler-cli): produce host binding type checking information for components (#60267)
Sets up the logic that produces the information necessary to type check host bindings of a component. Also introduces a compiler flag for toggling checking of host bindings.

PR Close #60267
2025-03-17 14:28:40 +01:00