Skip to content

Commit

Permalink
Fix false positives and memory leak for `function-calc-no-unspaced-op…
Browse files Browse the repository at this point in the history
…erator` (#6045)

Co-authored-by: Richard Hallows <jeddy3@users.noreply.github.com>
  • Loading branch information
ybiquitous and jeddy3 committed Apr 29, 2022
1 parent 0364db1 commit e747ad1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 152 deletions.
7 changes: 6 additions & 1 deletion lib/rules/function-calc-no-unspaced-operator/README.md
Expand Up @@ -9,7 +9,7 @@ a { top: calc(1px + 2px); }
* The space around this operator */
```

Before the operator, there must be a single whitespace or a newline plus indentation. After the operator, there must be a single whitespace or a newline.
This rule checks that there is a single whitespace or a newline plus indentation before the `+` or `-` operator, and a single whitespace or a newline after that operator.

The [`fix` option](../../../docs/user-guide/usage/options.md#fix) can automatically fix all of the problems reported by this rule.

Expand Down Expand Up @@ -41,6 +41,11 @@ a { top: calc(1px + 2px); }
a { top: calc(calc(1em * 2) / 3); }
```

<!-- prettier-ignore -->
```css
a { top: calc(calc(1em*2)/3); }
```

<!-- prettier-ignore -->
```css
a {
Expand Down
192 changes: 75 additions & 117 deletions lib/rules/function-calc-no-unspaced-operator/__tests__/index.js
Expand Up @@ -32,9 +32,27 @@ testRule({
{
code: 'a { top: calc(1px * 2); }',
},
{
code: 'a { top: calc(1px*2); }',
},
{
code: 'a { top: calc(1px *2); }',
},
{
code: 'a { top: calc(1px* 2); }',
},
{
code: 'a { top: calc(1px / 2); }',
},
{
code: 'a { top: calc(1px/2); }',
},
{
code: 'a { top: calc(1px /2); }',
},
{
code: 'a { top: calc(1px/ 2); }',
},
{
code: 'a { top: calc(1px * -0.2); }',
},
Expand Down Expand Up @@ -153,6 +171,36 @@ testRule({
code: 'margin-top: calc(var(--some-variable)\r\n\t+ var(--some-other-variable));',
description: 'CRLF newline and tab before operator',
},
{
code: 'a { padding: 10px calc(calc(1px + 2px)* 3px); }',
},
{
code: 'a { padding: 10px calc(calc(1px* 2px) + 3px); }',
},
{
code: 'a { padding: 10px calc(1px /2); }',
},
{
code: 'a { padding: 10px calc(1px/ 2); }',
},
{
code: 'a { padding: 10px calc(1px *2); }',
},
{
code: 'a { padding: 10px calc(1px* 2); }',
},
{
code: 'a { top: calc(calc(1px + 2px)* 3px); }',
},
{
code: 'a { top: calc(calc(1px* 2px) + 3px); }',
},
{
code: 'a { top: calc(10px*var(--foo)); }',
},
{
code: 'a { top: calc(10px/var(--foo)); }',
},
],

reject: [
Expand Down Expand Up @@ -257,60 +305,6 @@ testRule({
endLine: 1,
endColumn: 19,
},
{
code: 'a { top: calc(1px* 2); }',
fixed: 'a { top: calc(1px * 2); }',
message: messages.expectedBefore('*'),
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
{
code: 'a { top: calc(1px *2); }',
fixed: 'a { top: calc(1px * 2); }',
message: messages.expectedAfter('*'),
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
code: 'a { top: calc(1px/ 2); }',
fixed: 'a { top: calc(1px / 2); }',
message: messages.expectedBefore('/'),
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
{
code: 'a { top: calc(1px /2); }',
fixed: 'a { top: calc(1px / 2); }',
message: messages.expectedAfter('/'),
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
code: 'a { top: calc(calc(1px* 2px) + 3px); }',
fixed: 'a { top: calc(calc(1px * 2px) + 3px); }',
message: messages.expectedBefore('*'),
line: 1,
column: 23,
endLine: 1,
endColumn: 24,
},
{
code: 'a { top: calc(calc(1px + 2px)* 3px); }',
fixed: 'a { top: calc(calc(1px + 2px) * 3px); }',
message: messages.expectedBefore('*'),
line: 1,
column: 30,
endLine: 1,
endColumn: 31,
},
{
code: 'a { top: calc(1px +2px); }',
fixed: 'a { top: calc(1px + 2px); }',
Expand Down Expand Up @@ -403,60 +397,6 @@ testRule({
endLine: 1,
endColumn: 28,
},
{
code: 'a { padding: 10px calc(1px* 2); }',
fixed: 'a { padding: 10px calc(1px * 2); }',
message: messages.expectedBefore('*'),
line: 1,
column: 27,
endLine: 1,
endColumn: 28,
},
{
code: 'a { padding: 10px calc(1px *2); }',
fixed: 'a { padding: 10px calc(1px * 2); }',
message: messages.expectedAfter('*'),
line: 1,
column: 28,
endLine: 1,
endColumn: 29,
},
{
code: 'a { padding: 10px calc(1px/ 2); }',
fixed: 'a { padding: 10px calc(1px / 2); }',
message: messages.expectedBefore('/'),
line: 1,
column: 27,
endLine: 1,
endColumn: 28,
},
{
code: 'a { padding: 10px calc(1px /2); }',
fixed: 'a { padding: 10px calc(1px / 2); }',
message: messages.expectedAfter('/'),
line: 1,
column: 28,
endLine: 1,
endColumn: 29,
},
{
code: 'a { padding: 10px calc(calc(1px* 2px) + 3px); }',
fixed: 'a { padding: 10px calc(calc(1px * 2px) + 3px); }',
message: messages.expectedBefore('*'),
line: 1,
column: 32,
endLine: 1,
endColumn: 33,
},
{
code: 'a { padding: 10px calc(calc(1px + 2px)* 3px); }',
fixed: 'a { padding: 10px calc(calc(1px + 2px) * 3px); }',
message: messages.expectedBefore('*'),
line: 1,
column: 39,
endLine: 1,
endColumn: 40,
},
{
code: 'a { padding: 10px calc(1px +2px); }',
fixed: 'a { padding: 10px calc(1px + 2px); }',
Expand Down Expand Up @@ -564,6 +504,33 @@ testRule({
customSyntax: 'postcss-scss',
fix: true,

accept: [
{
code: 'a { top: calc(100%*#{$foo}); }',
},
{
code: 'a { top: calc(100% *#{$foo}); }',
},
{
code: 'a { top: calc(100%* #{$foo}); }',
},
{
code: 'a { top: calc(100% * #{$foo}); }',
},
{
code: 'a { top: calc(100%/#{$foo}); }',
},
{
code: 'a { top: calc(100% /#{$foo}); }',
},
{
code: 'a { top: calc(100%/ #{$foo}); }',
},
{
code: 'a { top: calc(100% / #{$foo}); }',
},
],

reject: [
{
code: 'a { top: calc(100%- #{$foo}); }',
Expand All @@ -574,15 +541,6 @@ testRule({
endLine: 1,
endColumn: 20,
},
{
code: 'a { top: calc(100% *#{$foo}); }',
fixed: 'a { top: calc(100% * #{$foo}); }',
message: messages.expectedAfter('*'),
line: 1,
column: 20,
endLine: 1,
endColumn: 21,
},
{
code: 'a { top: calc(100% -#{$foo}); }',
fixed: 'a { top: calc(100% - #{$foo}); }',
Expand Down

0 comments on commit e747ad1

Please sign in to comment.