From 0bff714ce96b6e4ef769dd80ca5abd2268625258 Mon Sep 17 00:00:00 2001 From: Shub Date: Sat, 19 Nov 2022 11:11:39 +0530 Subject: [PATCH] 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[];