Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa29461
commit d0ee0bc
Showing
9 changed files
with
249 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
const stylelint = require('stylelint'); | ||
|
||
const ruleName = 'plugin/foo'; | ||
|
||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
rejected: (selector) => `No "${selector}" selector`, | ||
}); | ||
|
||
/** @type {(value: unknown) => boolean} */ | ||
const isString = (value) => typeof value === 'string'; | ||
|
||
/** @type {import('stylelint').Rule} */ | ||
const ruleFunction = (primary) => { | ||
return (root, result) => { | ||
const validOptions = stylelint.utils.validateOptions(result, ruleName, { | ||
actual: primary, | ||
possible: [isString], | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkRules((rule) => { | ||
const { selector } = rule; | ||
|
||
if (primary !== selector) { | ||
stylelint.utils.report({ | ||
result, | ||
ruleName, | ||
message: messages.rejected(selector), | ||
node: rule, | ||
}); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
ruleFunction.ruleName = ruleName; | ||
ruleFunction.messages = messages; | ||
|
||
module.exports = stylelint.createPlugin(ruleName, ruleFunction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const getTestRule = require('../getTestRule.js'); | ||
|
||
const testRule = getTestRule(); | ||
|
||
testRule({ | ||
plugins: [require.resolve('./fixtures/plugin-foo.js')], | ||
ruleName: 'plugin/foo', | ||
config: ['.a'], | ||
|
||
accept: [ | ||
{ | ||
code: '.a {}', | ||
}, | ||
{ | ||
code: '.a {}', | ||
description: 'with description', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: '#a {}', | ||
message: 'No "#a" selector (plugin/foo)', | ||
}, | ||
{ | ||
code: '#a {}', | ||
message: 'No "#a" selector (plugin/foo)', | ||
description: 'with description', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const getTestRuleConfigs = require('../getTestRuleConfigs.js'); | ||
|
||
const testRuleConfigs = getTestRuleConfigs(); | ||
|
||
testRuleConfigs({ | ||
plugins: [require.resolve('./fixtures/plugin-foo.js')], | ||
ruleName: 'plugin/foo', | ||
|
||
accept: [ | ||
{ | ||
config: 'a', | ||
}, | ||
{ | ||
config: ['b'], | ||
description: 'string is allowed', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
config: 123, | ||
}, | ||
{ | ||
config: [/foo/], | ||
description: 'regex is not allowed', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
const { inspect } = require('util'); | ||
|
||
/** @type {import('.').getTestRuleConfigs} */ | ||
module.exports = function getTestRuleConfigs(options = {}) { | ||
return function testRuleConfigs({ | ||
ruleName, | ||
accept = [], | ||
reject = [], | ||
only = false, | ||
skip = false, | ||
plugins = options.plugins, | ||
}) { | ||
if (accept.length === 0 && reject.length === 0) { | ||
throw new TypeError('The either "accept" or "reject" property must not be empty'); | ||
} | ||
|
||
/** @type {import('stylelint').lint} */ | ||
let lint; | ||
|
||
beforeAll(() => { | ||
// eslint-disable-next-line n/no-unpublished-require | ||
lint = require('stylelint').lint; | ||
}); | ||
|
||
const testGroup = only ? describe.only : skip ? describe.skip : describe; | ||
|
||
testGroup(`${ruleName} configs`, () => { | ||
/** | ||
* @param {import('.').ConfigCase} case | ||
* @param {(warnings: unknown[]) => void} comparison | ||
*/ | ||
function testConfig({ config, description, only: onlyTest, skip: skipTest }, comparison) { | ||
const testFn = onlyTest ? test.only : skipTest ? test.skip : test; | ||
|
||
testFn(`${description || inspect(config)}`, async () => { | ||
const lintConfig = { | ||
plugins, | ||
rules: { [ruleName]: config }, | ||
}; | ||
const { results } = await lint({ code: '', config: lintConfig }); | ||
|
||
expect(results).toHaveLength(1); | ||
comparison(results[0].invalidOptionWarnings); | ||
}); | ||
} | ||
|
||
describe('accept', () => { | ||
accept.forEach((c) => { | ||
testConfig(c, (warnings) => { | ||
// eslint-disable-next-line jest/no-standalone-expect | ||
expect(warnings).toEqual([]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('reject', () => { | ||
reject.forEach((c) => { | ||
testConfig(c, (warnings) => { | ||
// eslint-disable-next-line jest/no-standalone-expect | ||
expect(warnings).toEqual([{ text: expect.stringMatching(`"${ruleName}"`) }]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
'use strict'; | ||
|
||
const getTestRule = require('./getTestRule'); | ||
const getTestRule = require('./getTestRule.js'); | ||
const getTestRuleConfigs = require('./getTestRuleConfigs.js'); | ||
|
||
global.testRule = getTestRule(); | ||
global.testRuleConfigs = getTestRuleConfigs(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters