angular/packages/compiler-cli/ngcc/src
Alex Rickabaugh e039075a28 fix(compiler-cli): better detect classes that are indirectly exported (#42207)
The compiler flag `compileNonExportedClasses` allows the Angular compiler to
process classes which are not exported at the top level of a source file.
This is often used to allow for AOT compilation of test classes inside
`it()` test blocks, for example.

Previously, the compiler would identify exported classes by looking for an
`export` modifier on the class declaration itself. This works for the
trivial case, but fails for indirectly exported classes:

```typescript
// Component is declared unexported.
@Component({...})
class FooCmp {...}

// Indirect export of FooCmp
export {FooCmp};
```

This is not an immediate problem for most application builds, since the
default value for `compileNonExportedClasses` is `true` and therefore such
classes get compiled regardless.

However, in the Angular Language Service now, `compileNonExportedClasses` is
forcibly overridden to `false`. That's because the tsconfig used by the IDE
and Language Service is often far broader than the application build's
configuration, and pulls in test files that can contain unexported classes
not designed with AOT compilation in mind.

Therefore, the Language Service has trouble working with such structures.

In this commit, the `ReflectionHost` gains a new API for detecting whether a
class is exported. The implementation of this method now not only considers
the `export` modifier, but also scans the `ts.SourceFile` for indirect
exports like the example above. This ensures the above case will be
processed directly in the Language Service.

This new operation is cached using an expando symbol on the `ts.SourceFile`,
ensuring good performance even when scanning large source files with lots of
exports (e.g. a FESM file under `ngcc`).

Fixes #42184.

PR Close #42207
2021-06-01 12:16:46 -07:00
..
analysis refactor(compiler-cli): replace the IncrementalDriver with a new design (#41475) 2021-04-13 13:05:35 -07:00
dependencies refactor(compiler-cli): update to use new file-system interfaces (#40281) 2021-01-08 09:34:44 -08:00
entry_point_finder fix(compiler-cli): ensure ngcc can handle wildcard base-paths (#41033) 2021-03-01 15:25:44 -08:00
execution fix(ngcc): do not compile JavaScript sources if typings-only processing is repeated (#41209) 2021-03-16 09:33:53 -07:00
host fix(compiler-cli): better detect classes that are indirectly exported (#42207) 2021-06-01 12:16:46 -07:00
locking refactor(compiler-cli): update to use new file-system interfaces (#40281) 2021-01-08 09:34:44 -08:00
migrations refactor(compiler-cli): implement DeclarationNode node type (#38959) 2020-10-12 08:32:46 -07:00
packages refactor(ngcc): support processing only the typings files of packages (#40976) 2021-02-24 14:23:14 -08:00
rendering fix(compiler-cli): set TS original node on imported namespace identifiers (#40711) 2021-02-11 15:58:25 -08:00
writing fix(compiler-cli): update type castings for JSON.parse usage (#40710) 2021-02-09 10:48:43 -08:00
command_line_options.ts refactor(ngcc): support processing only the typings files of packages (#40976) 2021-02-24 14:23:14 -08:00
constants.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
main.ts refactor(ngcc): support processing only the typings files of packages (#40976) 2021-02-24 14:23:14 -08:00
ngcc_options.ts refactor(ngcc): support processing only the typings files of packages (#40976) 2021-02-24 14:23:14 -08:00
path_mappings.ts refactor(compiler-cli): update to use new file-system interfaces (#40281) 2021-01-08 09:34:44 -08:00
utils.ts feat(ngcc): support __read helper as used by TypeScript 4.2 (#41201) 2021-03-16 11:06:31 -07:00