forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ivy): throw compilation error when providing undecorated classes (…
…angular#34460) Adds a compilation error if the consumer tries to pass in an undecorated class into the `providers` of an `NgModule`, or the `providers`/`viewProviders` arrays of a `Directive`/`Component`. PR Close angular#34460
- Loading branch information
Showing
20 changed files
with
613 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/compiler-cli/src/ngtsc/annotations/src/diagnostics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import {ErrorCode, makeDiagnostic} from '../../diagnostics'; | ||
import {Reference} from '../../imports'; | ||
import {InjectableClassRegistry} from '../../metadata'; | ||
import {ClassDeclaration} from '../../reflection'; | ||
|
||
/** | ||
* Gets the diagnostics for a set of provider classes. | ||
* @param providerClasses Classes that should be checked. | ||
* @param providersDeclaration Node that declares the providers array. | ||
* @param registry Registry that keeps track of the registered injectable classes. | ||
*/ | ||
export function getProviderDiagnostics( | ||
providerClasses: Set<Reference<ClassDeclaration>>, providersDeclaration: ts.Expression, | ||
registry: InjectableClassRegistry): ts.Diagnostic[] { | ||
const diagnostics: ts.Diagnostic[] = []; | ||
|
||
for (const provider of providerClasses) { | ||
if (registry.isInjectable(provider.node)) { | ||
continue; | ||
} | ||
|
||
const contextNode = provider.getOriginForDiagnostics(providersDeclaration); | ||
diagnostics.push(makeDiagnostic( | ||
ErrorCode.UNDECORATED_PROVIDER, contextNode, | ||
`The class '${provider.node.name.text}' cannot be created via dependency injection, as it does not have an Angular decorator. This will result in an error at runtime. | ||
Either add the @Injectable() decorator to '${provider.node.name.text}', or configure a different provider (such as a provider with 'useFactory'). | ||
`, | ||
[{node: provider.node, messageText: `'${provider.node.name.text}' is declared here.`}])); | ||
} | ||
|
||
return diagnostics; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.