Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change props validation, Fix #2141 #2166

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions server/src/modes/script/componentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ 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
Expand All @@ -123,11 +146,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(decoratorExpr.arguments[1]),
documentation: buildDocumentation(tsModule, propSymbol, checker)
};
}

if (decoratorName === 'Model') {
return {
name: propSymbol.name,
...getPropStatus(decoratorExpr.arguments[1]),
documentation: buildDocumentation(tsModule, propSymbol, checker)
};
}

return {
name: propSymbol.name,
...getPropStatus(decoratorExpr.arguments[0]),
documentation: buildDocumentation(tsModule, propSymbol, checker)
};
});
Expand All @@ -153,6 +189,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`
Expand All @@ -166,7 +204,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
* }
* }
* ```
Expand All @@ -175,8 +214,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)
};
});
Expand Down
2 changes: 2 additions & 0 deletions server/src/services/vueInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface ChildComponent {

export interface PropInfo {
name: string;
detailed: boolean;
required: boolean;
documentation?: string;
}
export interface DataInfo {
Expand Down