Skip to content

Commit 90b6190

Browse files
authored
Add a deprecation warning for strict unary operations (#1800)
Closes #1721
1 parent db1e126 commit 90b6190

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## 1.55.0
22

3+
* Emit a deprecation warning for `$a -$b` and `$a +$b`, since these look like
4+
they could be unary operations but they're actually parsed as binary
5+
operations. Either explicitly write `$a - $b` or `$a (-$b)`. See
6+
https://sass-lang.com/d/strict-unary for more details.
7+
38
### Dart API
49

510
* Add an optional `argumentName` parameter to `SassScriptException()` to make it

lib/src/callable/built_in.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class BuiltInCallable implements Callable, AsyncBuiltInCallable {
7272
///
7373
/// If passed, [url] is the URL of the module in which the function is
7474
/// defined.
75-
BuiltInCallable.overloadedFunction(
76-
this.name, Map<String, Callback> overloads,
75+
BuiltInCallable.overloadedFunction(this.name, Map<String, Callback> overloads,
7776
{Object? url})
7877
: _overloads = [
7978
for (var entry in overloads.entries)

lib/src/parse/stylesheet.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,34 @@ abstract class StylesheetParser extends Parser {
17881788
} else {
17891789
singleExpression_ = BinaryOperationExpression(operator, left, right);
17901790
allowSlash = false;
1791+
1792+
if (operator == BinaryOperator.plus ||
1793+
operator == BinaryOperator.minus) {
1794+
if (scanner.string.substring(
1795+
right.span.start.offset - 1, right.span.start.offset) ==
1796+
operator.operator &&
1797+
isWhitespace(scanner.string.codeUnitAt(left.span.end.offset))) {
1798+
logger.warn(
1799+
"This operation is parsed as:\n"
1800+
"\n"
1801+
" $left ${operator.operator} $right\n"
1802+
"\n"
1803+
"but you may have intended it to mean:\n"
1804+
"\n"
1805+
" $left (${operator.operator}$right)\n"
1806+
"\n"
1807+
"Add a space after ${operator.operator} to clarify that it's "
1808+
"meant to be a binary operation, or wrap\n"
1809+
"it in parentheses to make it a unary operation. This will be "
1810+
"an error in future\n"
1811+
"versions of Sass.\n"
1812+
"\n"
1813+
"More info and automated migrator: "
1814+
"https://sass-lang.com/d/strict-unary",
1815+
span: singleExpression_!.span,
1816+
deprecation: true);
1817+
}
1818+
}
17911819
}
17921820
}
17931821

pkg/sass_api/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: sass_api
22
# Note: Every time we add a new Sass AST node, we need to bump the *major*
33
# version because it's a breaking change for anyone who's implementing the
44
# visitor interface(s).
5-
version: 3.1.0
5+
version: 3.1.0-dev
66
description: Additional APIs for Dart Sass.
77
homepage: https://github.com/sass/dart-sass
88

0 commit comments

Comments
 (0)