Title: Different dependency injection behavior for abstract directives in Ivy · Issue #32981 · angular/angular · GitHub
Open Graph Title: Different dependency injection behavior for abstract directives in Ivy · Issue #32981 · angular/angular
X Title: Different dependency injection behavior for abstract directives in Ivy · Issue #32981 · angular/angular
Description: It looks like ngtsc behaves differently for abstract directives, compared to the behavior of ngc. Let's assume we have the following base class in our version 8 application: class ButtonBase { @ViewChild(...) ripple: MatRipple; // not us...
Open Graph Description: It looks like ngtsc behaves differently for abstract directives, compared to the behavior of ngc. Let's assume we have the following base class in our version 8 application: class ButtonBase { @Vie...
X Description: It looks like ngtsc behaves differently for abstract directives, compared to the behavior of ngc. Let's assume we have the following base class in our version 8 application: class ButtonBase { ...
Opengraph URL: https://github.com/angular/angular/issues/32981
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Different dependency injection behavior for abstract directives in Ivy","articleBody":"It looks like `ngtsc` behaves differently for abstract directives, compared to the behavior of `ngc`.\r\n\r\nLet's assume we have the following base class in our version 8 application:\r\n\r\n```ts\r\nclass ButtonBase {\r\n @ViewChild(...) ripple: MatRipple;\r\n\r\n // not using dependency injection.\r\n constructor(animationMode: string)\r\n}\r\n```\r\n\r\nIn version 9 with Ivy, the `ButtonClass` needs to be decorated with `@Directive()` because `ButtonClass` declares a class member with an Angular decorator. This is part of our `@angular/core` migration for version 9. See [migration code](https://github.com/angular/angular/tree/master/packages/core/schematics/migrations/undecorated-classes-with-decorated-fields).\r\n\r\nAdding `@Directive()` here would be problematic because the `ButtonBase` class is never intended to be used in combination with dependency injection. i.e. the derived classes manually pass in the constructor parameters through a `super(..)` call. \r\n\r\nThe **problem** here is that `ngtsc` checks the constructor of `ButtonBase` now (since we added `@Directive`) and throws since it cannot find a suitable injection token for type `string`. The difference is that `ngc` does not seem to throw here. It seems to skip DI checking for abstract directives. Though `ngc` throws if some other non-abstract directive/ or component inherits the constructor from `ButtonBase`. This is reasonable.\r\n\r\n```\r\nNo suitable injection token for parameter 'noDiParameter' of class 'BaseDir'. Found string\r\n```\r\n\r\n\r\n\u003cdetails>\r\n\u003csummary>Here is a patch that can be used to reproduce this behavior in the Angular `compiler-cli` tests.\u003c/summary>\r\n\r\n```patch\r\ndiff --git a/packages/compiler-cli/test/ngc_spec.ts b/packages/compiler-cli/test/ngc_spec.ts\r\nindex 48bac17623..417f6eebda 100644\r\n--- a/packages/compiler-cli/test/ngc_spec.ts\r\n+++ b/packages/compiler-cli/test/ngc_spec.ts\r\n@@ -2287,7 +2287,7 @@ describe('ngc transformer command-line', () => {\r\n expect(exitCode).toEqual(0);\r\n });\r\n \r\n- describe('base directives', () => {\r\n+ fdescribe('base directives', () => {\r\n it('should allow directives with no selector that are not in NgModules', () => {\r\n // first only generate .d.ts / .js / .metadata.json files\r\n writeConfig(`{\r\n@@ -2297,9 +2297,13 @@ describe('ngc transformer command-line', () => {\r\n write('main.ts', `\r\n import {Directive} from '@angular/core';\r\n \r\n- @Directive({})\r\n- export class BaseDir {}\r\n-\r\n+ @Directive()\r\n+ export class BaseDir {\r\n+ // should not check DI parameters here. Derived classes can\r\n+ // manually pass a value to the superclass.\r\n+ constructor(noDiParameter: string) {}\r\n+ }\r\n+ \r\n @Directive({})\r\n export abstract class AbstractBaseDir {}\r\n \r\n@@ -2309,5 +2313,37 @@ describe('ngc transformer command-line', () => {\r\n let exitCode = main(['-p', path.join(basePath, 'tsconfig.json')], errorSpy);\r\n expect(exitCode).toEqual(0);\r\n });\r\n+\r\n+ it('should check di when inheriting constructor from directive without selector', () => {\r\n+ // first only generate .d.ts / .js / .metadata.json files\r\n+ writeConfig(`{\r\n+ \"extends\": \"./tsconfig-base.json\",\r\n+ \"files\": [\"main.ts\"]\r\n+ }`);\r\n+ write('main.ts', `\r\n+ import {NgModule, Directive} from '@angular/core';\r\n+\r\n+ @Directive()\r\n+ export class BaseDir {\r\n+ constructor(noDiParameter: string) {}\r\n+ }\r\n+ \r\n+ @Directive({selector: 'greet'})\r\n+ export class GreetDir extends BaseDir {}\r\n+\r\n+ @NgModule({declarations: [GreetDir]})\r\n+ export class AppModule {}\r\n+ `);\r\n+\r\n+ // Stub the error spy because we don't want to call through and print the\r\n+ // expected error diagnostic.\r\n+ errorSpy.and.stub();\r\n+\r\n+ const exitCode = main(['-p', path.join(basePath, 'tsconfig.json')], errorSpy);\r\n+ expect(errorSpy).toHaveBeenCalledTimes(1);\r\n+ expect(errorSpy).toHaveBeenCalledWith(\r\n+ jasmine.stringMatching(/Can't resolve all parameters for GreetDir/));\r\n+ expect(exitCode).toEqual(1);\r\n+ });\r\n });\r\n });\r\ndiff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts\r\nindex c94c751234..ce0ac89aa6 100644\r\n--- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts\r\n+++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts\r\n@@ -1022,12 +1022,16 @@ runInEachFileSystem(os => {\r\n expect(jsContents).toContain('selectors: [[\"ng-component\"]]');\r\n });\r\n \r\n- it('should allow directives with no selector that are not in NgModules', () => {\r\n+ fit('should allow directives with no selector that are not in NgModules', () => {\r\n env.write('main.ts', `\r\n import {Directive} from '@angular/core';\r\n \r\n @Directive({})\r\n- export class BaseDir {}\r\n+ export class BaseDir {\r\n+ // should not check DI parameters here. Derived classes can\r\n+ // manually pass a value to the superclass.\r\n+ constructor(noDiParameter: string) {}\r\n+ }\r\n \r\n @Directive({})\r\n export abstract class AbstractBaseDir {}\r\n@@ -1044,6 +1048,27 @@ runInEachFileSystem(os => {\r\n expect(errors.length).toBe(0);\r\n });\r\n \r\n+ fit('should check di when inheriting constructor from directive without selector', () => {\r\n+ env.write('main.ts', `\r\n+ import {NgModule, Directive} from '@angular/core';\r\n+\r\n+ @Directive()\r\n+ export class BaseDir {\r\n+ constructor(noDiParameter: string) {}\r\n+ }\r\n+ \r\n+ @Directive({selector: 'greet'})\r\n+ export class GreetDir extends BaseDir {}\r\n+\r\n+ @NgModule({declarations: [GreetDir]})\r\n+ export class AppModule {}\r\n+ `);\r\n+ const errors = env.driveDiagnostics();\r\n+ expect(errors[0].messageText).toMatch(\r\n+ /No suitable injection token for parameter 'noDiParameter' of class 'BaseDir'/);\r\n+ expect(errors.length).toBe(1);\r\n+ });\r\n+\r\n it('should not allow directives with no selector that are in NgModules', () => {\r\n env.write('main.ts', `\r\n import {Directive, NgModule} from '@angular/core';\r\n```\r\n\r\n\u003c/details>\r\n","author":{"url":"https://github.com/devversion","@type":"Person","name":"devversion"},"datePublished":"2019-10-03T13:11:08Z","url":"https://github.com/angular/angular/issues/32981"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7e9e970a-7a75-3cb2-e281-b14a5f5a0e9e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDB2:26ECEB:1A95BD9:21DE29A:6B0139FB |
| html-safe-nonce | 9b6b27e52f74b768738a6607d0569d6d5f1082e75d5b4755f882e5556cacfa5e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREIyOjI2RUNFQjoxQTk1QkQ5OjIxREUyOUE6NkIwMTM5RkIiLCJ2aXNpdG9yX2lkIjoiMjA3NzAzNjcwMjg1MzA1MjkyMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ebe6da3ad283054fdbf8c902766c6c471325e9be36107218cea889ae6b1af319 |
| hovercard-subject-tag | issue:502057508 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/32981/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0279696fd270dc3dfacf88ec9be69fbab915d69a09b48b3913817dd2a5f0654a/angular/angular/issues/32981 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0279696fd270dc3dfacf88ec9be69fbab915d69a09b48b3913817dd2a5f0654a/angular/angular/issues/32981 |
| og:image:alt | It looks like ngtsc behaves differently for abstract directives, compared to the behavior of ngc. Let's assume we have the following base class in our version 8 application: class ButtonBase { @Vie... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devversion |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width