Skip to content

Commit fceb900

Browse files
authored
Add parens around negated variable when namespaced (#115)
Fixes #114. In fixing this, I also found a fixed a bug when migrating removed color functions, as a patch to namespace the argument would be applied before the patch adding the new argument name. This was fixed by ensuring the arguments of a function expression are visited after the function itself is patched.
1 parent 08fb490 commit fceb900

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
only be considered configured when the configuring declaration is upstream of
99
the `!default` declaration.
1010

11+
* When namespacing a negated variable, adds parentheses around it to prevent the
12+
`-` from being parsed as part of the namespace.
13+
14+
* Fix a bug in the migrating of removed color functions when the amount is a
15+
variable being namespaced.
16+
1117
## 1.0.0
1218

1319
* Initial release.

lib/src/migrators/module.dart

+17-4
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,10 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
522522
/// Adds a namespace to any function call that requires it.
523523
@override
524524
void visitFunctionExpression(FunctionExpression node) {
525-
super.visitFunctionExpression(node);
526-
if (node.namespace != null) return;
525+
if (node.namespace != null) {
526+
super.visitFunctionExpression(node);
527+
return;
528+
}
527529
if (references.sources.containsKey(node)) {
528530
var declaration = references.functions[node];
529531
_unreferencable.check(declaration, node);
@@ -561,6 +563,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
561563
', \$module: "$namespace"'));
562564
}, getFunctionCall: true);
563565
}
566+
super.visitFunctionExpression(node);
564567
}
565568

566569
/// Calls [patchNamespace] when the function [node] requires a namespace.
@@ -635,8 +638,13 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
635638
void _patchRemovedColorFunction(String name, Expression arg,
636639
{FileSpan existingArgName}) {
637640
var parameter = removedColorFunctions[name];
638-
var needsParens =
639-
parameter.endsWith('-') && arg is BinaryOperationExpression;
641+
// Surround the argument in parens if negated to avoid `-` being parsed
642+
// as part of the namespace.
643+
var needsParens = parameter.endsWith('-') &&
644+
(arg is BinaryOperationExpression ||
645+
arg is FunctionExpression ||
646+
(arg is VariableExpression &&
647+
references.variables[arg]?.sourceUrl != currentUrl));
640648
var leftParen = needsParens ? '(' : '';
641649
if (existingArgName == null) {
642650
addPatch(patchBefore(arg, '$parameter$leftParen'));
@@ -927,7 +935,12 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
927935
_renameReference(nameSpan(node), declaration);
928936
var namespace = _namespaceForDeclaration(declaration);
929937
if (namespace != null) {
938+
// Surround the variable in parens if negated to avoid `-` being parsed
939+
// as part of the namespace.
940+
var negated = matchesBeforeSpan(node.span, '-');
941+
if (negated) addPatch(patchBefore(node, '('));
930942
addPatch(patchBefore(node, '$namespace.'));
943+
if (negated) addPatch(patchAfter(node, ')'));
931944
}
932945
}
933946

lib/src/utils.dart

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ FileSpan extendBackward(FileSpan span, String text) {
8585
return span.file.span(start - text.length, span.end.offset);
8686
}
8787

88+
/// Returns true if [span] is preceded by exactly [text].
89+
bool matchesBeforeSpan(FileSpan span, String text) {
90+
var start = span.start.offset;
91+
if (start - text.length < 0) return false;
92+
return span.file.getText(start - text.length, start) == text;
93+
}
94+
8895
/// Returns whether [character] is whitespace, according to Sass's definition.
8996
bool isWhitespace(int character) =>
9097
character == $space ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<==> arguments
2+
--migrate-deps
3+
4+
<==> input/entrypoint.scss
5+
@import "library";
6+
a {
7+
b: -$amount;
8+
color: darken($color, $amount);
9+
background: darken($color, fn());
10+
}
11+
12+
<==> input/_library.scss
13+
$color: blue;
14+
$amount: 10%;
15+
16+
<==> output/entrypoint.scss
17+
@use "sass:color";
18+
@use "library";
19+
a {
20+
b: -(library.$amount);
21+
color: color.adjust(library.$color, $lightness: -(library.$amount));
22+
background: color.adjust(library.$color, $lightness: -(fn()));
23+
}

0 commit comments

Comments
 (0)