Commit 2da2fc2
fix(compiler): recognise Angular class decorators by their @angular/core import, like ngtsc
`@Component`, `@Directive`, `@Pipe`, `@Injectable` and `@NgModule` were
matched by name. Like ngtsc's `findAngularDecorator`, a class decorator
is now Angular's only when it's imported from `@angular/core`: by name
under any alias (`import {Component as Cmp}`), or through a namespace
import (`@ng.Component()`). This is the rule #504 uses for member
decorators, and the same check (`angular_core_decorator`).
Checked against @angular/compiler-cli 22.1.7 (44 probes in
tests/fixtures/class_decorators_ngtsc.json, 40 compared):
- Another library's, a local, an undeclared or a default-imported
`@Component` (and the other four) is left on the class, which isn't
compiled, and its `inputs:` aren't checked.
- One re-exported through another module (`import {Component} from
'./shared'`) is left alone too: ngtsc follows the re-export, oxc can't.
- An aliased one (`@Cmp`, `@D`, `@P`, `@M`, `@Inj`) and a namespaced
`@ng.Injectable` are now compiled; `setClassMetadata` lists them as
written (`type: Cmp`). An aliased `@Component` keeps `templateUrl` in
`setClassMetadata`, as ngtsc does.
- `import {Injectable as Component}` compiles as an `@Injectable`.
This holds for AOT compilation and decorator removal, setClassMetadata,
the `@Service` collision check, JIT, the hoisting of declarations a
decorator references, NAPI `extractComponentMetadataSync` /
`extractComponentUrls` / the pipe and class-metadata APIs, and the Vite
plugin: its quick decorator check lets `@Cmp(...)` / `@ng.Component(...)`
files through, and the HMR template/styles diff only blanks the
components the compiler compiled. The public `extract_pipe_metadata`,
`extract_injectable_metadata`, `extract_ng_module_metadata` and
`find_*_decorator_span`, which don't get the file's imports, still match
by name (`extract_pipe_metadata_in` takes them).
Not matched to ngtsc, on purpose: an aliased `@Injectable` gets a
factory (ngtsc's `needsFactory` compares the written name with
'Injectable', so its `ɵprov` points at a missing `ɵfac`).
117 unit-test sources that used `@Component` / `@Directive` without
importing it now import it from `@angular/core`.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>1 parent 1d7937a commit 2da2fc2
22 files changed
Lines changed: 1479 additions & 337 deletions
File tree
- crates/oxc_angular_compiler
- src
- component
- directive
- injectable
- ng_module
- pipe
- tests
- fixtures
- napi/angular-compiler
- src
- test
- vite-plugin
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| 102 | + | |
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
| |||
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
165 | | - | |
| 167 | + | |
166 | 168 | | |
167 | 169 | | |
168 | 170 | | |
| |||
887 | 889 | | |
888 | 890 | | |
889 | 891 | | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | | - | |
910 | | - | |
911 | | - | |
912 | | - | |
913 | 892 | | |
914 | | - | |
915 | | - | |
916 | | - | |
917 | | - | |
918 | | - | |
919 | | - | |
920 | | - | |
921 | | - | |
922 | | - | |
923 | | - | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
924 | 904 | | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
925 | 908 | | |
926 | 909 | | |
927 | 910 | | |
928 | 911 | | |
929 | 912 | | |
930 | 913 | | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
935 | | - | |
936 | | - | |
| 914 | + | |
937 | 915 | | |
| 916 | + | |
| 917 | + | |
938 | 918 | | |
939 | | - | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
| 919 | + | |
| 920 | + | |
944 | 921 | | |
945 | | - | |
946 | | - | |
947 | | - | |
948 | 922 | | |
949 | 923 | | |
950 | 924 | | |
951 | 925 | | |
952 | 926 | | |
953 | 927 | | |
954 | 928 | | |
955 | | - | |
| 929 | + | |
| 930 | + | |
956 | 931 | | |
957 | 932 | | |
958 | 933 | | |
959 | 934 | | |
960 | 935 | | |
961 | 936 | | |
962 | | - | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
963 | 942 | | |
964 | | - | |
| 943 | + | |
965 | 944 | | |
966 | 945 | | |
967 | 946 | | |
| |||
0 commit comments