Commit graph

2535 commits

Author SHA1 Message Date
Alan Agius
1b9fd46d14 feat(core): add support for Node.js version 18 (#47730)
This change aligns with the supported Node.js versions of the Angular CLI.
See: https://github.com/angular/angular-cli/pull/24026

BREAKING CHANGE: Angular no longer supports Node.js versions `14.[15-19].x` and `16.[10-12].x`. Current supported versions of Node.js are `14.20.x`, `16.13.x` and `18.10.x`.

PR Close #47730
2022-10-11 17:21:19 +00:00
JoostK
bc54687c7b fix(compiler-cli): exclude abstract classes from strictInjectionParameters requirement (#44615)
In AOT compilations, the `strictInjectionParameters` compiler option can
be enabled to report errors when an `@Injectable` annotated class has a
constructor with parameters that do not provide an injection token, e.g.
only a primitive type or interface.

Since Ivy it's become required that any class with Angular behavior
(e.g. the `ngOnDestroy` lifecycle hook) is decorated using an Angular
decorator, which meant that `@Injectable()` may need to have been added
to abstract base classes. Doing so would then report an error if
`strictInjectionParameters` is enabled, if the abstract class has an
incompatible constructor for DI purposes. This may be fine though, as
a subclass may call the constructor explicitly without relying on
Angular's DI mechanism.

Therefore, this commit excludes abstract classes from the
`strictInjectionParameters` check. This avoids an error from being
reported at compile time. If the constructor ends up being used by
Angular's DI system at runtime, then the factory function of the
abstract class will throw an error by means of the `ɵɵinvalidFactory`
instruction.

In addition to the runtime error, this commit also analyzes the inheritance
chain of an injectable without a constructor to verify that their inherited
constructor is valid.

BREAKING CHANGE: Invalid constructors for DI may now report compilation errors

When a class inherits its constructor from a base class, the compiler may now
report an error when that constructor cannot be used for DI purposes. This may
either be because the base class is missing an Angular decorator such as
`@Injectable()` or `@Directive()`, or because the constructor contains parameters
which do not have an associated token (such as primitive types like `string`).
These situations used to behave unexpectedly at runtime, where the class may be
constructed without any of its constructor parameters, so this is now reported
as an error during compilation.

Any new errors that may be reported because of this change can be resolved either
by decorating the base class from which the constructor is inherited, or by adding
an explicit constructor to the class for which the error is reported.

Closes #37914

PR Close #44615
2022-10-10 21:46:25 +00:00
Kristiyan Kostadinov
ed11a13c3c feat(core): drop support for TypeScript 4.6 and 4.7 (#47690)
Updates the version range in the compiler to require at least TypeScript 4.8. Note that I'm keeping the backwards-compatibility layer for 4.7 around for now until internal projects have been migrated to 4.8.

BREAKING CHANGE:
TypeScript versions older than 4.8 are no longer supported.

PR Close #47690
2022-10-10 16:18:56 +00:00
Charles Lyding
a792bf1703 perf(compiler-cli): minimize filesystem calls when generating shims (#47682)
Previously when a file was being analyzed to determine if a shim should
be generated, up to two calls to the host `fileExists` function per file
per generator were made. In the default host, each `fileExists` call made
two underlying file system calls. Following these calls, the file was then
read via `getSourceFile`. However, `getSourceFile` will return `undefined`
if the requested file does not exist. As a result, `getSourceFile` can be
used directly to request both potential file names and leverage the return
value to determine if the file does not exist. This avoids the need to call
`fileExists` at all.

PR Close #47682
2022-10-07 09:10:34 -07:00
Dylan Hunn
8df8c77915 refactor(compiler): Add getPotentialImportsFor method on template type checker (#47631)
`getPotentialImportsFor` returns an array of possible imports, including TypeScript module specifier and identifier name, for a requested trait in the context of a given component.

PR Close #47631
2022-10-05 13:44:05 -07:00
Martin Probst
19ad4987f9 fix(compiler-cli): use @ts-ignore. (#47636)
The previous commit 2e1dddec45 used `@ts-expect-error` to suppress the
current error, with the intent of being informed once that's no longer
an error, ie. when we updated to an upstream TS version that includes
this change.

However this unfortunately means the change is incompatible with the
fixed version, which prevents it from working with an updated TS version
in google3.

This change reverts back to the original `@ts-ignore` which is forwards
and backwards compatible, avoiding that problem (but unfortunately
losing the benefit of being notified once fixed).

PR Close #47636
2022-10-05 13:37:11 -07:00
Dylan Hunn
29194638c1 refactor(compiler): Rename PipeInScope -> PotentialPipe (#47561)
Now that `getPotentialTemplateDirectives` uses `PotentialDirective`, we should rename `PipeInScope` to match.

PR Close #47561
2022-10-04 14:40:04 -07:00
Dylan Hunn
79c016d433 refactor(compiler): Add getPotentialTemplateDirectives API method. (#47561)
`getPotentialTemplateDirectives` returns possible directives 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 standalone components in the language service.

PR Close #47561
2022-10-04 14:40:04 -07: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
Martin Probst
2e1dddec45 fix(compiler-cli): support hasInvalidatedResolutions. (#47585)
The latest TypeScript compiler exposes the previously private field
`hasInvalidatedResolutions`. That breaks Angular in the newer TS,
because the new field would be required on DelegatingCompilerHost.
However we cannot just add the field here, because it's not present in
the older compiler.

This change adds the field for delegation, which works at runtime
because the field is present. It suppresses the compiler error using a
`// @ts-expect-error`, which should be removed once Angular moves to a
TSC version that includes this change.

PR Close #47585
2022-10-04 11:50:47 -07:00
JoostK
8fcadaad48 perf(compiler-cli): cache source file for reporting type-checking diagnostics (#47471)
When reporting type-checking diagnostics in external templates we create a
`ts.SourceFile` of the template text, as this is needed to report Angular
template diagnostics using TypeScript's diagnostics infrastructure. Each
reported diagnostic would create its own `ts.SourceFile`, resulting in
repeatedly parsing of the template text and potentially high memory usage
if the template is large and there are many diagnostics reported. This commit
caches the parsed template in the template mapping, such that all reported
diagnostics get to reuse the same `ts.SourceFile`.

Closes #47470

PR Close #47471
2022-09-21 18:22:23 +02:00
Andrew Kushnir
710d1da4ed refactor(common): create an NgFor alias for NgForOf directive (#47309)
This commit adds a re-export of the `NgForOf` class as `NgFor` to improve the DX for cases when the directive is used as standalone. Developers can import `NgFor` class, which better matches the `ngFor` attribute used in a template.

PR Close #47309
2022-09-09 14:04:54 -07:00
JoostK
cf0c53aa1c fix(compiler): avoid errors for inputs with Object-builtin names (#47220)
Using raw objects as a lookup structure will inadvertently find methods defined on
`Object`, where strings are expected. This causes errors downstream when string
operations are applied on functions.

This commit switches over to use `Map`s in the DOM element schema registry to fix
this category of issues.

Fixes #46936

PR Close #47220
2022-09-06 11:55:13 -07:00
JoostK
4a53b7f36b refactor(compiler): show meaningful diagnostic when reporting a template error fails (#44001)
When the Angular compiler emits a diagnostic in a template file, it
forces TypeScript to parse that template. Templates are not TypeScript,
so this parse finds a bunch of parsing errors, which Angular then
ignores and we show the diagnostic anyways because we have more context.
This can lead to strange behavior in TypeScript because templates are so
weird that it can break the parser and crash the whole compiler.

For example, certain Angular templates can encounter failures fixed by
microsoft/TypeScript#45987, which are not easily debuggable and require
a TS upgrade to fix.

This commit introduces logic to handle the error gracefully, by falling
back to report the template error on the component class itself. The
diagnostic is extended to still reference the template location and
includes the failure's stack trace, to allow the parsing failure to be
reported to TypeScript (as parsing should in theory not cause a crash).

Closes #43970

PR Close #44001
2022-09-06 11:52:35 -07:00
Alan Agius
16f96eeabf refactor(compiler-cli): remove enableIvy options (#47346)
This option has no longer any effect as Ivy is the only rendering engine.

BREAKING CHANGE: Angular compiler option `enableIvy` has been removed as Ivy is the only rendering engine.

PR Close #47346
2022-09-06 11:33:48 -07:00
Kristiyan Kostadinov
54ceed53e2 refactor(compiler): add support for host directives (#46868)
This is the compile-time implementation of the `hostDirectives` feature plus a little bit of runtime code to illustrate how the newly-generated code will plug into the runtime. It works by creating a call to the new `ɵɵHostDirectivesFeature` feature whenever a directive has a `hostDirectives` field. Afterwards `ɵɵHostDirectivesFeature` will patch a new function onto the directive definition that will be invoked during directive matching.

For example, if we take the following definition:

```ts
@Directive({
  hostDirectives: [HostA, {directive: HostB, inputs: ['input: alias']}]
})
class MyDir {}
```

Will compile to:

```js
MyDir.ɵdir = ɵɵdefineComponent({
  features: [ɵɵHostDirectivesFeature([HostA, {
    directive: HostB,
    inputs: {
      input: "alias"
    }
  }])]
});
```

The template type checking is implemented during directive matching by adding the host directives applied on the host to the array of matched directives whenever the host is matched in a template.

Relates to #8785.

PR Close #46868
2022-08-22 16:00:35 -07:00
Dylan Hunn
edf7f670f0 refactor(compiler): Add a new helper method getOwningNgModule. (#47166)
This helper accepts a class for an Angular trait, and returns the NgModule which owns that trait. This will be useful for the language service import project, which needs to edit import arrays on the module.

PR Close #47166
2022-08-22 10:57:48 -07:00
Kristiyan Kostadinov
ddb95c5184 refactor(compiler): replace most usages of getMutableClone (#47167)
Replaces (almost) all of the usages of the deprecated `getMutableClone` function from TypeScript which has started to log deprecation warnings in version 4.8 and will likely be removed in version 5.0. The one place we have left is in the default import handling of ngtsc which will be more difficult to remove.

PR Close #47167
2022-08-22 10:40:50 -07:00
Dylan Hunn
fc97a41f07 refactor(compiler): Add a new helper method getPrimaryAngularDecorator. (#47180)
This helper accepts a class, and returns the primary Angular Decorator associated with that trait (e.g. the Component, Pipe, Directive, or NgModule decorator). This will be useful for the language service import project, which needs to edit import arrays inside the decorator.

PR Close #47180
2022-08-22 10:39:42 -07:00
Kristiyan Kostadinov
31429eaccc feat(core): support TypeScript 4.8 (#47038)
Adds support for TypeScript 4.8 and resolves some issues that came up as a result of the update.

Most of the issues came from some changes in TypeScript where the `decorators` and `modifiers` properties were removed from most node types, and were combined into a single `modifiers` array. Since we need to continue supporting TS 4.6 and 4.7 until v15, I ended up creating a new `ngtsc/ts_compatibility` directory to make it easier to reuse the new backwards-compatible code.

PR Close #47038
2022-08-16 16:02:47 +00:00
Paul Gschwendtner
c01ccc5582 test: update ngtsc closure fileoverview tests to reflect suppression updates (#47018)
tsickle has updated some of the suppressions. See for example:
19ee2d6938

PR Close #47018
2022-08-03 17:36:18 -07:00
Paul Gschwendtner
ea89677c12 feat(compiler-cli): support more recent version of tsickle (#47018)
Updates the tsickle version in the repository and accounts for its changes in
the `compiler-cli` package. Tsickle made a breaking change in the minor version
segment bump that would break the use with `@angular/compiler-cli`

Additionally the tsickle version for `@angular/bazel` is updated since
we updated `@bazel/typescript` to also account for the breaking changes.

See: 78a0528107

PR Close #47018
2022-08-03 17:36:18 -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
Asaf M
4ac2196695 docs: fixed typos (#47008)
Used my script: https://github.com/Uziel302/websites-spell-checker/blob/master/mdnversion

PR Close #47008
2022-08-01 11:15:54 -07:00
Cédric Exbrayat
49e1912e1d refactor(compiler-cli): remove unused error codes (#46847)
A few codes were unused, or no longer used.

PR Close #46847
2022-08-01 09:50:12 -07:00
dario-piotrowicz
3562eb107d refactor(compiler-cli): improve error for non-exported non-standalone (#46114)
improve the error message for non-standalone components which are not
exported from their module, and that are also imported directly as if
they were standalone

this change simply adds the suggestion to the developer to import the
ngModule instead

resolves #46004

PR Close #46114
2022-07-20 08:50:00 -07:00
Paul Gschwendtner
d42ed733d8 test: update source-map tests to account for source-map breaking change (#46888)
The source-map package now requires the
`SourceMapConsumer`/`SourceMapGenerator` classes to be instantiated
asynchronously. This commit updates our tests to account for that.

PR Close #46888
2022-07-19 09:40:54 -07:00
Paul Gschwendtner
f080ae5e51 build: remove source-map package from compiler-cli bundling external list (#46888)
The source-map package is no longer explicitly used in the
`compiler-cli` package and therefore can be removed from the ESBuild
bundling, denoting it as external. This should be a noop.

PR Close #46888
2022-07-19 09:40:54 -07:00
Cédric Exbrayat
7882746913 fix(compiler-cli): improve the missingControlFlowDirective message (#46846)
The extended diagnostics about missing control flow directive was only mentioning that the `CommonModule` should be imported.
Now that the control flow directives are available as standalone, the message mentions that directive itself can be imported.

The message now also mentions which import should be used for the directive (as it can be tricky to figure out that `NgForOf` is the directive corresponding to `*ngFor`).

PR Close #46846
2022-07-18 19:56:08 +00:00
JoostK
93d17ee888 fix(compiler-cli): only consider used pipes for inline type-check requirement (#46807)
After a bugfix in #46096, the compiler is now better capable of detecting pipes
which require an inline type constructor. However, there is an issue in how all
pipes are considered when verifying the inline type-ctor requirement: it should
only check actually used pipes.

Fixes #46747

PR Close #46807
2022-07-12 21:09:18 +00:00
JoostK
93c65e7b14 feat(compiler-cli): add extended diagnostic for non-nullable optional chains (#46686)
This commit adds an extended diagnostics check that is similar to the nullish
coalescing check, but targeting optional chains. If the receiver expression
of the optional chain is non-nullable, then the extended diagnostic can report
an error or warning that can be fixed by changing the optional chain into a
regular access.

Closes #44870

PR Close #46686
2022-07-12 17:45:00 +00:00
Andrew Kushnir
a6d5fe202c feat(core): alias createNgModuleRef as createNgModule (#46789)
This commit adds the `createNgModuleRef` function alias to the public API. The alias is called `createNgModule`. The `createNgModule` name is more consistent with the rest of the API surface, where functions that return `*Ref`s don't include the `Ref` into the function name, for example: `createPlatform`, `ViewContainerRef.createComponent`, etc.

DEPRECATED:

The `createNgModuleRef` is deprecated in favor of newly added `createNgModule` one.

PR Close #46789
2022-07-12 16:11:41 +00:00
John Vandenberg
c14c701775 docs: fix spelling (#46713)
PR Close #46713
2022-07-08 20:54:52 +00:00
Andrew Scott
9e836c232f feat(compiler): warn when style suffixes are used with attribute bindings (#46651)
This commit adds an extended diagnostic which warns when style suffixes such as '.px'
are used with attribute bindings (attr.width.px).

Fixes #36256

PR Close #46651
2022-07-08 18:32:58 +00:00
Jessica Janiuk
33ce3883a5 feat(compiler): Add extended diagnostic to warn when missing let on ngForOf (#46683)
In the case that a user accidentally forgot the let keyword, they dont get a very clear indicator of there being a problem.
They get an issue in the template iteration at runtime. This diagnostic will warn the user when the let keyword is missing.

PR Close #46683
2022-07-07 13:04:33 -07:00
Andrew Scott
6f11a58040 feat(compiler): Add extended diagnostic to warn when text attributes are intended to be bindings (#46161)
https://angular.io/guide/attribute-binding#attribute-class-and-style-bindings

Angular supports `attr.`, `style.`, and `class.` binding prefixes to
bind attributes, styles, and classes. If the key does not have the
binding syntax `[]` or the value does not have an interpolation `{{}}`,
the attribute will not be interpreted as a binding.

This diagnostic warns the user when the attributes listed above will not
be interpreted as bindings.

resolves #46137

PR Close #46161
2022-06-30 14:05:47 -07:00
Vaibhav Kumar
6c4d36f3ff docs: documentation text fix for compiler cli (#46541)
documentation text fix for compiler cli

Signed-off-by: Vaibhav Kumar <vaibhav.kumar@sourcefuse.com>

PR Close #46541
2022-06-28 09:35:32 -07:00
Paul Gschwendtner
05254f53ff build: replace extract_typings rule with shared rule from dev-infra (#46508)
This replaces the `extract_typings` rule with the shared rule from
dev-infra, avoiding us having to maintain two rules.

PR Close #46508
2022-06-27 09:05:09 -07:00
Alex Rickabaugh
714183d430 refactor(compiler-cli): add forceEmit flag to performCompilation (#46355)
In Bazel worker-land, workers which use incremental compilation must still
emit all declared outputs and cannot rely on these outputs persisting from
previous builds.

This commit adds a flag to `performCompilation` which can be used by the
worker infrastructure to instruct the compiler to always emit all possible
output files, regardless of any incremental build optimizations.

PR Close #46355
2022-06-14 15:56:32 +00:00
Andrew Kushnir
131d029da1 feat(compiler-cli): detect missing control flow directive imports in standalone components (#46146)
This commit adds an extended diagnostics check that verifies that all control flow directives (such as `ngIf`, `ngFor`) have the necessary directives imported in standalone components. Currently there is no diagnostics produced for such cases, which makes it harder to detect and find problems.

PR Close #46146
2022-06-10 21:22:53 +00:00
JoostK
f35f4751b5 fix(compiler-cli): use inline type-check blocks for components outside rootDir (#46096)
An inline type-check block is required when a reference to a component class
cannot be emitted from an ngtypecheck shim file, but the logic to detect this
situation did not consider the configured `rootDir`. When a `rootDir` is
configured the reference emitter does not allow generating an import outside
this directory, which meant that a shim file wouldn't be able to reference
the component class. Consequently, type-check block generation would fail
with a fatal error that is unaccounted for, as gathering diagnostics should
be non-fallible.

This commit fixes the problem by leveraging the existing `canReferenceType`
logic of the type-checking `Environment`, instead of the rudimentary check
whether the class is exported as top-level symbol (`checkIfClassIsExported`).
Instead, `canReferenceType` pre-flights the generation of an import using the
`ReferenceEmitter` to tell exactly whether it will succeed or not; thus taking
into account the `rootDirs` constraint as well.

Fixes #44999

PR Close #46096
2022-06-10 20:40:16 +00:00
Kristiyan Kostadinov
04acc6b14d fix(compiler-cli): don't emit empty providers array (#46301)
Saves us some bytes by not emitting `providers` in `defineInjector`. While the amount of bytes isn't huge, I think that this change is worthwhile, because `ng generate` currently generates `providers: []` with every `NgModule` which users can forget to remove.

PR Close #46301
2022-06-10 14:27:22 +00:00
Alex Rickabaugh
a006edefd1 refactor(compiler-cli): introduce onlyPublishPublicTypingsForNgModules (#45894)
When generating .d.ts metadata for NgModules, by default we emit type
references to their declarations, imports, and exports. However, this
information is not necessarily useful to consumers. References to private
directives (those that aren't exported by the NgModule) for example aren't
at all useful as they can only affect other components declared in the
NgModule. References to imports are of limited usefulness - they might be
helpful for an IDE to understand the DI structure of an application, but
aren't at all used by a downstream compiler.

Generating this metadata is not without cost. When an incremental build
system uses changes in inputs to determine when a rebuild is necessary, any
changes in .d.ts files might cause downstream targets to rebuild. If those
.d.ts changes are in the "private" side of the NgModule (imports or non-
exported directives/pipes), then these rebuilds are wholly unnecessary.

This commit introduces the `onlyPublishPublicTypingsForNgModules` flag for
the compiler. When this flag is set, the compiler will filter the emitted
references in NgModule .d.ts output and only reference those directives/
pipes that are exported from the NgModule (its public API surface). Omitting
the flag preserves the existing behavior of emitting all references, both
public and private.

This is especially useful for build systems such as Bazel.

PR Close #45894
2022-06-02 13:39:14 -07:00
Andrew Kushnir
a31d8218cc fix(core): update unknown property error to account for standalone components in AOT (#46159)
This commit updates the error message to use correct info depending on whether a component is standalone or not. Previously we were always referring to @NgModules as a place to fix the issue, but not we also mention @Component when needed (for standalone components).

PR Close #46159
2022-05-27 10:12:27 -07:00
Alex Rickabaugh
f0bf29a24d fix(compiler-cli): preserve forwardRef for component scopes (#46139)
Angular generally supports cycles between components in the same NgModule.
We have a mechanism of moving the component scope declaration into the
NgModule file in this case. This ensures that Angular never itself
introduces an import which creates a cycle.

What happens if the cycle already exists in the user's program, though, is a
bit different. In these cases, the "correct" emit for Angular is to generate
the component scope (whether direct or remote) inside of a closure, to
prevent evaluating the scope's references until module evaluation is
complete and all cyclic imports have been resolved. We don't want to do this
for *all* scopes because the code size cost of emitting a function wrapper
is non-zero.

In this fix, we take the presence of a `forwardRef` in a component's
`imports` or in an NgModule `declarations` or `imports` as a sign that
component scopes emitted into those files need to be protected against
cyclic references. In a future commit, we may introduce a warning or error
if cyclic imports are not protected behind `forwardRef` in these cases, but
this will take some time to implement.

PR Close #46139
2022-05-26 17:42:01 -07:00
dario-piotrowicz
8629f2d0af refactor(compiler-cli): fix "for to" typo in code comment (#46113)
fix the typo/extra for in a code comment saying
"this is used for to determine" so that it only says
"this is used to determine"

PR Close #46113
2022-05-24 10:52:17 -07:00
JoostK
e592cc7c1c refactor(compiler-cli): report better error when a ModuleWithProviders call occurs within Component.imports (#46009)
This commit improves the reported error when importing e.g. `RouterModule.forRoot()`
from within `Component.imports`. Such import is not supported, as standalone components
can only refer to other standalone entities or NgModules in their `imports` array;
`ModuleWithProviders` are not supported as `Component.imports` is meant to be used
for the compilation scope of the component, _not_ for configuring DI.

Closes #46003

PR Close #46009
2022-05-23 13:50:05 -07:00
JoostK
3d19f32942 refactor(compiler-cli): extract ModuleWithProviders analysis out of NgModule handler (#46009)
This commit moves the foreign function resolver logic for detecting a
`ModuleWithProviders` in a return type position of a function call, as the logic can
then be reused for standalone components in a subsequent commit.

PR Close #46009
2022-05-23 13:50:05 -07:00
Paul Gschwendtner
91070d9512 ci: fix formatting for babel_ast_host.ts file after recent changes (#46082)
The formatting of the `babel_ast_host.ts` file is invalid due to a
recently-merged PR. The PR had a passing `lint` state but this seemed
to just appear like this because the Git comparison range on upstream
branches can become invalid (due to a known bug in CircleCI -- reported)

PR Close #46082
2022-05-23 13:30:38 -07:00
dario-piotrowicz
18b0ee073f fix(compiler-cli): update unknown tag error for aot standalone components (#45919)
update the error message presented during aot compilation when an unrecognized
tag/element is found in a standalone component so that it does not mention
the ngModule anymore

Note: the jit variant is present in PR #45920

resolves #45818

PR Close #45919
2022-05-23 13:18:49 -07:00