Skip to content

Commit

Permalink
Merge pull request #465 from airyboy/bug/operator-no-unspaced
Browse files Browse the repository at this point in the history
operator-no-unspaced: fix signed parameter (#450)
  • Loading branch information
kristerkari committed Apr 9, 2020
2 parents fd7ec93 + 386b834 commit cb64bed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/rules/operator-no-unspaced/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,51 @@ testRule(rule, {
]
});

// @function calls inside interpolation
testRule(rule, {
ruleName,
config: [undefined],
syntax: "scss",
skipBasicChecks: true,

accept: [
{
code: `
--my-var: #{scale-color(#fff, $lightness: -75%)};
`,
description:
"Function call in interpolation, negative unit value parameter: #{scale-color(#fff, $lightness: -75%)}",
},
{
code: `
--my-var: #{math.acos(-0.5)};
`,
description:
"Function call in interpolation, negative parameter: #{math.acos(-0.5)}",
},
{
code: `
--my-var: #{math.acos(0.7 - 0.5)};
`,
description:
"Function call in interpolation, expression parameter: #{math.acos(0.7 - 0.5)}",
},
],

reject: [
{
code: `
--my-var: #{math.acos(0.7-0.5)};
`,
description:
"Function call in interpolation, expression parameter: #{math.acos(0.7-0.5)}",
message: messages.expectedBefore("-"),
line: 2,
column: 32
},
]
});

// @import
testRule(rule, {
ruleName,
Expand Down
14 changes: 11 additions & 3 deletions src/utils/sassValueParser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ function checkMinus(string, index) {
const isParensBefore_ = isParensBefore(before);
// The early check above helps prevent deep recursion here
const isPrecedingOperator_ = isPrecedingOperator(string, index);
const isInsideFunctionCall_ = isInsideFunctionCall(string, index);


if (isAtStart_) {
// console.log("-, -<sth> or - <sth>")
Expand All @@ -347,8 +349,14 @@ function checkMinus(string, index) {
return "op";
}

// e.g. `#{10px -1}`
// e.g. `#{10px -1}`, `#{math.acos(-0.5)}`
if (isInsideInterpolation(string, index)) {
if (isInsideFunctionCall_.is && (
(isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && !isNumberAfter_.opsBetween))) {
return "sign";
}

return "op";
}

Expand Down Expand Up @@ -691,7 +699,7 @@ function isInsideFunctionCall(string, index) {
const result = { is: false, fn: null };
const before = string.substring(0, index).trim();
const after = string.substring(index + 1).trim();
const beforeMatch = before.match(/([a-zA-Z_-][a-zA-Z0-9_-]*)\([^(){},]+$/);
const beforeMatch = before.match(/([a-zA-Z_-][a-zA-Z0-9_-]*)\([^(){}]+$/);

if (beforeMatch && beforeMatch[0] && after.search(/^[^(,]+\)/) !== -1) {
result.is = true;
Expand Down Expand Up @@ -833,7 +841,7 @@ function isValueWithUnitAfter(after) {
// Again, ` d.10px` - .10px is separated from a sequence
// and is considered a value with a unit
const matches = after.match(
/^\s*([+/*%-]\s*)*(\d+(\.\d+){0,1}|\.\d+)[a-zA-Z_0-9-]+(?:$|[)}, ])/
/^\s*([+/*%-]\s*)*(\d+(\.\d+){0,1}|\.\d+)[a-zA-Z_0-9-%]+(?:$|[)}, ])/
);

if (matches) {
Expand Down

0 comments on commit cb64bed

Please sign in to comment.