Skip to content
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

[Stylelint] Add typing to Stylelint rules #4392

Merged
merged 5 commits into from
Jun 28, 2023
Merged
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 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) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technical note - these types are inferred because we added the stylelint.Rule type

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;
};