Skip to content

Commit

Permalink
Fix @component no argument error
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jun 8, 2019
1 parent f427c88 commit 11bdeea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
16 changes: 11 additions & 5 deletions server/src/modes/script/childComponents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as ts from 'typescript';
import { getLastChild, buildDocumentation, getNodeFromExportNode } from './componentInfo';
import {
getLastChild,
buildDocumentation,
getNodeFromExportNode,
getClassDecoratorArgumentType
} from './componentInfo';
import { T_TypeScript } from '../../services/dependencyService';

interface InternalChildComponent {
Expand All @@ -22,9 +27,11 @@ export function getChildComponents(
let type = defaultExportType;
if (defaultExportType.isClass()) {
// get decorator argument type when class
type = checker.getTypeAtLocation(
(defaultExportType.symbol.declarations[0].decorators![0].expression as ts.CallExpression).arguments[0]
);
const classDecoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
if (!classDecoratorArgumentType) {
return undefined;
}
type = classDecoratorArgumentType;
}

const componentsSymbol = checker.getPropertyOfType(type, 'components');
Expand Down Expand Up @@ -65,7 +72,6 @@ export function getChildComponents(

if (objectLiteralSymbol.flags & tsModule.SymbolFlags.Alias) {
const definitionSymbol = checker.getAliasedSymbol(objectLiteralSymbol);
// definitionObjectLiteralSymbol.
if (tsModule.isClassDeclaration(
definitionSymbol.valueDeclaration
)) {
Expand Down
50 changes: 34 additions & 16 deletions server/src/modes/script/componentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t
if (defaultExportType.isClass()) {
result.push.apply(result, getClassProps(defaultExportType) || []);

const decoratorArgumentType = checker.getTypeAtLocation(
(defaultExportType.symbol.declarations[0].decorators![0].expression as ts.CallExpression).arguments[0]
);
result.push.apply(result, getObjectProps(decoratorArgumentType) || []);
const decoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
if (decoratorArgumentType) {
result.push.apply(result, getObjectProps(decoratorArgumentType) || []);
}
} else {
result.push.apply(result, getObjectProps(defaultExportType) || []);
}
Expand Down Expand Up @@ -199,10 +199,10 @@ function getData(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: ts
if (defaultExportType.isClass()) {
result.push.apply(result, getClassData(defaultExportType) || []);

const decoratorArgumentType = checker.getTypeAtLocation(
(defaultExportType.symbol.declarations[0].decorators![0].expression as ts.CallExpression).arguments[0]
);
result.push.apply(result, getObjectData(decoratorArgumentType) || []);
const decoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
if (decoratorArgumentType) {
result.push.apply(result, getObjectData(decoratorArgumentType) || []);
}
} else {
result.push.apply(result, getObjectData(defaultExportType) || []);
}
Expand Down Expand Up @@ -264,10 +264,10 @@ function getComputed(
if (defaultExportType.isClass()) {
result.push.apply(result, getClassComputed(defaultExportType) || []);

const decoratorArgumentType = checker.getTypeAtLocation(
(defaultExportType.symbol.declarations[0].decorators![0].expression as ts.CallExpression).arguments[0]
);
result.push.apply(result, getObjectComputed(decoratorArgumentType) || []);
const decoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
if (decoratorArgumentType) {
result.push.apply(result, getObjectComputed(decoratorArgumentType) || []);
}
} else {
result.push.apply(result, getObjectComputed(defaultExportType) || []);
}
Expand Down Expand Up @@ -348,10 +348,10 @@ function getMethods(
if (defaultExportType.isClass()) {
result.push.apply(result, getClassMethods(defaultExportType) || []);

const decoratorArgumentType = checker.getTypeAtLocation(
(defaultExportType.symbol.declarations[0].decorators![0].expression as ts.CallExpression).arguments[0]
);
result.push.apply(result, getObjectMethods(decoratorArgumentType) || []);
const decoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
if (decoratorArgumentType) {
result.push.apply(result, getObjectMethods(decoratorArgumentType) || []);
}
} else {
result.push.apply(result, getObjectMethods(defaultExportType) || []);
}
Expand Down Expand Up @@ -429,6 +429,24 @@ export function getLastChild(d: ts.Declaration) {
return children[children.length - 1];
}

export function getClassDecoratorArgumentType(
tsModule: T_TypeScript,
type: ts.Type,
checker: ts.TypeChecker
) {
const decorators = type.symbol.declarations[0].decorators;
if (!decorators || decorators.length === 0) {
return undefined;
}

const decoratorArguments = (decorators[0].expression as ts.CallExpression).arguments;
if (!decoratorArguments || decoratorArguments.length === 0) {
return undefined;
}

return checker.getTypeAtLocation(decoratorArguments[0]);
}

export function buildDocumentation(tsModule: T_TypeScript, s: ts.Symbol, checker: ts.TypeChecker) {
let documentation = s
.getDocumentationComment(checker)
Expand Down

0 comments on commit 11bdeea

Please sign in to comment.