Commit graph

1587 commits

Author SHA1 Message Date
Kristiyan Kostadinov
f280467398 fix(compiler-cli): account for multiple generated namespace imports in HMR (#58924)
The current HMR compiler assumes that there will only be one namespace import in the generated code (`@angular/core`). This is incorrect, because the compiler may need to generate additional imports in some cases (e.g. importing directives through a module). These changes adjust the compiler to capture all the namespaces in an array and pass them along.

Fixes #58915.

PR Close #58924
2024-11-28 10:00:56 +01:00
Jessica Janiuk
38fe180d34 refactor(compiler): Adds ingest and flags for defer details (#58833)
This adds TDeferDetailsFlags to indicate the presence of hydration triggers, and any future flags we add to defer.

PR Close #58833
2024-11-27 17:00:05 +01:00
Kristiyan Kostadinov
4e9244990a fix(compiler-cli): more accurate diagnostics for host binding parser errors (#58870)
Currently host bindings are in a bit of a weird state, because their source spans all point to the root object literal, rather than the individual expression. This is tricky to handle at the moment, because the object is being passed around as a `Record<string, string>` since the compiler needs to support both JIT and non-JIT environments, and because the AOT compiler evaluates the entire literal rather than doing it expression-by-expression. As a result, when we report errors in one of the host bindings, we end up highlighting the entire expression which can be very noisy in an IDE.

These changes aim to report a more accurate error for the most common case where the `host` object is initialized to a `string -> string` object literal by matching the failing expression to one of the property initializers. Note that this isn't 100% reliable, because we can't map cases like `host: SOME_CONST`, but it's still better than the current setup.

PR Close #58870
2024-11-25 15:25:48 +00:00
Kristiyan Kostadinov
c421ffdbfb fix(compiler): control flow nodes with root at the end projected incorrectly (#58607)
Fixes an edge case where a control flow node that has non-projectable nodes followed by an element node at the end would cause the entire control flow node to be project. For example if we have a projection target of `Main: <ng-content/> Slot: <ng-content select="[foo]"/>`, inserting a node of `@if (true) {Hello <span foo>world</span>}` would project the entire `Hello world` into the `[foo]` slot.

In the process of working on the issue, I also found that `@let` declarations at the root of the control flow node would prevent content projection as well.

PR Close #58607
2024-11-12 18:05:00 +00:00
Kristiyan Kostadinov
086cb2c40e fix(compiler-cli): report individual diagnostics for unused imports (#58589)
Initially the unused imports check was implemented so that it reports one diagnostic per component with the individual unused imports being highlighted through the `relatedInformation`. This works fine when reporting errors to the command line, but vscode appears to only show `relatedInformation` when the user hovers over a diagnostic which is a sub-par experience.

These changes switch to reporting a diagnostic for each unused import instead.

PR Close #58589
2024-11-11 15:31:00 +00:00
Alan Agius
d271c4422a fix(compiler-cli): correct extraction of generics from type aliases (#58548)
**Before:**
```ts
type HttpEvent = | HttpSentEvent
  | HttpHeaderResponse
  | HttpResponse<T>
  | HttpProgressEvent
  | HttpUserEvent<T>
```

**After:**
```ts
type HttpEvent<T> = | HttpSentEvent
  | HttpHeaderResponse
  | HttpResponse<T>
  | HttpProgressEvent
  | HttpUserEvent<T>
```

PR Close #58548
2024-11-08 17:15:06 +00:00
Alan Agius
ade9ba789c refactor(compiler-cli): Improved filterMethodOverloads to Include Members without body (#58445)
Previously, `filterMethodOverloads` excluded all members without a body, causing issues with the extraction of functions and members in TypeScript types.

PR Close #58445
2024-11-07 21:28:54 +00:00
Paul Gschwendtner
6338cb7b3b fix(compiler-cli): do not fail fatal when references to non-existent module are discovered (#58515)
Currently when application source code references e.g. an NgModule that
points to references that aren't available, the compiler will break at
runtime without any actionable/reasonable error.

This could happen for example when a library is referenced in code, but
the library is simply not available in the `node_modules`. Clearly,
TypeScript would issue import diagnostics here, but the Angular compiler
shouldn't break fatally. This is useful for migrations which may run
against projects which aren't fully compilable. The compiler handles
this fine in all cases, except when processing `.d.ts` currently... and
the runtime exception invalides all other information of the program
etc.

This commit fixes this by degrading such unexpected cases for `.d.ts`
metadata reading to be handled gracefully. This matches existing logic
where the `.d.ts` doesn't necessarily match the "expecation"/"expected
format".

The worst case is that the Angular compiler will not have type
information for some directives of e.g. a library that just isn't
installed in local `node_modules`; compared to magical errors and
unexpected runtime behavior.

PR Close #58515
2024-11-06 13:04:22 +01:00
Kristiyan Kostadinov
7d0ba0cac8 refactor(compiler): trigger hmr load on initialization (#58465)
Adjusts the HMR initialization to avoid the edge case where a developer makes change to a non-rendered component that exists in a lazy loaded chunk that has not been loaded yet. The changes include:
* Moving the `import` statement out into a separate function.
* Adding a null check for `d.default` before calling `replaceMEtadata`.
* Triggering the `import` callback eagerly on initialization.

Example of the new generated code:

```js
(() => {
  function Cmp_HmrLoad(t) {
    import(
      /* @vite-ignore */ "/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t)
    ).then((m) => m.default && i0.ɵɵreplaceMetadata(Cmp, m.default, [/* Dependencies go here */]));
  }
  (typeof ngDevMode === "undefined" || ngDevMode) && Cmp_HmrLoad(Date.now());
  (typeof ngDevMode === "undefined" || ngDevMode) &&
    import.meta.hot &&
    import.meta.hot.on("angular:component-update", (d) => {
      if (d.id === "test.ts%40Cmp") {
        Cmp_HmrLoad(d.timestamp);
      }
    });
})();
```

PR Close #58465
2024-11-01 19:14:16 +00:00
Kristiyan Kostadinov
c0738c90c3 fix(compiler-cli): make the unused imports diagnostic easier to read (#58468)
The unused imports diagnostic reports once on the entire initializer and then again once per unused imports. This ends up being a bit hard to follow, because in a lot of cases the code snippet looks identical.

These changes switch to highlighting the `imports:` part of the property declaration and only highlighting the unused imports without a message.

PR Close #58468
2024-11-01 14:33:55 +00:00
Kristiyan Kostadinov
21adbba784 fix(compiler): avoid having to duplicate core environment (#58444)
Switches to referencing the core environment directly in the generated code, instead of having to duplicate it.

PR Close #58444
2024-11-01 14:32:57 +00:00
Kristiyan Kostadinov
2f9af914b7 fix(compiler-cli): disable standalone by default on older versions of Angular (#58405)
Disables the standalone by default behavior in the compiler when running against and older version of Angular. This is necessary, because the language service may be using the latest version of the compiler against and older version of core in a particular workspace.

PR Close #58405
2024-10-30 09:23:12 -07:00
Alan Agius
7de7c52769 build: remove usages of useDefineForClassFields: false (#58297)
When setting `"useDefineForClassFields": false`, static fields are compiled within a block that relies on the `this` context. This output makes it more difficult for bundlers to treeshake and eliminate unused code.

PR Close #58297
2024-10-28 12:26:05 -07:00
Matthieu Riegler
5d9cc8f408 refactor(core): remove the standalone feature (#58288)
By removing the standalone feature, we reduce the amount of code generated for components but at the cost of including the `StandaloneService` in the main bundle even if no standalone components are included in it.

PR Close #58288
2024-10-24 16:19:02 -07:00
Matthieu Riegler
bc9ef72fae refactor(compiler): update compliance tests. (#58238)
`standalone: true` is now the default at runtime.

PR Close #58238
2024-10-24 12:44:12 -07:00
Kristiyan Kostadinov
d0c74f3891 fix(compiler-cli): report when NgModule imports or exports itself (#58231)
Reports a diagnostic if an NgModule refers to itself in its `imports` or `exports`. Previously the compiler would go into an infinite loop.

Fixes #58224.

PR Close #58231
2024-10-18 09:26:44 +00:00
Kristiyan Kostadinov
e5a6165b54 test(compiler-cli): fix broken test (#58217)
Fixes a test that broke because a pipe wasn't marked explicitly as `standalone: false`.

PR Close #58217
2024-10-16 09:47:10 +00:00
Matthieu Riegler
48bfb879c5 refactor(compiler-cli): Don't extract abstract overload multiple times (#57707)
Prior to this commit, each abstract method that was overloaded was extracted. With this commit it will be extracted only once. Every overload was and still will be supported by the signatures.

fixes #57693

PR Close #57707
2024-10-16 07:31:48 +00:00
Matthieu Riegler
0c9d721ac1 feat(compiler): add support for the typeof keyword in template expressions. (#58183)
This commit adds the support for `typeof` in template expressions like interpolation, bindings, control flow blocks etc.

PR Close #58183
2024-10-16 07:31:00 +00:00
Kristiyan Kostadinov
231e6ff6ca feat(compiler-cli): generate the HMR replacement module (#58205)
Adds the ability to generate the function that replaces the component's metadata during HMR. The HMR update module is a function that is loaded dynamically and as such it has some special considerations:
* It isn't bundled, because doing so will result in multiple version of core.
* Since it isn't bundled, all dependencies have to be passed in as parameters. These changes include some special logic to determine and output those dependencies.
* While HMR is enabled, we have to disable the functionality that generates dynamic imports and drop the dependencies inside `@defer` blocks, because we need to retain the ability to refer to them in case they're needed inside the HMR update function.
* The function is returned by the `NgCompiler` as a string for the CLI's sake.

PR Close #58205
2024-10-16 07:22:45 +00:00
Matthieu Riegler
7ed566524d refactor(compiler-cli): Migrate manually ngtsc tests to standalone by default (#58169)
This commit is part of the migration to standalone by default.

PR Close #58169
2024-10-15 16:05:14 +00:00
Matthieu Riegler
b631bb4111 refactor(compiler-cli): Update Golden partials (#58169)
After flipping the standalone default value, this is reflected in the partial output result.

PR Close #58169
2024-10-15 16:05:14 +00:00
Charles Lyding
bbca205d5e refactor(compiler): adjust HMR initializer block for improved Vite support (#58173)
For the HMR initializer block to support being used in a Vite setup with
import analysis, the import call expression needs to be a runtime generated
value and include the `@vite-ignore` special comment. Without the first,
Vite will error prior to loading the application. Without the second, a
warning will be shown for each import which is effectively each component
within the application when HMR is enabled.

PR Close #58173
2024-10-14 15:21:40 +00:00
Matthieu Riegler
c42759b7a0 refactor(compiler-cli): Update compliance golden partials (#58160)
This commit is part of the update to standalone by default

PR Close #58160
2024-10-14 14:58:58 +00:00
Matthieu Riegler
7aa3097ab6 refactor(compiler-cli): Migrate manually ngtsc tests to standalone by default (#58160)
This commit is part of the migration to standalone by default.

PR Close #58160
2024-10-14 14:58:58 +00:00
Matthieu Riegler
09df589e31 refactor(core): Migrate all packages with the explicit-standalone-flag schematic. (#58160)
All components, directives and pipes will now use standalone as default.
Non-standalone decorators have now `standalone: false`.

PR Close #58160
2024-10-14 14:58:57 +00:00
Kristiyan Kostadinov
fb44323c51 fix(compiler-cli): incorrectly generating relative file paths on case-insensitive platforms (#58150)
We're using `path.relative` to compute a relative path between a `SourceFile` and the one of the `rootDirs`. The problem is that the `rootDirs` get passed through `getCanonicalFileName` which lowercases the path in some platforms, while `SourceFile.fileName` is always case-insensitive. This yields a path outside of the project which we were ignoring.

This change passes the `SourceFile.fileName` before passing it through `path.relative` to ensure that we get a valid result.

PR Close #58150
2024-10-11 07:03:15 +00:00
Kristiyan Kostadinov
852c042520 refactor(compiler-cli): output HMR initializer code (#58150)
Adds the logic to the compiler that will output the HMR initializer code for each component, if enabled.

PR Close #58150
2024-10-11 07:03:15 +00:00
Dylan Hunn
09f589f000 fix(compiler): this.a should always refer to class property a (#55183)
Consider a template with a context variable `a`:
```
<ng-template let-a>{{this.a}}</ng-template>
```

t push -fAn interpolation inside that template to `this.a` should intuitively read the class variable `a`. However, today, it refers to the context variable `a`, both in the TCB and the generated code.

In this commit, the above interpolation now refers to the class field `a`.

BREAKING CHANGE: `this.foo` property reads no longer refer to template context variables. If you intended to read the template variable, do not use `this.`.
Fixes #55115

PR Close #55183
2024-10-08 16:02:18 +00:00
Kristiyan Kostadinov
8d8c03abc4 fix(compiler-cli): defer symbols only used in types (#58104)
Currently we don't defer any symbols that have references outside of the `import` statement and the `imports` array. This is a bit too aggressive, because it's possible that the symbol is only used for types (e.g. `viewChild<SomeCmp>('ref')`) which will be stripped when emitting to JS.

These changes expand the logic so that references inside type nodes aren't considered.

**Note:** one special case is when the symbol used in constructor-based DI (e.g. `constructor(someCmp: SomeCmp)`, because these constructors will be compiled to `directiveInject` calls. We don't need to worry about them, because the compiler introduces an addition `import * as i1 from './some-cmp';` import that it uses to refer to the symbol.

Fixes #55991.

PR Close #58104
2024-10-07 08:26:07 -07:00
Charles Lyding
d9dc41a18c test(compiler-cli): add compliance tests for external runtime styles (#57613)
To ensure that the external runtime style component feature is correctly
emitted by the Angular compiler, compliance tests have been added for
file-based component styles. Additionally, the partial golden generator
has been updated to work with file-based component styles.

PR Close #57613
2024-10-07 08:20:22 -07:00
Kristiyan Kostadinov
33fe252c58 fix(compiler-cli): do not report unused declarations coming from an imported array (#57940)
Some apps follow a pattern where they have an array of common declarations which is imported in most standalone components, but only some of the declarations are used. Such cases will currently raise the unused imports diagnostic but can be hard to fix, because it would require either removing declarations from the common array which can break other components, or copying only the necessary declarations from the array. Since neither of these solutions is great, this commit tweaks the logic for the diagnostic so that unused imports coming from _exported_ arrays are not reported (either from the same file or another one).

PR Close #57940
2024-09-30 13:27:29 -07:00
Alex Rickabaugh
d9687f43dd feat(compiler-cli): 'strictStandalone' flag enforces standalone (#57935)
Add the `strictStandalone` flag to `angularCompilerOptions`. When set to
true, the compiler will require that all declarations of components,
directive, and pipes be standalone. When `standalone: false` is provided,
an error is raised.

Note that until the default value of the standalone flag is flipped, this
does not catch the case where a declaration does not specify a value for
`standalone`.

The default value of the `strictStandalone` flag is `false`.

PR Close #57935
2024-09-26 14:22:24 -07:00
Joey Perrott
9dbe6fc18b refactor: update license text to point to angular.dev (#57901)
Update license text to point to angular.dev instead of angular.io

PR Close #57901
2024-09-24 15:33:00 +02:00
Kristiyan Kostadinov
39098f3a9b refactor(compiler): finalize hydrate syntax (#57831)
Finalizes compiler implementation of the new `hydrate` triggers by:
* Reworking the logic that was depending on the `hydrateSpan` to distinguish hydrate triggers from non-hydrate triggers.
* Fixing that the `hydrate when` trigger didn't have a `hydrateSpan`.
* Adding an error if a parameter is passed into a `hydrate` trigger.
* Add an error if other `hydrate` triggers are used with `hydrate never`.
* Replacing the `prefetch` and `hydrate` flags in the template pipeline with a `modifiers` field.
* Fixing an error that was being thrown when reifying `hydrate` triggers in the pipeline.
* Adding quick info support for the `hydrate` keyword in the language service.
* Adding some tests for the new logic.

PR Close #57831
2024-09-17 11:05:17 +02:00
Jessica Janiuk
79b54bba9c refactor(compiler): initial integration of hydrate triggers into the compiler (#57831)
Sets up the AST for hydrate triggers.

PR Close #57831
2024-09-17 11:05:17 +02:00
Matthieu Riegler
1698bd3ed8 refactor(compiler-cli): Do extract symbols from private modules. (#57611)
Modules like `core/primitives` are considered private and their symbols shouldn't be exposed nor linked in the docs.

PR Close #57611
2024-09-06 14:15:18 +00:00
Kristiyan Kostadinov
a2e4ee0cb3 feat(compiler): add diagnostic for unused standalone imports (#57605)
Adds a new diagnostic that will report cases where a declaration is in the `imports` array, but isn't being used anywhere. The diagnostic is reported as a warning by default and can be controlled using the following option in the tsconfig:

```
{
  "angularCompilerOptions": {
    "extendedDiagnostics": {
      "checks": {
        "unusedStandaloneImports": "suppress"
      }
    }
  }
}
```

**Note:** I'll look into a codefix for the language service in a follow-up.

Fixes #46766.

PR Close #57605
2024-09-03 14:30:56 -07:00
Matthieu Riegler
39b7be8588 refactor(compiler-cli): Add support for inheritance in API extraction (#57588)
This commit adds the `extends` and `implements` properties to the `ClassEntry` & `InterfaceEntry`

PR Close #57588
2024-09-03 07:47:24 -07:00
Andrew Kushnir
001f319865 Revert "refactor(compiler-cli): extract function overload separatly (#56489)" (#57594)
This reverts commit 37b88a5a98.

PR Close #57594
2024-08-29 16:21:46 -07:00
Matthieu Riegler
ea3d37687b refactor(compiler-cli): Add a map of every symbols used inside a package (#57346)
This commit changes the structure of the API extraction files to include all symbols used inside a package.

The structure is a `Map`, Symbol => package
eg: 'ApplicationRef' => '@angular/core'

PR Close #57346
2024-08-29 13:39:58 -07:00
Matthieu Riegler
37b88a5a98 refactor(compiler-cli): extract function overload separatly (#56489)
in order for the docs to process function entry, this commit refactor function extraction by keeping the implementation as a the default entry and adds all the overloads into a separate array of entries.

PR Close #56489
2024-08-29 07:49:38 -07:00
Kristiyan Kostadinov
b063468027 feat(core): support TypeScript 5.6 (#57424)
Updates the repo to add support for the upcoming TypeScript 5.6.

PR Close #57424
2024-08-19 22:45:45 -07:00
Matthieu Riegler
04911d7599 docs(docs-infra): Add support for function/method overloads (#57255)
PR Close #57255
2024-08-13 12:09:01 -07:00
Kristiyan Kostadinov
d9d68e73d2 fix(compiler): reduce chance of conflicts between generated factory and local variables (#57181)
Currently we use some short variable names like `t` and `r` in the generated factory functions. They can conflict with local symbols with the same names, if they're used for DI.

These changes rename the parameters to reduce the change for conflicts.

Fixes #57168.

PR Close #57181
2024-08-07 17:25:05 +00:00
Jessica Janiuk
d73a3741a2 Revert "fix(compiler): reduce chance of conflicts between generated factory and local variables (#57181)" (#57230)
This reverts commit 67e09404db.

PR Close #57230
2024-08-01 19:33:04 +00:00
Paul Gschwendtner
e11c0c42d2 fix(compiler-cli): run JIT transforms on @NgModule classes with jit: true (#57212)
This commit is similar to 98ed5b609e, and
makes use of the preparation work implemented there.

Similar to directives and components marked via `jit: true`, we also
need to do the same for JIT marked `@NgModule` classes. This is mostly
important for downleveling of decorators to support dependency injection
of such classes.

Inside Google3, migrating from `ts_library` to `ng_module` turns of
decorator downleveling, so the `jit: true` for NgModule's is implicitly
requesting/reliant on this transform— as expected.

PR Close #57212
2024-07-31 14:14:14 +00:00
Kristiyan Kostadinov
67e09404db fix(compiler): reduce chance of conflicts between generated factory and local variables (#57181)
Currently we use some short variable names like `t` and `r` in the generated factory functions. They can conflict with local symbols with the same names, if they're used for DI.

These changes add a `ɵ` to the generated variables to reduce the chance of conflicts.

Fixes #57168.

PR Close #57181
2024-07-29 13:46:48 -07:00
Kristiyan Kostadinov
d4ff6bc0b2 fix(compiler-cli): add warning for unused let declarations (#57033)
Adds a new extended diagnostic that will flag `@let` declarations that aren't used within the template. The diagnostic can be turned off through the `extendedDiagnostics` compiler option.

PR Close #57033
2024-07-23 08:27:17 -07:00
Andrew Scott
4ac39aeea9 Revert "fix(compiler-cli): add warning for unused let declarations (#57033)" (#57088)
This reverts commit c76b440ac0.

PR Close #57088
2024-07-22 15:28:03 -07:00