refactor(language-service): add generic decorator property verifications (#32252)

This PR makes finding class declarations properties in decorators are
applied to more generic to all properties that may be in a decorator,
and adds helper methods enabling getting the property assignment of a
property value and verifying that a property assignment is actually in a
decorator applied to a class.

This is done so that it will be easier to provide Angular definitions
for decorator properties moving forward. Most immediately, this will
provide decorator class verification for #32238.

PR Close #32252
This commit is contained in:
ayazhafiz
2019-08-21 20:46:30 -05:00
committed by atscott
parent 3dd614db61
commit c624b14e8e
3 changed files with 43 additions and 22 deletions

View File

@ -7,7 +7,7 @@
*/
import * as ts from 'typescript';
import {getClassDeclFromTemplateNode} from '../src/template';
import {getClassDeclFromDecoratorProp} from '../src/template';
import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';
@ -22,7 +22,10 @@ describe('getClassDeclFromTemplateNode', () => {
class MyComponent {}`,
ts.ScriptTarget.ES2015, true /* setParentNodes */);
function visit(node: ts.Node): ts.ClassDeclaration|undefined {
return getClassDeclFromTemplateNode(node) || node.forEachChild(visit);
if (ts.isPropertyAssignment(node)) {
return getClassDeclFromDecoratorProp(node);
}
return node.forEachChild(visit);
}
const classDecl = sourceFile.forEachChild(visit);
expect(classDecl).toBeTruthy();
@ -37,9 +40,8 @@ describe('getClassDeclFromTemplateNode', () => {
const sourceFile = tsLS.getProgram() !.getSourceFile('/app/app.component.ts');
expect(sourceFile).toBeTruthy();
const classDecl = sourceFile !.forEachChild(function visit(node): ts.Node | undefined {
const candidate = getClassDeclFromTemplateNode(node);
if (candidate) {
return candidate;
if (ts.isPropertyAssignment(node)) {
return getClassDeclFromDecoratorProp(node);
}
return node.forEachChild(visit);
});