Skip to content

Commit

Permalink
Add a deprecation warning for strict unary operations
Browse files Browse the repository at this point in the history
Closes #1721
  • Loading branch information
nex3 committed Sep 13, 2022
1 parent e2f9705 commit a56085b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## 1.54.10

* 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.

## 1.54.9

* Fix an incorrect span in certain `@media` query deprecation warnings.
Expand Down
3 changes: 1 addition & 2 deletions lib/src/callable/built_in.dart
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
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
4 changes: 4 additions & 0 deletions pkg/sass_api/CHANGELOG.md
@@ -1,3 +1,7 @@
## 3.0.5

* No user-visible changes.

## 3.0.4

* `UnaryOperationExpression`s with operator `not` now include a correct span,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Expand Up @@ -2,15 +2,15 @@ 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.0.4
version: 3.0.5-dev
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: ">=2.17.0 <3.0.0"

dependencies:
sass: 1.54.9
sass: 1.54.10

dev_dependencies:
dartdoc: ^5.0.0
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: sass
version: 1.54.9
version: 1.54.10-dev
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit a56085b

Please sign in to comment.