angular-cn/packages/compiler-cli/test/ngtsc
Alex Rickabaugh d2742cf473 feat(ivy): compile @Injectable on classes not meant for DI (#28523)
In the past, @Injectable had no side effects and existing Angular code is
therefore littered with @Injectable usage on classes which are not intended
to be injected.

A common example is:

@Injectable()
class Foo {
  constructor(private notInjectable: string) {}
}

and somewhere else:

providers: [{provide: Foo, useFactory: ...})

Here, there is no need for Foo to be injectable - indeed, it's impossible
for the DI system to create an instance of it, as it has a non-injectable
constructor. The provider configures a factory for the DI system to be
able to create instances of Foo.

Adding @Injectable in Ivy signifies that the class's own constructor, and
not a provider, determines how the class will be created.

This commit adds logic to compile classes which are marked with @Injectable
but are otherwise not injectable, and create an ngInjectableDef field with
a factory function that throws an error. This way, existing code in the wild
continues to compile, but if someone attempts to use the injectable it will
fail with a useful error message.

In the case where strictInjectionParameters is set to true, a compile-time
error is thrown instead of the runtime error, as ngtsc has enough
information to determine when injection couldn't possibly be valid.

PR Close #28523
2019-02-13 19:13:10 -08:00
..
fake_core fix(ivy): proper resolution of Enums in Component decorator (#27971) 2019-01-10 10:49:03 -08:00
BUILD.bazel feat(ivy): add source mappings to compiled Angular templates (#28055) 2019-02-12 20:58:28 -08:00
env.ts build: support running ngtsc tests on windows (#28352) 2019-02-05 14:31:10 -05:00
ngtsc_spec.ts feat(ivy): compile @Injectable on classes not meant for DI (#28523) 2019-02-13 19:13:10 -08:00
sourcemap_utils.ts feat(ivy): add source mappings to compiled Angular templates (#28055) 2019-02-12 20:58:28 -08:00
template_mapping_spec.ts test(ivy): add template source mapping tests (#28055) 2019-02-12 20:58:28 -08:00
template_typecheck_spec.ts build: remove unused "test.sh" leftover code in compiler-cli (#28352) 2019-02-05 14:31:10 -05:00