From f7bd33b04208f5dd8f4e575e49e91aa069c4c3da Mon Sep 17 00:00:00 2001 From: Shub Date: Fri, 18 Nov 2022 08:20:01 +0530 Subject: [PATCH 1/6] [#6452] feat: enable passing plugin functions as config value --- lib/augmentConfig.js | 16 ++++++++++++++-- types/stylelint/index.d.ts | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/augmentConfig.js b/lib/augmentConfig.js index 636ffde31f..aa9208f551 100644 --- a/lib/augmentConfig.js +++ b/lib/augmentConfig.js @@ -150,7 +150,13 @@ function absolutizePaths(config, configDir, cwd) { } if (config.plugins) { - config.plugins = [config.plugins].flat().map((lookup) => getModulePath(configDir, lookup, cwd)); + config.plugins = [config.plugins].flat().map((lookup) => { + if (typeof lookup === 'string') { + return getModulePath(configDir, lookup, cwd); + } + + return lookup; + }); } if (config.processors) { @@ -322,7 +328,13 @@ function addPluginFunctions(config) { const pluginFunctions = {}; for (const pluginLookup of normalizedPlugins) { - let pluginImport = require(pluginLookup); + let pluginImport; + + if (typeof pluginLookup === 'string') { + pluginImport = require(pluginLookup); + } else { + pluginImport = pluginLookup; + } // Handle either ES6 or CommonJS modules pluginImport = pluginImport.default || pluginImport; diff --git a/types/stylelint/index.d.ts b/types/stylelint/index.d.ts index 0e8b02ada8..91460ceb39 100644 --- a/types/stylelint/index.d.ts +++ b/types/stylelint/index.d.ts @@ -7,7 +7,10 @@ declare module 'stylelint' { export type Severity = 'warning' | 'error'; export type ConfigExtends = string | string[]; - export type ConfigPlugins = string | string[]; + export type PluginImportType = + | { default?: { ruleName: string; rule: Rule } } + | { ruleName: string; rule: Rule }; + export type ConfigPlugins = string | string[] | PluginImportType | PluginImportType[]; export type ConfigProcessor = string | [string, Object]; export type ConfigProcessors = string | ConfigProcessor[]; export type ConfigIgnoreFiles = string | string[]; From ac83ec7cd00d70e0fa1e81d08ed18552d048cb96 Mon Sep 17 00:00:00 2001 From: Shub Date: Sat, 19 Nov 2022 11:11:39 +0530 Subject: [PATCH 2/6] fix: linitng errors and plugins test --- lib/__tests__/plugins.test.js | 42 +++++++++++++++++++++++++++++++++++ types/stylelint/index.d.ts | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/__tests__/plugins.test.js b/lib/__tests__/plugins.test.js index 7d3d61d6d9..adac1e3f22 100644 --- a/lib/__tests__/plugins.test.js +++ b/lib/__tests__/plugins.test.js @@ -39,6 +39,14 @@ const configRelativeAndExtendRelative = { }, }; +const configPluginRequire = { + plugins: [require('./fixtures/plugin-warn-about-foo')], + rules: { + 'plugin/warn-about-foo': 'always', + 'block-no-empty': true, + }, +}; + const processorRelative = postcss().use( stylelint({ config: configRelative, configBasedir: __dirname }), ); @@ -52,6 +60,7 @@ const processorRelativeAndExtendRelative = postcss().use( configBasedir: __dirname, }), ); +const processorPluginRequire = postcss().use(stylelint({ config: configPluginRequire })); it('one plugin runs', async () => { const result = await processorRelative.process(cssWithFoo, { from: undefined }); @@ -108,6 +117,39 @@ it('config with its own plugins extending another config that invokes a plugin w expect(result.warnings()[0].node).toBeTruthy(); }); +it('plugin with CJS require()', async () => { + const result = await processorPluginRequire.process(cssWithFoo, { from: undefined }); + + expect(result.warnings()).toHaveLength(2); + expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)'); + expect(result.warnings()[1].text).toBe('Unexpected empty block (block-no-empty)'); + expect(result.warnings()[0].node).toBeTruthy(); +}); + +it('plugin with ESM import()', async () => { + const result = await postcss() + .use( + stylelint({ + config: { + // Following object mocks `await import()` style imports, since + // jest without node --experimental-vm-modules enabled fails to + // support dynamic ESM imports. + plugins: [{ default: require('./fixtures/plugin-warn-about-foo') }], // Similar to `await import()` + rules: { + 'plugin/warn-about-foo': 'always', + 'block-no-empty': true, + }, + }, + }), + ) + .process(cssWithFoo, { from: undefined }); + + expect(result.warnings()).toHaveLength(2); + expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)'); + expect(result.warnings()[1].text).toBe('Unexpected empty block (block-no-empty)'); + expect(result.warnings()[0].node).toBeTruthy(); +}); + describe('plugin using exposed rules via stylelint.rules', () => { const cssWithDirectiveLower = '/** @@check-color-hex-case */ a { color: #eee; }'; const cssWithDirectiveUpper = '/** @@check-color-hex-case */ a { color: #EEE; }'; diff --git a/types/stylelint/index.d.ts b/types/stylelint/index.d.ts index 91460ceb39..a2d7f0e87f 100644 --- a/types/stylelint/index.d.ts +++ b/types/stylelint/index.d.ts @@ -7,10 +7,10 @@ declare module 'stylelint' { export type Severity = 'warning' | 'error'; export type ConfigExtends = string | string[]; - export type PluginImportType = + export type PluginType = | { default?: { ruleName: string; rule: Rule } } | { ruleName: string; rule: Rule }; - export type ConfigPlugins = string | string[] | PluginImportType | PluginImportType[]; + export type ConfigPlugins = string | PluginType | (string | PluginType)[]; export type ConfigProcessor = string | [string, Object]; export type ConfigProcessors = string | ConfigProcessor[]; export type ConfigIgnoreFiles = string | string[]; From e4298bb58287459afb3e1c48200a8684d1eae64c Mon Sep 17 00:00:00 2001 From: Shub Date: Wed, 23 Nov 2022 23:26:05 +0530 Subject: [PATCH 3/6] doc: update user-guide for plugins config --- docs/user-guide/configure.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/user-guide/configure.md b/docs/user-guide/configure.md index 4fb0e6c8a7..9223105bdc 100644 --- a/docs/user-guide/configure.md +++ b/docs/user-guide/configure.md @@ -234,6 +234,16 @@ To use one, add a `"plugins"` array to your config, containing "locaters" identi - npm module name - absolute path - path relative to the invoking configuration file +- stylelint plugin object of format + +```js +{ + ruleName: "plugin-name", + rule: function plugin(opts) { /* Plugin Code */ } +} +``` + +> Read more about writing [Stylint Plugins here](../developer-guide/plugins.md). Once the plugin is declared, within your `"rules"` object _you'll need to add options_ for the plugin's rule(s), just like any standard rule. Look at the plugin's documentation to know what the rule name should be. From 6c6a9f0f6d9aaf7f5e7323f8c593393b62f8d04b Mon Sep 17 00:00:00 2001 From: Richard Hallows Date: Thu, 1 Dec 2022 13:30:07 +0000 Subject: [PATCH 4/6] Update configure.md --- docs/user-guide/configure.md | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/docs/user-guide/configure.md b/docs/user-guide/configure.md index 9223105bdc..31f141144a 100644 --- a/docs/user-guide/configure.md +++ b/docs/user-guide/configure.md @@ -229,21 +229,11 @@ For example, [stylelint-scss](https://github.com/stylelint-scss/stylelint-scss) You'll find more in [awesome stylelint](https://github.com/stylelint/awesome-stylelint#plugins). -To use one, add a `"plugins"` array to your config, containing "locaters" identifying the plugins you want to use. As with `extends`, above, a "locater" can be either a: +To use one, add a `"plugins"` array to your config, containing either [plugin objects](../developer-guide/plugins.md) or "locaters" identifying the plugins you want to use. As with `extends`, above, a "locater" can be either a: - npm module name - absolute path - path relative to the invoking configuration file -- stylelint plugin object of format - -```js -{ - ruleName: "plugin-name", - rule: function plugin(opts) { /* Plugin Code */ } -} -``` - -> Read more about writing [Stylint Plugins here](../developer-guide/plugins.md). Once the plugin is declared, within your `"rules"` object _you'll need to add options_ for the plugin's rule(s), just like any standard rule. Look at the plugin's documentation to know what the rule name should be. From 40a3c1a8648da8497d70b00d9f284ce735dbf7aa Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Thu, 1 Dec 2022 22:51:50 +0900 Subject: [PATCH 5/6] Add changelog entry --- .changeset/fast-cats-tan.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fast-cats-tan.md diff --git a/.changeset/fast-cats-tan.md b/.changeset/fast-cats-tan.md new file mode 100644 index 0000000000..f755e43bc5 --- /dev/null +++ b/.changeset/fast-cats-tan.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Added: support for plugin objects as config values From 7615c750b6ca66400b4f6ffd59dbf4e9be9bd369 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Thu, 1 Dec 2022 22:53:39 +0900 Subject: [PATCH 6/6] patch -> minor --- .changeset/fast-cats-tan.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fast-cats-tan.md b/.changeset/fast-cats-tan.md index f755e43bc5..958b1fff85 100644 --- a/.changeset/fast-cats-tan.md +++ b/.changeset/fast-cats-tan.md @@ -1,5 +1,5 @@ --- -"stylelint": patch +"stylelint": minor --- Added: support for plugin objects as config values