From b4139ddfe60a5edeb62ec82cec32e9eb7ef45531 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Sun, 1 May 2022 17:02:19 +0900 Subject: [PATCH] Fix false positives for non-standard variables in `function-calc-no-unspaced-operator` (#6053) --- .../__tests__/index.js | 24 +++++++++++++++++++ .../index.js | 3 +++ 2 files changed, 27 insertions(+) diff --git a/lib/rules/function-calc-no-unspaced-operator/__tests__/index.js b/lib/rules/function-calc-no-unspaced-operator/__tests__/index.js index f4bc6f9ebf..412710f879 100644 --- a/lib/rules/function-calc-no-unspaced-operator/__tests__/index.js +++ b/lib/rules/function-calc-no-unspaced-operator/__tests__/index.js @@ -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', @@ -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', diff --git a/lib/rules/function-calc-no-unspaced-operator/index.js b/lib/rules/function-calc-no-unspaced-operator/index.js index 0c4846bcc3..c15e450232 100644 --- a/lib/rules/function-calc-no-unspaced-operator/index.js +++ b/lib/rules/function-calc-no-unspaced-operator/index.js @@ -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'); @@ -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); const operator = firstNode.value.slice(operatorIndex, operatorIndex + 1);