Skip to content

Implement Stylelint for CSS/SCSS linting and confirm ESLint for JS #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = {
'default-case': 'off',
'global-require': 'off',
'implicit-arrow-linebreak': 'off',
camelcase: ['error', { allow: ['pfV5', 'pfM'] }],
'import/no-extraneous-dependencies': [
'error',
{
Expand Down
6 changes: 6 additions & 0 deletions .stylelintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: ['./stylelint-pf-prefix-rule.js'],
rules: {
'custom/pf-prefix': true,
},
};
61 changes: 61 additions & 0 deletions stylelint-pf-prefix-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const stylelint = require('stylelint');

const ruleName = 'custom/pf-prefix';
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: (name) =>
`Expected "${name}" to be prefixed with "pf-v5-" or "pf-m-" if it starts with "pf-" or ".pf-".`,
});

module.exports = stylelint.createPlugin(ruleName, () => {
return (root, result) => {
// Regular expression to match variables, class names, mixins, etc., that start with pf- or .pf-
const pfRegex = /(\.pf-|pf-)(?!v5-|m-)/;

root.walkDecls((decl) => {
const value = decl.value;
const prop = decl.prop;

// Check the value and property of the declaration
if (pfRegex.test(value) || pfRegex.test(prop)) {
stylelint.utils.report({
message: messages.expected(prop),
node: decl,
result,
ruleName,
});
}
});

root.walkRules((rule) => {
const selector = rule.selector;

// Check the selector (e.g., class names, IDs)
if (pfRegex.test(selector)) {
stylelint.utils.report({
message: messages.expected(selector),
node: rule,
result,
ruleName,
});
}
});

root.walkAtRules((atRule) => {
const name = atRule.name;
const params = atRule.params;

// Check the name and parameters of at-rules (e.g., mixins, functions)
if (pfRegex.test(name) || pfRegex.test(params)) {
stylelint.utils.report({
message: messages.expected(name),
node: atRule,
result,
ruleName,
});
}
});
};
});

module.exports.ruleName = ruleName;
module.exports.messages = messages;
Loading