Commit graph

3171 commits

Author SHA1 Message Date
Andrew Scott
72e6a948bb refactor(router): Update recognize to use Observable instead of Promise (#46021)
The `Observable` chain is currenlty the most straightforward way to
handle navigation cancellations where we ensure that the cancelled
navigation does not continue to be processed. Until we design and
implement an alternative way to accomplish equivalent functionality,
we need to maintain the `Observable` chain wherever we might execute
user code. One reason for this isthat user code may contain redirects so we do not
want to execute those redirects if the navigation was already cancelled.

PR Close #46021
2022-06-13 22:53:49 +00:00
Paul Draper
b1a3dec58d fix(core): Resolve forwardRef declarations for jit (#46334)
Fix forwardRef() usage for declarations for jit. Resolves #45741.

PR Close #46334
2022-06-13 16:59:59 +00:00
Andrew Scott
caa2f3560d refactor(router): clean up internal hooks (#46321)
* beforePreactivation hook is unused
* The only place that uses afterPreactivation does not use the arguments

Not to say we won't want to provide hooks similar to this in the future,
but the current state is over-engineered for what it's being used for.

PR Close #46321
2022-06-10 15:23:57 +00:00
Andrew Kushnir
aa2e8acd0e docs: annotate more APIs with the @developerPreview tag (#46234)
This commit updates a few more standalone-related APIs with the `@developerPreview` tag.

PR Close #46234
2022-06-06 11:37:55 -07:00
Andrew Kushnir
612b4088b9 refactor(core): export missing util function (#46274)
This commit exports the `isHostComponentStandalone` function and also changes a location where it's imported from. The function was moved recently in a different PR, which caused some conflicts after merging other PRs that relied on the old structure.

PR Close #46274
2022-06-06 11:35:25 -07:00
dario-piotrowicz
d4f11147f4 refactor(core): fix eaither typo in transitiveScopesFor comment (#46261)
fix the 'eaither' typo (instead of 'either') in the tsdoc comment
for the transitiveScopesFor function

PR Close #46261
2022-06-06 10:17:01 -07:00
Dylan Hunn
f18e1739b8 fix(forms): allow FormBuilder.group(...) to accept optional fields. (#46253)
Consider the case in which `FormBuilder` is used to construct a group with an optional field:

```
const controls = { name: fb.control('') };
const foo: FormGroup<{
  name: FormControl<string | null>;
  address?: FormControl<string | null>;
}> = fb.group<{
  name: FormControl<string | null>;
  address?: FormControl<string | null>;
}>(controls);
```

Today, with fully strict TypeScript settings, the above will not compile:

```
Types of property 'controls' are incompatible.
Type '{ name: FormControl<string | null>; address?: FormControl<FormGroup<SubFormControls> | null | undefined> | undefined; }' is not assignable to type '{ name: FormControl<string | null>; address?: FormGroup<SubFormControls> | undefined; }'.
```

Notice that the `fb.group(...)` is calculating the following type for address: `address?: FormControl<FormGroup<string|null>`. This is clearly wrong -- an extraneous `FormControl` has been added!

This is coming from the calculation of the result type of `fb.group(...)`. In the type definition, if we cannot detect the outer control type, [we assume it's just an unwrapped value, and automatically wrap it in `FormControl`](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L66).

Because the optional `{address?: FormControl<string|null>}` implicitly makes the RHS have type `FormControl<string|null>|undefined`, [the relevant condition is not satisfied](https://github.com/angular/angular/blob/14.0.0/packages/forms/src/form_builder.ts#L55). In particular, the condition expects just `FormGroup<T>`, not `FormGroup<T>|undefined`. So we assume `T` is a value type, and it gets wrapped with `FormControl`.

The solution is to add the cases where `undefined` is included in the union type when detecting which control `T` is (if any).

PR Close #46253
2022-06-06 10:14:19 -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
cc183928e1 refactor(core): split RuntimeError unit test up (#46239)
split the single RuntimeError utils test testing about the correct
formatting of errors in multiple separate unit tests (for better
granularity and clearness)

PR Close #46239
2022-06-06 10:12:06 -07:00
dario-piotrowicz
0b7ab075b7 refactor(core): improve the runtime errors formatting (#46239)
the formatted error messages always include a period separator between the
provided error message and the find-more suffix, this is not always
desirable as it may add periods when they shoud not be, so improve the
formatting by checking and applying the period only if the provided message
doesn't end with a character which already represents a separator

additionally also improve the formatting by trimming the provided error
message

note that such trimming needs to be performed before the separator check
so that for example an error message like `"some error! "` doesn't produce
`"some error! ."` but it successfully produces "some error!"

PR Close #46239
2022-06-06 10:12:05 -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
Kristiyan Kostadinov
6c44222fa1 refactor(core): don't use patched data in LifecycleHooksFeature (#46237)
We don't need to read patched data off the component instance since we already know what the `LView` is.

PR Close #46237
2022-06-03 10:06:19 -07:00
Kristiyan Kostadinov
0206c10f8e refactor(core): minor ComponentDef improvements (#46093)
Makes the following improvements in the runtime:
* Uses the unique ID of the component definition to keep track of its injector in the `StandaloneFeature`, instead of the definition itself. This reduces the amount of memory we can leak, if something doesn't get cleaned up.
* Changes the naming and description of the `ComponentDef.id` to reflect what it is used for.

PR Close #46093
2022-06-02 13:41:27 -07:00
Kristiyan Kostadinov
8fb737cfa4 refactor(core): remove unused error handler logic (#46216)
There's some old logic in the error handler that tries to read an `ngErrorHandler` property off of the errors that are being logged. As far as I can tell, this is a ViewEngine leftover and it isn't actually being used anywhere.

PR Close #46216
2022-06-02 13:40:16 -07:00
Kristiyan Kostadinov
d981aabfcf refactor(core): clean up closure deoptimization (#46149)
In #45445 function inlining had to be disabled on one of our instructions, because Closure wasn't optimizing it correctly, causing an error at runtime. The error has been resolved so we can remove the deoptimization hint.

PR Close #46149
2022-06-02 13:39:50 -07:00
Alex Rickabaugh
025903566e docs(core): mark the standalone APIs as Developer Preview (#46050)
This commit adds the new `@developerPreview` tag to all of the standalone
component related APIs. With this, AIO will show an API status label which
links to the documentation on Developer Preview.

PR Close #46050
2022-06-01 16:01:59 -07:00
Andrew Kushnir
6d1e6a8bc7 docs(core): improve standalone-related docs and add more usage examples (#46202)
This commit updates the `bootstrapApplication` and the `importProvidersFrom` function docs with additional content that includes usage examples.

PR Close #46202
2022-06-01 11:08:04 -07:00
Andrew Kushnir
9850ea499e docs: add the Standalone guide link to API docs of component/directive/pipe decorators (#46184)
This commit adds the Standalone guide link to the `@Component`, `@Directive` and `@Pipe` decorator properties related to standalone functionality to improve discoverability of the guide as well as providing extra context in API docs.

PR Close #46184
2022-05-31 13:56:57 -07:00
Andrew Kushnir
7f65089de8 docs(core): improve inject function docs (#46168)
This commit updates the `inject` function docs by:
- rephrasing a description to include more usage cases
- adding usage examples
- making a function itself a public API (vs its alias const that was used previously)

PR Close #46168
2022-05-31 13:55:14 -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
Pawel Kozlowski
12b4ec3d85 docs: create a dedicated page for the NG0203 error (#46166)
Adds a dedicated error page for NG0203 (injection context).

PR Close #46166
2022-05-27 10:15:55 -07:00
Pawel Kozlowski
6b52f03f5f fix(core): add more details to the MISSING_INJECTION_CONTEXT error (#46166)
The MISSING_INJECTION_CONTEXT runtime error can be thrown when the
inject function is used outside of the class creation context.
Before this change we've only used a vague terms of the
'injection context'. This commit extends the error message to
indicate that we really talk about the class construction context.

PR Close #46166
2022-05-27 10:15:55 -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
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 Scott
53ca936366 feat(router): Add ability to create UrlTree from any ActivatedRouteSnapshot (#45877)
This exposes a new function from the router public API that allows
developers to create a `UrlTree` from _any_ `ActivatedRouteSnapshot`.
The current Router APIs only support creating a `UrlTree` from an
`ActivatedRoute` which is both active _and_ materially appears in the
`UrlTree` (it cannot be an empty path named outlet). This is because the
implementation of the current way of creating a `UrlTree` attempts to
look up the `UrlSegment` of the `ActivatedRoute` in the currently active
`UrlTree` of the router. When this doesn't work, the `UrlTree` creation
fails.

Note that this API does not replace the current one. That would actually be a
breaking change but should be done at some point in the future (v15). That is,
`router.navigate` should call this new function. At that point, we can
remove `_lastPathIndex`, `_urlSegment`, and `_correctedPathIndex` from
the `ActivatedRoute`, along with all of the logic associated with
determining what those should be. In addition, this would unblock a fix
for #26081 because the `applyRedirects` and `recognize` operations
could be combined into one.  Overall, this would simplify logic in the router
and reduce code size. It also exposes core routing capabilities as a helper function
which were previously private API, which is a necessary step towards #42953.

As a stress test for this new function, it _was_ swapped in as the
default for `UrlTree` creation in #45859 and tested internally. The
results indicate that this function behaves correctly.

resolves #42191 (Tested directly)
resolves #38276 (The test with a guard covers this case)
resolves #22763 (Tested directly)

PR Close #45877
2022-05-24 10:40:24 -07:00
dario-piotrowicz
390e9896d3 docs: update directives info to include standalone (#46078)
update the directives api doc to inform developers that
directives can also be declared as standalone

PR Close #46078
2022-05-23 13:54:11 -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
Andrew Kushnir
dfba192706 fix(core): handle AOT-compiled standalone components in TestBed correctly (#46052)
Previously, the code in TestBed didn't take into account the fact that the `cmp.dependencies` array after the AOT compilation might contain regular (NgModule-based) Components/Directive/Pipes. As a result, some NgModule-specific code paths were invoked for non-NgModule types, thus leading to errors.

This commit updates the code to handle AOT-compiled structure of standalone components correctly.

PR Close #46052
2022-05-23 09:25:08 -07:00
Andrew Kushnir
8704574f7e fix(core): restore NgModule state correctly after TestBed overrides (#46049)
This commit updates the NgModule logic to account for a case when a type has more than one generated def. This is a common situation for NgModules which have at least two: ɵmod and ɵinj. Previously, the second def was not stored before applying overrides, thus leaving it modified after the test, leaking the state as a result. This fix ensures that we store all defs before applying any overrides.

PR Close #46049
2022-05-23 09:23:11 -07:00
George Kalpakas
cd0096df2c docs: fix inline JSDoc tags (@see --> @link) (#46040)
In some places, the [@see][1] JSDoc tag was incorrectly used instead of
the [@link][2] inline tag, leading to warnings during doc generation and
the `@see` tags being ignored (and thus shown in the docs as is).
Replace the `@see` tags with the intended `@link` tags.

[1]: https://jsdoc.app/tags-see.html
[2]: https://jsdoc.app/tags-inline-link.html

PR Close #46040
2022-05-19 13:32:20 -07:00
Andrew Kushnir
ca04ecedec refactor(core): avoid code duplication in standalone-related logic (#45949)
This commit updates the standalone-related logic to address the feedback from previous code reviews (specifically: https://github.com/angular/angular/pull/45687#discussion_r854499586).

PR Close #45949
2022-05-16 10:05:23 -07:00
Andrew Kushnir
cd04588643 refactor(core): tree-shake away a DI error message string in prod bundles (#45994)
This commit adds the `ngDevMode` check to tree-shake away an error message string (which also retains a reference to an extra function).

PR Close #45994
2022-05-16 10:04:57 -07:00
Andrew Scott
4d2e006372 refactor(router): Only create the Route injector when the path matches (#45990)
Currently, the `Router` will create the `EnvironmentInjector` for a
`Route` with `providers` as soon as it _attempts_ to match segments to
the `Route`. Instead, this change updates the logic to only create the
injector once we have confirmed that the `Route` matches. Note that this
will include partial matches where the final navigation still fails with
a "cannot match any routes to segment" error. We need to maintain the
injector hierarchy so we still need to create parent injectors before
descending into the child routes and determining if there is a full
match or just a partial one.

resolves #45988

PR Close #45990
2022-05-13 12:19:16 -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
Andrew Kushnir
eb3c96e7b1 test(core): reset Testability getter after each test (#45947)
This commit updates the Testability-related tests to reset the testability getter class after each test to avoid leaking the state between tests. This should improve the stability of the testability tests that are currently flaky on CI.

PR Close #45947
2022-05-10 16:02:53 -07:00
Andrew Kushnir
3165fa3f4e perf(platform-browser): avoid including Testability by default in bootstrapApplication (#45885)
The Testability-related logic was refactored in https://github.com/angular/angular/pull/45657 to become tree-shaking-friendly: it was decoupled from the core providers of the `BrowserModule`. This commit updates the newly-introduced `bootstrapApplication` function to exclude Testability-providers by default (note: the Testability is still included in the NgModule-based bootstrap).

In order to add the Testability to the app bootstrapped via `bootstrapApplication`, the `provideProtractorTestingSupport` function is introduced.

PR Close #45885
2022-05-10 13:34:28 -07:00
Kristiyan Kostadinov
74321a45a0 refactor(core): remove ManualOnPush change detection (#45943)
We've had a TODO to expose ManualOnPush for a long time, but it hasn't moved since then. These changes remove it since it would be easy to re-introduce if we wanted to, it frees up an extra space in the flags bitmap and it removes some `render3` tests that we won't have to migrate to `TestBed`.

PR Close #45943
2022-05-10 09:37:07 -07:00
JoostK
75b3d0f843 perf(core): allow checkNoChanges mode to be tree-shaken in production (#45913)
This commit guards all logic that exists for the `checkNoChanges` mode
with `ngDevMode` checks such that the logic can be tree-shaken.

PR Close #45913
2022-05-09 12:04:04 -07:00
Cédric Exbrayat
def1f0fb93 refactor(core): declare TestModuleMetadata as an interface (#45891)
This allows the documentation for the options `errorOnUnknownElements` and `errorOnUnknownProperties` to be displayed in the docs.
They aren't currently displayed, as `TestModuleMetadata` is a type and not an interface.
See https://next.angular.io/api/core/testing/TestModuleMetadata

PR Close #45891
2022-05-09 11:56:18 -07:00
Andrew Kushnir
c9b40b5100 refactor(core): make Testability compatible with tree-shaking (#45657)
This commit refactors the `Testability`-related logic to extract the necessary providers into a separate array, so that it can later become it's own NgModule (or exposed as an array of providers) and be excluded from the new APIs by default.

PR Close #45657
2022-05-05 11:50:23 -07:00
Andrew Kushnir
d380bb49b7 refactor(core): rename internal bootstrap function (#45896)
This commit renames an internal function that implements the core bootstrap logic. The function is exported as a private symbol (with `ɵ`), but in order to avoid any possible confusion, we include "internal" into the function name as well.

PR Close #45896
2022-05-05 10:55:56 -07:00
Paul Gschwendtner
68a6a075f4 build: clean up references to old master branch (#45856)
Cleans up all references to the `master` branch we renamed to
`main` across Angular.

PR Close #45856
2022-05-04 16:23:33 -07:00
Paul Gschwendtner
95555658cf build: disable bazel nodejs linker to improve stability on windows (#45872)
The NodeJS Bazel linker does not work well on Windows because there
is no sandboxing and linker processes from different tests will attempt
to modify the same `node_modules`, causing concurrency race conditions
and resulting in flakiness.

PR Close #45872
2022-05-04 16:20:57 -07:00
Dylan Hunn
2dbdebc646 feat(forms): Add FormBuilder.nonNullable. (#45852)
With typed forms, all `FormControl`s are nullable by default, because they can be reset to `null`. This behavior is possible to change by passing the option `initialValueIsDefault: true`. However, in a large form, this is extremely cumbersome, as the option must be repeated over and over. Additionally, it is not possible to take full advantage of `FormBuilder`, since `FormBuilder.group` and `FormBuilder.array` will produce nullable controls.

This PR introduces a new accessor `FormBuilder.nonNullable`, which produces *non-nullable* controls. Specifically, any call to `.control` will produce controls with `{initialValueIsDefault: true}`, and calls to `.array` or `.group` that implicitly build inner controls will have the same effect.

```ts
let nfb = new FormBuilder().nonNullable;
let name = nfb.group({who: 'Alex'}); // FormGroup<{who: FormControl<string>}>
name.reset();
console.log(name); // {who: 'Alex'}
```

PR Close #45852
2022-05-04 12:46:05 -07:00
Andrew Kushnir
fa755b2a54 fix(core): prevent BrowserModule providers from being loaded twice (#45826)
This commit updates the logic of the `BrowserModule` to detect a situation when it's used in the `bootstrapApplication` case, which already includes `BrowserModule` providers.

PR Close #45826
2022-05-03 16:08:04 -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
Andrew Kushnir
401dec46eb feat(core): update TestBed to recognize Standalone Components (#45809)
This commit updates an internal logic of the TestBed to recognize Standalone Components to be able to apply the necessary overrides correctly.

PR Close #45809
2022-05-03 10:33:52 -07:00