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

Fix function-calc-no-unspaced-operator performance #7505

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/heavy-toys-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `function-calc-no-unspaced-operator` performance
3 changes: 3 additions & 0 deletions lib/rules/function-calc-no-unspaced-operator/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const meta = {
const OPERATORS = new Set(['+', '-']);
const OPERATOR_REGEX = /[+-]/;
const ALL_OPERATORS = new Set([...OPERATORS, '*', '/']);
const CALC_FUNC_REGEX = /calc\(/i;

/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
Expand All @@ -53,6 +54,8 @@ const rule = (primary, _secondaryOptions, context) => {

if (!OPERATOR_REGEX.test(value)) return;

if (!CALC_FUNC_REGEX.test(value)) return;
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved

let needsFix = false;
const valueIndex = declarationValueIndex(decl);
const parsedValue = valueParser(value);
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/function-calc-no-unspaced-operator/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const meta = {
const OPERATORS = new Set(['+', '-']);
const OPERATOR_REGEX = /[+-]/;
const ALL_OPERATORS = new Set([...OPERATORS, '*', '/']);
const CALC_FUNC_REGEX = /calc\(/i;

/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
Expand All @@ -50,6 +51,8 @@ const rule = (primary, _secondaryOptions, context) => {

if (!OPERATOR_REGEX.test(value)) return;

if (!CALC_FUNC_REGEX.test(value)) return;

let needsFix = false;
const valueIndex = declarationValueIndex(decl);
const parsedValue = valueParser(value);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
},
"prettier": "@stylelint/prettier-config",
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 2023
},
"extends": [
"stylelint",
"stylelint/jest"
Expand Down
6 changes: 2 additions & 4 deletions scripts/benchmark-rule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const { bold, yellow } = picocolors;

const [, , ruleName, ruleOptions, ruleContext] = argv;

const ruleFunc = rules[ruleName];

function printHelp() {
const script = 'node benchmark-rule.mjs';

Expand All @@ -32,7 +30,7 @@ function printHelp() {
);
}

if (!ruleFunc || !ruleOptions) {
if (!ruleName || !(ruleName in rules) || !ruleOptions) {
printHelp();
exit(-1);
}
Expand Down Expand Up @@ -65,7 +63,7 @@ const ruleSettings = normalizeRuleSettings(parsedOptions);
const [primary, secondary] = ruleSettings;
const context = ruleContext ? JSON.parse(ruleContext) : {};

const rule = ruleFunc(primary, secondary, context);
const rule = (await rules[ruleName])(primary, secondary, context);

const processor = postcss().use(rule);

Expand Down