From 3f975232c7cd551bc9c74962095dcc9b127af489 Mon Sep 17 00:00:00 2001 From: gengyang Date: Mon, 16 Mar 2020 20:40:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20bug=20of=20validate?= =?UTF-8?q?=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/material-parser/package.json | 3 +- packages/material-parser/schemas/schema.yml | 7 +- .../src/accesser/LocalAccesser.ts | 1 + .../src/otter-core/schema/types.ts | 79 ++++++++----------- .../material-parser/src/parser/ReactParser.ts | 2 + .../src/parser/resolver/index.ts | 9 +-- .../material-parser/src/scanner/Scanner.ts | 9 ++- .../material-parser/src/types/IGenerator.ts | 2 +- packages/material-parser/src/types/IParser.ts | 2 +- .../material-parser/src/validate/schema.json | 16 +++- .../test/parser/ReactParser.ts | 41 +++++----- 11 files changed, 85 insertions(+), 86 deletions(-) diff --git a/packages/material-parser/package.json b/packages/material-parser/package.json index c81deee55..068a7fed4 100644 --- a/packages/material-parser/package.json +++ b/packages/material-parser/package.json @@ -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", diff --git a/packages/material-parser/schemas/schema.yml b/packages/material-parser/schemas/schema.yml index 471812e32..71d2f8ec0 100644 --- a/packages/material-parser/schemas/schema.yml +++ b/packages/material-parser/schemas/schema.yml @@ -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: diff --git a/packages/material-parser/src/accesser/LocalAccesser.ts b/packages/material-parser/src/accesser/LocalAccesser.ts index 2f2b60564..e3bc885ce 100644 --- a/packages/material-parser/src/accesser/LocalAccesser.ts +++ b/packages/material-parser/src/accesser/LocalAccesser.ts @@ -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); } diff --git a/packages/material-parser/src/otter-core/schema/types.ts b/packages/material-parser/src/otter-core/schema/types.ts index 1107ab720..5fe064355 100644 --- a/packages/material-parser/src/otter-core/schema/types.ts +++ b/packages/material-parser/src/otter-core/schema/types.ts @@ -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; } diff --git a/packages/material-parser/src/parser/ReactParser.ts b/packages/material-parser/src/parser/ReactParser.ts index e80426213..ca1418f16 100644 --- a/packages/material-parser/src/parser/ReactParser.ts +++ b/packages/material-parser/src/parser/ReactParser.ts @@ -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') { diff --git a/packages/material-parser/src/parser/resolver/index.ts b/packages/material-parser/src/parser/resolver/index.ts index 732851407..24c6aed9c 100644 --- a/packages/material-parser/src/parser/resolver/index.ts +++ b/packages/material-parser/src/parser/resolver/index.ts @@ -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); diff --git a/packages/material-parser/src/scanner/Scanner.ts b/packages/material-parser/src/scanner/Scanner.ts index 98ef329e3..6c26b0bf2 100644 --- a/packages/material-parser/src/scanner/Scanner.ts +++ b/packages/material-parser/src/scanner/Scanner.ts @@ -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({ diff --git a/packages/material-parser/src/types/IGenerator.ts b/packages/material-parser/src/types/IGenerator.ts index ff37a8b8a..bd16d9ead 100644 --- a/packages/material-parser/src/types/IGenerator.ts +++ b/packages/material-parser/src/types/IGenerator.ts @@ -1,5 +1,5 @@ import { IComponentMaterial } from '../otter-core'; -import IMaterialParsedModel from './IMaterialParsedModel'; +import { IMaterialParsedModel } from './IMaterialParsedModel'; import IMaterialScanModel from './IMaterialScanModel'; /** diff --git a/packages/material-parser/src/types/IParser.ts b/packages/material-parser/src/types/IParser.ts index 2c72ed208..3906b5ec9 100644 --- a/packages/material-parser/src/types/IParser.ts +++ b/packages/material-parser/src/types/IParser.ts @@ -1,4 +1,4 @@ -import IMaterialParsedModel from './IMaterialParsedModel'; +import { IMaterialParsedModel } from './IMaterialParsedModel'; import IMaterialScanModel from './IMaterialScanModel'; /** diff --git a/packages/material-parser/src/validate/schema.json b/packages/material-parser/src/validate/schema.json index 5aed3e9d0..5a0364f76 100644 --- a/packages/material-parser/src/validate/schema.json +++ b/packages/material-parser/src/validate/schema.json @@ -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": { diff --git a/packages/material-parser/test/parser/ReactParser.ts b/packages/material-parser/test/parser/ReactParser.ts index 27b2e2b9d..543557d3a 100644 --- a/packages/material-parser/test/parser/ReactParser.ts +++ b/packages/material-parser/test/parser/ReactParser.ts @@ -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); -});