Skip to content

Commit

Permalink
[Stylelint] Add typing to Stylelint rules (#4392) (#4447)
Browse files Browse the repository at this point in the history
* Add typing to Stylelint rules
* Extract get color property parent into function
* Optchain instead of unwrapping source file

---------



(cherry picked from commit d95f642)

Signed-off-by: Matt Provost <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Josh Romero <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2023
1 parent bc6a961 commit 1c42460
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/osd-stylelint-plugin-stylelint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"devOnly": true
},
"peerDependencies": {
"postcss": "^8.4.12",
"stylelint": "^14.5.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getUntrackedMessage,
getNotCompliantMessage,
getRulesFromConfig,
isColorProperty,
getColorPropertyParent,
isValidOptions,
ValueBasedConfig,
} from '../../utils';
Expand All @@ -32,12 +32,12 @@ const messages = ruleMessages(ruleName, {
expected: (message) => `${message}`,
});

const ruleFunction = (
const ruleFunction: stylelint.Rule = (
primaryOption: Record<string, any>,
secondaryOptionObject: Record<string, any>,
context
) => {
return (postcssRoot: any, postcssResult: any) => {
return (postcssRoot, postcssResult) => {
const validOptions = isValidOptions(postcssResult, ruleName, primaryOption);
if (!validOptions) {
return;
Expand All @@ -47,15 +47,16 @@ const ruleFunction = (

const isAutoFixing = Boolean(context.fix);

postcssRoot.walkDecls((decl: any) => {
if (!isColorProperty(decl.prop)) {
postcssRoot.walkDecls((decl) => {
const parent = getColorPropertyParent(decl);
if (!parent) {
return;
}

let shouldReport = false;

const nodeInfo = {
selector: decl.parent.selector,
selector: parent.selector,
prop: decl.prop,
value: decl.value,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const messages = ruleMessages(ruleName, {
expected: (message) => `${message}`,
});

const ruleFunction = (
const ruleFunction: stylelint.Rule = (
primaryOption: Record<string, any>,
secondaryOptionObject: Record<string, any>,
context
) => {
return (postcssRoot: any, postcssResult: any) => {
return (postcssRoot, postcssResult) => {
const validOptions = isValidOptions(postcssResult, ruleName, primaryOption);
if (!validOptions) {
return;
Expand All @@ -41,15 +41,19 @@ const ruleFunction = (

const isAutoFixing = Boolean(context.fix);

postcssRoot.walkRules((rule: any) => {
postcssRoot.walkRules((rule) => {
const selectorRule = getRuleFromConfig(rules, rule.selector);
if (!selectorRule) {
return;
}

let shouldReport = false;

const file = postcssRoot.source.input.file;
const file = postcssRoot.source?.input.file;
if (!file) {
return;
}

const approvedFiles = selectorRule.approved;

const reportInfo = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* GitHub history for details.
*/

import { Rule, Declaration } from 'postcss';

const COLOR_PROPERTIES = [
'all',
'animation',
Expand Down Expand Up @@ -58,3 +60,11 @@ const COLOR_PROPERTIES = [
export const isColorProperty = (prop: string) => {
return COLOR_PROPERTIES.includes(prop);
};

export const getColorPropertyParent = (decl: Declaration) => {
if (!isColorProperty(decl.prop)) {
return undefined;
}

return decl.parent as Rule;
};

0 comments on commit 1c42460

Please sign in to comment.