From 6351c00e6b764fb04ab5806dc38cff8feb86583a Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 23 Apr 2022 12:09:07 +0200 Subject: [PATCH] Make compatible with both old and new plugin versions --- example.js | 11 +++++-- index.js | 38 +++++++++++++++++++++-- package.json | 4 ++- yarn.lock | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index 3d50e77..41481bf 100644 --- a/example.js +++ b/example.js @@ -2,12 +2,19 @@ const { validateValue } = require("@validatem/core"); const postcssNested = require("postcss-nested"); +const postcssNestedProps = require("postcss-nested-props"); const isPostCSSPlugin = require("./"); -/* Plugin (factory) */ +/* Plugin (factory), PostCSS <= 7 */ +console.log(validateValue(postcssNestedProps, [ isPostCSSPlugin ])); // { process: [Function (anonymous)] } + +/* Plugin function (instance), PostCSS <= 7 */ +console.log(validateValue(postcssNestedProps(), [ isPostCSSPlugin ])); // { postcssPlugin: 'postcss-nested-props', postcssVersion: '6.0.23' } + +/* Plugin (factory), PostCSS 8 */ console.log(validateValue(postcssNested, [ isPostCSSPlugin ])); // [Function (anonymous)] { postcss: true } -/* Plugin function (instance) */ +/* Plugin function (instance), PostCSS 8 */ console.log(validateValue(postcssNested(), [ isPostCSSPlugin ])); // { postcssPlugin: 'postcss-nested', Rule: [Function: Rule] } console.log(validateValue("hello world", [ isPostCSSPlugin ])); /* diff --git a/index.js b/index.js index ff6cd7f..50071d5 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const wrapError = require("@validatem/wrap-error"); const hasShape = require("@validatem/has-shape"); const isFunction = require("@validatem/is-function"); const isPlainObject = require("@validatem/is-plain-object"); +const isValue = require("@validatem/is-value"); const allowExtraProperties = require("@validatem/allow-extra-properties"); const either = require("@validatem/either"); const required = require("@validatem/required"); @@ -18,16 +19,47 @@ function hasPostCSSProperty(object) { } } -let plugin = [ +let plugin7 = [ isFunction, hasPostCSSProperty, + allowExtraProperties(hasShape({ + process: [ required, isFunction ] + })), +]; + +let pluginFunction7 = [ + isFunction, + allowExtraProperties(hasShape({ + postcssPlugin: [ required ], + postcssVersion: [ required ] + })) ]; -let pluginFunction = [ +let plugin8 = [ + isFunction, + hasPostCSSProperty, + allowExtraProperties(hasShape({ + postcss: [ required, isValue(true) ] + })), +]; + +let pluginFunction8 = [ isPlainObject, allowExtraProperties(hasShape({ postcssPlugin: [ required ], })) ]; -module.exports = wrapError("Must be a PostCSS plugin", either([ plugin, pluginFunction ])); +module.exports = wrapError("Must be a PostCSS plugin", either([ + // NOTE: The order of these rules matters, we need to test the v7 options first, to prevent accidental evaluation of the `postcss` property in v7 plugins by the `plugin8` rule + plugin7, pluginFunction7, + plugin8, pluginFunction8 +])); + +module.exports.v7 = wrapError("Must be a PostCSS <= v7 plugin", either([ + plugin7, pluginFunction7 +])); + +module.exports.v8 = wrapError("Must be a PostCSS v8 plugin", either([ + plugin8, pluginFunction8 +])); diff --git a/package.json b/package.json index d8b8a5e..045d312 100644 --- a/package.json +++ b/package.json @@ -17,11 +17,13 @@ "@validatem/has-shape": "^0.1.5", "@validatem/is-function": "^0.1.0", "@validatem/is-plain-object": "^0.1.1", + "@validatem/is-value": "^0.1.0", "@validatem/required": "^0.1.1", "@validatem/wrap-error": "^0.1.2" }, "devDependencies": { "@validatem/core": "^0.3.3", - "postcss-nested": "^5.0.0" + "postcss-nested": "^5.0.0", + "postcss-nested-props": "^2.0.0" } } diff --git a/yarn.lock b/yarn.lock index d94d98c..b55c3cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -99,6 +99,13 @@ "@validatem/error" "^1.0.0" is-plain-obj "^2.1.0" +"@validatem/is-value@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@validatem/is-value/-/is-value-0.1.0.tgz#b4c7481818a88d7c24400b9b587f83b388b37d56" + integrity sha512-FrwC6AP4W8dN6GCJEX02qNphoRUaN2JtdbIU2ztwPpLFUSgaJ+zvUrIPYDr8f+8YfrI/QIHm+uiY2TNEjQq/iQ== + dependencies: + "@validatem/error" "^1.0.0" + "@validatem/match-special@^0.1.0": version "0.1.0" resolved "https://registry.npmjs.org/@validatem/match-special/-/match-special-0.1.0.tgz" @@ -171,6 +178,13 @@ default-value "^1.0.0" split-filter-n "^1.1.2" +ansi-styles@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" + integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA== + dependencies: + color-convert "^1.9.0" + as-expression@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/as-expression/-/as-expression-1.0.0.tgz" @@ -181,6 +195,27 @@ assure-array@^1.0.0: resolved "https://registry.npmjs.org/assure-array/-/assure-array-1.0.0.tgz" integrity sha1-T0rRaodlnWIApPtxA0YgM9IW7B8= +chalk@^2.4.1: + version "2.4.2" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" + integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== + dependencies: + ansi-styles "^3.2.1" + escape-string-regexp "^1.0.5" + supports-color "^5.3.0" + +color-convert@^1.9.0: + version "1.9.3" + resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8" + integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg== + dependencies: + color-name "1.1.3" + +color-name@1.1.3: + version "1.1.3" + resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" + integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= + create-error@^0.3.1: version "0.3.1" resolved "https://registry.npmjs.org/create-error/-/create-error-0.3.1.tgz" @@ -203,11 +238,21 @@ es6-promise-try@0.0.1: resolved "https://registry.npmjs.org/es6-promise-try/-/es6-promise-try-0.0.1.tgz" integrity sha1-EPFA2tJ0Wc75SZc+XSGgh/cnSyA= +escape-string-regexp@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= + flatten@^1.0.3: version "1.0.3" resolved "https://registry.npmjs.org/flatten/-/flatten-1.0.3.tgz" integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg== +has-flag@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" + integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= + is-arguments@^1.0.4: version "1.0.4" resolved "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz" @@ -223,6 +268,15 @@ is-plain-obj@^2.1.0: resolved "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz" integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA== +postcss-nested-props@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/postcss-nested-props/-/postcss-nested-props-2.0.0.tgz#91ad31b05cbfbf0ff8a38b6c22ef46efbdb0b03a" + integrity sha512-fwq48WtIGZCjPMAjUAT6ZFkGZxPmlT+MVA+KE5QtnqORIa1n4rKEr2KQ2yS+cZOH1oEOb5zc/BoPcPe4+FR8+Q== + dependencies: + postcss "^6.0.14" + pseudo-classes "^1.0.0" + pseudo-elements "^1.1.0" + postcss-nested@^5.0.0: version "5.0.6" resolved "https://registry.yarnpkg.com/postcss-nested/-/postcss-nested-5.0.6.tgz#466343f7fc8d3d46af3e7dba3fcd47d052a945bc" @@ -238,11 +292,42 @@ postcss-selector-parser@^6.0.6: cssesc "^3.0.0" util-deprecate "^1.0.2" +postcss@^6.0.14: + version "6.0.23" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.23.tgz#61c82cc328ac60e677645f979054eb98bc0e3324" + integrity sha512-soOk1h6J3VMTZtVeVpv15/Hpdl2cBLX3CAw4TAbkpTJiNPk9YP/zWcD1ND+xEtvyuuvKzbxliTOIyvkSeSJ6ag== + dependencies: + chalk "^2.4.1" + source-map "^0.6.1" + supports-color "^5.4.0" + +pseudo-classes@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/pseudo-classes/-/pseudo-classes-1.0.0.tgz#60a69b67395c36ff119c4d1c86e1981785206b96" + integrity sha1-YKabZzlcNv8RnE0chuGYF4Uga5Y= + +pseudo-elements@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/pseudo-elements/-/pseudo-elements-1.1.0.tgz#9ba6dd8ac3ce1f3d7d36d4355aa3e28d08391f28" + integrity sha1-m6bdisPOHz19NtQ1WqPijQg5Hyg= + +source-map@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" + integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== + split-filter-n@^1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/split-filter-n/-/split-filter-n-1.1.2.tgz" integrity sha512-+hXSQYpKe1uyXPXI4zQtAJAlaF2EzEc+BaF2goMeNL5oUD5YLqrVcpjxELJxpomXfwMCUaYLAszEbdY9gKVdHQ== +supports-color@^5.3.0, supports-color@^5.4.0: + version "5.5.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" + integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== + dependencies: + has-flag "^3.0.0" + util-deprecate@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"