-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try adding a new linter rule to prevent leaky selectors
- Loading branch information
1 parent
5a9af29
commit c5cc0ec
Showing
2 changed files
with
45 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
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,42 @@ | ||
// eslint-disable @typescript-eslin/no-var-requires | ||
const stylelint = require('stylelint'); | ||
Check failure on line 2 in ember-osf-web-stylelint.js GitHub Actions / lint
|
||
|
||
/* | ||
* This rule prevents the use of bare element selectors in CSS | ||
* Bare element selectors are selectors that are not wrapped in or paired with a class or id | ||
*/ | ||
|
||
const ruleName = 'ember-osf-web-stylelint-plugin/no-unlocalized-selectors'; | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
expected: selector => `Rule "${selector}" should be wrapped in or paired with a local-class or ID`, | ||
}); | ||
|
||
module.exports = stylelint.createPlugin(ruleName, _ => | ||
(postcssRoot, postcssResult) => { | ||
postcssRoot.walkRules(rule => { | ||
const selector = rule.selector; | ||
const isChildRule = rule.parent.type === 'rule'; // top-level rules have rule.parent.type === 'root' | ||
const hasGlobal = selector.includes(':global'); | ||
if ( | ||
isChildRule || | ||
(!hasGlobal && (selector.includes('.') || selector.includes('#'))) // has a local-class or local-id | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
/^[a-z]+/.test(selector) || // starts with a letter | ||
/^:global\([a-z]+/.test(selector) // or starts with :global | ||
) { | ||
stylelint.utils.report({ | ||
ruleName, | ||
result: postcssResult, | ||
message: messages.expected(selector), | ||
node: rule, | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
module.exports.ruleName = ruleName; | ||
module.exports.messages = messages; | ||