Commit graph

549 commits

Author SHA1 Message Date
Alex Rickabaugh
7de1469be6 feat(core): introduce EnvironmentProviders wrapper type (#47669)
This commit introduces a new type `EnvironmentProviders` which can be used
in contexts where Angular accepted `Provider`s destined for
`EnvironmentInjector`s. This includes contexts such as `@NgModule.providers`
and `Route.providers`.

The new type is useful for preventing such providers from accidentally
ending up in `@Component.providers`. It can be used as the return type of
provider functions (such as `provideRouter`) to enforce this safety.

Because `Provider` allows `any[]` nested arrays, the compile-time safety
provided by `EnvironmentProviders` is easily circumvented. However, the
runtime shape of `EnvironmentProviders` is not compatible with component
injectors and will result in a runtime error if it leaks through (NG0207).

A new function `makeEnvironmentProviders` is used to construct this new type
from an array of providers.

The existing `importProvidersFrom` operation previously returned a very
similar type `ImportedNgModuleProviders` which had the same goal. This
machinery is switched over to use the new `EnvironmentProviders` interface
instead (in fact, `ImportedNgModuleProviders` is now just an alias to
`EnvironmentProviders`).

PR Close #47669
2022-10-07 14:03:13 -07:00
Kristiyan Kostadinov
db28badfe6 feat(core): enable the new directive composition API (#47642)
Enables the new directive composition API by exposing the `hostDirectives` property on the `Directive` and `Component` decorators. Also cleans up some casts that were put in place while the feature was being developed.

Fixes #8785.

PR Close #47642
2022-10-06 18:11:45 +00:00
Kristiyan Kostadinov
02f3d12a0d refactor(core): support host directives on a root component (#47620)
Adds support for host directives on the root component by calling `findHostDirectiveDefs` and passing the results to the relevant functions.

PR Close #47620
2022-10-04 11:51:29 -07:00
Kristiyan Kostadinov
07d9a27da2 refactor(core): handle ngOnChanges in host directives (#47597)
The `NgOnChanges` feature matches some legacy ViewEngine behavior where the keys in the `SimpleChanges` object are based on the *declared* names of the inputs, not the public or minified names. This is achieved by constructing the `DirectiveDef.declaredInputs` object at the same time as when `DirectiveDef.inputs` is created.

This logic breaks down for host directives, because they can re-alias the input under a different public name which won't be present in the `declaredInputs`.

These changes add some logic to patch the directive def aliases onto the `declaredInputs`. There is some validation in place to ensure that this patching doesn't overwrite any pre-existing inputs.

PR Close #47597
2022-10-03 10:07:51 -07:00
Kristiyan Kostadinov
50f8928d56 refactor(core): add host directive definitions validation (#47589)
Adds some logic to ensure that host directives are configured correctly.

PR Close #47589
2022-09-30 10:52:12 -07:00
Ciprian Sauliuc
752e2245f4 docs: add oxford commas and rephrase sentences (#47540)
PR Close #47540
2022-09-29 16:41:32 -07:00
Kristiyan Kostadinov
76a8c68cc1 refactor(core): add input and output filtering for host directives (#47536)
Adds the logic that will filter out unexposed inputs/outputs and apply the aliases that the author specified when writing the host directives.

PR Close #47536
2022-09-29 15:48:59 -07:00
Andrew Kushnir
120555a626 feat(core): support object-based DI flags in TestBed.inject() (#46761)
This commit applies the changes similar to the ones performed for the `inject()` function in df246bb235.

The `TestBed.inject` function is updated to use previously added object-based API for options: now the flags argument supports passing an object which configures injection flags.

DEPRECATED:

The bit field signature of `TestBed.inject()` has been deprecated, in favor of the new options object.

PR Close #46761
2022-09-27 10:09:53 -07:00
Andrew Kushnir
841c8e5138 feat(core): support object-based DI flags in Injector.get() (#46761)
This commit applies the changes similar to the ones performed for the `inject()` function in df246bb235.

The `Injector.get` function is updated to use previously added object-based API for options: now the flags argument supports passing an object which configures injection flags.

DEPRECATED:

The bit field signature of `Injector.get()` has been deprecated, in favor of the new options object.

PR Close #46761
2022-09-27 10:09:53 -07:00
Kristiyan Kostadinov
3fe21a67ca refactor(core): expose host directives to their host through DI (#47476)
Exposes the host directives to the host and its descendants through DI. This can be useful, because it allows the host to further configure the host directives.

PR Close #47476
2022-09-23 13:58:17 -07:00
Kristiyan Kostadinov
adb1a61677 refactor(core): invoke basic host directives (#47430)
Expands the runtime to allow for basic host directives to be invoked within a template. This is achieved by making a second pass over the directives that were matched based on their selectors and producing a new array of directives that include host directives. Note that the ordering in the array is important, because it determines which host bindings and DI tokens will be overwritten.

PR Close #47430
2022-09-19 09:43:06 +02:00
Pawel Kozlowski
4e10a74941 fix(core): imply @Optional flag when a default value is provided (#47242)
Unify default value handling across injector and node injector: as long
as a default value is provided it has the same effect as specifying the
@Optional() flag.

Fixes #47109

PR Close #47242
2022-09-08 09:34:17 -07:00
Eduardo Speroni
dbed2cf079 fix(core): check if transplanted views are attached to change detector (#46974)
Prevents change detection on views transplanted in OnPush components that have been detached from change detection.

PR Close #46974
2022-08-08 11:33:29 -07:00
Andrew Kushnir
c1ad37d161 refactor(core): drop unused fields from the RootContext (#46806)
The `RootContext` implementation contained a number of fields that were needed to support an experimental `renderComponent` function. The `renderComponent` function was removed, which allows us to cleanup the `RootContext` further.

The only field that remains on the `RootContext` is the list of bootstrapped components. This list is presumably mostly unused right now (it just contains the current component) and further refactoring can happen in a followup PR.

PR Close #46806
2022-08-05 09:58:06 -07:00
Cédric Exbrayat
76790a63de fix(core): improve the missing control flow directive message (#46903)
Similarly to what has been done in #46846 for the extended diagnostics about missing control flow directive that was only mentioning that the `CommonModule` should be imported, this commit improves the validation done by the JiT compiler.
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 #46903
2022-07-21 08:27:31 +00:00
Andrew Kushnir
7a6509bdc1 refactor(core): NgModuleRef should not implement EnvironmentInjector interface (#46896)
This commit refactors the `NgModuleRef` implementation to drop functions required by the `EnvironmentInjector` interface. Previously the idea was that the `NgModuleRef` can act as an Injector to facilitate easier transition to standalone. However, from the mental model perspective, the `NgModuleRef` has the `injector` field, which is the correct injector reference and can be used is needed as an `EnvironmentInjector`.

PR Close #46896
2022-07-20 08:49:15 -07:00
Andrew Scott
422f06d1cd Revert "fix(core): Fix runInContext for NgModuleRef injector (#46877)" (#46896)
This reverts commit 14081dc48d.

PR Close #46896
2022-07-20 08:49:15 -07:00
Andrew Scott
14081dc48d fix(core): Fix runInContext for NgModuleRef injector (#46877)
The `runInContext` for `NgModuleRef` was previously an infinite loop.

PR Close #46877
2022-07-19 09:37:32 -07:00
Andrew Kushnir
10becab70e feat(core): add reflectComponentType function (#46685)
This commit introduces a new function that allows creating a object which exposes a number of getters to retrieve information about a given component.

Closes #44926.

PR Close #46685
2022-07-14 18:52:37 +00:00
Andrew Kushnir
d1e83e1b30 feat(core): add createComponent function (#46685)
This commit introduces a new function that allows creating a `ComponentRef` instance based on provided Component and a set of options. The function can be used to cover a number of use-cases where the `ComponentFactory` symbol was used previously.

Closes #45263.

PR Close #46685
2022-07-14 18:52:37 +00:00
Andrew Kushnir
66e41223f3 refactor(core): improve an error message when ENVIRONMENT_INITIALIZER is not a multi provider (#46829)
Currently if the `ENVIRONMENT_INITIALIZER` token is not configured with `multi: true` flag, the code fails while trying to iterate over the value. This commit checks whether the `ENVIRONMENT_INITIALIZER` token value type is an array and throws a helpful error message.

PR Close #46829
2022-07-13 22:01:14 +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
Alex Rickabaugh
fa52b6e906 feat(core): options object to supersede bit flags for inject() (#46649)
`inject()` originated as a private API and was made public to support
`InjectionToken` factories in Ivy. For code-size and performance reasons,
when we code generate `inject()` calls we use a bit field to indicate the
various injection modes (optional, skip-self, etc). However, this doesn't
make for a very nice public API.

This commit introduces an alternative object-based API for options. All 4
flags are supported as `boolean` fields on an options object, and converted
to bit flags internally. If TypeScript can prove that `optional` injection
is not requested, it can narrow the return type and remove the `null` type.

DEPRECATED:

The bit field signature of `inject()` has been deprecated, in favor of the
new options object. Correspondingly, `InjectFlags` is deprecated as well.

Fixes #46251

PR Close #46649
2022-07-01 10:21:18 -07:00
Pawel Kozlowski
af20112222 feat(core): support the descendants option for ContentChild queries (#46638)
This change adds the support for the descendants option for ContentChild queries.

Closes #31921

PR Close #46638
2022-07-01 10:20:26 -07:00
Alex Rickabaugh
a7a14df5f8 feat(core): introduce EnvironmentInjector.runInContext API (#46653)
This commit introduces a new API on `EnvironmentInjector` to run a function
in the context of the injector. This makes `inject()` available within the
body of the function to inject dependencies. We expect this functionality to
be very useful for designing ergonomic APIs both in and out of Angular, as
it should allow for a smaller and more functional API style.

Note that it's possible to implement nearly identical functionality in user
code already today:

```typescript
function runInContext<T>(injector: EnvironmentInjector, fn: () => T): T {
  const token = new InjectionToken<T>('TOKEN');
  const tmpInjector = createEnvironmentInjector([
    {provide: token, useFactory: () => fn()},
  ], injector);

  return tmpInjector.get(token);
}
```

The factory provider for `token` in this example runs in the context of the
`tmpInjector`, giving it access to `inject` that can retrieve values from
`injector` as well. This is nearly identical, although because of the child
injector `self` and `skipSelf` injection options don't function correctly.

PR Close #46653
2022-07-01 08:54:52 -07:00
Alex Rickabaugh
1434ba904d refactor(core): remove experimental Renderer3 abstraction (#46605)
This commit removes the `Renderer3` experiment which attempted to use the
real DOM API as Angular's renderer. As shown in the diff, having this
experiment around added real code complexity to Angular that could not be
removed by an optimizer.

Since we no longer feel this experiment is worth continuing, we're removing
the `Renderer3` concept and all supporting code.

PR Close #46605
2022-06-30 09:04:56 -07:00
Alex Rickabaugh
31b396d49c test(core): migrate tests off of Renderer3 interfaces (#46605)
This commit migrates any remaining Angular tests which are using some form
of Renderer3 interfaces. Instead, they're switched to Renderer2.

PR Close #46605
2022-06-30 09:04:56 -07:00
Pawel Kozlowski
08733b0566 test(core): remove enableRenderer3 and Renderer3 from tests (#46612)
Remove calls to enableRenderer3 in the functional unit tests.
This effectivelly cuts code paths going through the Renderer3
in the functional tests.

PR Close #46612
2022-06-29 11:18:22 -07:00
Andrew Kushnir
d287d9cb33 test(core): move Pipe-related tests to the acceptance folder (#46571)
This commit refactors a  Pipe-related test to use TestBed and moves it to the `acceptance` folder.

PR Close #46571
2022-06-29 10:01:35 -07:00
Alex Rickabaugh
a322b7d694 refactor(core): disable renderer3 unless explicitly enabled by tests (#46530)
The `Renderer3` abstraction in Angular was an experimental code path in Ivy
which uses direct DOM operations instead of the former `Renderer2` path. To
allow `Renderer2` to tree-shake away, `Renderer3` is the default _unless_
`Renderer2` is provided. It was only an experiment, and never meant to be a
production code path.

However, it's possible for `Renderer3` to leak into user code. This commit
prevents that possibility by causing the `Renderer3` path to throw, unless
an explicit function has been called to enable it.

PR Close #46530
2022-06-28 11:15:54 -07:00
Pawel Kozlowski
2ea991633a test(core): convert view container TemplateFixture tests to TestBed (#46544)
Convert all ViewContainerRef tests with the hand-written generated
code to TestBed (and use code generated by the JiT compiler).

PR Close #46544
2022-06-28 11:15:02 -07:00
Pawel Kozlowski
879ac84862 test(core): convert remaining query tests to TestBed (#46549)
This commit moves the remaining hand-written query tests to TestBed.

PR Close #46549
2022-06-28 11:14:33 -07:00
Andrew Kushnir
ff071bbc88 test(core): replace hand-written instructions in component tests with TestBed (#46532)
This commit updates a set of misc tests to avoid using hand-written instructions and replace them with TestBed APIs.

PR Close #46532
2022-06-28 09:36:18 -07:00
Andrew Kushnir
a3f7046380 test(core): replace hand-written instructions in tests with TestBed APIs (#46525)
This commit updates a set of tests to avoid using hand-written instructions and replace them with TestBed APIs. Some tests were moved to the `acceptance` folder to colocate them with other renderer-related tests.

PR Close #46525
2022-06-27 15:43:10 -07:00
Pawel Kozlowski
2e59f9f612 test(core): convert listener TemplateFixture tests to TestBed (#46517)
Convert some of the query tests (read option) from TemplateFixture
to TestBed.

PR Close #46517
2022-06-27 15:16:11 -07:00
Pawel Kozlowski
e42db01dc0 test(core): convert listener TemplateFixture tests to TestBed (#46509)
Converts listeners tests with hand-written generated code to
TestBed fixtures.

PR Close #46509
2022-06-27 09:04:39 -07:00
Pawel Kozlowski
779da7f9c4 test(core): re-organize event listener tests (#46509)
Re-organize acceptance test for even listeners by moving
all the tests under an appropriate describe block.

PR Close #46509
2022-06-27 09:04:39 -07:00
JoostK
ddd6d66203 fix(core): deduplicate imports of standalone components in JIT compiler (#46439)
During JIT compilation of standalone components the compiler did not deduplicate
declarations in the imports array, unlike the AOT compiler. This may result in
runtime errors during directive matching, when the same component is found
multiple times resulting in NG0300, for example:

> NG0300: Multiple components match node with tagname mat-form-field: MatFormField and MatFormField.

This commit fixes the issue by deduplicating imports in the JIT compiler.

Relates to https://github.com/angular/angular/issues/46109#issuecomment-1160705031

Closes #46109

PR Close #46439
2022-06-21 12:41:16 -07:00
Kristiyan Kostadinov
08d3db232c fix(platform-server): invalid style attribute being generated for null values (#46433)
Fixes that the server renderer was producing an invalid `style` attribute when a null value is passed in. Also aligns the behavior with the DOM renderer by removing the attribute when it's empty.

Fixes #46385.

PR Close #46433
2022-06-21 11:54:52 -07:00
Andrew Kushnir
bb7c80477b fix(core): make parent injector argument required in createEnvironmentInjector (#46397)
Previously, the `createEnvironmentInjector` function allowed creating an instance of an EnvironmentInjector without providing a parent injector. This resulted in an injector instance, which was detached from the DI tree, thus having limited value. This commit updates the types of the `createEnvironmentInjector` function to make the parent injector a required argument.

PR Close #46397
2022-06-17 09:17:24 -07:00
dario-piotrowicz
0d10fe52f1 refactor(core): add helpful info in the pipe not found error message (#46247)
Add info to the pipe not found error message so to give some help to the developer for
resolving the problem more efficiently

(Note: this change also distinguishes the case in which the hosting component is standalone)

PR Close #46247
2022-06-06 10:13:28 -07:00
dario-piotrowicz
d846bba678 refactor(core): improve code around element validation (#46175)
improve code regarding element validation by creating a new file
exporting validation functions and not exporting utils previously
globally available

PR Close #46175
2022-06-06 10:10:41 -07:00
Andrew Kushnir
b7394bf77f fix(core): include component name into unknown element/property error message (#46160)
This commit adds the component name into unknown element/property error message, so that it's easier to find a location of a template where the problem happened.

Closes #46080.

PR Close #46160
2022-05-27 11:45:48 -07:00
Andrew Kushnir
bd6d82bcb7 fix(core): better error message when unknown property is present (#46147)
Prior to this commit, the error message that was produced for the unknown property situation, din't contain extra info on how the problem can be fixed. This commit adds more info to the error message and makes it similar to the one we use during the AOT compilation.

PR Close #46147
2022-05-26 11:24:47 -07:00
dario-piotrowicz
a1e5aad5dc fix(core): improve TestBed declarations standalone error message (#45999)
improve the error message developers get when adding a standalone
component in the TestBed.configureTestingModule's declarations array,
by making more clear the fact that this error originated from the
TestBed call

resolves #45923

PR Close #45999
2022-05-25 12:20:52 -07:00
Andrew Kushnir
985f8d721e fix(core): produce proper error message for unknown props on <ng-template>s (#46068)
Currently for cases when an unknown structural directive is applied to `<ng-template>`s, an error message thrown by the framework doesn't contain a tag name, for example:

```
NG0303: Can't bind to 'unknownDir' since it isn't a known property of 'null'.
```

The underlying reason is that the tag name for the `<ng-template>` is not produced (`null` is useed as a value) by the compiler in case of inline templates and runtime logic relies on this effect.

This commit handles this situation when an error message is thrown, as the fastest way to improve the error message. More refactoring would be needed to avoid relying on the mentioned effect at runtime.

PR Close #46068
2022-05-23 10:03:23 -07:00
dario-piotrowicz
b1a4b305db fix(core): update unknown tag error for jit standalone components (#45920)
update the error message presented during jit compilation when an unrecognized
tag/element is found in a standalone component so that it does not mention
the ngModule anymore

Note: the aot variant is present in PR #45919

resolves #45818

PR Close #45920
2022-05-23 09:27:08 -07:00
Alex Rickabaugh
9c1735bdde fix(core): set correct context for inject() for component ctors (#45991)
The `inject()` function was introduced with Ivy to support imperative
injection in factory/constructor contexts, such as directive or service
constructors as well as factory functions defined in `@Injectable` or
`InjectionToken`. However, `inject()` in a component/directive constructor
did not work due to a flaw in the logic for creating the internal factory
for components/directives.

The original intention of this logic was to keep `ɵɵdirectiveInject` tree-
shakable for applications which don't use any component-level DI. However,
this breaks the `inject()` functionality for component/directive
constructors.

This commit fixes that issue and adds tests for all the various cases in
which `inject()` should function. As a result `ɵɵdirectiveInject` is no
longer tree-shakable, but that's totally acceptable as any application that
uses `*ngIf` or `*ngFor` already contains this function. It's possible to
change how `inject()` works to restore this tree-shakability if needed.

PR Close #45991
2022-05-13 11:57:43 -07:00
Cédric Exbrayat
a6675925b0 feat(core): allow to throw on unknown properties in tests (#45853)
Allows to provide a TestBed option to throw on unknown properties in templates:

```ts
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(), {
    errorOnUnknownProperties: true
  }
);
```

The default value of `errorOnUnknownProperties` is `false`, so this is not a breaking change.

PR Close #45853
2022-05-03 15:49:00 -07:00
Pawel Kozlowski
410d81f0e2 refactor(core): disallow standalone components in importProvidersFrom calls (#45837)
This commit narrows down acceptable argument types of the
`importProvidersFrom` function. More specifically, it rejects
standalone components as a source of imports.

PR Close #45837
2022-05-03 13:12:36 -07:00