diff --git a/src/main/options-normalizer.js b/src/main/options-normalizer.js index 69fd3384..a46a579d 100644 --- a/src/main/options-normalizer.js +++ b/src/main/options-normalizer.js @@ -118,6 +118,9 @@ function optionInfoToSchema(optionInfo, { isCLI, optionInfos }) { parameters.preprocess = value => Number(value); } break; + case "string": + SchemaConstructor = vnopts.StringSchema; + break; case "choice": SchemaConstructor = vnopts.ChoiceSchema; parameters.choices = optionInfo.choices.map(choiceInfo => diff --git a/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap b/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap new file mode 100644 index 00000000..77d3ae01 --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/plugin-options-string.js.snap @@ -0,0 +1,34 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` 1`] = ` +"Snapshot Diff: +- First value ++ Second value + +@@ -17,10 +17,12 @@ + Defaults to avoid. + --no-bracket-spacing Do not print spaces between brackets. + --end-of-line + Which end of line characters to apply. + Defaults to auto. ++ --foo-string foo description ++ Defaults to bar. + --html-whitespace-sensitivity + How to handle whitespaces in HTML. + Defaults to css. + --jsx-bracket-same-line Put > on the last line instead of at a new line. + Defaults to false." +`; + +exports[`show detailed external option with \`--help foo-string\` (stderr) 1`] = `""`; + +exports[`show detailed external option with \`--help foo-string\` (stdout) 1`] = ` +"--foo-string + + foo description + +Default: bar +" +`; + +exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `Array []`; diff --git a/tests_integration/__tests__/plugin-options-string.js b/tests_integration/__tests__/plugin-options-string.js new file mode 100644 index 00000000..3c12c1e8 --- /dev/null +++ b/tests_integration/__tests__/plugin-options-string.js @@ -0,0 +1,56 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); +const snapshotDiff = require("snapshot-diff"); + +describe("show external options with `--help`", () => { + const originalStdout = runPrettier("plugins/options-string", ["--help"]) + .stdout; + const pluggedStdout = runPrettier("plugins/options-string", [ + "--help", + "--plugin=./plugin" + ]).stdout; + expect(snapshotDiff(originalStdout, pluggedStdout)).toMatchSnapshot(); +}); + +describe("show detailed external option with `--help foo-string`", () => { + runPrettier("plugins/options-string", [ + "--plugin=./plugin", + "--help", + "foo-string" + ]).test({ + status: 0 + }); +}); + +describe("external options from CLI should work", () => { + runPrettier( + "plugins/options-string", + [ + "--plugin=./plugin", + "--stdin-filepath", + "example.foo", + "--foo-string", + "baz" + ], + { input: "hello-world" } + ).test({ + stdout: "foo:baz", + stderr: "", + status: 0, + write: [] + }); +}); + +describe("external options from config file should work", () => { + runPrettier( + "plugins/options-string", + ["--config=./config.json", "--stdin-filepath", "example.foo"], + { input: "hello-world" } + ).test({ + stdout: "foo:baz", + stderr: "", + status: 0, + write: [] + }); +}); diff --git a/tests_integration/plugins/options-string/config.json b/tests_integration/plugins/options-string/config.json new file mode 100644 index 00000000..b6cca6ce --- /dev/null +++ b/tests_integration/plugins/options-string/config.json @@ -0,0 +1,4 @@ +{ + "plugins": ["./plugin"], + "fooString": "baz" +} \ No newline at end of file diff --git a/tests_integration/plugins/options-string/plugin.js b/tests_integration/plugins/options-string/plugin.js new file mode 100644 index 00000000..11090200 --- /dev/null +++ b/tests_integration/plugins/options-string/plugin.js @@ -0,0 +1,31 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + since: "1.0.0" + } + ], + options: { + fooString: { + type: "string", + default: "bar", + description: "foo description" + } + }, + parsers: { + "foo-parser": { + parse: text => ({ text }), + astFormat: "foo-ast" + } + }, + printers: { + "foo-ast": { + print: (path, options) => + options.fooString ? `foo:${options.fooString}` : path.getValue().text + } + } +};