From f123af3ca1828a4b8e4bc009013061fbdde41309 Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Fri, 25 Aug 2017 22:45:15 +0700 Subject: [PATCH] Fixed bad JSON schema for some formatting options --- edge/createFormattingOptions.js | 35 ++++++++++++++++++++++----------- edge/reviseDocumentation.js | 24 ++++++++++++++++++---- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/edge/createFormattingOptions.js b/edge/createFormattingOptions.js index 5b0751f..542e203 100644 --- a/edge/createFormattingOptions.js +++ b/edge/createFormattingOptions.js @@ -39,22 +39,22 @@ const schema = { }, insertNewLineAroundImports: { description: 'Insert a new-line around a group of @import/@require(s).\nOnly apply to imports outside a block when set to "root", or only apply to imports inside a block when set to "nested".\nCheck the detailed examples below.', - oneOf: [true, false, 'root', 'nested'], + enum: [true, false, 'root', 'nested'], default: true, }, insertNewLineAroundBlocks: { description: 'Insert a new-line around blocks.\nOnly apply to top-level blocks when set to "root", or only apply to nested blocks when set to "nested".\nCheck the detailed examples below.', - oneOf: [true, false, 'root', 'nested'], + enum: [true, false, 'root', 'nested'], default: true, }, insertNewLineAroundProperties: { description: 'Insert a new-line around a group of CSS properties.\nUnlike insertNewLineAroundBlocks and insertNewLineAroundOthers, this option cannot be set to "root" nor "nested" because CSS properties cannot be placed at the top level.\nCheck the detailed examples below.', - oneOf: [true, false], + type: 'boolean', default: false, }, insertNewLineAroundOthers: { description: 'Insert a new-line around a group of non-properties, non-imports and non-blocks.\nOnly apply to others outside a block when set to "root", or only apply to others inside a block when set to "nested".\nCheck the detailed examples below.', - oneOf: [true, false, 'root', 'nested'], + enum: [true, false, 'root', 'nested'], default: false, }, insertNewLineBetweenSelectors: { @@ -177,14 +177,14 @@ const schema = { }, newLineChar: { description: 'Represent a new-line character. You may want to change this to "\\r\\n" for Microsoft Windows.', - oneOf: ['\n', '\r\n'], + enum: ['\n', '\r\n'], default: '\n', hideInDemo: true, hideInVSCE: true, }, quoteChar: { description: 'Represent a quote character that is used to begin and terminate a string. You must choose either a single-quote or a double-quote.', - oneOf: ['\'', '"'], + enum: ['\'', '"'], default: '\'', example: { values: ['\'', '"'], @@ -197,9 +197,13 @@ const schema = { sortProperties: { description: 'Can be either false for doing nothing, "alphabetical" for sorting CSS properties from A to Z, "grouped" for sorting CSS properties according to Stylint, or an array of property names that defines the property order (for example, ["color", "background", "display"]).', oneOf: [ - false, - 'alphabetical', - 'grouped', + { + enum: [ + false, + 'alphabetical', + 'grouped', + ] + }, { type: 'array', items: { type: 'string' }, @@ -332,7 +336,16 @@ function createFormattingOptions(options = {}) { } function verify(data, info) { - if (info.oneOf !== undefined) { + if (_.some(info.enum)) { + return _.some(info.enum, item => { + if (_.isObject(item)) { + return verify(data, item) + } else { + return data === item + } + }) + + } else if (info.oneOf !== undefined) { const matchAnyValue = info.oneOf.some(item => { if (_.isObject(item)) { try { @@ -371,7 +384,7 @@ function verify(data, info) { throw new Error(`Expected ${data} to be null`) } - } else if (info.type !== typeof data) { + } else if (info.type !== typeof data) { // 'boolean', 'string', 'number', 'object' throw new Error(`Expected ${data} to be ${info.type}`) } diff --git a/edge/reviseDocumentation.js b/edge/reviseDocumentation.js index aaff6d7..6aec6b2 100644 --- a/edge/reviseDocumentation.js +++ b/edge/reviseDocumentation.js @@ -49,20 +49,36 @@ function createFormattingTogglersForDemo() { `` ) - } else if (_.some(item.oneOf)) { + } else if (item.enum !== undefined) { return ( `` + ) + + } else if (item.oneOf !== undefined) { + return ( + `` ) } }) - .compact() .join('') .value() + + function _enum(item) { + return item.enum.map(stub => ``).join('') + } } function createFormattingDescription() { @@ -135,7 +151,7 @@ function getType(item) { if (item.type) { return item.type + (item.items ? ('<' + getType(item.items) + '>') : '') } else { - return item.oneOf.map(getType).join(' | ') + return (item.enum || item.oneOf).map(item => getType(item)).join(' | ') } } else { return _.escape(JSON.stringify(item))