Skip to content

Commit

Permalink
Fix function-calc-no-unspaced-operator performance (#7505)
Browse files Browse the repository at this point in the history
How to measure:

```shell
npm run -s benchmark-rule -- function-calc-no-unspaced-operator true
```

Before:

```
Warnings: 0
Mean: 125.72145828571429 ms
Deviation: 14.960783775263804 ms
```

After:

```
Warnings: 0
Mean: 110.21394642857142 ms
Deviation: 10.1318139617483 ms
```
  • Loading branch information
ybiquitous committed Jan 30, 2024
1 parent 398d3a8 commit 5e2eff7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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;

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

0 comments on commit 5e2eff7

Please sign in to comment.