Skip to content

Commit

Permalink
Add a deprecation warning for strict unary operations (#1800)
Browse files Browse the repository at this point in the history
Closes #1721
  • Loading branch information
nex3 authored Sep 15, 2022
1 parent db1e126 commit 90b6190
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 1.55.0

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

### Dart API

* Add an optional `argumentName` parameter to `SassScriptException()` to make it
Expand Down
3 changes: 1 addition & 2 deletions lib/src/callable/built_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class BuiltInCallable implements Callable, AsyncBuiltInCallable {
///
/// If passed, [url] is the URL of the module in which the function is
/// defined.
BuiltInCallable.overloadedFunction(
this.name, Map<String, Callback> overloads,
BuiltInCallable.overloadedFunction(this.name, Map<String, Callback> overloads,
{Object? url})
: _overloads = [
for (var entry in overloads.entries)
Expand Down
28 changes: 28 additions & 0 deletions lib/src/parse/stylesheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,34 @@ abstract class StylesheetParser extends Parser {
} else {
singleExpression_ = BinaryOperationExpression(operator, left, right);
allowSlash = false;

if (operator == BinaryOperator.plus ||
operator == BinaryOperator.minus) {
if (scanner.string.substring(
right.span.start.offset - 1, right.span.start.offset) ==
operator.operator &&
isWhitespace(scanner.string.codeUnitAt(left.span.end.offset))) {
logger.warn(
"This operation is parsed as:\n"
"\n"
" $left ${operator.operator} $right\n"
"\n"
"but you may have intended it to mean:\n"
"\n"
" $left (${operator.operator}$right)\n"
"\n"
"Add a space after ${operator.operator} to clarify that it's "
"meant to be a binary operation, or wrap\n"
"it in parentheses to make it a unary operation. This will be "
"an error in future\n"
"versions of Sass.\n"
"\n"
"More info and automated migrator: "
"https://sass-lang.com/d/strict-unary",
span: singleExpression_!.span,
deprecation: true);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sass_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 3.1.0
version: 3.1.0-dev
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit 90b6190

Please sign in to comment.