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

operator-no-unspaced: fix signed parameter (#450) #465

Merged
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
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