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

Refactor to improve types for hue-* rules #5494

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 27 additions & 11 deletions lib/rules/hue-degree-notation/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const valueParser = require('postcss-value-parser');
Expand All @@ -22,7 +20,8 @@ const HUE_FIRST_ARG_FUNCS = ['hsl', 'hsla', 'hwb'];
const HUE_THIRD_ARG_FUNCS = ['lch'];
const HUE_FUNCS = new Set([...HUE_FIRST_ARG_FUNCS, ...HUE_THIRD_ARG_FUNCS]);

function rule(primary, secondary, context) {
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
Expand Down Expand Up @@ -78,18 +77,29 @@ function rule(primary, secondary, context) {
}
});
};
}
};

/**
* @param {string} value
*/
function asDegree(value) {
return `${value}deg`;
}

/**
* @param {string} value
*/
function asNumber(value) {
const { number } = valueParser.unit(value);
const dimension = valueParser.unit(value);
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] valueParser.unit() returns valueParser.Dimension or false. See below:

https://github.com/TrySound/postcss-value-parser/blob/v4.1.0/lib/index.d.ts#L134


if (dimension) return dimension.number;

return number;
throw new TypeError(`The "${value}" value must have a unit`);
}

/**
* @param {import('postcss-value-parser').FunctionNode} node
*/
function findHue(node) {
const args = node.nodes.filter(({ type }) => type === 'word' || type === 'function');
const value = node.value.toLowerCase();
Expand All @@ -102,19 +112,25 @@ function findHue(node) {
return args[2];
}

return false;
return undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] The findHue() function now returns valueParser.Node or undefined. I think it's a more common pattern to return undefined instead of false.

}

/**
* @param {string} value
*/
function isDegree(value) {
const { unit } = valueParser.unit(value);
const dimension = valueParser.unit(value);

return unit && unit.toLowerCase() === 'deg';
return dimension && dimension.unit.toLowerCase() === 'deg';
}

/**
* @param {string} value
*/
function isNumber(value) {
const { unit } = valueParser.unit(value);
const dimension = valueParser.unit(value);

return unit === '';
return dimension && dimension.unit === '';
}

rule.ruleName = ruleName;
Expand Down