From 483952abcb6903a067387cf366958bb7e4806be5 Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Fri, 14 Aug 2020 22:26:05 +0800 Subject: [PATCH 1/2] WIP --- server/src/modes/script/componentInfo.ts | 40 ++++++++++++++++++++++-- server/src/services/vueInfoService.ts | 2 ++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/server/src/modes/script/componentInfo.ts b/server/src/modes/script/componentInfo.ts index 105f6bc90a..f76aa4d08a 100644 --- a/server/src/modes/script/componentInfo.ts +++ b/server/src/modes/script/componentInfo.ts @@ -100,6 +100,21 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t const result: PropInfo[] = getClassAndObjectInfo(tsModule, defaultExportType, checker, getClassProps, getObjectProps); return result.length === 0 ? undefined : result; + function getPropStatus (propertyValue: ts.Node | undefined): { detailed: boolean, required: boolean } { + if (!propertyValue) { return { detailed: false, required: true }; } + if (!tsModule.isObjectLiteralExpression(propertyValue)) { return { detailed: false, required: true }; } + + function isRequired (propertyValue: ts.ObjectLiteralExpression) { + const propertyValueSymbol = checker.getSymbolAtLocation(propertyValue); + const requiredValue = propertyValueSymbol?.members?.get('required' as ts.__String)?.valueDeclaration; + if (!requiredValue || !tsModule.isPropertyAssignment(requiredValue)) { return false; } + if (requiredValue?.initializer.kind === tsModule.SyntaxKind.TrueKeyword) { return true; } + return false; + } + + return { required: isRequired(propertyValue), detailed: true }; + } + function getClassProps(type: ts.Type) { const propDecoratorNames = ['Prop', 'Model', 'PropSync']; const propsSymbols = type @@ -123,11 +138,24 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t const decoratorName = decoratorExpr.expression.getText(); const firstNode = decoratorExpr.arguments[0]; if (decoratorName === 'PropSync' && tsModule.isStringLiteral(firstNode)) { - return { name: firstNode.text, documentation: buildDocumentation(tsModule, propSymbol, checker) }; + return { + name: firstNode.text, + ...getPropStatus(args[1]), + documentation: buildDocumentation(tsModule, propSymbol, checker) + }; + } + + if (decoratorName === 'Model') { + return { + name: propSymbol.name, + ...getPropStatus(args[1]), + documentation: buildDocumentation(tsModule, propSymbol, checker) + }; } return { name: propSymbol.name, + ...getPropStatus(args[0]), documentation: buildDocumentation(tsModule, propSymbol, checker) }; }); @@ -153,6 +181,8 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t .map(expr => { return { name: (expr as ts.StringLiteral).text, + detailed: false, + required: true, documentation: `\`\`\`js\n${formatJSLikeDocumentation( propsDeclaration.parent.getFullText().trim() )}\n\`\`\`\n` @@ -166,7 +196,8 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t * { * props: { * foo: { type: Boolean, default: true }, - * bar: { type: String, default: 'bar' } + * bar: { type: String, default: 'bar' }, + * cac: String * } * } * ``` @@ -175,8 +206,13 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t const propsType = checker.getTypeOfSymbolAtLocation(propsSymbol, propsDeclaration); return checker.getPropertiesOfType(propsType).map(s => { + const status = tsModule.isPropertyAssignment(s.valueDeclaration) + ? getPropStatus(s.valueDeclaration.initializer) + : { detailed: false, required: true }; + return { name: s.name, + ...status, documentation: buildDocumentation(tsModule, s, checker) }; }); diff --git a/server/src/services/vueInfoService.ts b/server/src/services/vueInfoService.ts index 13bb9fcb06..a8c515d3a2 100644 --- a/server/src/services/vueInfoService.ts +++ b/server/src/services/vueInfoService.ts @@ -45,6 +45,8 @@ export interface ChildComponent { export interface PropInfo { name: string; + detailed: boolean; + required: boolean; documentation?: string; } export interface DataInfo { From 122b9222265a7ff568fca6f3f0ed9ec5485beb33 Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Wed, 26 Aug 2020 14:19:00 +0800 Subject: [PATCH 2/2] Fix build error --- server/src/modes/script/componentInfo.ts | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/server/src/modes/script/componentInfo.ts b/server/src/modes/script/componentInfo.ts index f76aa4d08a..40c0dbf8a2 100644 --- a/server/src/modes/script/componentInfo.ts +++ b/server/src/modes/script/componentInfo.ts @@ -100,15 +100,23 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t const result: PropInfo[] = getClassAndObjectInfo(tsModule, defaultExportType, checker, getClassProps, getObjectProps); return result.length === 0 ? undefined : result; - function getPropStatus (propertyValue: ts.Node | undefined): { detailed: boolean, required: boolean } { - if (!propertyValue) { return { detailed: false, required: true }; } - if (!tsModule.isObjectLiteralExpression(propertyValue)) { return { detailed: false, required: true }; } + function getPropStatus(propertyValue: ts.Node | undefined): { detailed: boolean; required: boolean } { + if (!propertyValue) { + return { detailed: false, required: true }; + } + if (!tsModule.isObjectLiteralExpression(propertyValue)) { + return { detailed: false, required: true }; + } - function isRequired (propertyValue: ts.ObjectLiteralExpression) { + function isRequired(propertyValue: ts.ObjectLiteralExpression) { const propertyValueSymbol = checker.getSymbolAtLocation(propertyValue); const requiredValue = propertyValueSymbol?.members?.get('required' as ts.__String)?.valueDeclaration; - if (!requiredValue || !tsModule.isPropertyAssignment(requiredValue)) { return false; } - if (requiredValue?.initializer.kind === tsModule.SyntaxKind.TrueKeyword) { return true; } + if (!requiredValue || !tsModule.isPropertyAssignment(requiredValue)) { + return false; + } + if (requiredValue?.initializer.kind === tsModule.SyntaxKind.TrueKeyword) { + return true; + } return false; } @@ -140,7 +148,7 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t if (decoratorName === 'PropSync' && tsModule.isStringLiteral(firstNode)) { return { name: firstNode.text, - ...getPropStatus(args[1]), + ...getPropStatus(decoratorExpr.arguments[1]), documentation: buildDocumentation(tsModule, propSymbol, checker) }; } @@ -148,14 +156,14 @@ function getProps(tsModule: T_TypeScript, defaultExportType: ts.Type, checker: t if (decoratorName === 'Model') { return { name: propSymbol.name, - ...getPropStatus(args[1]), + ...getPropStatus(decoratorExpr.arguments[1]), documentation: buildDocumentation(tsModule, propSymbol, checker) }; } return { name: propSymbol.name, - ...getPropStatus(args[0]), + ...getPropStatus(decoratorExpr.arguments[0]), documentation: buildDocumentation(tsModule, propSymbol, checker) }; });