Commit graph

1621 commits

Author SHA1 Message Date
Kristiyan Kostadinov
b6b5adca38 fix(compiler): account for type-only imports in defer blocks (#52343)
Fixes that `@defer` blocks didn't account for type-only imports which could cause the import to be considered as not deferrable.

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

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

Fixes #52315.

PR Close #52322
2023-10-23 09:27:23 -07:00
Payam Valadkhan
ae3acca20d refactor(compiler-cli): remove unnecessary compilationMode args (#52215)
After previous commits, some `compilationMode` args in some helper functions became unused. In this change those args are cleaned up.

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

PR Close #52215
2023-10-19 09:38:30 -07:00
Payam Valadkhan
749d4c498e refactor(compiler-cli): add local compilation logic to reflection host getConstructorParameters method (#52215)
Currently the reflection host's `getConstructorParameters` method is not aware of the compilation mode, and it generates result mainly assuming the compilation mode is full and we have access to global type info. As a result, its result is not very suitable for local compilation usage, particularly for deciding if a symbol is imported as type or not. This change plumbs a flag `isLocalCompilation` into reflection host to make it aware of the compilation mode.

Also changes made to the logic in the method `getConstructorParameters` so that in local compilation mode:
 - returns NO_VALUE_DECLARATION type value ref only if the type is a type parameter
 - returns local type value ref for any imported symbol, unless the import is type only in which case returns TYPE_ONLY_IMPORT type value ref

PR Close #52215
2023-10-19 09:38:30 -07:00
Andrew Scott
459b275a3a docs: Update control flow diagnostic to direct devs to built-ins (#52268)
The commit adds messaging to the control flow template diagnostic to direct developers to the new
built-in control flow syntax in Angular.

PR Close #52268
2023-10-19 09:24:08 -07:00
Jeremy Elbourn
634c529504 refactor(compiler): extract generic info for api reference (#52204)
This commit extracts the API reference info for generic parameters for
classes, methods, interfaces, and functions. It includes any constraints
and the default type if present.

PR Close #52204
2023-10-17 12:29:24 +02:00
Pawel Kozlowski
4da0dbedf5 Revert "refactor(compiler-cli): remove MethodIdentifier type (#49611)" (#52174)
This reverts commit c2b1a242e8.

PR Close #52174
2023-10-12 12:35:48 +02:00
Matthieu Riegler
c2b1a242e8 refactor(compiler-cli): remove MethodIdentifier type (#49611)
`MethodIdentifier` is unused as is `IdentifierKind.Method`. They both can be removed.

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

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

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

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

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

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

PR Close #52118
2023-10-10 12:40:10 -07:00
Matthieu Riegler
8eef694def feat(core): Provide a diagnostic for missing Signal invocation in template interpolation. (#49660)
To improve DX for beginners, this commit adds an extended diagnostic for Signals in template interpolations.

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

Fixes #52052.

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

Fixes #52052.

PR Close #52069
2023-10-10 09:47:47 -07:00
Kristiyan Kostadinov
503e67dca2 refactor(compiler): clean up compatibility code for old TS versions (#52099)
Cleans up some code that was left in place to support old versions of TypeScript.

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

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

PR Close #51947
2023-10-09 14:34:55 -07:00
Jeremy Elbourn
7bfe20707f feat(compiler): extract api for fn overloads and abtract classes (#52040)
This commit adds support for extracting function overloads. Interestingly, this worked in an earlier version when the code was extracting all statements in every source file, but the existing compiler API for extracting all exported declarations from an entry-point only returns the first function declaration in cases when there are overloads.

This also marks abstract classes as abstract, required inputs as required, and filters out Angular-private APIs.

PR Close #52040
2023-10-09 12:03:20 -07:00
Kristiyan Kostadinov
386e1e9500 fix(compiler): work around TypeScript bug when narrowing switch statements (#52110)
We type check `@switch` blocks by generating identical TS `switch` statements in the TCB, however TS currently has a bug where parenthesized `switch` block expressions don't narrow their types. Since we use parenthesized expressions to wrap AST nodes for diagnostics, this will bug will affect all Angular-generated `switch` statements.

These changes work around the issue by generating `if`/`else if`/`else` statements that represent the `switch`.

Some alternatives that were considered:
1. Moving the `switch` expression to a constant - this is fairly simple to implement, but it won't fully resolve the narrowing issue since the same constant will have to be used in expressions inside the different cases.
2. Removing the outer-most parenthesis from the switch expression - this works and allows us to continue using switch statements, but because we use parenthesized expressions to map diagnostics to their template locations, I wasn't sure if it won't lead to worse template dignostics.

Fixes #52077.

PR Close #52110
2023-10-09 10:54:28 -07:00
Andrew Scott
023a181ba5 feat(language-service): Implement outlining spans for control flow blocks (#52062)
This commit implements the getOutlingSpans to retrieve Angular-specific
outlining spans. At the moment, these spans are limited to control-flow
blocks in templates.

This is required for folding ranges (https://github.com/angular/vscode-ng-language-service/issues/1930)

PR Close #52062
2023-10-09 10:20:26 -07:00
Kristiyan Kostadinov
40c53577b8 refactor(compiler): introduce unknown block node (#52047)
Adds an `UnknownBlock` node to the Ivy AST to represent blocks that haven't been recognized by the compiler. This will make it easier to integrate blocks into the language service.

PR Close #52047
2023-10-05 13:10:05 -07:00
Jeremy Elbourn
a7fa25306f feat(compiler): extract api docs for interfaces (#52006)
This adds API doc extraction for interfaces, largely using the same code paths for classes. The primary difference between classes and interfaces is that classes have member _declarations_ while interfaces have member _signatures_. This largely doesn't matter for the purposes of extraction, but the types are distinct with no common base types, so we have to do a fair amount of type unioning and aliasing.

PR Close #52006
2023-10-04 11:58:09 -07:00
Kristiyan Kostadinov
1d871c03a5 fix(compiler): forward referenced dependencies not identified as deferrable (#52017)
Fixes that we weren't accounting for dependencies using `forwardRef` when determining if they can be lazy-loaded.

Fixes #52014.

PR Close #52017
2023-10-04 08:57:47 -07:00
Kristiyan Kostadinov
02edb43067 fix(compiler): narrow the type of the aliased if block expression (#51952)
Currently the TCB for aliased `if` blocks looks something like this:

```
// Markup: `@if (expr; as alias) { {{alias}} }

if (block.condition) {
  var alias = block.condition;
  "" + alias;
}
```

The problem with this approach is that the type of `alias` won't be narrowed. This is something that `NgIf` currently supports.

These changes resolve the issue by emitting the variable outside the `if` block and using the variable reference instead:

```
// Markup: `@if (expr; as alias) { {{alias}} }

var alias = block.condition;
if (alias) {
  "" + alias;
}
```

PR Close #51952
2023-10-04 08:49:59 -07:00
Kristiyan Kostadinov
0eae992c4e fix(compiler): allow nullable values in for loop block (#51997)
Updates the TCB for `@for` loop blocks to allow nullable values. The runtime already supports it and this makes it easier to switch from `NgFor`.

Fixes #51993.

PR Close #51997
2023-10-04 06:39:43 -07:00
Kristiyan Kostadinov
43e6fb0606 feat(core): enable block syntax (#51994)
Enables the new `@` block syntax by default by removing the `enabledBlockTypes` flags. There are still some internal flags that allow special use cases to opt out of the block syntax, like during XML parsing and when compiling older libraries (see #51979).

PR Close #51994
2023-10-03 15:26:05 -07:00
Kristiyan Kostadinov
23bfa10ac8 fix(compiler): add diagnostic for inaccessible deferred trigger (#51922)
If a trigger element can't be accessed from the defer block, we don't generate any instructions for it. These changes add a diagnostic that will surface the error to users.

PR Close #51922
2023-09-27 12:59:34 -07:00
Payam Valadkhan
f91f222b55 fix(compiler-cli): resolve component encapsulation enum in local compilation mode (#51848)
Currently the field encapsulation undergoes some static analysis to check if it is `ViewEncapsulation` enum. Such static check fails in local compilation mode in g3 as the symbol cannot be resolved. On the other hand this field has to be resolved statically as its value determined the generated code. So in local compilation mode we add a lighter resolving logic which relies only on local information.

PR Close #51848
2023-09-26 09:11:15 -07:00
Payam Valadkhan
377a7abfda fix(compiler-cli): bypass static resolving of the component's changeDetection field in local compilation mode (#51848)
Currently the field changeDetection undergoes some static analysis to check if it is `ChangeDetectionStrategy` enum. Such static check fails in local compilation mode in g3 as the symbol cannot be resolved. So in local compilation mode we bypass such resolving and just write the expression as is into the component definition.

PR Close #51848
2023-09-26 09:11:15 -07:00
Kristiyan Kostadinov
8be2c48b7c feat(core): implement new block syntax (#51891)
Switches the syntax for blocks from `{#block}{/block}` to `@block {}` based on the feedback from the community.

Read more about the decision-making process in our blog: https://blog.angular.io/meet-angulars-new-control-flow-a02c6eee7843

The existing block types changed in the following ways:

**Conditional blocks:**
```html
<!-- Before -->
{#if cond}
  Main content
  {:else if otherCond}
    Else if content
  {:else}
    Else content
{/if}

<!-- After -->
@if (cond) {
  Main content
} @else if (otherCond) {
  Else if content
} @else {
  Else content
}
```

**Deferred blocks**
```html
<!-- Before -->
{#defer when isLoaded}
  Main content
  {:loading} Loading...
  {:placeholder} <icon>pending</icon>
  {:error} Failed to load
{/defer}

<!-- After -->
@defer (when isLoaded) {
  Main content
} @loading {
  Loading...
} @placeholder {
  <icon>pending</icon>
} @error {
  Failed to load
}
```

**Switch blocks:**
```html
<!-- Before -->
{#switch value}
  {:case 1}
    One
  {:case 2}
    Two
  {:default}
    Default
{/switch}

<!-- After -->
@switch (value) {
  @case (1) {
    One
  }

  @case (2) {
    Two
  }

  @default {
    Default
  }
}
```

**For loops**
```html
<!-- Before -->
{#for item of items; track item}
  {{item.name}}
  {:empty} No items
{/for}

<!-- After -->
@for (item of items; track item) {
  {{item.name}}
} @empty {
  No items
}
```

PR Close #51891
2023-09-26 09:10:04 -07:00
Kristiyan Kostadinov
d6bfebe2c8 refactor(compiler): generate arrow functions for setClassMetadata calls (#51637)
Reworks the `setClassMetadata` calls to generate arrow functions instead of full anonymous function declarations. While this won't have an effect on production bundle sizes, it's easier to read and it should lead to small parsing time gains in dev mode.

PR Close #51637
2023-09-25 09:27:26 -07:00
Kristiyan Kostadinov
7cce28ae9c refactor(compiler): extract deferred block trigger information (#51830)
Reworks the compiler to use the API introduced in #51816 to match triggers to the element nodes they point to. This will be used to generate the new instructions for `on interaction` and `prefetch on interaction`.

PR Close #51830
2023-09-22 12:17:54 -07:00
Jeremy Elbourn
34495b3533 feat(compiler): extract docs via exports (#51828)
So far this docs extraction has pulls API info from all exported symbols in the program. This commit changes to extracting only symbols that are exported via a specified entry-point. This commit also exports the docs entities through the compiler-cli `index.ts`.

PR Close #51828
2023-09-20 18:34:55 +02:00
Payam Valadkhan
5b66330329 fix(compiler-cli): allow non-array imports for standalone component in local compilation mode (#51819)
Currently the compiler in local mode assumes that the standalone component imports are array expressions. This is not always true as they can be const variables as well. This change allow non-array expressions for standalone component imports field and passes that expression to the downstream tools such as deps tracker to compute the component's deps in runtime.

PR Close #51819
2023-09-20 12:24:54 +02:00
Payam Valadkhan
19c3dc18d3 fix(compiler-cli): fix NgModule injector def in local compilation mode when imports/exports are non-array expressions (#51819)
Current implementation assumes that NgModule imports/exports fields are always arrays and thus it concats them for the injector definition. But this is not always the case and imports/exports could be non-arrays such as const variable. Such pattern happens in g3 and so must be addressed.

PR Close #51819
2023-09-20 12:24:54 +02:00
Kristiyan Kostadinov
14d89d79ba refactor(compiler): implement template type checking for tracking expressions (#51690)
Adds support for template type checking of the `track` expression of a `for` loop block. Tracking expressions are treated as any other expression for type checking, however we have some special validation that doesn't allow them to access template variables and local references.

PR Close #51690
2023-09-20 11:26:05 +02:00
Kristiyan Kostadinov
aaa597393d refactor(compiler): implement template type checking for loop blocks (#51690)
Adds support for template type checking inside `for` blocks. It is implemented by generating a JS `for...of` statement inside the TCB. The various loop variables (e.g. `$index`) are implemented by declaring a local number variable.

PR Close #51690
2023-09-20 11:26:05 +02:00
Kristiyan Kostadinov
d42e02333a refactor(compiler): implement template type checking for if blocks (#51690)
Adds support for template type checking inside `if` blocks. It is implemented by generating a JS `if` statement inside the TCB which allows us to do type narrowing of the expression. The `as` parameter is implemented by declaring a variable inside the `if` statement.

PR Close #51690
2023-09-20 11:26:05 +02:00
Kristiyan Kostadinov
29ced9b066 refactor(compiler): implement template type checking for switch blocks (#51690)
Adds support for template type checking inside `switch` blocks. It is implemented by generating a JS `switch` statement inside the TCB which allows us to do type narrowing of the expression.

PR Close #51690
2023-09-20 11:26:05 +02:00
Kristiyan Kostadinov
8c10ba1a38 refactor(compiler): update binder to account for new semantics (#51816)
When the `TargetBinder` was written, the only embedded-view-based nodes were templates, but now we have `{#if}`, `{#switch}` and `{#defer}` which have similar semantics. These changes rework the binder to account for the new nodes.

PR Close #51816
2023-09-19 12:16:00 +02:00
Kristiyan Kostadinov
e23aaa7d75 feat(core): drop support for older TypeScript versions (#51792)
Drops support for versions of TypeScript older than 5.2

BREAKING CHANGE:
Versions of TypeScript older than 5.2 are no longer supported.

PR Close #51792
2023-09-19 12:04:09 +02:00
Jeremy Elbourn
2e41488296 feat(compiler): extract docs info for enums, pipes, and NgModules (#51733)
Based on top of #51717

This commit adds extraction for enums, pipes, and NgModules. It also adds a couple of tests for JsDoc extraction that weren't covered in the previous commit.

PR Close #51733
2023-09-18 12:29:30 +02:00
Jeremy Elbourn
e0b1bb33d7 feat(compiler): extract doc info for JsDoc (#51733)
Based on top of #51713

This commit adds docs extraction for information provided in JsDoc comments, including descriptions and Jsdoc tags.

PR Close #51733
2023-09-18 12:29:28 +02:00
Jeremy Elbourn
a24ae994a0 feat(compiler): extract docs for top level functions and consts (#51733)
Based on top of ##51700

Also updates extraction to ignore un-exported statements.

PR Close #51733
2023-09-18 12:29:26 +02:00
Jeremy Elbourn
b9c70158ab feat(compiler): extract docs for accessors, rest params, and types (#51733)
Based on top of #51697

Adds extraction for accessors (getters/setters), rest params, and resolved type info for everything so far. This also refactors function extraction into a new class and splits tests for common class info and directive info into separate files.

PR Close #51733
2023-09-18 12:29:24 +02:00
Jeremy Elbourn
c7daf7ea16 feat(compiler): extract directive docs info (#51733)
Based on top of #51685

This expands on the extraction with information for directives, including inputs and outputs. As part of this change, I've refactored the extraction code related to class and to directives into their own extractor classes to more cleanly separate extraction logic based on type of statement.

PR Close #51733
2023-09-18 12:29:22 +02:00
Jeremy Elbourn
7f6d9a73ab feat(compiler): expand class api doc extraction (#51733)
Based on top of #51682

This expands on the skeleton previously added to extract docs info for classes, including properties, methods, and method parameters. Type information and Angular-specific info (e.g. inputs) will come in future PRs.

PR Close #51733
2023-09-18 12:29:20 +02:00
Jeremy Elbourn
7e82df45c5 feat(compiler): initial skeleton for API doc extraction (#51733)
This commit adds a barebones skeleton for extracting information to be used for extracting info that can be used for API reference generation. Subsequent PRs will expand on this with increasingly real extraction. I started with @alxhub's #51615 and very slightly polished to get to this minimal commit.

PR Close #51733
2023-09-18 12:29:19 +02:00