Commit graph

328 commits

Author SHA1 Message Date
Dylan Hunn
b40f1e5b86 refactor(compiler): Remove deep imports in the language service (#54695)
Previously, the language service relied on deep imports such as `@angular/compiler/render3/...`. This is bad form, because that creates a dependency on the package's internal structure. Additionally, this is not compatible with google3.

In this PR, I replace all the deep imports with shallow imports, in some cases adding the missing symbol to the `compiler.ts` exports.

PR Close #54695
2024-03-05 10:28:45 +01:00
Kristiyan Kostadinov
981c28c15f refactor(compiler-cli): do not emit signal unwrap calls in versions older than 17.2 (#54423)
In order to allow both signals and non-signals in two-way bindings, we have to pass the expression through `ɵunwrapWritableSignal`. The problem is that the language service uses a bundled compiler that is fairly new, but it may be compiling an older version of Angular that doesn't expose `ɵunwrapWritableSignal` (see https://github.com/angular/vscode-ng-language-service/issues/2001).

These changes add a `_angularCoreVersion` flag to the compiler which the language service can use to pass the parsed Angular version to the compiler which can then decide whether to emit the function.

PR Close #54423
2024-02-13 15:53:42 -08: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
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
Andrew Scott
abdbdf787e refactor(language-service): Add flag to allow disabling block syntax parsing (#52691)
This commit adds a flag to the language service config options to disable block
parsing in the compiler.

PR Close #52691
2023-11-08 10:25:22 -08:00
Andrew Scott
c3d60ff603 refactor(language-service): Improve autocomplete snippet of for block (#52405)
The `for` block has several parts which we know are required. This
commit improves the autocomplete snippet of the `for` block by adding
those required parts and providing placeholders.

PR Close #52405
2023-10-31 13:08:10 -07:00
Andrew Scott
bf5bda448f refactor(language-service): Add quick info for built in control flow/blocks (#52386)
Adds hover info for:

* Defer blocks
* Triggers and trigger behavior keywords
* For loop empty block
* Track keyword in for loop block

resolves https://github.com/angular/vscode-ng-language-service/issues/1946

PR Close #52386
2023-10-31 12:56:45 -07:00
Dylan Hunn
e6affeff61 fix(language-service): Autocomplete block keywords in more cases (#52198)
Previously, autocompletions were not available in two main cases. We correct them.

1. Autocompletions immediately after `@` were usually not working, for example `foo @|`. We fix this by causing the lexer to not consider the `@` part of the text node.
2. Autocompletions such as `@\nfoo`, where a newline follows a bare `@`, were not working because the language service visitor considered us inside the subsequent text node. We fix this by adding a block name span for the block keyword, and special-case whether we are completing inside the name span. If we are, we don't continue to the following text node.

PR Close #52198
2023-10-23 12:01:52 -07:00
Dylan Hunn
449830f24e feat(language-service): Complete inside @switch (#52153)
We now visit the unknown nodes inside a `@switch` block, enabling completions in that context.

PR Close #52153
2023-10-11 09:14:13 -07:00
Dylan Hunn
7c052bb6ef feat(language-service): Support autocompletion for blocks (#52121)
This commit introduces basic autocompletion support for the new block keywords. After typing `@`, the language service suggests the various block names.

PR Close #52121
2023-10-10 10:29:23 -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
Dylan Hunn
04169e15d0 refactor(language-service): Prepare to support blocks in the langauge service (#52038)
Two key refactors to enable deeper language service support for blocks:

(1) We now generate accurate source spans for the various block types. Additionally, all the top-level source spans for a block are now *inclusive* of all the connected or descending blocks. This helps the language service visit connected blocks.

(2) The language service's template visitor was previously skipping over the AST nodes corresponding to several block types. We are now careful to visit all such nodes.

PR Close #52038
2023-10-05 14:35:00 -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
Andrew Scott
08482f2c7d fix(language-service): Retain correct language service when ts.Project reloads (#51912)
When the `ts.Project` creates the language service plugin (in this case,
the Angular Language Service), it sets the project's language service to
the new language service returned by the plugin create:
https://sourcegraph.com/github.com/microsoft/TypeScript@b12af0fa2bbd4b015e59adcfb49988cea7f919a1/-/blob/src/server/project.ts?L2035-2044

The project may be reloaded in response to various events, such as a
change to the tsconfig file, which then recreates the plugin. When this
happens, the language service that gets passed to the plugin `create`
function will not be the typescript language service, but rather the
previous instance of the new language service returned by the last call
to `create`.

This commit ensures that subsequent calls to `create` for the
`NgLanguageService` plugin for a project after the first call are able
to retrieve and hold on to the _TypeScript_ language service.

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

PR Close #51912
2023-10-04 11:27:43 -07: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
Andrew Scott
e2416a284f feat(language-service): Enable go to definition of styleUrl (#51746)
This commit enables 'go to definition' action for the new 'styleUrl' property

PR Close #51746
2023-09-18 10:37:19 +02:00
Kristiyan Kostadinov
52cc7f839b build: align with internal tsconfig options (#51728)
Currently internally Angular has some customized tsconfig files, because we don't align with the tsconfig of the rest of g3. These changes enable `noImplicitReturns` and `noPropertyAccessFromIndexSignature` to align better with the internal config.

PR Close #51728
2023-09-12 11:39:42 -07:00
Joey Perrott
88b1575271 fix(language-service): correct incomplete escaping (#51557)
Correct incomplete escaping and replace all instances of $

PR Close #51557
2023-08-29 19:48:25 +00:00
Kristiyan Kostadinov
36b180ade4 refactor(compiler): implement conditional block AST (#51299)
Adds the AST for `if`, `else if` and `else` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Kristiyan Kostadinov
4424920f0b refactor(compiler): implement for block AST (#51299)
Adds the AST for `for` and `empty` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Kristiyan Kostadinov
31c6c5e944 refactor(compiler): implement switch block AST (#51299)
Adds the AST for `switch`, `case` and `default` blocks.

PR Close #51299
2023-08-10 13:48:55 -07:00
Kristiyan Kostadinov
d11548f2ef refactor(compiler): store deferred triggers as a map (#51262)
Stores the `deferred` block triggers as a map instead of an array, because triggers can't be duplicated and because having to search through an array will be inconvenient later on.

I've also added a `DeferredBlock.visitAll` method to deduplicate the logic from the various visitor implementations.

PR Close #51262
2023-08-04 11:27:39 -04:00
Matthieu Riegler
a871e23857 docs: remove duplicate words. (#51215)
Using the `\b(\w+)\s+\1\b` we can find duplicate word. Let's remove them.

PR Close #51215
2023-08-01 12:08:33 -07:00
Kristiyan Kostadinov
9e61616ffe refactor(compiler): introduce deferred block AST (#51050)
Adds the logic to create `defer`-specific AST nodes from the generic HTML `BlockGroup` and `Block`. The logic for parsing the triggers will be in the next commit.

PR Close #51050
2023-07-17 21:05:47 +00:00
aanchal
24c8e4e753 docs: removed duplicated words (#50648)
PR Close #50648
2023-06-12 11:43:42 +02:00
Kristiyan Kostadinov
cf15c290f0 build: update to TypeScript 5.1 final (#50548)
Bumps us up to the final version of TypeScript 5.1 which dropped overnight.

PR Close #50548
2023-06-02 14:06:46 -07:00
Kristiyan Kostadinov
be97c87023 refactor(compiler): required inputs prerequisite refactors (#49333)
Based on the discussion in https://github.com/angular/angular/pull/49304#discussion_r1124732608. Reworks the compiler internals to allow for additional information about inputs to be stored. This is a prerequisite for required inputs.

PR Close #49333
2023-03-14 09:27:49 -07:00
Andrew Kushnir
83a6e203e3 refactor(compiler): drop obsolete NgFactory and NgSummary config options (#48268)
The options to generate NgFactory and NgSummary files were added to Ivy for backwards compatibility with ViewEngine. Since ViewEngine was deprecated and removed, the NgFactory and NgSummary files are no longer used as well.

This commit drops obsolete options to generate NgFactory and NgSummary files. Also, the logic that generates those files is also removed.

PR Close #48268
2023-02-21 13:03:59 -08:00
Kristiyan Kostadinov
d0145033bd fix(language-service): generate forwardRef for same file imports (#48898)
Adds some logic that will generate a `forwardRef` if necessary when automatically fixing an import.

PR Close #48898
2023-02-02 13:40:17 -08:00
Kristiyan Kostadinov
6beff5e8d7 refactor(compiler): rework and expose APIs to be used in schematics (#48730)
Reworks some of the existing compiler APIs to make them easier to use in a schematic and exposes a few new ones to surface information we already had. High-level list of changes:
* `getPotentialImportsFor` now requires a class reference, instead of a `PotentialDirective | PotentialPipe`.
* New `getNgModuleMetadata` method has been added to the type checker.
* New `getPipeMetadata` method has been added to the type checker.
* New `getUsedDirectives` method has been added to the type checker.
* New `getUsedPipes` method has been added to the type checker.
* The `decorator` property was exposed on the `TypeCheckableDirectiveMeta`. The property was already present at runtime, but it wasn't specified on the interface.

PR Close #48730
2023-01-13 12:24:32 -08:00
Dylan Hunn
4ae384fd61 feat(language-service): Allow auto-imports of a pipe via quick fix when its selector is used, both directly and via reexports. (#48354)
A previous PR introduced a new compiler abstraction that tracks *all* known exports and re-exports of Angular traits. This PR harnesses that abstraction in the language service, in order to allow automatic imports of pipes.

PR Close #48354
2023-01-12 13:46:46 -08:00
Paul Gschwendtner
acf98268f8 refactor: update language-service package and tests to work with ESM (#48521)
The Angular Language Service package is now tested & compiled using ESM.
Previously it was a mismatch of CommonJS and ESM.

Still the language-service ESM output is transformed into a single UMD
bundle to allow for module resolution to be overriden. See
`bundles/BUILD`.

This is kept as is. To fully ship ESM (language-service is an exception
here), we need to:

 * Update all code to no longer reference typescript via import. Instead
   typescript needs to be passed around so that the extension can
   control the version
 * The VSCode extension/ the TS server needs to be able to load ESM. It
   looks like the server supports ESM global plugins, but the extension
   might not yet.

This is out of scope for the dev-infra effort as it requires more
insight into VSCode & the extension system & the TS language server.

PR Close #48521
2022-12-19 19:50:44 +00:00
Kristiyan Kostadinov
0138d75335 refactor(language-service): make selector nullable (#48193)
This is a follow-up from https://github.com/angular/angular/pull/48147. Changes the `DirectiveSymbol.selector` to be nullable since it's possible to have directives without a selector.

PR Close #48193
2022-11-23 09:27:03 -08:00
Kristiyan Kostadinov
fd2eea5961 fix(language-service): correctly handle host directive inputs/outputs (#48147)
Adds some logic to correctly handle hidden or aliased inputs/outputs in the language service.

Fixes #48102.

PR Close #48147
2022-11-22 09:47:49 -08:00
Dylan Hunn
ef6dbc8850 refactor(compiler): Add getPotentialPipes API method. (#48090)
`getPotentialPipes` returns possible pipes which can be used in the provided context, whether already in scope or requiring an import.

This is necessary to implement auto-import support for pipes in the language service.

PR Close #48090
2022-11-17 11:00:50 -08:00
Dylan Hunn
192207ec9a refactor(language-service): Move some quick fix code into a shared file (#48074)
The `fix_missing_import` quick fix has several generic helper functions that deal with imports and decorators, which I want to reuse for other import-related features. Move these functions into `ts_utils.ts` instead. No substantive code changes are present.

PR Close #48074
2022-11-16 17:46:24 -08:00
Dylan Hunn
ce8160ecb2 fix(language-service): Prevent crashes on unemitable references (#47938)
Currently, when generating an import of a selector, the language service might crash if the compiler cannot emit a reference to the new symbol's file from the target component's file. (This might happen because the two are the same file.) We should handle that case by reusing the existing import if possible, or otherwise failing gracefully.

PR Close #47938
2022-11-03 17:49:11 -07:00
Dylan Hunn
5f0b53c735 feat(language-service): Allow auto-imports to suggest multiple possible imports. (#47787)
This change allows the language service to suggest imports for *all* directives returned from the compiler, and for *all* import specifiers for each directive. The first change has immediate effect, since there could already be multiple directives with the same selector. The second change is future-proofing; the compiler currently only returns a single best specifier, but will return more in the future.

PR Close #47787
2022-10-21 19:12:36 +02:00
Dylan Hunn
bebef5fb43 feat(language-service): Quick fix to import a component when its selector is used (#47088)
The language service can now generate an import corresponding to a selector. This includes both the TypeScript module import and the decorator import. This applies to both standalone components and components declared in NgModules.

PR Close #47088
2022-10-10 21:32:43 +00:00
Dylan Hunn
3783ee01ac refactor(compiler): Rename DirectiveInScope -> PotentialDirective (#47561)
After implementing `getPotentialTemplateDirectives`, we will use this data struture to represent both in-scope and out-of-scope directives. So this rename is an advance cleanup.

PR Close #47561
2022-10-04 14:40:04 -07:00
ivanwonder
e7ee53c541 feat(language-service): support to fix invalid banana in box (#47393)
The Angular compiler will report the invalid banana in box, this code fixes
will try to fix the error and all the same errors in the selected file.

Fixes #44941

PR Close #47393
2022-09-27 10:16:11 -07:00
Dylan Hunn
daac13783b refactor(language-service): Create helper methods for manipulating object and array ASTs. (#47181)
Create three new helper methods: `addElementToArrayLiteral`, `objectPropertyAssignmentForKey`, and `updateObjectValueForKey`. These methods make interacting with array and object literals easier.

These will be useful for the standalone imports feature, which will need to add new terms to import arrays in Component and NgModule decorators.

PR Close #47181
2022-08-22 10:44:21 -07:00
ivanwonder
598b72bd05 feat(language-service): support fix the component missing member (#46764)
The diagnostic of the component missing member comes from the ts service,
so the all code fixes for it are delegated to the ts service.

The code fixes are placed in the LS package because only LS can benefit from
it now, and The LS knows how to provide code fixes by the diagnostic and NgCompiler.

The class `CodeFixes` is useful to extend the code fixes if LS needs to
provide more code fixes for the template in the future. The ts service uses
the same way to provide code fixes.

1622247636/src/services/codeFixProvider.ts (L22)

Fixes https://github.com/angular/vscode-ng-language-service/issues/1610

PR Close #46764
2022-08-02 11:58:21 -07:00
John Vandenberg
c14c701775 docs: fix spelling (#46713)
PR Close #46713
2022-07-08 20:54:52 +00:00
Andrew Scott
fa754cd9db fix(language-service): Prevent TSServer from removing templates from project (#45965)
As part of the `updateProjectIfDirty` process and inside `updateNonInferredProjectFiles`
TS Server will remove the template files that we added as roots in
`readResource`.
https://sourcegraph.com/github.com/microsoft/TypeScript@c300fea3250abd7f75920d95a58d9e742ac730ee/-/blob/src/server/editorServices.ts?L2363

The external files are added to the list here so ensuring that the
templates are included in the `getExternalFiles` will prevent this from
happening
https://sourcegraph.com/github.com/microsoft/TypeScript@c300fea3250abd7f75920d95a58d9e742ac730ee/-/blob/src/server/editorServices.ts?L2395:18

PR Close #45965
2022-05-12 09:25:18 -07:00
Andrew Scott
d48cfbca75 fix(language-service): Add resource files as roots to their associated projects (#45601)
When an external template is read, adds the template file to to the project which contains.
This is necessary to keep the projects open when navigating away from HTML files.
Since a `tsconfig` cannot express including non-TS files,
we need another way to indicate the template files are considered part of the project.

Note that this does not ensure that the project in question _directly_ contains the component
file. That is, the project might just include the component file through the program rather
than directly in the `include` glob of the `tsconfig`. This distinction is somewhat important
because the TypeScript language service/server prefers projects which _directly_ contain the TS
file (see `projectContainsInfoDirectly` in the TS codebase). What this means it that there can
possibly be a different project used between the TS and HTML files.

For example, in Nx projects, the referenced configs are `tsconfig.app.json` and
`tsconfig.editor.json`. `tsconfig.app.json` comes first in the base `tsconfig.json` and
contains the entry point of the app. `tsconfig.editor.json` contains the `**.ts` glob of all TS
files. This means that `tsconfig.editor.json` will be preferred by the TS server for TS files
but the `tsconfig.app.json` will be used for HTML files since it comes first and we cannot
effectively express `projectContainsInfoDirectly` for HTML files.

We could consider also updating the language server implementation to attempt
to select the project to use for the template file based on which project
contains its component file directly, using either the internal `project.projectContainsInfoDirectly`
or as a workaround, check `project.isRoot(componentTsFile)`.

Finally, keeping the projects open is hugely important in the solution style config case like
Nx. When a TS file is opened, TypeScript will only retain `tsconfig.editor.json` and not
`tsconfig.app.json`. However, if our extension does not also know to select
`tsconfig.editor.json`, it will automatically select `tsconfig.app.json` since it is defined
first in the `tsconfig.json` file. So we need to teach TS server that we are (1) interested in
keeping projects open when there is an HTML file open and (2) optionally attempt to do this
_only_ for projects that we know the TS language service will prioritize in TS files (i.e.,
attempt to only keep `tsconfig.editor.json` open and allow `tsconfig.app.json` to close)
and prioritize that project for all requests.

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

PR Close #45601
2022-05-10 09:36:26 -07:00
Alex Rickabaugh
9b35787bb3 refactor(compiler-cli): unify tracked template scope dependencies (#45672)
Previously, the compiler tracked directives and pipes in template scopes
separately. This commit refactors the scope system to unify them into a
single data structure, disambiguated by a `kind` field.

PR Close #45672
2022-04-20 05:45:56 -07:00
ivanwonder
f57e46c538 fix(language-service): two-way binding completion should not remove the trailing quote (#45582)
We allow the path to contain both the `t.BoundAttribute` and `t.BoundEvent` for two-way
bindings but do not want the path to contain both the `t.BoundAttribute` with its
children when the position is in the value span because we would then logically create a path
that also contains the `PropertyWrite` from the `t.BoundEvent`. This early return condition
ensures we target just `t.BoundAttribute` for this case and exclude `t.BoundEvent` children.

Fixes https://github.com/angular/vscode-ng-language-service/issues/1626

PR Close #45582
2022-04-15 13:53:48 -07:00
JoostK
ff32301bf5 refactor(compiler-cli): track whether a TcbPosition corresponds with a shim file (#45454)
Extends `TcbPosition` with a field that indicates whether the `tcbPath` is a
type-checking shim file, or an original source file with an inline type check
block.

This field is used in an upcoming commit that fixes an inconsistency with how
inline type check blocks are incorrectly interpreted as a type-checking shim
file instead.

PR Close #45454
2022-03-29 14:09:33 -07:00