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 for non-standard variables in function-calc-no-unspaced-operator #6053

Merged
merged 1 commit into from May 1, 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
24 changes: 24 additions & 0 deletions lib/rules/function-calc-no-unspaced-operator/__tests__/index.js
Expand Up @@ -115,6 +115,18 @@ testRule({
code: 'a { top: calc(-@x - 2rem); }',
description: 'Less variable syntax',
},
{
code: 'a { top: calc(@x-y * 2); }',
description: 'Less variable syntax with hyphens',
},
{
code: 'a { top: calc(-@x-y * 2); }',
description: 'Less variable syntax with hyphens and a unary operator',
},
{
code: 'a { top: calc(100% * @x-y); }',
description: 'Less variable syntax with hyphens at the end',
},
{
code: 'a { top: calc($x-y-z - 2rem); }',
description: 'postcss-simple-vars and SCSS variable with hyphens',
Expand All @@ -127,6 +139,18 @@ testRule({
code: 'a { top: calc(100% - #{$foo}); }',
description: 'Scss interpolation',
},
{
code: 'a { top: calc(#{$foo-bar} * 5); }',
description: 'Scss interpolation with hyphens',
},
{
code: 'a { top: calc(-#{$foo-bar} * 5); }',
description: 'Scss interpolation with hyphens and a unary operator',
},
{
code: 'a { top: calc(100% * #{$foo-bar}); }',
description: 'Scss interpolation with hyphens at the end',
},
{
code: 'a { top: calc(100% - #{map-get($container-max-widths, xl)}); }',
description: 'Scss interpolation with function',
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/function-calc-no-unspaced-operator/index.js
Expand Up @@ -4,6 +4,7 @@ const valueParser = require('postcss-value-parser');

const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
Expand Down Expand Up @@ -166,6 +167,8 @@ const rule = (primary, _secondaryOptions, context) => {

if (firstNode.type !== 'word') return false;

if (!isStandardSyntaxValue(firstNode.value)) return false;

const operatorIndex = firstNode.value.search(OPERATOR_REGEX);
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] If the isStandardSyntaxValue() check is not performed, firstNode.value like #{$foo-bar} matches /[+-]/ (OPERATOR_REGEX):

const operator = firstNode.value.slice(operatorIndex, operatorIndex + 1);

Expand Down