From 96d90ae80c488f9e14c4f917e0e6afb33abd4938 Mon Sep 17 00:00:00 2001 From: MiniDigger | Martin Date: Thu, 17 Oct 2024 15:25:46 +0200 Subject: [PATCH] fix tests by adding validation logic for rules --- index.js | 23 +++++++++++++++++---- test/invalid-config.test.js | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 test/invalid-config.test.js diff --git a/index.js b/index.js index 894ec62..c87962d 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ const addNamespace = util.deprecate((namespace, namespaces) => { namespaces.push(namespace); } }, 'Using the "namespace" option of @namics/stylelint-bem is deprecated. ' + -'Please use the new namespaces option which allows using multiple namespaces'); + 'Please use the new namespaces option which allows using multiple namespaces'); module.exports = stylelint.createPlugin(ruleName, (options) => { options = options || ''; @@ -176,13 +176,28 @@ module.exports = stylelint.createPlugin(ruleName, (options) => { return `use the ${getValidSyntax(className, namespaces)} syntax`; } } - + const isString = (val) => typeof val === 'string' || val instanceof String; return (root, result) => { + let possible; + if (options === true || options === false) { + possible = [true, false]; + } else { + possible = { + patternPrefixes: [isString], + helperPrefixes: [isString], + namespaces: [isString], + namespace: isString, + }; + } + const validOptions = stylelint.utils.validateOptions( result, ruleName, - { actual: options }, - ); + { + actual: options, + optional: true, + possible, + }); if (!validOptions) { return; diff --git a/test/invalid-config.test.js b/test/invalid-config.test.js new file mode 100644 index 0000000..fb51c67 --- /dev/null +++ b/test/invalid-config.test.js @@ -0,0 +1,40 @@ +const { ruleName } = require('../index'); + +testRule({ + ruleName, + config: { + invalidKey: 'string', + }, + reject: [ + { + code: '.test {}', + message: `Invalid option name "invalidKey" for rule "${ruleName}"`, + }, + ], +}); + +testRule({ + ruleName, + config: { + namespace: true, + }, + reject: [ + { + code: '.test {}', + message: `Invalid value "true" for option "namespace" of rule "${ruleName}"`, + }, + ], +}); + +testRule({ + ruleName, + config: { + patternPrefixes: 42, + }, + reject: [ + { + code: '.test {}', + message: `Invalid value "42" for option "patternPrefixes" of rule "${ruleName}"`, + }, + ], +});