Skip to content

Commit

Permalink
Refactor to improve types for media-feature-colon-* rules (#5509)
Browse files Browse the repository at this point in the history
This change removes `// @ts-nocheck` from the `media-feature-colon-*` rules.
  • Loading branch information
ybiquitous committed Sep 2, 2021
1 parent 0c5c4f5 commit e5034d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
16 changes: 8 additions & 8 deletions lib/rules/media-feature-colon-space-after/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const atRuleParamIndex = require('../../utils/atRuleParamIndex');
Expand All @@ -15,19 +13,21 @@ const messages = ruleMessages(ruleName, {
rejectedAfter: () => 'Unexpected whitespace after ":"',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('space', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('space', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'never'],
});

if (!validOptions) {
return;
}

/** @type {Map<import('postcss').AtRule, number[]> | undefined} */
let fixData;

mediaFeatureColonSpaceChecker({
Expand Down Expand Up @@ -60,9 +60,9 @@ function rule(expectation, options, context) {
const beforeColon = params.slice(0, index + 1);
const afterColon = params.slice(index + 1);

if (expectation === 'always') {
if (primary === 'always') {
params = beforeColon + afterColon.replace(/^\s*/, ' ');
} else if (expectation === 'never') {
} else if (primary === 'never') {
params = beforeColon + afterColon.replace(/^\s*/, '');
}
});
Expand All @@ -75,7 +75,7 @@ function rule(expectation, options, context) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
16 changes: 8 additions & 8 deletions lib/rules/media-feature-colon-space-before/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const atRuleParamIndex = require('../../utils/atRuleParamIndex');
Expand All @@ -15,19 +13,21 @@ const messages = ruleMessages(ruleName, {
rejectedBefore: () => 'Unexpected whitespace before ":"',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('space', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('space', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'never'],
});

if (!validOptions) {
return;
}

/** @type {Map<import('postcss').AtRule, number[]> | undefined} */
let fixData;

mediaFeatureColonSpaceChecker({
Expand Down Expand Up @@ -60,9 +60,9 @@ function rule(expectation, options, context) {
const beforeColon = params.slice(0, index);
const afterColon = params.slice(index);

if (expectation === 'always') {
if (primary === 'always') {
params = beforeColon.replace(/\s*$/, ' ') + afterColon;
} else if (expectation === 'never') {
} else if (primary === 'never') {
params = beforeColon.replace(/\s*$/, '') + afterColon;
}
});
Expand All @@ -75,7 +75,7 @@ function rule(expectation, options, context) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
22 changes: 17 additions & 5 deletions lib/rules/mediaFeatureColonSpaceChecker.js
@@ -1,12 +1,19 @@
// @ts-nocheck

'use strict';

const atRuleParamIndex = require('../utils/atRuleParamIndex');
const report = require('../utils/report');
const styleSearch = require('style-search');

module.exports = function (opts) {
/**
* @param {{
* root: import('postcss').Root,
* locationChecker: (args: { source: string, index: number, err: (message: string) => void }) => void,
* fix: ((node: import('postcss').AtRule, index: number) => boolean) | null,
* result: import('stylelint').PostcssResult,
* checkedRuleName: string,
* }} opts
*/
module.exports = function mediaFeatureColonSpaceChecker(opts) {
opts.root.walkAtRules(/^media$/i, (atRule) => {
const params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;

Expand All @@ -15,19 +22,24 @@ module.exports = function (opts) {
});
});

/**
* @param {string} source
* @param {number} index
* @param {import('postcss').AtRule} node
*/
function checkColon(source, index, node) {
opts.locationChecker({
source,
index,
err: (m) => {
err: (message) => {
const colonIndex = index + atRuleParamIndex(node);

if (opts.fix && opts.fix(node, colonIndex)) {
return;
}

report({
message: m,
message,
node,
index: colonIndex,
result: opts.result,
Expand Down

0 comments on commit e5034d2

Please sign in to comment.