Skip to content

Commit

Permalink
added stylelint linting and config
Browse files Browse the repository at this point in the history
  • Loading branch information
calebnance committed Nov 8, 2023
1 parent 6623276 commit 05c830a
Show file tree
Hide file tree
Showing 6 changed files with 1,096 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"parserOptions": {
"ecmaVersion": 2020
},
"ignorePatterns": ["src/data/*.js"],
"rules": {
"global-require": 0,
"prettier/prettier": ["error"],
"no-case-declarations": 0,
"no-param-reassign": 0,
"no-promise-executor-return": 0,
"no-restricted-globals": 0,
"react/forbid-prop-types": 0,
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"react/jsx-fragments": 0,
"react/jsx-no-constructed-context-values": 0,
"react/jsx-props-no-spreading": 0,
"react/no-danger": 0
},
Expand Down
1 change: 1 addition & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.js
11 changes: 11 additions & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["stylelint-config-standard-scss"],
"plugins": [
"./custom-rules/hex-lowercase.js",
"stylelint-order"
],
"rules": {
"custom-rules/hex-lowercase": true,
"order/properties-alphabetical-order": true
}
}
59 changes: 59 additions & 0 deletions custom-rules/hex-lowercase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable-next-line */
const stylelint = require('stylelint');

// pulled from
// https://medium.com/swlh/writing-your-first-custom-stylelint-rule-a9620bb2fb73
const { report, ruleMessages, validateOptions } = stylelint.utils;

const ruleName = 'custom-rules/hex-lowercase';

const messages = ruleMessages(ruleName, {
expected: (unfixed, fixed) => `Expected "${unfixed}" to be "${fixed}"`
});

module.exports = stylelint.createPlugin(
ruleName,
(primaryOption, secondaryOptionObject, context) =>
function lint(postcssRoot, postcssResult) {
const validOptions = validateOptions(postcssResult, ruleName, {
// no options for now...
});

if (!validOptions) {
return;
}

const isAutoFixing = Boolean(context.fix);
postcssRoot.walkDecls((decl) => {
const hasCapitalLetters =
/^#([A-F0-9]{6}|[A-F0-9]{3})$/.test(decl.value) &&
/[A-Z]/.test(decl.value);

if (!hasCapitalLetters) {
return;
}

const { value } = decl;
const newValue = value.toLowerCase();

if (isAutoFixing) {
if (decl.raws.value) {
decl.raws.value.raw = newValue;
} else {
decl.value = newValue;
}
} else {
report({
ruleName,
result: postcssResult,
message: messages.expected(value, newValue),
node: decl,
word: value
});
}
});
}
);

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

0 comments on commit 05c830a

Please sign in to comment.