mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-03-06 10:27:22 +00:00
feat: support params & returns of func propType
This commit is contained in:
parent
0b80be68ae
commit
0e46e49c42
@ -1,18 +1,46 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
|
import { set, get } from 'lodash';
|
||||||
|
import { debug } from '../../../core';
|
||||||
|
|
||||||
|
const log = debug.extend('parse:js');
|
||||||
|
|
||||||
const parseJsDoc = require('react-docgen/dist/utils/parseJsDoc').default;
|
const parseJsDoc = require('react-docgen/dist/utils/parseJsDoc').default;
|
||||||
const { getMemberValuePath, resolveToValue } = require('react-docgen').utils;
|
const { getMemberValuePath, resolveToValue } = require('react-docgen').utils;
|
||||||
|
|
||||||
|
function getType(type = 'void') {
|
||||||
|
const typeOfType = typeof type;
|
||||||
|
if (typeOfType === 'string') {
|
||||||
|
return typeOfType;
|
||||||
|
} else if (typeOfType === 'object') {
|
||||||
|
return get(type, 'name', 'void');
|
||||||
|
}
|
||||||
|
return 'void';
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateRaw(params = [], returns = { type: 'void' }): string {
|
||||||
|
const raw = `(${params.filter(x => !!x).map(x => `${x.name}: ${getType(x.type)}`).join(', ')}) => ${returns ? getType(returns.type) : 'void'}`;
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
function resolveDocumentation(documentation) {
|
function resolveDocumentation(documentation) {
|
||||||
documentation._props.forEach(propDescriptor => {
|
documentation._props.forEach(propDescriptor => {
|
||||||
const { description } = propDescriptor;
|
const { description } = propDescriptor;
|
||||||
if (description.includes('@')) {
|
if (description.includes('@') && propDescriptor?.type?.name === 'func') {
|
||||||
const jsDoc = parseJsDoc(description);
|
const jsDoc = parseJsDoc(description);
|
||||||
propDescriptor.description = jsDoc.description;
|
propDescriptor.description = jsDoc.description;
|
||||||
if (jsDoc.params) {
|
if (jsDoc.params) {
|
||||||
propDescriptor.params = jsDoc.params;
|
set(propDescriptor, ['type', 'params'], jsDoc.params);
|
||||||
}
|
}
|
||||||
if (jsDoc.returns) {
|
if (jsDoc.returns) {
|
||||||
propDescriptor.returns = jsDoc.returns;
|
set(propDescriptor, ['type', 'returns'], jsDoc.returns);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const raw = generateRaw(jsDoc.params, jsDoc.returns);
|
||||||
|
if (raw) {
|
||||||
|
set(propDescriptor, ['type', 'raw'], raw);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -37,19 +37,25 @@ export function transformType(itemType: any) {
|
|||||||
case 'array':
|
case 'array':
|
||||||
case 'element':
|
case 'element':
|
||||||
case 'node':
|
case 'node':
|
||||||
|
case 'void':
|
||||||
break;
|
break;
|
||||||
case 'func':
|
case 'func':
|
||||||
if (params) {
|
if (params) {
|
||||||
result.params = params.map(x => ({
|
result.params = params.map(x => {
|
||||||
...x,
|
const res: any = {
|
||||||
propType: transformType(x.propType),
|
name: x.name,
|
||||||
}));
|
propType: transformType(x.type || x.propType),
|
||||||
|
};
|
||||||
|
if (x.description) {
|
||||||
|
res.description = x.description;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (returns) {
|
if (returns) {
|
||||||
result.returns = returns.map(x => ({
|
result.returns = {
|
||||||
...x,
|
propType: transformType(returns.type || returns.propType),
|
||||||
propType: transformType(x.propType),
|
};
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
if (raw) {
|
if (raw) {
|
||||||
result.raw = raw;
|
result.raw = raw;
|
||||||
@ -243,7 +249,8 @@ export function transformItem(name: string, item: any) {
|
|||||||
flowType,
|
flowType,
|
||||||
tsType,
|
tsType,
|
||||||
type = tsType || flowType,
|
type = tsType || flowType,
|
||||||
required,
|
optional,
|
||||||
|
required = optional,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
...others
|
...others
|
||||||
} = item;
|
} = item;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Parser, ComponentDoc } from 'react-docgen-typescript';
|
import { Parser, ComponentDoc } from 'react-docgen-typescript';
|
||||||
import ts, { SymbolFlags, TypeFlags } from 'typescript';
|
import ts, { SymbolFlags, TypeFlags, SyntaxKind } from 'typescript';
|
||||||
import { isEmpty, isEqual } from 'lodash';
|
import { isEmpty, isEqual } from 'lodash';
|
||||||
import { debug } from '../../core';
|
import { debug } from '../../core';
|
||||||
import { Json } from '../../types';
|
import { Json } from '../../types';
|
||||||
@ -43,20 +43,12 @@ function getFunctionParams(parameters: any[] = [], checker, parentIds, type) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function getFunctionReturns(node: any, checker, parentIds, type) {
|
||||||
* Indicates that a symbol is an alias that does not merge with a local declaration.
|
const propType = getDocgenTypeHelper(checker, node.type, false, getNextParentIds(parentIds, type));
|
||||||
* OR Is a JSContainer which may merge an alias with a local declaration
|
return {
|
||||||
*/
|
propType,
|
||||||
// function isNonLocalAlias(
|
};
|
||||||
// symbol: ts.Symbol | undefined,
|
}
|
||||||
// excludes = SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace,
|
|
||||||
// ): symbol is ts.Symbol {
|
|
||||||
// if (!symbol) return false;
|
|
||||||
// return (
|
|
||||||
// (symbol.flags & (SymbolFlags.Alias | excludes)) === SymbolFlags.Alias ||
|
|
||||||
// !!(symbol.flags & SymbolFlags.Alias && symbol.flags & SymbolFlags.Assignment)
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
const blacklistNames = [
|
const blacklistNames = [
|
||||||
'prototype',
|
'prototype',
|
||||||
@ -138,10 +130,10 @@ function getDocgenTypeHelper(
|
|||||||
function getShapeFromArray(symbolArr: ts.Symbol[], _type: ts.Type) {
|
function getShapeFromArray(symbolArr: ts.Symbol[], _type: ts.Type) {
|
||||||
const shape: Array<{
|
const shape: Array<{
|
||||||
key:
|
key:
|
||||||
| {
|
| {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
| string;
|
| string;
|
||||||
value: any;
|
value: any;
|
||||||
}> = symbolArr.map(prop => {
|
}> = symbolArr.map(prop => {
|
||||||
const propType = checker.getTypeOfSymbolAtLocation(
|
const propType = checker.getTypeOfSymbolAtLocation(
|
||||||
@ -230,6 +222,14 @@ function getDocgenTypeHelper(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
if (type?.kind === SyntaxKind.VoidExpression) {
|
||||||
|
return makeResult({
|
||||||
|
name: 'void',
|
||||||
|
raw: 'void',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const pattern = /^__global\.(.+)$/;
|
const pattern = /^__global\.(.+)$/;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (parentIds.includes(type?.symbol?.id)) {
|
if (parentIds.includes(type?.symbol?.id)) {
|
||||||
@ -281,9 +281,7 @@ function getDocgenTypeHelper(
|
|||||||
return makeResult({
|
return makeResult({
|
||||||
name: 'union',
|
name: 'union',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
value: type.types.map(t =>
|
value: type.types.map(t => getDocgenTypeHelper(checker, t, true, getNextParentIds(parentIds, type))),
|
||||||
getDocgenTypeHelper(checker, t, true, getNextParentIds(parentIds, type)),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
} else if (isComplexType(type)) {
|
} else if (isComplexType(type)) {
|
||||||
return makeResult({
|
return makeResult({
|
||||||
@ -328,6 +326,12 @@ function getDocgenTypeHelper(
|
|||||||
parentIds,
|
parentIds,
|
||||||
type,
|
type,
|
||||||
),
|
),
|
||||||
|
returns: getFunctionReturns(
|
||||||
|
checker.typeToTypeNode(type, type?.symbol?.valueDeclaration),
|
||||||
|
checker,
|
||||||
|
parentIds,
|
||||||
|
type,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
} else if (
|
} else if (
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@ -327,8 +327,27 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
propType: 'bool',
|
propType: 'bool',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: 'desc',
|
||||||
name: 'optionalFunc',
|
name: 'optionalFunc',
|
||||||
propType: 'func',
|
propType: {
|
||||||
|
params: [
|
||||||
|
{
|
||||||
|
description: 'The title of the book.',
|
||||||
|
name: 'title',
|
||||||
|
propType: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'The author of the book.',
|
||||||
|
name: 'author',
|
||||||
|
propType: 'string',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
raw: '(title: string, author: string) => any',
|
||||||
|
returns: {
|
||||||
|
propType: 'any',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
defaultValue: 123,
|
defaultValue: 123,
|
||||||
@ -547,6 +566,30 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
version: '1.0.0',
|
version: '1.0.0',
|
||||||
},
|
},
|
||||||
props: [
|
props: [
|
||||||
|
{
|
||||||
|
name: 'error',
|
||||||
|
propType: {
|
||||||
|
isRequired: true,
|
||||||
|
params: [
|
||||||
|
{
|
||||||
|
name: 'a',
|
||||||
|
propType: 'string',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
raw: '(a: string) => number',
|
||||||
|
returns: {
|
||||||
|
propType: 'number',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'void',
|
||||||
|
propType: {
|
||||||
|
isRequired: true,
|
||||||
|
type: 'void',
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'object',
|
name: 'object',
|
||||||
propType: {
|
propType: {
|
||||||
@ -640,9 +683,7 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
{
|
{
|
||||||
name: 'fun',
|
name: 'fun',
|
||||||
propType: {
|
propType: {
|
||||||
raw: '(a: string[]) => void',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'a',
|
name: 'a',
|
||||||
propType: {
|
propType: {
|
||||||
@ -651,6 +692,11 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: '(a: string[]) => void',
|
||||||
|
returns: {
|
||||||
|
propType: 'number',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -667,14 +713,17 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
name: 'func',
|
name: 'func',
|
||||||
propType: {
|
propType: {
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
raw: '{ (arg: string): number; (a: string): Element; }',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'arg',
|
name: 'arg',
|
||||||
propType: 'string',
|
propType: 'string',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: '(arg: string) => number',
|
||||||
|
returns: {
|
||||||
|
propType: 'number',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -693,9 +742,7 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
{
|
{
|
||||||
name: 'a',
|
name: 'a',
|
||||||
propType: {
|
propType: {
|
||||||
raw: '(arg: string, num: number) => void',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'arg',
|
name: 'arg',
|
||||||
propType: 'string',
|
propType: 'string',
|
||||||
@ -705,6 +752,11 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
propType: 'number',
|
propType: 'number',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: '(arg: string, num: number) => void',
|
||||||
|
returns: {
|
||||||
|
propType: 'number',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -778,14 +830,17 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
name: 'refFunc',
|
name: 'refFunc',
|
||||||
propType: {
|
propType: {
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
raw: '(p: Props) => void',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'p',
|
name: 'p',
|
||||||
propType: 'object',
|
propType: 'object',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: '(p: Props) => void',
|
||||||
|
returns: {
|
||||||
|
propType: 'number',
|
||||||
|
},
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -796,14 +851,14 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
value: [
|
value: [
|
||||||
'element',
|
'element',
|
||||||
{
|
{
|
||||||
raw: 'Func',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'a',
|
name: 'a',
|
||||||
propType: 'string',
|
propType: 'string',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: 'Func',
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -908,14 +963,14 @@ Generated by [AVA](https://avajs.dev).
|
|||||||
name: 'func2',
|
name: 'func2',
|
||||||
propType: {
|
propType: {
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
raw: 'Func',
|
params: [
|
||||||
type: 'func',
|
|
||||||
value: [
|
|
||||||
{
|
{
|
||||||
name: 'a',
|
name: 'a',
|
||||||
propType: 'string',
|
propType: 'string',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
raw: 'Func',
|
||||||
|
type: 'func',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Binary file not shown.
@ -39,7 +39,8 @@ Demo.propTypes = {
|
|||||||
optionalBool: PropTypes.bool,
|
optionalBool: PropTypes.bool,
|
||||||
/**
|
/**
|
||||||
* desc
|
* desc
|
||||||
* @param {{ok:String}} userName
|
* @param {string} title - The title of the book.
|
||||||
|
* @param {string} author - The author of the book.
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
optionalFunc: PropTypes.func,
|
optionalFunc: PropTypes.func,
|
||||||
|
|||||||
@ -16,7 +16,8 @@ Demo.propTypes = {
|
|||||||
optionalBool: PropTypes.bool,
|
optionalBool: PropTypes.bool,
|
||||||
/**
|
/**
|
||||||
* desc
|
* desc
|
||||||
* @param {{ok:String}} userName
|
* @param {string} title - The title of the book.
|
||||||
|
* @param {string} author - The author of the book.
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
optionalFunc: PropTypes.func,
|
optionalFunc: PropTypes.func,
|
||||||
|
|||||||
@ -24,6 +24,8 @@ type Union =
|
|||||||
};
|
};
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
error(a: string): number;
|
||||||
|
void: void;
|
||||||
object: Object;
|
object: Object;
|
||||||
trigger?: Array<'click' | 'hover' | 'contextMenu'>;
|
trigger?: Array<'click' | 'hover' | 'contextMenu'>;
|
||||||
str?: string;
|
str?: string;
|
||||||
@ -75,7 +77,6 @@ interface Props {
|
|||||||
elementType?: React.ElementType;
|
elementType?: React.ElementType;
|
||||||
union: Union;
|
union: Union;
|
||||||
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
|
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
|
||||||
func(a: string): JSX.Element;
|
|
||||||
func2: Func;
|
func2: Func;
|
||||||
html: HTMLBaseElement;
|
html: HTMLBaseElement;
|
||||||
loading?: boolean | { delay?: number };
|
loading?: boolean | { delay?: number };
|
||||||
@ -93,8 +94,8 @@ App.defaultProps = {
|
|||||||
a: '1',
|
a: '1',
|
||||||
b: '2',
|
b: '2',
|
||||||
},
|
},
|
||||||
func(a) {
|
func(a: string) {
|
||||||
return a;
|
return 123;
|
||||||
},
|
},
|
||||||
str: 'str',
|
str: 'str',
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user