-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: annotate remaining base classes with @directive for… (#17279)
* build: add lint rule to ensure classes with angular features are decorated Read more about this here: https://hackmd.io/vuQfavzfRG6KUCtU7oK_EA. * refactor: annotate base classes with @directive for ivy. These base classes define class members with Angular features. Therefore they need to be decorated with `@Directive()` so that the classes can be extended in Ivy. This is an official migration that will be performed for CLI applications in version 9. angular/angular#32590.
- Loading branch information
1 parent
230fd2f
commit b228f07
Showing
8 changed files
with
114 additions
and
7 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
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
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
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,65 @@ | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
|
||
const RULE_FAILURE = `Undecorated class defines fields with Angular decorators. Undecorated ` + | ||
`classes with Angular fields cannot be extended in Ivy since no definition is generated. ` + | ||
`Add a "@Directive" decorator to fix this.`; | ||
|
||
/** | ||
* Rule that doesn't allow undecorated class declarations with fields using Angular | ||
* decorators. | ||
*/ | ||
export class Rule extends Lint.Rules.TypedRule { | ||
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new Walker(sourceFile, this.getOptions(), program.getTypeChecker())); | ||
} | ||
} | ||
|
||
class Walker extends Lint.RuleWalker { | ||
constructor( | ||
sourceFile: ts.SourceFile, options: Lint.IOptions, private _typeChecker: ts.TypeChecker) { | ||
super(sourceFile, options); | ||
} | ||
|
||
visitClassDeclaration(node: ts.ClassDeclaration) { | ||
if (this._hasAngularDecorator(node)) { | ||
return; | ||
} | ||
|
||
for (let member of node.members) { | ||
if (member.decorators && this._hasAngularDecorator(member)) { | ||
this.addFailureAtNode(node, RULE_FAILURE); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** Checks if the specified node has an Angular decorator. */ | ||
private _hasAngularDecorator(node: ts.Node): boolean { | ||
return !!node.decorators && node.decorators.some(d => { | ||
if (!ts.isCallExpression(d.expression) || | ||
!ts.isIdentifier(d.expression.expression)) { | ||
return false; | ||
} | ||
|
||
const moduleImport = this._getModuleImportOfIdentifier(d.expression.expression); | ||
return moduleImport ? moduleImport.startsWith('@angular/core') : false; | ||
}); | ||
} | ||
|
||
/** Gets the module import of the given identifier if imported. */ | ||
private _getModuleImportOfIdentifier(node: ts.Identifier): string|null { | ||
const symbol = this._typeChecker.getSymbolAtLocation(node); | ||
if (!symbol || !symbol.declarations.length) { | ||
return null; | ||
} | ||
const decl = symbol.declarations[0]; | ||
if (!ts.isImportSpecifier(decl)) { | ||
return null; | ||
} | ||
const importDecl = decl.parent.parent.parent; | ||
const moduleSpecifier = importDecl.moduleSpecifier; | ||
return ts.isStringLiteral(moduleSpecifier) ? moduleSpecifier.text : null; | ||
} | ||
} |
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