Commit Graph

335 Commits

Author SHA1 Message Date
Keen Yee Liau 1140bbc25c refactor(language-service): reformat using clang-format (#36426)
clang-format was recently updated and any PRs that touch files in the
language service will have to reformat all the files.

Instead of changing the formatting in those PRs, this PR formats all
files in language-service package once and for all.

PR Close #36426
2020-04-06 09:29:42 -07:00
Ayaz Hafiz fe2b6923ba fix(language-service): infer type of elements of array-like objects (#36312)
Currently the language service only provides support for determining the
type of array-like members when the array-like object is an `Array`.
However, there are other kinds of array-like objects, including
`ReadonlyArray`s and `readonly`-property arrays. This commit adds
support for retrieving the element type of arbitrary array-like objects.

Closes #36191

PR Close #36312
2020-04-01 13:24:53 -07:00
ayazhafiz df890d7629 fix(compiler): record correct end of expression (#34690)
This commit fixes a bug with the expression parser wherein the end index
of an expression node was recorded as the start index of the next token,
not the end index of the current token.

Closes #33477
Closes https://github.com/angular/vscode-ng-language-service/issues/433

PR Close #34690
2020-03-20 10:19:02 -07:00
ayazhafiz acc483e2eb feat(language-service): improve non-callable error message (#35271)
This commit improves the context of a non-callable function error
message by providing the affected call target and its non-callable type.

PR Close #35271
2020-03-17 09:28:59 -07:00
Keen Yee Liau 31bec8ce61 feat(compiler): Propagate source span and value span to Variable AST (#36047)
This commit propagates the `sourceSpan` and `valueSpan` of a `VariableBinding`
in a microsyntax expression to `ParsedVariable`, and subsequently to
View Engine Variable AST and Ivy Variable AST.

Note that this commit does not propagate the `keySpan`, because it involves
significant changes to the template AST.

PR Close #36047
2020-03-16 10:52:57 -07:00
ayazhafiz 18b1bd4415 fix(language-service): infer $implicit value for ngIf template contexts (#35941)
Today, the language service infers the type of variables bound to the
"ngIf" template context member of an NgIf directive, but does not do the
same for the the "$implicit" context member. This commit adds support
for that.

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

PR Close #35941
2020-03-11 14:52:27 -04:00
Keen Yee Liau 06779cfe24 feat(compiler): Add sourceSpan and keySpan to TemplateBinding (#35897)
This commit adds fine-grained text spans to TemplateBinding for microsyntax expressions.

1. Source span
   By convention, source span refers to the entire span of the binding,
   including its key and value.
2. Key span
   Span of the binding key, without any whitespace or keywords like `let`

The value span is captured by the value expression AST.

This is part of a series of PRs to fix source span mapping in microsyntax expression.
For more info, see the doc https://docs.google.com/document/d/1mEVF2pSSMSnOloqOPQTYNiAJO0XQxA1H0BZyESASOrE/edit?usp=sharing

PR Close #35897
2020-03-11 14:51:56 -04:00
Alex Rickabaugh 95c729f5d1 build: typescript 3.8 support (#35864)
This commit adds support in the Angular monorepo and in the Angular
compiler(s) for TypeScript 3.8. All packages can now compile with
TS 3.8.

For most of the repo, only a handful few typings adjustments were needed:

* TS 3.8 has a new `CustomElementConstructor` DOM type, which enforces a
  zero-argument constructor. The `NgElementConstructor` type previously
  declared a required `injector` argument despite the fact that its
  implementation allowed `injector` to be optional. The interface type was
  updated to reflect the optionality of the argument.
* Certain error messages were changed, and expectations in tests were
  updated as a result.
* tsserver (part of language server) now returns performance information in
  responses, so test expectations were changed to only assert on the actual
  body content of responses.

For compiler-cli and schematics (which use the TypeScript AST) a major
breaking change was the introduction of the export form:

```typescript
export * as foo from 'bar';
```

This is a `ts.NamespaceExport`, and the `exportClause` of a
`ts.ExportDeclaration` can now take this type as well as `ts.NamedExports`.
This broke a lot of places where `exportClause` was assumed to be
`ts.NamedExports`.

For the most part these breakages were in cases where it is not necessary
to handle the new `ts.NamedExports` anyway. ngtsc's design uses the
`ts.TypeChecker` APIs to understand syntax and so automatically supports the
new form of exports.

The View Engine compiler on the other hand extracts TS structures into
metadata.json files, and that format was not designed for namespaced
exports. As a result it will take a nontrivial amount of work if we want to
support such exports in View Engine. For now, these new exports are not
accounted for in metadata.json, and so using them in "folded" Angular
expressions will result in errors (probably claiming that the referenced
exported namespace doesn't exist).

Care was taken to only use TS APIs which are present in 3.7/3.6, as Angular
needs to remain compatible with these for the time being.

This commit does not update angular.io.

PR Close #35864
2020-03-10 17:51:20 -04:00
ivanwonder 3d46a45fa8 fix(language-service): resolve the variable from the template context first (#35982)
PR Close #35982
2020-03-10 13:27:04 -04:00
ayazhafiz 406419bc0f fix(language-service): fix calculation of pipe spans (#35986)
This commit accomplishes two tasks:

- Fixes the span of queried pipes to only be applied on pipe names
- By consequence, fixes how pipes are located in arguments (previously,
  pipes with arguments could not be found because the span of a pipe
  uses a relative span, while the template position is absolute)

The screenshots attached to the PR for this commit demonstrate the
change.

Closes https://github.com/angular/vscode-ng-language-service/issues/677

PR Close #35986
2020-03-10 13:26:40 -04:00
Keen Yee Liau bef14cf424 refactor(compiler): rename _ParseAST.optionalCharacter TemplateBinding.expression (#35886)
This commit renames
1. _ParseAST.optionalCharacter -> consumeOptionalCharacter
2. TemplateBinding.expression -> value

PR Close #35886
2020-03-06 12:39:49 -05:00
ivanwonder 4e1d780554 fix(language-service): resolve the real path for symlink (#35895)
when AOT resolves the module name, it will preserve the path of the symlink, but the ts-server will return the real path for symlink.

PR Close #35895
2020-03-06 12:38:55 -05:00
Keen Yee Liau 89b5b97368 build: avoid running duplicate tests in language service (#35816)
The test libs should only be included in one jasmine_node_test
otherwise `bazel build //packages/language-service/...` would
end up running `feature_test` and `infra_test` twice.

PR Close #35816
2020-03-03 08:59:36 -08:00
ayazhafiz 47a1811e0b feat(language-service): modularize error messages (#35678)
This commit performs a modularization of the Language Service's existing
diagnostic messages. Such a modularization has two primary advantages:

- Centralization and decoupling of error messages from the code that
  generates them makes it easy to add/delete/edit diagnostic messages,
  and allows for independent iteration of diagnostic messages and
  diagnostic generation.
- Prepares for additional features like annotating the locations where a
  diagnostic is generated and enabling the configuration of which
  diagnostics should be reported by the language service.

Although it would be preferable to place the diagnostics registry in an
independent JSON file, for ease of typing diagnostic types as an enum
variant of 'ts.DiagnosticCategory', the registry is stored as an object.

Part of #32663.

PR Close #35678
2020-03-03 08:49:34 -08:00
Keen Yee Liau 208ef7bd62 refactor(compiler): Remove NullAstVisitor and visitAstChildren (#35619)
This commit removes the `NullAstVisitor` and `visitAstChildren` exported
from `packages/compiler/src/expression_parser/ast.ts` because they
contain duplicate and buggy implementation, and their use cases could be
sufficiently covered by `RecursiveAstVisitor` if the latter implements the
`visit` method. This use case is only needed in the language service.

With this change, any visitor that extends `RecursiveAstVisitor` could
just define their own `visit` function and the parent class will behave
correctly.

A bit of historical context:
In language service, we need a way to tranverse the expression AST in a
selective manner based on where the user's cursor is. This means we need a
"filtering" function to decide which node to visit and which node to not
visit. Instead of refactoring `RecursiveAstVisitor` to support this,
`visitAstChildren` was created. `visitAstChildren` duplicates the
implementation of `RecursiveAstVisitor`, but introduced some bugs along
the way. For example, in `visitKeyedWrite`, it visits
```
obj -> key -> obj
```
instead of
```
obj -> key -> value
```

Moreover, because of the following line
```
visitor.visit && visitor.visit(ast, context) || ast.visit(visitor, context);
```
`visitAstChildren` visits every node *twice*.

PR Close #35619
2020-02-28 07:13:17 -08:00
ayazhafiz 73ee57a6e3 test(language-service): differentiate feature and internal infra tests (#35688)
This commit differentiates language service feature and language service
infrastructure tests. This is primarily to make testing of different
components at the development level easier. This commit continues a
small effort to expand our test coverage and normalize testing
structure.

Also adds test coverage to language service tests. We have quite a bit
to go on that front 🙂.

PR Close #35688
2020-02-28 07:10:46 -08:00
Ayaz Hafiz 500e49f0ac refactor(language-service): normalize hover tests (#35656)
This commit normalizes hover and util tests in the language service.
This is part of a small effort to simplify and normalize the language
service testing structure, which currently contains specs that are
largely created and left without relation to other tests.

PR Close #35656
2020-02-26 12:57:49 -08:00
Felix Becker 1e20b2ca36 build(packaging): add repository.directory field to package.jsons (#27544)
PR Close #27544
2020-02-25 13:12:45 -08:00
ivanwonder 8e354dae00 fix(language-service): get the right 'ElementAst' in the nested HTML tag (#35317)
For example, '<div><p string-model~{cursor}></p></div>', when provide the hover info for 'string-model', the 'path.head' is root tag 'div'. Use the parent of 'path.tail' instead.

PR Close #35317
2020-02-25 13:12:08 -08:00
Greg Magolan dde68ff954 build: add npm_integration_test && angular_integration_test (#33927)
* it's tricky to get out of the runfiles tree with `bazel test` as `BUILD_WORKSPACE_DIRECTORY` is not set but I employed a trick to read the `DO_NOT_BUILD_HERE` file that is one level up from `execroot` and that contains the workspace directory. This is experimental and if `bazel test //:test.debug` fails than `bazel run` is still guaranteed to work as  `BUILD_WORKSPACE_DIRECTORY` will be set in that context

* test //integration:bazel_test and //integration:bazel-schematics_test exclusively

* run "exclusive" and "manual" bazel-in-bazel integration tests in their own CI job as they take 8m+ to execute

```
//integration:bazel-schematics_test                                      PASSED in 317.2s
//integration:bazel_test                                                 PASSED in 167.8s
```

* Skip all integration tests that are now handled by angular_integration_test except the tests that are tracked for payload size; these are:
- cli-hello-world*
- hello_world__closure

* add & pin @babel deps as newer versions of babel break //packages/localize/src/tools/test:test

@babel/core dep had to be pinned to 7.6.4 or else //packages/localize/src/tools/test:test failed. Also //packages/localize uses @babel/generator, @babel/template, @babel/traverse & @babel/types so these deps were added to package.json as they were not being hoisted anymore from @babel/core transitive.

NB: integration/hello_world__systemjs_umd test must run with systemjs 0.20.0
NB: systemjs must be at 0.18.10 for legacy saucelabs job to pass
NB: With Bazel 2.0, the glob for the files to test `"integration/bazel/**"` is empty if integation/bazel is in .bazelignore. This glob worked under these conditions with 1.1.0. I did not bother testing with 1.2.x as not having integration/bazel in .bazelignore is correct.

PR Close #33927
2020-02-24 08:59:18 -08:00
ivanwonder 049f118c1b fix(language-service): provide hover for interpolation in attribute value (#35494)
I think the bug is introduced in my PR#34847. For example, 'model="{{title}}"', the attribute value cannot be parsed by 'parseTemplateBindings'.

PR Close #35494
2020-02-21 12:35:51 -08:00
Pusztai Tibor 54fd33fb80 fix(language-service): infer context type of structural directives (#35537) (#35561)
PR Close #35561
2020-02-21 09:11:54 -08:00
ivanwonder 66c06eb1ad fix(language-service): provide completions for the structural directive that only injects the 'ViewContainerRef' (#35466)
The completions list don't include the 'ngTemplateOutlet'. The directive is a structural directive when it has injected the 'TemplateRef' or 'ViewContainerRef'.

PR Close #35466
2020-02-20 15:12:50 -08:00
Keen Yee Liau 81241af7ac fix(language-service): Suggest ? and ! operator on nullable receiver (#35200)
Under strict mode, the language service fails to typecheck nullable
symbols that have already been verified to be non-null.

This generates incorrect (false positive) and confusing diagnostics
for users.

To work around this issue in the short term, this commit changes the
diagnostic message from an error to a suggestion, and prompts users to
use the safe navigation operator (?) or non-null assertion operator (!).

For example, instead of

```typescript
{{ optional && optional.toString() }}
```

the following is cleaner:

```typescript
{{ optional?.toString() }}
{{ optional!.toString() }}
```

Note that with this change, users who legitimately make a typo in their
code will no longer see an error. I think this is acceptable, since
false positive is worse than false negative. However, if users follow
the suggestion, add ? or ! to their code, then the error will be surfaced.
This seems a reasonable trade-off.

References:

1. Safe navigation operator (?)
   https://angular.io/guide/template-syntax#the-safe-navigation-operator----and-null-property-paths
2. Non-null assertion operator (!)
   https://angular.io/guide/template-syntax#the-non-null-assertion-operator---

PR closes https://github.com/angular/angular/pull/35070
PR closes https://github.com/angular/vscode-ng-language-service/issues/589

PR Close #35200
2020-02-10 16:43:44 -08:00
Keen Yee Liau a8609ba0ad refactor(language-service): dedupe diagnostics using ts utility function (#35086)
This commit makes a minor refactoring that replaces `uniqueBySpan` with
`ts.sortAndDeduplicateDiagnostics()`.

PR Close #35086
2020-02-03 08:57:58 -08:00
Keen Yee Liau c8584e5d36 refactor(language-service): Replace TypeDiagnostic with ng.Diagnostic (#35085)
This commit cleans up `expression_type.ts` by

1. Removing the unnecessary `TypeDiagnostic` class. It's replaced by
`ng.Diagnostic`.
2. Consolidating `reportError()` and `reportWarning()` to
`reportDiagnostic()`.

This is prep work so that we could make some of the type diagnostics a
suggestion in later PRs.

PR Close #35085
2020-02-03 08:57:18 -08:00
Ayaz Hafiz b608fa55f0 fix(language-service): warn, not error, on missing context members (#35036)
The language service reports an error when a directive's template
context is missing a member that is being used in a template (e.g. if
`$implicit` is being used with a template context typed as `any`).
While this diagnostic message is valuable, typing template contexts
loosely as `any` or `object` is very widespread in community packages,
and often still compiles correctly, so reporting the diagnostic as an
error may be misleading to users.

This commit changes the diagnostic to be a warning, and adds additional
information about how the user can eliminate the warning entirely -- by
refining the template context type.

Closes https://github.com/angular/vscode-ng-language-service/issues/572

PR Close #35036
2020-01-29 12:22:09 -08:00
ayazhafiz b64ead5cb8 fix(language-service): prune duplicate returned definitions (#34995)
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see https://github.com/angular/angular/pull/34847#discussion_r371006680.

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.

PR Close #34995
2020-01-29 12:21:04 -08:00
ayazhafiz e7c74cbd69 feat(language-service): completions for output $event properties in (#34570)
This commit adds support for completions of properties on `$event`
variables in bound outputs.

This is the second major PR to support completions for `$event`
variables (https://github.com/angular/vscode-ng-language-service/issues/531).
The final completion support that must be provided is for `$event`
variables in bindings targeting DOM events, like `(click)`.

PR Close #34570
2020-01-28 09:07:43 -08:00
ayazhafiz 24864ee71e feat(language-service): provide completion for $event variable (#34570)
This commit adds a completion for the `$event` variable in bound event
expressions.

This is the first in a series of PRs to support completions for
properties on `$event` variables (https://github.com/angular/vscode-ng-language-service/issues/531).

PR Close #34570
2020-01-28 09:07:43 -08:00
Ayaz Hafiz 90b303faa3 refactor(language-service): make findOutputBinding a utility function (#34997)
This commit makes `findOutputBinding` a utility function for the
language service, which will be used by the `expression_diagnostics`
module in #34570. Keeping the function in `locate_symbol` results in a
circular dependency between `expression_diagnostics` and
`locate_symbol`.

PR Close #34997
2020-01-27 15:53:57 -08:00
ivanwonder 09869af90f fix(language-service): remove repeated symbol definitions for structural directive (#34847)
For the structural directive, the 'path' will contain multiple `BoundDirectivePropertyAst` which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 `BoundDirectivePropertyAst`, 'ngForOf' and 'ngForTrackBy').

PR Close #34847
2020-01-27 10:47:46 -08:00
ivanwonder e7dff9eb0c feat(language-service): provide hover for microsyntax in structural directive (#34847)
PR Close #34847
2020-01-27 10:47:46 -08:00
Keen Yee Liau 3414ef276e refactor(language-service): Avoid leaking host outside of LanguageService (#34941)
As part of the effort to tighten the API surface of
`TypeScriptServiceHost` in preparation for the migration to Ivy, I realized
some recently added APIs are not strictly needed.
They can be safely removed without sacrificing functionality.

This allows us to clean up the code, especially in the implementation of
QuickInfo, where the `TypeScriptServiceHost` is leaked outside of the
`LanguageService` class.

This refactoring also cleans up some duplicate code where the QuickInfo
object is generated. The logic is now consolidated into a simple
`createQuickInfo` method shared across two different implementations.

PR Close #34941
2020-01-24 15:53:52 -08:00
Keen Yee Liau 0223aa6121 fix(language-service): Diagnostic span should point to class name (#34932)
Right now, if an Angular diagnostic is generated for a TypeScript node,
the span points to the decorator Identifier, i.e. the Identifier node
like `@NgModule`, `@Component`, etc.
This is weird. It should point to the class name instead.
Note, we do not have a more fine-grained breakdown of the span when
diagnostics are emitted, this work remains to be done.

PR Close #34932
2020-01-23 15:55:44 -08:00
Keen Yee Liau b50ed5c22c fix(language-service): Make metadata in Declaration non-optional (#34936)
The `metadata` field in `Declaration` should not be optional.

PR Close #34936
2020-01-23 15:54:59 -08:00
ayazhafiz 4ba478267e feat(language-service): specific suggestions for template context diags (#34751)
This commit elaborates diagnostics produced for invalid template
contexts by including the name of the embedded template type using the
template context, and in the common case that the implicity property is
being referenced (e.g. in a `for .. of ..` expression), suggesting to
refine the type of the context. This suggestion is provided because
users will sometimes use a base class as the type of the context in the
embedded view, and a more specific context later on (e.g. in an
`ngOnChanges` method).

Closes https://github.com/angular/vscode-ng-language-service/issues/251

PR Close #34751
2020-01-23 12:25:42 -08:00
Paul Gschwendtner 970b22f98e test: setup circular dependency tests for all entry points (#34774)
Sets up circular dependency tests for all entry-points in the
project (except for the ones part of a deprecated package).

PR Close #34774
2020-01-23 11:36:40 -08:00
ayazhafiz 68025ce09a test(language-service): recognize inputs/ouputs declared in decorator (#34875)
This commit adds a regression test to check that the language service
recognizes inputs and outputs declared in a directive decorator.

See #34874.

PR Close #34875
2020-01-23 10:15:12 -08:00
Ayaz Hafiz 1ea04ffc05 feat(language-service): support multiple symbol definitions (#34782)
In Angular, symbol can have multiple definitions (e.g. a two-way
binding). This commit adds support for for multiple definitions for a
queried location in a template.

PR Close #34782
2020-01-23 10:14:19 -08:00
Keen Yee Liau f723a27a71 refactor(language-service): Consistent naming between ts and ng LanguageService (#34888)
This commit makes the Angular Language Service interface a strict subset
of TypeScript's Language Service by renaming all methods to be
consistent with TypeScript's.

The custom Angular `LanguageService` interface was needed before the
inception of TypeScript tsserver plugin, but is now obsolete since
Angular LS is a proper tsserver plugin.

This allows us to easily adapt to upstream TS changes in the future, and
also allows us to reuse all data types defined in TypeScript.

PR Close #34888
2020-01-22 14:36:04 -05:00
ayazhafiz 6c8e322fa1 refactor(language-service): cleanup of low-hanging TODOs (#34784)
Cleans up some simple TODOs. Also removes
`typescript_symbols#getTypeWrapper`, which I thought I had but failed to
remove in #34571.

PR Close #34784
2020-01-21 13:09:30 -05:00
Greg Magolan 93c2df23bf build: upgrade to rules_nodejs 1.0.0 (first stable release) (#34736)
Brings in the fix for stamping which was preventing many targets from getting cached.

PR Close #34736
2020-01-15 14:58:07 -05:00
Andrius 1f79e624d1 build: typescript 3.7 support (#33717)
This PR updates TypeScript version to 3.7 while retaining compatibility with TS3.6.

PR Close #33717
2020-01-14 16:42:21 -08:00
ayazhafiz d7ea389c84 feat(language-service): provide completion for $event variable (#34566)
This commit adds a completion for the `$event` variable in bound event
expressions.

This is the first in a series of PRs to support completions for
properties on `$event` variables (https://github.com/angular/vscode-ng-language-service/issues/531).

PR Close #34566
2020-01-14 15:54:02 -08:00
Keen Yee Liau 84c659e246 refactor(language-service): Construct proper template AST from HTML ast (#34764)
The Template AST that corresponds to a given HTML AST is not always
complete, and often has to be reconstructed. This commit refactors the
code to make it easier to adapt to multiple cases.

PR Close #34764
2020-01-14 13:32:58 -08:00
ivanwonder 1a453872c8 fix(language-service): apply suggested change. (#34564)
PR Close #34564
2020-01-14 09:30:27 -08:00
ivanwonder 6f916c1240 fix(language-service): only visit directives (#34564)
PR Close #34564
2020-01-14 09:30:27 -08:00
ivanwonder c8c6fd7153 refactor(language-service): test case (#34564)
PR Close #34564
2020-01-14 09:30:27 -08:00
ivanwonder 7005645592 fix(language-service): break the hover/definitions for two-way binding (#34564)
PR Close #34564
2020-01-14 09:30:27 -08:00