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",
"jest": "^24.8.0",
"jest-watch-typeahead": "^0.3.1",
"js-yaml": "^3.13.1"
"js-yaml": "^3.13.1",
"json-schema-to-typescript": "^8.2.0"
},
"scripts": {
"build": "tsc",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,12 +7,29 @@ import { getFromFixtures } from '../helpers';
const multiExportedComptPath = getFromFixtures('multiple-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 = {
// cwd: multiExportedComptPath,
// entry: multiExportedComptPath,
// cwd: singleExportedComptPath,
// entry: singleExportedComptPath,
// accesser: 'local',
// isExportedAsMultiple: true,
// isExportedAsMultiple: false,
// };
// const scanner = new Scanner(options);
@ -22,19 +39,3 @@ const singleExportedComptPath = getFromFixtures('single-exported-component');
// 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);
});