Skip to content

Commit

Permalink
Refactor to improve types for hue-* rules (#5494)
Browse files Browse the repository at this point in the history
This change removes `// @ts-nocheck` from the `hue-*` rules.
  • Loading branch information
ybiquitous committed Aug 24, 2021
1 parent 1b564b2 commit 4969f65
Showing 1 changed file with 27 additions and 11 deletions.
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);

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

/**
* @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

0 comments on commit 4969f65

Please sign in to comment.