Skip to content

Commit

Permalink
Update generate-schema script (#11043)
Browse files Browse the repository at this point in the history
* Support custom parser and use anyOf

* Update snapshots
  • Loading branch information
sosukesuzuki committed Jun 11, 2021
1 parent ce96e48 commit 3332a28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
29 changes: 20 additions & 9 deletions scripts/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,32 @@ function generateSchema(options) {
}

function optionToSchema(option) {
let schema;
if (option.type === "choice") {
const choicesSchema = option.choices.map(choiceToSchema);
let key = "oneOf";
if (option.name === "parser") {
// To support custom parser
// ref: https://github.com/SchemaStore/schemastore/pull/1636
choicesSchema.push({ type: "string", description: "Custom parser" });
// We should use "anyOf" for "parser" option.
// ref: https://github.com/SchemaStore/schemastore/pull/1642
key = "anyOf";
}
schema = { [key]: choicesSchema };
} else {
schema = { type: optionTypeToSchemaType(option.type) };
}
if (option.array) {
schema = wrapWithArraySchema(schema);
}
return {
description: option.description,
default: option.default,
...(option.array ? wrapWithArraySchema : identity)(
option.type === "choice"
? { oneOf: option.choices.map(choiceToSchema) }
: { type: optionTypeToSchemaType(option.type) }
),
...schema,
};
}

function identity(x) {
return x;
}

function wrapWithArraySchema(items) {
return { type: "array", items };
}
Expand Down
10 changes: 7 additions & 3 deletions tests/integration/__tests__/__snapshots__/schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ This option cannot be used with --range-start and --range-end.",
"type": "boolean",
},
"parser": Object {
"default": undefined,
"description": "Which parser to use.",
"oneOf": Array [
"anyOf": Array [
Object {
"description": "Flow",
"enum": Array [
Expand Down Expand Up @@ -264,7 +262,13 @@ This option cannot be used with --range-start and --range-end.",
"lwc",
],
},
Object {
"description": "Custom parser",
"type": "string",
},
],
"default": undefined,
"description": "Which parser to use.",
},
"pluginSearchDirs": Object {
"default": Array [],
Expand Down

0 comments on commit 3332a28

Please sign in to comment.