Skip to content

Commit

Permalink
Fix decorator is identifier error
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Oct 28, 2019
1 parent f680988 commit 200d1a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
8 changes: 3 additions & 5 deletions server/src/modes/script/childComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getLastChild,
buildDocumentation,
getNodeFromExportNode,
getClassDecoratorArgumentType,
getComponentDecoratorArgumentType,
isClassType
} from './componentInfo';
import { T_TypeScript } from '../../services/dependencyService';
Expand All @@ -28,7 +28,7 @@ export function getChildComponents(
let type = defaultExportType;
if (isClassType(tsModule, type)) {
// get decorator argument type when class
const classDecoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
const classDecoratorArgumentType = getComponentDecoratorArgumentType(tsModule, defaultExportType, checker);
if (!classDecoratorArgumentType) {
return undefined;
}
Expand Down Expand Up @@ -73,9 +73,7 @@ export function getChildComponents(

if (objectLiteralSymbol.flags & tsModule.SymbolFlags.Alias) {
const definitionSymbol = checker.getAliasedSymbol(objectLiteralSymbol);
if (tsModule.isClassDeclaration(
definitionSymbol.valueDeclaration
)) {
if (tsModule.isClassDeclaration(definitionSymbol.valueDeclaration)) {
const defaultExportNode = definitionSymbol.valueDeclaration;
result.push({
name: componentName,
Expand Down
40 changes: 29 additions & 11 deletions server/src/modes/script/componentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t
const propsSymbols = type
.getProperties()
.filter(property =>
getPropertyDecoratorNames(property, tsModule.SyntaxKind.PropertyDeclaration).some(decoratorName =>
getPropertyDecoratorNames(tsModule, property, tsModule.SyntaxKind.PropertyDeclaration).some(decoratorName =>
propDecoratorNames.includes(decoratorName)
)
);
Expand Down Expand Up @@ -194,7 +194,7 @@ function getData(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: ts
.getProperties()
.filter(
property =>
!getPropertyDecoratorNames(property, tsModule.SyntaxKind.PropertyDeclaration).some(decoratorName =>
!getPropertyDecoratorNames(tsModule, property, tsModule.SyntaxKind.PropertyDeclaration).some(decoratorName =>
noDataDecoratorNames.includes(decoratorName)
) &&
!property.name.startsWith('_') &&
Expand Down Expand Up @@ -334,7 +334,7 @@ function getMethods(
.getProperties()
.filter(
property =>
!getPropertyDecoratorNames(property, tsModule.SyntaxKind.MethodDeclaration).some(
!getPropertyDecoratorNames(tsModule, property, tsModule.SyntaxKind.MethodDeclaration).some(
decoratorName => decoratorName === 'Watch'
) && !isInternalHook(property.name)
);
Expand Down Expand Up @@ -408,13 +408,24 @@ export function isClassType(tsModule: T_TypeScript, type: ts.Type) {
}
}

export function getClassDecoratorArgumentType(tsModule: T_TypeScript, type: ts.Type, checker: ts.TypeChecker) {
export function getComponentDecoratorArgumentType(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;
const componentDecorator = decorators.find(el => {
const decoratorName = tsModule.isCallExpression(el.expression)
? el.expression.expression.getText()
: el.expression.getText();
return decoratorName === 'Component';
});

if (!componentDecorator || !tsModule.isCallExpression(componentDecorator.expression)) {
return undefined;
}

const decoratorArguments = componentDecorator.expression.arguments;
if (!decoratorArguments || decoratorArguments.length === 0) {
return undefined;
}
Expand All @@ -432,7 +443,7 @@ function getClassAndObjectInfo<C, O>(
const result: Array<C | O> = [];
if (isClassType(tsModule, defaultExportType)) {
result.push.apply(result, getClassResult(defaultExportType) || []);
const decoratorArgumentType = getClassDecoratorArgumentType(tsModule, defaultExportType, checker);
const decoratorArgumentType = getComponentDecoratorArgumentType(tsModule, defaultExportType, checker);
if (decoratorArgumentType) {
result.push.apply(result, getObjectResult(decoratorArgumentType) || []);
}
Expand All @@ -442,7 +453,11 @@ function getClassAndObjectInfo<C, O>(
return result;
}

function getPropertyDecoratorNames(property: ts.Symbol, checkSyntaxKind: ts.SyntaxKind): string[] {
function getPropertyDecoratorNames(
tsModule: T_TypeScript,
property: ts.Symbol,
checkSyntaxKind: ts.SyntaxKind
): string[] {
if (property.valueDeclaration.kind !== checkSyntaxKind) {
return [];
}
Expand All @@ -456,10 +471,13 @@ function getPropertyDecoratorNames(property: ts.Symbol, checkSyntaxKind: ts.Synt
return [];
}

return decorators
.map(decorator => decorator.expression as ts.CallExpression)
.filter(decoratorExpression => decoratorExpression.expression !== undefined)
.map(decoratorExpression => decoratorExpression.expression.getText());
return decorators.map(decorator => {
if (tsModule.isCallExpression(decorator.expression)) {
return decorator.expression.expression.getText();
} else {
return decorator.expression.getText();
}
});
}

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

0 comments on commit 200d1a6

Please sign in to comment.