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 false positives and memory leak for function-calc-no-unspaced-operator #6045

Merged
merged 2 commits into from Apr 29, 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
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.

ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
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({
{
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] Testing --fix has been done.

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)); }',
},
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] The case raised a memory leak. See #6044 (comment)

],

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