-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
stylelint-polaris/coverage
rule (#7551)
- Loading branch information
1 parent
7a6fb7c
commit d7dc443
Showing
4 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/stylelint-polaris': minor | ||
--- | ||
|
||
Add `stylelint-polaris/coverage` rule |
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,70 @@ | ||
const stylelint = require('stylelint'); | ||
|
||
const {isObject} = require('../../utils'); | ||
|
||
const ruleName = 'stylelint-polaris/coverage'; | ||
|
||
/** | ||
* @typedef {{ | ||
* [category: string]: import('stylelint').ConfigRules | ||
* }} PrimaryOptions | ||
*/ | ||
|
||
module.exports = stylelint.createPlugin( | ||
ruleName, | ||
/** @param {PrimaryOptions} primaryOptions */ | ||
(primaryOptions) => { | ||
const isPrimaryOptionsValid = validatePrimaryOptions(primaryOptions); | ||
|
||
const rules = !isPrimaryOptionsValid | ||
? [] | ||
: Object.entries(primaryOptions).flatMap( | ||
([categoryName, categoryConfigRules]) => | ||
Object.entries(categoryConfigRules).map( | ||
([categoryRuleName, categoryRuleSettings]) => ({ | ||
categoryRuleName, | ||
categoryRuleSettings, | ||
coverageRuleName: `${ruleName}/${categoryName}`, | ||
}), | ||
), | ||
); | ||
|
||
return (root, result) => { | ||
const validOptions = stylelint.utils.validateOptions(result, ruleName, { | ||
actual: isPrimaryOptionsValid, | ||
}); | ||
|
||
if (!validOptions) return; | ||
|
||
for (const rule of rules) { | ||
const {categoryRuleName, categoryRuleSettings, coverageRuleName} = rule; | ||
|
||
stylelint.utils.checkAgainstRule( | ||
{ | ||
ruleName: categoryRuleName, | ||
ruleSettings: categoryRuleSettings, | ||
root, | ||
}, | ||
(warning) => { | ||
stylelint.utils.report({ | ||
result, | ||
node: warning.node, | ||
ruleName: coverageRuleName, | ||
message: warning.text.replace(categoryRuleName, coverageRuleName), | ||
}); | ||
}, | ||
); | ||
} | ||
}; | ||
}, | ||
); | ||
|
||
function validatePrimaryOptions(primaryOptions) { | ||
if (!isObject(primaryOptions)) return false; | ||
|
||
for (const categoryConfigRules of Object.values(primaryOptions)) { | ||
if (!isObject(categoryConfigRules)) return false; | ||
} | ||
|
||
return true; | ||
} |
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,29 @@ | ||
const {ruleName} = require('.'); | ||
|
||
const config = { | ||
motion: { | ||
'at-rule-disallowed-list': [['keyframes'], {severity: 'warning'}], | ||
}, | ||
}; | ||
|
||
testRule({ | ||
ruleName, | ||
plugins: [__dirname], | ||
config, | ||
customSyntax: 'postcss-scss', | ||
accept: [ | ||
{ | ||
code: '@media (min-width: 320px) {}', | ||
description: 'Uses allowed at-rule', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: '@keyframes foo {}', | ||
description: 'Uses disallowed at-rule', | ||
message: | ||
'Unexpected at-rule "keyframes" (stylelint-polaris/coverage/motion)', | ||
}, | ||
], | ||
}); |
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