Commit Graph

130 Commits

Author SHA1 Message Date
Keen Yee Liau e9e7c33f3c fix(language-service): Add plugin option to force strictTemplates (#41062)
This commit adds a new configuration option, `forceStrictTemplates` to the
language service plugin to allow users to force enable `strictTemplates`.

This is needed so that the Angular extension can be used inside Google without
changing the underlying compiler options in the `ng_module` build rule.

PR Close #41062
2021-03-03 09:48:06 -08:00
Andrew Scott 10aa5641dd fix(language-service): only provide template results on reference requests (#41041)
VSCode only de-duplicates references results for "go to references" requests
but does not de-duplicate them for "find all references" requests. The
result is that users see duplicate references for results in TypeScript
files - one from the built-in TS extension and one from us.
While this is an issue in VSCode (see https://github.com/microsoft/vscode/issues/117095)
this commit provides a quick workaround on our end until it can be addressed there.

This commit should be reverted when microsoft/vscode/issues/117095 is resolved.

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

PR Close #41041
2021-03-03 09:39:11 -08:00
ivanwonder 8110cf0ed2 fix(language-service): can't provide the Input and Output custom binding property name (#41005)
Now the language service always uses the name of the JavaScript property on the
component or directive instance for this input or output. This PR will use the right
binding property name.

PR Close #41005
2021-03-01 15:26:42 -08:00
Andrew Scott d1535a1a77 test(language-service): remove circular dependency (#40966)
This commit removes the circular dependency from buffer->util->project->buffer.

PR Close #40966
2021-02-24 08:54:35 -08:00
Andrew Scott cf687fe8ab refactor(language-service): Remove old testing helpers (#40966)
All specs have been switched to the new testing package. The old test
helpers are no longer needed.

PR Close #40966
2021-02-24 08:54:35 -08:00
Andrew Scott dcee784b4f refactor(language-service): convert references_spec to new testing package (#40966)
refactor(language-service): convert references_spec to new testing package

PR Close #40966
2021-02-24 08:54:35 -08:00
Andrew Scott 000ec6be3c refactor(language-service): migrate type_definitions_spec to the new testing package (#40966)
refactor(language-service): migrate type_definitions_spec to the new testing package

PR Close #40966
2021-02-24 08:54:35 -08:00
Andrew Scott d1b7774753 refactor(language-service): migrate quick_info_spec to new testing package (#40966)
refactor(language-service): migrate quick_info_spec to new testing package

PR Close #40966
2021-02-24 08:54:34 -08:00
Andrew Scott d2b43d577b refactor(language-service): migrate definitions_spec to new testing package (#40966)
refactor(language-service): migrate definitions_spec to new testing package

PR Close #40966
2021-02-24 08:54:34 -08:00
Andrew Scott 8808002e54 refactor(language-service): migrate gettcb_spec to new testing package (#40966)
refactor(language-service): migrate gettcb_spec to new testing package

PR Close #40966
2021-02-24 08:54:34 -08:00
Andrew Scott af3f95bd75 refactor(language-service): migrate diagnostic_spec to new test infrastructure (#40966)
refactor(language-service): migrate diagnostic_spec to new test infrastructure

PR Close #40966
2021-02-24 08:54:34 -08:00
Andrew Scott bc5c9ee234 refactor(language-service): Migrate completions_spec to new testing API (#40966)
refactor(language-service): Migrate completions_spec to new testing API

PR Close #40966
2021-02-24 08:54:34 -08:00
Alex Rickabaugh 23360d1215 test(language-service): update compiler_spec to use the new testing env (#40966)
This commit updates compiler_spec.ts in the Ivy LS suite to utilize the new
testing environment which was introduced in the previous commit. Eventually
all specs should be converted, but converting one right now helps ensure
that the new testing env is working properly and able to support real tests.

PR Close #40966
2021-02-24 08:54:34 -08:00
Keen Yee Liau 7101267923 refactor(language-service): add type guard for NgLanguageService (#40954)
Type guard should be colocated with the `NgLanguageService` interface,
not in `@angular/vscode-ng-language-service`.

PR Close #40954
2021-02-23 10:49:37 -08:00
Andrew Scott ad38cbbe09 perf(language-service): Skip Angular analysis when quick info requested outside a template (#40956)
The Angular LS does not provide quick info when the given position is not
inside a template. As an optimization, we can quickly look at the
file and determine if we are at a position that is part of an Angular
template. If not, we bail before asking the compiler for any more
information. Note that the Angular LS _already_ provides no quick info
when outside a template file, but currently asks the compiler to analyze
the program before it determines that information.

PR Close #40956
2021-02-23 10:37:48 -08:00
Andrew Scott f31a6015a0 perf(language-service): short-circuit LS operations (#40946)
When certain information is requested from the Angular Language Service, we
know that there will be no additional Angular information if the requested
position is not in an inline template, template url, or style url. To avoid
unnecessary compiler compilations, we short circuit and return `undefined`
before asking the compiler for any type of answer which would trigger a
partial compilation, at the very least.

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

PR Close #40946
2021-02-22 13:19:09 -08:00
Alex Rickabaugh 53c65f468f test(language-service): update compiler_spec to use the new testing env (#40679)
This commit updates compiler_spec.ts in the Ivy LS suite to utilize the new
testing environment which was introduced in the previous commit. Eventually
all specs should be converted, but converting one right now helps ensure
that the new testing env is working properly and able to support real tests.

PR Close #40679
2021-02-22 08:40:41 -08:00
Alex Rickabaugh c9879deded test(language-service): introduce new, more configurable testing env (#40679)
The Ivy Language Service codebase testing suite contains a few testing
utilities which allow for assertions of Language Service operations against
an in-memory project. However, this existing utility lacks the flexibility
to test more complex scenarios, such as those involving multiple TS projects
with dependencies between them.

This commit introduces a new 'testing' package for the Ivy LS which attempts
to more faithfully represent the possible states of an IDE, and allows for
testing of more advanced scenarios. The new utility borrows from the prior
version and is geared towards more ergonomic testing. Only basic
functionality is present in this initial implementation, but this will grow
over time.

PR Close #40679
2021-02-22 08:40:41 -08:00
JoostK 94f4d5cba6 refactor(compiler-cli): remove event output helper from TCB (#40738)
In 5c547675b11a24b16c20df1718583a0e7ed49cbd the `EventEmitter.subscribe`
API was extended with a new signature that allows the emitter's generic
type `T` to flow into the subscribe callback. This new signature removes
the need for the special `_outputHelper` function that used to be
emitted into TCBs when `strictOutputEventTypes`/`strictTemplates` is
enabled.

PR Close #40738
2021-02-10 11:06:35 -08:00
Andrew Scott 5cde4ad591 feat(language-service): add command for getting components for a template file (#40655)
This commit adds a feature to the Angular Language Service that enables
getting the locations for components that use a template file.

Part of https://github.com/angular/vscode-ng-language-service/issues/1081

PR Close #40655
2021-02-04 10:47:22 -08:00
ayazhafiz 950875c1ba refactor(language-service): pull out interfaces on package toplevel (#40621)
Two motivations behind this change:

1. We would like to expose the types of the Language Service to external
   users (like the VSCode extension) via the npm package, on the top
   level of the package
2. We would like the View Engine and Ivy LS to share a common interface
   (notably after the inclusion of `getTcb`, the Ivy LS upholds a
   strict superset of `ts.LanguageService`; previously both VE and Ivy
   LS were aligned on `ts.LanguageService`.)

To this end, this commit refactors the exports on the toplevel of the
`language-service/` package to just be types common to both the VE and
Ivy language services. The VE and Ivy build targets then import and use
these types accordingly, and the expectation is that an external user
will just import the relevant typings from the toplevel package without
diving into either the VE or Ivy sources.

Follow up on #40607

PR Close #40621
2021-02-03 09:19:54 -08:00
Alex Rickabaugh a3b0864428 refactor(compiler-cli): remove the overrideComponentTemplate API (#40585)
The `TemplateTypeChecker.overrideComponentTemplate` operation was originally
conceived as a "fast path" for the Language Service to react to a template
change without needing to go through a full incremental compilation step. It
served this purpose until the previous commit, which switches the LS to use
the new resource-only incremental change operation provided by `NgCompiler`.

`overrideComponentTemplate` is now no longer utilized, and is known to have
several hard-to-overcome issues that prevent it from being useful in any
other situations. As such, this commit removes it entirely.

PR Close #40585
2021-02-02 16:24:57 -08:00
Alex Rickabaugh e3bd23c915 perf(language-service): update NgCompiler via resource-only path when able (#40585)
This commit changes the Language Service's "compiler factory" mechanism to
leverage the new resource-only update path for `NgCompiler`. When an
incoming change only affects a resource file like a component template or
stylesheet, going through the new API allows the Language Service to avoid
unnecessary incremental steps of the `NgCompiler` and return answers more
efficiently.

PR Close #40585
2021-02-02 16:24:56 -08:00
Andrew Scott 6b4909c588 fix(compiler): Don't set expression text to synthetic `$implicit` when empty (#40583)
When parsing interpolations, if we encounter an empty interpolation
(`{{}}`), the current code uses a "pretend" value of `$implicit` for the
name as if the interplotion were really `{{$implicit}}`. This is
problematic because the spans are then incorrect downstream since they
are based off of the `$implicit` text.

This commit changes the interpretation of empty interpolations so that
the text is simply an empty string.

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

PR Close #40583
2021-01-28 09:06:17 -08:00
JoostK c18c7e23ec fix(compiler): exclude trailing whitespace from element source spans (#40513)
If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes #39148

PR Close #40513
2021-01-28 08:53:02 -08:00
Alex Rickabaugh 21e24d1474 refactor(compiler-cli): introduce CompilationTicket system for NgCompiler (#40561)
Previously, the incremental flow for NgCompiler was simple: when creating a
new NgCompiler instance, the consumer could pass state from a previous
compilation, which would cause the new compilation to be performed
incrementally. "Local" information about TypeScript files which had not
changed would be passed from the old compilation to the new and reused,
while "global" information would always be recalculated.

However, this flow could be made more efficient in certain cases, such as
when no TypeScript files are changed in a new compilation. In this case,
_all_ information extracted during the first compilation is reusable. Doing
this involves reusing the previous `NgCompiler` instance (the container for
such global information) and updating it, instead of creating a new one for
the next compilation. This approach works cleanly, but complicates the
lifecycle of `NgCompiler`.

To prevent consumers from having to deal with the mechanics of reuse vs
incremental steps of `NgCompiler`, a new `CompilationTicket` mechanism is
added in this commit. Consumers obtain a `CompilationTicket` via one of
several code paths depending on the nature of the incoming compilation, and
use the `CompilationTicket` to obtain an `NgCompiler` instance. This
instance may be a fresh compilation, a new `NgCompiler` for an incremental
compilation, or an existing `NgCompiler` that's been updated to optimally
process a resource-only change. Consumers can use the new `NgCompiler`
without knowledge of its provenance.

PR Close #40561
2021-01-27 10:45:57 -08:00
Alex Rickabaugh a8c5c8ed2d fix(language-service): recognize incomplete pipe bindings with whitespace (#40346)
The Language Service uses the source span of AST nodes to recognize which
node a user has selected, given their cursor position in a template. This is
used to trigger autocompletion.

The previous source span of BindingPipe nodes created a problem when:

1) the pipe binding had no identifier (incomplete or in-progress expression)
2) the user typed trailing whitespace after the pipe character ('|')

For example, the expression `{{foo | }}`. If the cursor preceded the '}' in
that expression, the Language Service was unable to detect that the user was
autocompleting the BindingPipe expression, since the span of the BindingPipe
ended after the '|'.

This commit changes the expression parser to expand the span of BindingPipe
expressions with a missing identifier, to include any trailing whitespace.
This allows the Language Service to correctly recognize this case as
targeting the BindingPipe and complete it successfully. The `nameSpan` of
the BindingPipe is also moved to be right-aligned with the end of any
whitespace present in the pipe binding expression.

This change allows for the disabled test in the Language Service for pipe
completion in this case to be re-enabled.

PR Close #40346
2021-01-27 10:44:40 -08:00
Keen Yee Liau e43cba5c82 fix(language-service): implement realpath to resolve symlinks (#40593)
The `LanguageServiceAdapter` must implement `realpath` in order to resolve
symlinks in `node_modules`.

Local libraries are often symlinked in `node_modules` by adding a local
dependency in `package.json`.

Fix https://github.com/angular/vscode-ng-language-service/issues/1083

PR Close #40593
2021-01-27 10:36:04 -08:00
Keen Yee Liau ecae75f477 feat(language-service): Add diagnostics to suggest turning on strict mode (#40423)
This PR adds a way for the language server to retrieve compiler options
diagnostics via `languageService.getCompilerOptionsDiagnostics()`.

This will be used by the language server to show a prompt in the editor if
users don't have `strict` or `fullTemplateTypeCheck` turned on.

Ref https://github.com/angular/vscode-ng-language-service/issues/1053

PR Close #40423
2021-01-25 14:17:31 -08:00
ayazhafiz d482f5cdd3 feat(language-service): view template typecheck block (#39974)
This patch adds an API to retrieve the template typecheck block for a
template (if any) at a file location, and a selection of the TS node
in the TCB corresponding to the template node at which the request for
a TCB was made (if any).

Probably not something we want to land soon, but a useful debugging tool
for folks working with TCBs.

PR Close #39974
2021-01-21 14:05:29 -08:00
Andrew Scott 5bbb5ea955 refactor(language-service): do not mutate the original template node span (#40484)
Rather than mutating the span on the template when renaming literal strings,
this commit updates the logic to mutate the `TextSpan` equivalent that
is used by the Language Service.

PR Close #40484
2021-01-20 16:59:54 -08:00
Andrew Scott 402e2e6189 refactor(language-service): de-duplicate rename and reference results (#40454)
The initial implementation assumed that the consuming editors would
de-duplicate rename locations. In fact, vscode treats overlapping rename
locations as distinct and errors when trying to preview the renames.

This commit updates the language service to de-duplicate exact file+span
matches before returning rename and reference locations.

While vscode _does_ de-duplicate reference results, it still makes sense
to de-duplicate them on our side when possible to make tests more
understandable. If a template has 3 instances of a variable, it makes
sense to get get 3 reference results rather than 4+ with some duplicates.

PR Close #40454
2021-01-19 14:21:15 -08:00
Andrew Scott 3e97a1ea43 fix(language-service): fix go to definition for template variables and references (#40455)
The current "go to definition" is broken for template variables and
references when a template is overridden. This is because we get the
file url from the source span, which uses the overridden name
'override.html'. Instead, we can retrieve the template file from the
compiler in the same manner that is done for references.

Another way to fix this would have been to use the real template file path when
overriding a template, but this was the more straightforward fix since
the strategy was already used in find references and rename locations.

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

PR Close #40455
2021-01-19 13:03:14 -08:00
Andrew Scott 4e8198d60f feat(language-service): Implement `getRenameInfo` (#40439)
The `getRenameInfo` action is used by consumers to

1. Determine if a location is a candidate for renames
2. Determine what text to use as the starting point for the rename

PR Close #40439
2021-01-15 16:44:30 -08:00
Zach Arend 9a5ac47331 feat(language-service): initial implementation for `findRenameLocations` (#40140)
This commit lays the groundwork for potentially providing rename
locations from the Ivy native LS. The approach is very similar to what
was done with the feature to find references. One difference, however,
is that we did not require the references to be fully "correct". That
is, the exact text spans did not matter so much, as long as we provide a
location that logically includes the referenced item.

An example of a necessary difference between rename locations and references is
directives. The entire element in the template is a "reference" of the
directive's class. However, it's not a valid location to be renamed. The
same goes for aliased inputs/outputs. The locations in the template
directly map to the class property, which is correct for references, but
would not be correct for rename locations, which should instead map to
the string node fo the alias.

As an initial approach to address the aforementioned issues with rename
locations, we check that all the rename location nodes have the same text. If
_any_ node has text that differs from the request, we do not return any
rename locations. This works as a way to prevent renames that could
break the the program by missing some required nodes in the rename action, but
allowing other nodes to be renamed.

PR Close #40140
2021-01-14 16:24:39 -08:00
Alexey Elin cf02cf1e18 docs: remove duplicated the (#40434)
PR Close #40434
2021-01-14 11:33:57 -08:00
Zach Arend 4db89f4576 fix(compiler-cli): report non-template diagnostics (#40331)
Report non-template diagnotics when calling `getDiagnotics` function of
the language service we only returned template diagnotics. This change
causes it to return all diagnotics, not just diagnostics from the
template type checker.

PR Close #40331
2021-01-13 10:55:04 -08:00
Keen Yee Liau 6a9e328432 feat(language-service): log Angular compiler options (#40364)
This commit records the Angular compiler options in the log file to help
debugging.

PR Close #40364
2021-01-08 13:45:47 -08:00
Keen Yee Liau 811cacc80c fix(language-service): reinstate overridden compiler option after change (#40364)
Currently the language service has to force `compileNonExportedClasses` to
`true` to handle inline NgModules in tests, regardless of the value in user's
tsconfig.json.

However, the override is not reinstated after the compiler option changes
(triggered by a change in tsconfig.json).
This commit fixes the bug.

PR Close #40364
2021-01-08 13:45:47 -08:00
Andrew Scott 57043721db docs(language-service): Add comment about approach to quick info for two-way bindings (#40185)
This commit simply adds a comment about why quick info only gets data
for the `BoundAttribute` of a two-way binding.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott 7d74853a1d fix(language-service): Support completions of two-way bindings (#40185)
This commit adds special handling to the completion builder by detecting
a two way binding context and ensuring that we filter out any `Input`s
that do not support two way binding.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott ebb7ac5979 fix(language-service): Support 'find references' for two-way bindings (#40185)
Rather than expecting that a position in a template only targets a
single node, this commit simply adjusts the approach to account for two way
bindings. Specifically, we attempt to get references for each targeted
node and then return the combination of all results, or `undefined` if
none of the target nodes had references.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott a9d8c228d9 fix(language-service): Support 'go to definition' for two-way bindings (#40185)
Rather than expecting that a position in a template only targets a
single node, this commit adjusts the approach to account for two way
bindings. In particular, we attempt to get definitions for each targeted
node and then return the combination of all results, or `undefined` if
none of the target nodes had definitions.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott d70c26cc06 refactor(language-service): Have TemplateTarget recognize two way bindings (#40185)
Adjust the visitor logic of the template target as well as the
consumption of the visitor result to account for two-way bindings.

This sets up downstream consumers for being able to handle the
possibility of a template position that targets both an input and an
output.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott a893187d51 refactor(language-service): Add type to `TemplateTarget` that can indicate multi-node targets (#40185)
The current template target implementation only allows a way to
represent the template position as targeting a single node in the
template AST. However, there is at least one case (banana-in-a-box)
where a given template position refers to two template targets.

This commit expands the contexts that the `TemplateTarget` can return to
include support for the banana-in-a-box syntax, which has two logically
targetted AST nodes given a position within the `keySpan` of the
binding.

PR Close #40185
2021-01-07 13:18:38 -08:00
Andrew Scott 989b4a94d4 refactor(compiler-cli): Return symbols for all matching outputs (#40144)
This commit ensures that the template type checker returns symbols for
all outputs if a template output listener binds to more than one.

PR Close #40144
2021-01-06 13:53:46 -08:00
Andrew Scott da2be4b710 refactor(compiler-cli): Return symbols for all matching inputs (#40144)
This commit ensures that the template type checker returns symbols for
all inputs if an attribute binds to more than one.

PR Close #40144
2021-01-06 13:53:46 -08:00
Keen Yee Liau 183fb7e7b9 fix(language-service): return all typecheck files via getExternalFiles (#40162)
We need a means to preserve typecheck files when a project is reloaded,
otherwise the Ivy compiler will throw an error when it's unable to find
them. This commit implements `getExternalFiles()` called by the langauge
server to achieve this goal.

For more info see https://github.com/angular/vscode-ng-language-service/issues/1030

PR Close #40162
2021-01-06 11:34:15 -08:00
Keen Yee Liau 0264f76e94 fix(language-service): LSParseConfigHost.resolve should not concat abs paths (#40242)
`ts.server.ServerHost.resolvePath()` is different from Angular's
`FileSystem.resolve()` because the signature of the former is

```ts
resolvePath(path: string): string;	// ts.server.ServerHost
```

whereas the signature of the latter is
```ts
resolve(...paths: string[]): AbsoluteFsPath; // FileSystem on compiler-cli
```

The current implementation calls `path.join()` to concatenate all the input
paths and pass the result to `ts.server.ServerHost.resolvePath()`, but doing
so results in filenames like
```
/foo/bar/baz/foo/bar/baz/tsconfig.json
```
if both input paths are absolute.

`ts.server.ServerHost` should not be used to implement the
`resolve()` method expected by Angular's `FileSystem`.
We should use Node's `path.resolve()` instead, which will correctly collapse
the absolute paths.

Fix https://github.com/angular/vscode-ng-language-service/issues/1035

PR Close #40242
2021-01-06 10:54:40 -08:00
Keen Yee Liau a62416c6e4 fix(language-service): include compilerOptions.rootDir in rootDirs (#40243)
When resolving references, the Ivy compiler has a few strategies it could use.

For relative path, one of strategies is [`RelativePathStrategy`](
https://github.com/angular/angular/blob/master/packages/compiler-cli/src/
ngtsc/imports/README.md#relativepathstrategy). This strategy
relies on `compilerOptions.rootDir` and `compilerOptions.rootDirs` to perform
the resolution, but language service only passes `rootDirs` to the compiler,
and not `rootDir`.

In reality, `rootDir` is very different from `rootDirs` even though they
sound the same.
According to the official [TS documentation][1],
> `rootDir` specifies the root directory of input files. Only use to control
> the output directory structure with --outDir.

> `rootDirs` is a list of root folders whose combined content represent the
> structure of the project at runtime. See [Module Resolution documentation](
> https://www.typescriptlang.org/docs/handbook/
> module-resolution.html#virtual-directories-with-rootdirs)
> for more details.

For now, we keep the behavior between compiler and language service consistent,
but we will revisit the notion of `rootDir` and how it is used later.

Fix angular/vscode-ng-language-service#1039

[1]: https://www.typescriptlang.org/docs/handbook/compiler-options.html

PR Close #40243
2021-01-06 10:54:11 -08:00