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 property-* rules #5833

Merged
merged 1 commit into from
Jan 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/rules/property-allowed-list/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const isCustomProperty = require('../../utils/isCustomProperty');
Expand All @@ -17,10 +15,11 @@ const messages = ruleMessages(ruleName, {
rejected: (property) => `Unexpected property "${property}"`,
});

function rule(list) {
/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
actual: primary,
possible: [isString, isRegExp],
});

Expand All @@ -39,7 +38,7 @@ function rule(list) {
return;
}

if (matchesStringOrRegExp(vendor.unprefixed(prop), list)) {
if (matchesStringOrRegExp(vendor.unprefixed(prop), primary)) {
return;
}

Expand All @@ -51,7 +50,7 @@ function rule(list) {
});
});
};
}
};

rule.primaryOptionArray = true;

Expand Down
11 changes: 5 additions & 6 deletions lib/rules/property-case/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const isCustomProperty = require('../../utils/isCustomProperty');
Expand All @@ -14,10 +12,11 @@ const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});

function rule(expectation, options, context) {
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['lower', 'upper'],
});

Expand All @@ -36,7 +35,7 @@ function rule(expectation, options, context) {
return;
}

const expectedProp = expectation === 'lower' ? prop.toLowerCase() : prop.toUpperCase();
const expectedProp = primary === 'lower' ? prop.toLowerCase() : prop.toUpperCase();

if (prop === expectedProp) {
return;
Expand All @@ -56,7 +55,7 @@ function rule(expectation, options, context) {
});
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/property-disallowed-list/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const isCustomProperty = require('../../utils/isCustomProperty');
Expand All @@ -17,10 +15,11 @@ const messages = ruleMessages(ruleName, {
rejected: (property) => `Unexpected property "${property}"`,
});

function rule(list) {
/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
actual: primary,
possible: [isString, isRegExp],
});

Expand All @@ -39,7 +38,7 @@ function rule(list) {
return;
}

if (!matchesStringOrRegExp(vendor.unprefixed(prop), list)) {
if (!matchesStringOrRegExp(vendor.unprefixed(prop), primary)) {
return;
}

Expand All @@ -51,7 +50,7 @@ function rule(list) {
});
});
};
}
};

rule.primaryOptionArray = true;

Expand Down
36 changes: 21 additions & 15 deletions lib/rules/property-no-unknown/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const isCustomProperty = require('../../utils/isCustomProperty');
Expand All @@ -9,6 +7,7 @@ const optionsMatches = require('../../utils/optionsMatches');
const properties = require('known-css-properties').all;
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const { isAtRule, isRule } = require('../../utils/typeGuards');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const { isBoolean, isRegExp, isString } = require('../../utils/validateTypes');
Expand All @@ -19,19 +18,20 @@ const messages = ruleMessages(ruleName, {
rejected: (property) => `Unexpected unknown property "${property}"`,
});

function rule(actual, options) {
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
const allValidProperties = new Set(properties);

return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{ actual: primary },
{
actual: options,
actual: secondaryOptions,
possible: {
ignoreProperties: [isString, isRegExp],
checkPrefixed: isBoolean,
checkPrefixed: [isBoolean],
ignoreSelectors: [isString, isRegExp],
ignoreAtRules: [isString, isRegExp],
},
Expand All @@ -43,10 +43,13 @@ function rule(actual, options) {
return;
}

const shouldCheckPrefixed = options && options.checkPrefixed;
const shouldCheckPrefixed = secondaryOptions && secondaryOptions.checkPrefixed;

root.walkDecls(checkStatement);

/**
* @param {import('postcss').Declaration} decl
*/
function checkStatement(decl) {
const prop = decl.prop;

Expand All @@ -66,22 +69,25 @@ function rule(actual, options) {
return;
}

if (optionsMatches(options, 'ignoreProperties', prop)) {
if (optionsMatches(secondaryOptions, 'ignoreProperties', prop)) {
return;
}

const { selector } = decl.parent;
const parent = decl.parent;

if (selector && optionsMatches(options, 'ignoreSelectors', selector)) {
if (
parent &&
isRule(parent) &&
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] Only Rule node has a selector property. If not using isRule() and just using the in operator (like 'selector' in parent, the TypeScript compiler cannot ensure that the parent variable has a selector property.

optionsMatches(secondaryOptions, 'ignoreSelectors', parent.selector)
) {
return;
}

let node = decl.parent;
/** @type {import('postcss').Node | undefined} */
let node = parent;

while (node && node.type !== 'root') {
const { type, name } = node;

if (type === 'atrule' && optionsMatches(options, 'ignoreAtRules', name)) {
if (isAtRule(node) && optionsMatches(secondaryOptions, 'ignoreAtRules', node.name)) {
return;
}

Expand All @@ -100,7 +106,7 @@ function rule(actual, options) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
13 changes: 6 additions & 7 deletions lib/rules/property-no-vendor-prefix/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const isAutoprefixable = require('../../utils/isAutoprefixable');
Expand All @@ -16,15 +14,16 @@ const messages = ruleMessages(ruleName, {
rejected: (property) => `Unexpected vendor-prefix "${property}"`,
});

function rule(actual, options, context) {
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{ actual: primary },
{
optional: true,
actual: options,
actual: secondaryOptions,
possible: {
ignoreProperties: [isString, isRegExp],
},
Expand All @@ -40,7 +39,7 @@ function rule(actual, options, context) {
const unprefixedProp = vendor.unprefixed(prop);

//return early if property is to be ignored
if (optionsMatches(options, 'ignoreProperties', unprefixedProp)) {
if (optionsMatches(secondaryOptions, 'ignoreProperties', unprefixedProp)) {
return;
}

Expand Down Expand Up @@ -69,7 +68,7 @@ function rule(actual, options, context) {
});
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down