896 Commits

Author SHA1 Message Date
Pete Bacon Darwin
f2a1c66031 feat(ivy): ngcc - add typings to ModuleWithProviders functions ()
Exported functions or static method that return a `ModuleWithProviders`
compatible structure need to provide information about the referenced
`NgModule` type in their return type.

This allows ngtsc to be able to understand the type of `NgModule` that is
being returned from calls to the function, without having to dig into the
internals of the compiled library.

There are two ways to provide this information:

* Add a type parameter to the `ModuleWithProviders` return type. E.g.

```
static forRoot(): ModuleWithProviders<SomeNgModule>;
```

* Convert the return type to a union that includes a literal type. E.g.

```
static forRoot(): (SomeOtherType)&{ngModule:SomeNgModule};
```

This commit updates the rendering of typings files to include this type
information on all matching functions/methods.

PR Close 
2018-12-20 11:58:49 -05:00
Pete Bacon Darwin
cfb8c17511 feat(ivy): ngcc - map functions as well as classes from source to typings ()
To support updating `ModuleWithProviders` calls,
we need to be able to map exported functions between
source and typings files, as well as classes.

PR Close 
2018-12-20 11:58:49 -05:00
Keen Yee Liau
2c9b6c0c1f fix(compiler-cli): create LiteralLikeNode for String and Number literal ()
Typescript 3.2 introduced BigInt type, and consequently the
implementation for checkExpressionWorker() in checkers.ts is refactored.

For NumberLiteral and StringLiteral types, 'text' filed must be present
in the Node type, therefore they must be LiteralLikeNode instead of
Node.

PR Close 
2018-12-18 13:20:01 -08:00
Igor Minar
17e702bf8b feat: add support for typescript 3.2 ()
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html
https://blogs.msdn.microsoft.com/typescript/2018/11/29/announcing-typescript-3-2/

Any application using tsickle for closure compatibility will need to update it's tsickle
dependency to 0.34

PR Close 
2018-12-18 13:20:01 -08:00
JoostK
4376ec207c perf(ivy): let ngcc first check marker file before assembling bundle ()
With the bundle info being assembled into a single object before the
transform is started, we now greedily create a TypeScript program up-front.
If a marker file exists that indicates that the bundle could be skipped
the program creation has already taken place which takes a significant
amount of time. This commit moves the marker check to occur before the
bundle is assembled.

PR Close 
2018-12-17 09:35:16 -08:00
JoostK
52544ffaa3 fix(ivy): ngcc generates setClassMetadata calls for ES5 bundles ()
ngcc would feed ngtsc with the function declaration inside of an IIFE as
that is considered the class symbol's declaration node, according to
TypeScript's `ts.Symbol.valueDeclaration`. ngtsc however only considered
variable decls and actual class decls as potential class declarations,
so given the function declaration node it would fail to generate the
`setClassMetadata` call.

ngtsc no longer makes its own assumptions about what classes look like,
but always asks the reflection host to yield this kind of information.

PR Close 
2018-12-17 09:35:16 -08:00
JoostK
f4a797db24 fix(ivy): prevent ngcc from inadvertently dropping Ivy definitions ()
A surprising interaction with the MagicString library caused inserted
Ivy definitions to be dropped during the removal of decorators, iff all
decorators on the class could be removed. In that case, the removal
location corresponds with the exact location where Ivy definitions were
inserted into.

This commit moves the removal of decorators to occur before Ivy
definitions are inserted. This effectively avoids the problem, as later
inserted text fragments will be retained by MagicString.

PR Close 
2018-12-14 15:26:15 -08:00
JoostK
023bd31965 fix(ivy): ngcc should not emit TypeScript syntax ()
If a template contains specific TypeScript syntax, such as a non-null
assertion, the code that is emitted from ngcc into a JavaScript bundle
should not retain such syntax as it is invalid in JS.

A full-blown TypeScript emit of a complete ts.SourceFile would be
required to be able to emit JS and possibly downlevel into a lower
language target, which is not an option for ngcc as it currently
operates on partial ASTs, not full source files.

Instead, ngtsc no longer produces TypeScript specific syntax in the first
place, such that TypeScript print logic will only generate JS code.

PR Close 
2018-12-14 15:19:31 -08:00
JoostK
a9543457ef fix(ivy): prevent invalid forward references in setClassMetadata call ()
In Ivy, a pure call to `setClassMetadata` is inserted to retain the
information that would otherwise be lost while eliding the Angular
decorators. In the past, the Angular constructor decorators were
wrapped inside of an anonymous function which was only evaluated once
`ReflectionCapabilities` was requested for such metadata. This approach
prevents forward references from inside the constructor parameter
decorators from being evaluated before they are available.

In the `setClassMetadata` call, the constructor parameters were not wrapped
within an anonymous function, such that forward references were evaluated
too early, causing runtime errors.

This commit changes the `setClassMetadata` call to pass the constructor
parameter decorators inside of an anonymous function again, such that
forward references are not resolved until requested by
`ReflectionCapabilities`, therefore avoiding the early reads of forward refs.

PR Close 
2018-12-14 10:24:16 -08:00
JoostK
a8ebc837ea fix(ivy): support complex type nodes in ModuleWithProviders ()
With ngcc's ability to fixup pre-Ivy ModuleWithProviders such that they
include a reference to the NgModule type, the type may become a qualified
name:

```
import {ModuleWithProviders} from '@angular/core';
import * as ngcc0 from './module';

export declare provide(): ModuleWithProviders<ngcc0.Module>;
```

ngtsc now takes this situation into account when reflecting a
ModuleWithProvider's type argument.

PR Close 
2018-12-14 10:23:43 -08:00
Igor Minar
7fabe4429d fix(ivy): add support for optional nullable injection tokens ()
FW-778 #resolve

PR Close 
2018-12-12 13:04:29 -08:00
Alex Eagle
50687e11cf build: fix type-check errors introduced during rules_ts 0.21 ()
PR Close 
2018-12-10 16:33:41 -08:00
Alex Rickabaugh
aa48810d80 feat(ivy): generate flat module index files ()
Previously, ngtsc did not respect the angularCompilerOptions settings
for generating flat module indices. This commit adds a
FlatIndexGenerator which is used to implement those options.

FW-738 #resolve

PR Close 
2018-12-07 09:22:29 -08:00
Alex Rickabaugh
352c582f98 refactor(ivy): allow for shim generators not based on existing files ()
Previously the ngtsc ShimGenerator interface expected that all shims would
be generated using the contents of existing ts.SourceFiles. This assumption
was true for ngfactory and ngsummary files, but breaks down for flat module
index files, which are standalone.

This commit prepares for flat module index generation by enabling shim
generators which don't require an existing file.

PR Close 
2018-12-07 09:22:29 -08:00
JoostK
159ab1c257 fix(ivy): ngtsc should include generic types on injectable definitions ()
Analogously to directives, the `ngInjectableDef` field in .d.ts files is
annotated with the type of service that it represents. If the service
contains required generic type arguments, these must be included in
the .d.ts file.

PR Close 
2018-12-06 13:33:13 -08:00
Paul Gschwendtner
ca1c430f30 fix(compiler-cli): ngtsc shim files not being generated on case-insensitive platforms ()
Common insensitive platforms are `win32/win64` (see:
[here](3e4c5c95ab/src/compiler/sys.ts (L681-L682)))

Currently when running `bazel build packages/core --define=compile=aot`, the `compiler-cli` will throw because it cannot find the `index.ngfactory.ts` file in the compiler host. This is because the shim host wrapper is not properly generating the requested `ngfactory` file.

This happens because we call `getCanonicalFileName` that returns a path that is different to the actual program filenames that are used to construct a map of generated files. Since the generators always use the paths which are not "canonical" and pases them internally like that, we can just stop manually calling `getCanonicalFileName`.

PR Close 
2018-12-06 09:24:52 -08:00
Alex Rickabaugh
276bdd1f3e fix(ivy): move the generation of ɵNonEmptyModule to shim construction ()
ngfactory files have a ɵNonEmptyModule constant included if there are no
other exported factory symbols. Previously this extra export was added
dynamically in a TS transformer.

However, synthetically constructed exports don't get properly downleveled
during JS emit, and this generated constant caused issues with downstream
tests.

Instead, this commit configures the shim to always have this export to
begin with, and to filter it out if it's not required.

Testing strategy: covered by existing ngtsc_spec tests which verify the
presence of the ɵNonEmptyModule symbol.

PR Close 
2018-12-05 16:26:39 -08:00
Alex Rickabaugh
d553ec2f36 fix(ivy): generate correct moduleNames for factories and summaries ()
In ngtsc, files loaded into the ts.Program have a "module name", set via
ts.SourceFile.moduleName, which ends up being written into an AMD module
name triple-slash directive in the generated .js file.

For generated shim files (ngfactories, ngsummaries) that are constructed
synthetically, there was previously no moduleName set, which caused some
issues with downstream tests.

This commit adds logic to compute and set moduleNames for both generated
ngfactory and ngsummary shims.

PR Close 
2018-12-05 16:26:39 -08:00
Alex Rickabaugh
dfdaaf6a0d fix(ivy): deduplicate directives in component scopes ()
A previous fix to ngtsc opened the door for duplicate directives in
the 'directives' array of a component. This would happen if the directive
was declared in a module which was imported more than once within the
component's module.

This commit adds deduplication when the component's scope is materialized,
so declarations which arrive via more than one module import are coalesced.

PR Close 
2018-12-05 14:36:24 -08:00
Alex Rickabaugh
0d8ab323a7 fix(ivy): add missing directoryExists() method to shim CompilerHost ()
The method `ts.CompilerHost.directoryExists` is optional, and was not
previously handled by our ts.CompilerHost wrapper for factory and
summary shims (GeneratedShimsHostWrapper).

TypeScript checks for the existence of this method and silently ignores
things like typeRoots if it's not found. This commit adds proper handling
of directoryExists() to the shim.

A test is also added which verifies typeRoots behavior works when shims
are enabled.

PR Close 
2018-12-05 10:46:51 -08:00
Alex Rickabaugh
345bdd3db0 fix(ivy): generate empty ngfactory files if needed ()
Previously the ngfactory shim generator in ngtsc would always write two
imports in the factory file shims:

1) an import to @angular/core
2) an import to the base file

If the base file has no exports, import  would be empty. This turns out
to cause issues downstream.

This commit changes the generated shim so if there are no exports in the
base file, the generated shim is empty too.

PR Close 
2018-12-05 10:46:51 -08:00
Andrew Kushnir
8e644d99fc fix(ivy): taking "interpolation" config option into account (FW-723) ()
PR Close 
2018-12-04 14:04:14 -08:00
Alex Rickabaugh
159788685a fix(ivy): resolve resources using TS module resolution semantics ()
Previously ngtsc assumed resource files (templateUrl, styleUrls) would be
physically present in the file system relative to the .ts file which
referenced them. However, ngc previously resolved such references in the
context of ts.CompilerOptions.rootDirs. Material depends on this
functionality in its build.

This commit introduces resolution of resources by leveraging the TypeScript
module resolver, ts.resolveModuleName(). This resolver is used in a way
which will never succeed, but on failure will return a list of locations
checked. This list is then filtered to obtain the correct potential
locations of the resource.

PR Close 
2018-12-04 14:03:55 -08:00
Alex Rickabaugh
cfb67edd85 feat(ivy): support styleUrls in ngtsc ()
This commit adds support for resolution of styleUrls to ngtsc. Previously
this field was never read, and so components with styleUrls would appear
unstyled after compilation.

PR Close 
2018-12-04 14:03:54 -08:00
JoostK
b5ed403bc4 fix(ivy): prevent ngtsc from synchronous compilation for in-flight resouces ()
When a single resource is preloaded twice in ngtsc, the second request
would be recognized as in-flight in which case `undefined` would
be returned, which signals to the compilation that is can resume
synchronously. The compilation would then proceed immediately and call
`load`, only to find out that the request is still in-flight which is
not allowed.

This commit caches the Promise of the in-flight fetch requests, such
that subsequent preload requests can return the corresponding Promise
instance.

PR Close 
2018-12-04 14:03:54 -08:00
Paul Gschwendtner
d3c08e74f6 fix(compiler-cli): flatModuleIndex files not generated on windows with multiple input files ()
* Currently when building a `ng_module` with Bazel and having the flat module id option set, the flat module files are not being generated because `@angular/compiler-cli` does not properly determine the entry-point file.

Note that this logic is not necessarily specific to Bazel and the same problem can happen without Bazel if multiple TypeScript input files are specified while the `flatModuleIndex` option has been enabled.

PR Close 
2018-12-04 14:01:25 -08:00
JoostK
75723d5c89 feat(ivy): ngtsc support for static resolution of array.slice() ()
For ngcc's processing of ES5 bundles, the spread syntax has been
downleveled from `[...ARRAY]` to become `ARRAY.slice()`. This commit
adds basic support for static resolution of such call.

PR Close 
2018-12-03 14:38:41 -08:00
Pete Bacon Darwin
912b0529c1 feat(ivy): ngcc - render private declaration exports ()
Ngcc will now render additional exports for classes that are referenced in
`NgModule` decorated classes, but which were not publicly exported
from an entry-point of the package.

This is important because when ngtsc compiles libraries processed by ngcc
it needs to be able to publcly access decorated classes that are referenced
by `NgModule` decorated classes in order to build templates that use these
classes.

Doing this re-exporting is not without its risks. There are chances that
the class is not exported correctly: there may already be similarly named
exports from the entry-point or the class may be being aliased. But there
is not much more we can do from the point of view of ngcc to workaround
such scenarios. Generally, packages should have been built so that this
approach works.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
bf3ac41e36 feat(ivy): ngcc - add PrivateDeclarationsAnalyzer ()
This analyzer searches the source for declared classes that are not
exported publicly from the entry-point.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
b55e1c2ba9 refactor(ivy): ngcc - encapsulate variables into "bundles" ()
There are a number of variables that need to be passed around
the program, in particular to the renderers, which benefit from being
stored in well defined objects.

The new `EntryPointBundle` structure is a specific format of an entry-point
and contains the compiled `BundleProgram` objects for the source and typings,
if appropriate.

This change helps with future refactoring, where we may need to add new
properties to this object. It allows us to maintain more stable APIs between
the constituent parts of ngcc, rather than passing lots of primitive values
around throughout the program.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
64f6820660 refactor(ivy): ngcc - subclasses do not need to declare protected variables ()
These variables are already declared as protected in the super class, and
so it is redundant to do it again in the subclasses.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
4526b3ef50 refactor(ivy): ngcc - remove unused code ()
PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
49c73bc170 style(ivy): ngcc - fix typos ()
PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
4a70b669be feat(ivy): register references from NgModule annotations ()
The `NgModuleDecoratorHandler` can now register all the references that
it finds in the `NgModule` metadata, such as `declarations`, `imports`,
`exports` etc.

This information can then be used by ngcc to work out if any of these
references are internal only and need to be manually exported from a
library's entry-point.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
b93c1dffa1 style(ivy): ngcc - fix misspelled method ()
PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
84ce45ca16 refactor(ivy): ngcc - make EntryPoint an interface rather than a type ()
By inverting the relationship between `EntryPointPaths` and
`EntryPointFormat` we can have interfaces rather than types.

Thanks to @gkalpak for this idea.

PR Close 
2018-11-30 14:02:03 -08:00
Pete Bacon Darwin
0c6e1f4a1b fix(ivy): ngcc - support typings (d.ts) classes that are not publicly exported ()
If a decorated class is not publicly exported via an entry-point then the
previous approach to finding the associated typings file failed.

Now we ensure that we extract all the class declarations from the
dtsTypings program, even if they are not exported from the entry-point.
This is achieved by also parsing statements of each source file, rather
than just parsing classes that are exported from the entry-point.

Because we now look at all the files, it is possible for there to be multiple
class declarations with the same local name. In this case, only the first
declaration with a given name is added to the map; subsequent classes are
ignored.

We are most interested in classes that are publicly exported from the
entry-point, so these are added to the map first, to ensure that they are
not ignored.

PR Close 
2018-11-30 14:02:03 -08:00
Andrew Kushnir
aedc343003 feat(ivy): updated translation const names (that include message ids) ()
PR Close 
2018-11-30 10:00:54 -08:00
Alex Rickabaugh
412e47d311 fix(ivy): support multiple directives with the same selector ()
Previously the concept of multiple directives with the same selector was
not supported by ngtsc. This is due to the treatment of directives for a
component as a Map from selector to the directive, which is an erroneous
representation.

Now the directives for a component are stored as an array which supports
multiple directives with the same selector.

Testing strategy: a new ngtsc_spec test asserts that multiple directives
with the same selector are matched on an element.

PR Close 
2018-11-29 21:35:28 -08:00
Andrew Kushnir
d819c00fee fix(ivy): take preserveWhitespaces config option into account (FW-650) ()
PR Close 
2018-11-28 11:41:49 -08:00
Marc Laval
c2f30542e7 fix(ivy): should support components without selector ()
PR Close 
2018-11-27 10:17:35 -08:00
JoostK
0d9b27ff26 fix(ivy): let ngcc transform @angular/core typings with relative imports ()
PR Close 
2018-11-21 09:20:11 -08:00
JoostK
c8c8648abf fix(ivy): prevent ngcc from referencing missing ɵsetClassMetadata ()
When ngtsc compiles @angular/core, it rewrites core imports to the
r3_symbols.ts file that exposes all internal symbols under their
external name. When creating the FESM bundle, the r3_symbols.ts file
causes the external symbol names to be rewritten to their internal name.

Under ngcc compilations of FESM bundles, the indirection of
r3_symbols.ts is no longer in place such that the external names are
retained in the bundle. Previously, the external name `ɵdefineNgModule`
was explicitly declared internally to resolve this issue, but the
recently added `setClassMetadata` was not declared as such, causing
runtime errors.

Instead of relying on the r3_symbols.ts file to perform the rewrite of
the external modules to their internal variants, the translation is
moved into the `ImportManager` during the compilation itself. This
avoids the need for providing the external name manually.

PR Close 
2018-11-21 09:20:11 -08:00
Alex Rickabaugh
4390e10dfd fix(ivy): don't update parent pointers in the ivy switch transform ()
Now that the Ivy switch transform uses ts.getMutableClone() to copy
statements, there's no need to set .parent pointers on the resulting
updated nodes. Doing this was causing assertion failures deep in
TypeScript in some cases.

PR Close 
2018-11-20 12:03:39 -08:00
Alex Rickabaugh
d97994b27f fix(ivy): clone ts.SourceFile in ivy_switch when triggered ()
Make a copy of the ts.SourceFile before modifying it in the ivy_switch
transform. It's suspected that the Bazel tsc_wrapped host's SourceFile
cache has issues when the ts.SourceFiles are mutated.

PR Close 
2018-11-13 14:00:20 -08:00
Paul Gschwendtner
0ada23a5fb fix(compiler-cli): only pass canonical genfile paths to compiler host ()
In a more specific scenario: Considering people use a custom TypeScript compiler host with `NGC`, they _could_ expect only posix paths in methods like `writeFile`. This at first glance sounds like a trivial issue that should be just fixed by the actual compiler host, but usually TypeScript internal API's just pass around posix normalized paths, and therefore it would be good to follow the same standards when passing JSON genfiles to the `CompilerHost`.

For normal TypeScript files (and TS genfiles), this is already consistent because those will be handled by the actual TypeScript `Program` (see `emitCallback`).

PR Close 
2018-11-13 10:51:19 -08:00
cexbrayat
f5a0ec0d7c fix(ivy): ngcc should not fail on invalid package.json ()
Some package.json files may have invalid JSON, for example package.json blueprints from `@schematics/angular` (see https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/workspace/files/package.json).

This makes ngcc more resilient, by simpling logging a warning if an error is encountered, instead of failing as it does right now.

PR Close 
2018-11-13 10:48:31 -08:00
JoostK
97ef8ae9e7 fix(ivy): let ngcc not consider deep imports as missing dependencies ()
This fixes an issue where packages would be skipped if they contained
e.g. RxJS 5 style imports such as
```
import { observeOn } from 'rxjs/operators/observeOn';
```

Given that no package.json file can be found at the imported path, the
dependency would be reported missing, causing the package to be skipped.

PR Close 
2018-11-12 12:50:06 -08:00
Huáng Jùnliàng
6744b19297 refactor(compiler): typo ()
PR Close 
2018-11-05 12:53:04 -08:00
Kara Erickson
4e4bca6bbc Revert "fix(ivy): correct ngtsc path handling in Windows ()"
This reverts commit d0037b22ef1e6879545bffdf013b5b32917821af. The commit must be temporarily reverted because
there were unforeseen breakages in g3.
2018-11-05 11:18:52 -08:00