fix: 🐛 fix bug of validate schema

This commit is contained in:
gengyang 2020-03-16 20:40:34 +08:00
parent 64c9daa1f4
commit 3f975232c7
11 changed files with 85 additions and 86 deletions

View File

@ -18,7 +18,8 @@
"globby": "^10.0.1", "globby": "^10.0.1",
"jest": "^24.8.0", "jest": "^24.8.0",
"jest-watch-typeahead": "^0.3.1", "jest-watch-typeahead": "^0.3.1",
"js-yaml": "^3.13.1" "js-yaml": "^3.13.1",
"json-schema-to-typescript": "^8.2.0"
}, },
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",

View File

@ -34,8 +34,6 @@ definitions:
- componentName - componentName
- title - title
- npm - npm
- docUrl
- screenshot
PropsSection: PropsSection:
type: object type: object
required: required:
@ -143,7 +141,10 @@ definitions:
value: value:
type: array type: array
items: items:
type: string oneOf:
- type: string
- type: number
- type: boolean
isRequired: isRequired:
type: boolean type: boolean
OneOfType: OneOfType:

View File

@ -68,6 +68,7 @@ class LocalAccesser extends BaseAccesser {
this.scanner = new Scanner(options); this.scanner = new Scanner(options);
const ecology = await BaseParser.recognizeEcology(options); const ecology = await BaseParser.recognizeEcology(options);
if (ecology === 'react') { if (ecology === 'react') {
// debugger;
this.parser = new ReactParser(options); this.parser = new ReactParser(options);
this.generator = new Generator(options); this.generator = new Generator(options);
} }

View File

@ -10,23 +10,8 @@
*/ */
export type IComponentMaterial = BasicSection & PropsSection & ConfigureSection; export type IComponentMaterial = BasicSection & PropsSection & ConfigureSection;
export type PropType = BasicType | RequiredType | ComplexType; export type PropType = BasicType | RequiredType | ComplexType;
export type BasicType = export type BasicType = "array" | "bool" | "func" | "number" | "object" | "string" | "node" | "element" | "any";
| 'array' export type ComplexType = OneOf | OneOfType | ArrayOf | ObjectOf | Shape | Exact;
| 'bool'
| 'func'
| 'number'
| 'object'
| 'string'
| 'node'
| 'element'
| 'any';
export type ComplexType =
| OneOf
| OneOfType
| ArrayOf
| ObjectOf
| Shape
| Exact;
export type ConfigureProp = { export type ConfigureProp = {
title?: string; title?: string;
extraProps?: { extraProps?: {
@ -39,11 +24,11 @@ export interface BasicSection {
componentName: string; componentName: string;
title: string; title: string;
description?: string; description?: string;
docUrl: string; docUrl?: string;
screenshot: string; screenshot?: string;
icon?: string; icon?: string;
tags?: string[]; tags?: string[];
devMode?: 'proCode' | 'lowCode'; devMode?: "proCode" | "lowCode";
npm: Npm; npm: Npm;
[k: string]: any; [k: string]: any;
} }
@ -71,31 +56,31 @@ export interface RequiredType {
isRequired?: boolean; isRequired?: boolean;
} }
export interface OneOf { export interface OneOf {
type: 'oneOf'; type: "oneOf";
value: string[]; value: (string | number | boolean)[];
isRequired?: boolean; isRequired?: boolean;
[k: string]: any; [k: string]: any;
} }
export interface OneOfType { export interface OneOfType {
type: 'oneOfType'; type: "oneOfType";
value: PropType[]; value: PropType[];
isRequired?: boolean; isRequired?: boolean;
[k: string]: any; [k: string]: any;
} }
export interface ArrayOf { export interface ArrayOf {
type: 'arrayOf'; type: "arrayOf";
value: PropType; value: PropType;
isRequired?: boolean; isRequired?: boolean;
[k: string]: any; [k: string]: any;
} }
export interface ObjectOf { export interface ObjectOf {
type: 'objectOf'; type: "objectOf";
value: PropType; value: PropType;
isRequired?: boolean; isRequired?: boolean;
[k: string]: any; [k: string]: any;
} }
export interface Shape { export interface Shape {
type: 'shape'; type: "shape";
value: { value: {
name?: string; name?: string;
propType?: PropType; propType?: PropType;
@ -104,7 +89,7 @@ export interface Shape {
[k: string]: any; [k: string]: any;
} }
export interface Exact { export interface Exact {
type: 'exact'; type: "exact";
value: { value: {
name?: string; name?: string;
propType?: PropType; propType?: PropType;
@ -127,38 +112,38 @@ export interface ConfigureSection {
[k: string]: any; [k: string]: any;
} }
export interface ConfigureFieldProp { export interface ConfigureFieldProp {
type: 'field'; type: "field";
name?: string; name?: string;
setter?: ConfigureFieldSetter; setter?: ConfigureFieldSetter;
[k: string]: any; [k: string]: any;
} }
export interface ConfigureFieldSetter { export interface ConfigureFieldSetter {
componentName: componentName:
| 'List' | "List"
| 'Object' | "Object"
| 'Function' | "Function"
| 'Node' | "Node"
| 'Mixin' | "Mixin"
| 'Expression' | "Expression"
| 'Switch' | "Switch"
| 'Number' | "Number"
| 'Input' | "Input"
| 'TextArea' | "TextArea"
| 'Date' | "Date"
| 'DateYear' | "DateYear"
| 'DateMonth' | "DateMonth"
| 'DateRange' | "DateRange"
| 'ColorPicker' | "ColorPicker"
| 'CodeEditor' | "CodeEditor"
| 'Select' | "Select"
| 'RadioGroup'; | "RadioGroup";
props?: { props?: {
[k: string]: any; [k: string]: any;
}; };
[k: string]: any; [k: string]: any;
} }
export interface ConfigureGroupProp { export interface ConfigureGroupProp {
type: 'group'; type: "group";
items: ConfigureProp[]; items: ConfigureProp[];
[k: string]: any; [k: string]: any;
} }

View File

@ -37,6 +37,8 @@ function transformType(type: any): any {
if (Array.isArray(value)) { if (Array.isArray(value)) {
if (name === 'enum') { if (name === 'enum') {
result.type = 'oneOf'; result.type = 'oneOf';
} else if (name === 'union') {
result.type = 'oneOfType';
} }
result.value = value.map(transformType); result.value = value.map(transformType);
} else if (typeof value === 'object') { } else if (typeof value === 'object') {

View File

@ -80,11 +80,10 @@ export default function findExportedComponentDefinition(ast: any) {
if (isComponentDefinition(definition)) { if (isComponentDefinition(definition)) {
acc.push(definition); acc.push(definition);
} else { } else {
definition = resolveToValue(resolveIIFE(definition)); // definition = resolveToValue(resolveIIFE(definition));
if (!isComponentDefinition(definition)) { // if (!isComponentDefinition(definition)) {
definition = resolveTranspiledClass(definition); // definition = resolveTranspiledClass(definition);
// console.log("path"); // }
}
const resolved = resolveToValue(resolveHOC(definition)); const resolved = resolveToValue(resolveHOC(definition));
if (isComponentDefinition(resolved)) { if (isComponentDefinition(resolved)) {
acc.push(resolved); acc.push(resolved);

View File

@ -69,11 +69,12 @@ class Scanner implements IScanner {
log('entryFile', entryFile); log('entryFile', entryFile);
model.mainEntry = entryFilePath; model.mainEntry = entryFilePath;
// 记录入口文件 // 记录入口文件
model.modules.push({ // model.modules.push({
filePath: entryFilePath, // filePath: entryFilePath,
fileContent: entryFile, // fileContent: entryFile,
}); // });
log('model', model); log('model', model);
// debugger;
if (options.isExportedAsMultiple) { if (options.isExportedAsMultiple) {
// 解析 entryFile提取出 export 语句 // 解析 entryFile提取出 export 语句
const modules = await this.parseEntryFile({ const modules = await this.parseEntryFile({

View File

@ -1,5 +1,5 @@
import { IComponentMaterial } from '../otter-core'; import { IComponentMaterial } from '../otter-core';
import IMaterialParsedModel from './IMaterialParsedModel'; import { IMaterialParsedModel } from './IMaterialParsedModel';
import IMaterialScanModel from './IMaterialScanModel'; import IMaterialScanModel from './IMaterialScanModel';
/** /**

View File

@ -1,4 +1,4 @@
import IMaterialParsedModel from './IMaterialParsedModel'; import { IMaterialParsedModel } from './IMaterialParsedModel';
import IMaterialScanModel from './IMaterialScanModel'; import IMaterialScanModel from './IMaterialScanModel';
/** /**

View File

@ -53,9 +53,7 @@
"required": [ "required": [
"componentName", "componentName",
"title", "title",
"npm", "npm"
"docUrl",
"screenshot"
] ]
}, },
"PropsSection": { "PropsSection": {
@ -225,7 +223,17 @@
"value": { "value": {
"type": "array", "type": "array",
"items": { "items": {
"oneOf": [
{
"type": "string" "type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
}
]
} }
}, },
"isRequired": { "isRequired": {

View File

@ -7,12 +7,29 @@ import { getFromFixtures } from '../helpers';
const multiExportedComptPath = getFromFixtures('multiple-exported-component'); const multiExportedComptPath = getFromFixtures('multiple-exported-component');
const singleExportedComptPath = getFromFixtures('single-exported-component'); const singleExportedComptPath = getFromFixtures('single-exported-component');
// test.serial('parse es6 multiple exported component by local', async t => { test.serial('parse es6 multiple exported component by local', async t => {
const options: IMaterializeOptions = {
cwd: multiExportedComptPath,
entry: multiExportedComptPath,
accesser: 'local',
isExportedAsMultiple: true,
};
const scanner = new Scanner(options);
const scanModel = await scanner.scan();
const parser = new ReactParser(options);
// debugger;
const actual: IMaterialParsedModel[] = await parser.parse(scanModel);
t.snapshot(actual);
});
// test.serial('parse es6 single exported component by local', async t => {
// const options: IMaterializeOptions = { // const options: IMaterializeOptions = {
// cwd: multiExportedComptPath, // cwd: singleExportedComptPath,
// entry: multiExportedComptPath, // entry: singleExportedComptPath,
// accesser: 'local', // accesser: 'local',
// isExportedAsMultiple: true, // isExportedAsMultiple: false,
// }; // };
// const scanner = new Scanner(options); // const scanner = new Scanner(options);
@ -22,19 +39,3 @@ const singleExportedComptPath = getFromFixtures('single-exported-component');
// t.snapshot(actual); // t.snapshot(actual);
// }); // });
test.serial('parse es6 single exported component by local', async t => {
const options: IMaterializeOptions = {
cwd: singleExportedComptPath,
entry: singleExportedComptPath,
accesser: 'local',
isExportedAsMultiple: false,
};
const scanner = new Scanner(options);
const scanModel = await scanner.scan();
const parser = new ReactParser(options);
const actual: IMaterialParsedModel[] = await parser.parse(scanModel);
t.snapshot(actual);
});