From 66fe2f78fa99789e9f784149381ff60265754481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 23 Jan 2017 22:33:58 +0100 Subject: [PATCH] Validate user-provided config with jest-validate (#301) * Validate user-provided config with jest-validate * Don't use object shorthand * Update jest-validate to 18.2 * Fix Node4 destructuring from require * Make use of jest-validate for deprecation message * Remove chalk reference --- package.json | 1 + src/deprecated.js | 13 +++++++++++++ src/options.js | 14 +++++++++++--- yarn.lock | 13 +++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/deprecated.js diff --git a/package.json b/package.json index 94423d66..098bf546 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "devDependencies": { "jest": "18.0.0", + "jest-validate": "18.2.0", "rollup": "0.41.1", "rollup-plugin-commonjs": "7.0.0", "rollup-plugin-json": "2.1.0", diff --git a/src/deprecated.js b/src/deprecated.js new file mode 100644 index 00000000..49c123e7 --- /dev/null +++ b/src/deprecated.js @@ -0,0 +1,13 @@ +"use strict"; + +const deprecated = { + useFlowParser: config => +` The ${"\"useFlowParser\""} option is deprecated. Use ${"\"parser\""} instead. + + Prettier now treats your configuration as: + { + ${"\"parser\""}: ${config.seFlowParser ? "\"flow\"" : "\"babylon\""} + }` +}; + +module.exports = deprecated; diff --git a/src/options.js b/src/options.js index e2bba684..e696e7ee 100644 --- a/src/options.js +++ b/src/options.js @@ -1,4 +1,8 @@ "use strict"; + +var validate = require("jest-validate").validate; +var deprecatedConfig = require("./deprecated"); + var defaults = { // Number of spaces the pretty-printer should use per tab tabWidth: 2, @@ -14,15 +18,19 @@ var defaults = { parser: "babylon" }; +var exampleConfig = Object.assign({}, defaults, { + filename: "testFilename", + printWidth: 80, + originalText: "text" +}); + // Copy options and fill in default values. function normalize(options) { + validate(options, { exampleConfig, deprecatedConfig }); const normalized = Object.assign({}, options || {}); // For backward compatibility. Deprecated in 0.0.10 if ("useFlowParser" in normalized) { - console.warn( - 'The `"useFlowParser": true/false` option is deprecated. Use `parser: "flow"` instead.' - ); normalized.parser = normalized.useFlowParser ? "flow" : "babylon"; delete normalized.useFlowParser; } diff --git a/yarn.lock b/yarn.lock index a2ac592b..433de800 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1441,6 +1441,15 @@ jest-util@^18.1.0: jest-mock "^18.0.0" mkdirp "^0.5.1" +jest-validate@18.2.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-18.2.0.tgz#01f93ac78f23901cf9889808e2bbb931ffc58d86" + dependencies: + chalk "^1.1.1" + jest-matcher-utils "^18.1.0" + leven "^2.0.0" + pretty-format "^18.1.0" + jest@18.0.0: version "18.0.0" resolved "https://registry.yarnpkg.com/jest/-/jest-18.0.0.tgz#ef12f70befe0fcb30f1c61c0ae58748706267d4b" @@ -1551,6 +1560,10 @@ lcid@^1.0.0: dependencies: invert-kv "^1.0.0" +leven@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/leven/-/leven-2.0.0.tgz#74c45744439550da185801912829f61d22071bc1" + levn@~0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee"