Skip to content

Commit

Permalink
Preserve parentheses around var() functions in calculations (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 authored Sep 16, 2021
1 parent 30cc9dc commit 52ef3c6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.41.1

* Preserve parentheses around `var()` functions in calculations, because they
could potentially be replaced with sub-expressions that might need to be
parenthesized.

## 1.41.0

* Calculation values can now be combined with strings using the `+` operator.
Expand Down
9 changes: 8 additions & 1 deletion lib/src/visitor/async_evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,14 @@ class _EvaluateVisitor
/// Evaluates [node] as a component of a calculation.
Future<Object> _visitCalculationValue(Expression node) async {
if (node is ParenthesizedExpression) {
return await _visitCalculationValue(node.expression);
var inner = node.expression;
var result = await _visitCalculationValue(inner);
return inner is FunctionExpression &&
inner.name.toLowerCase() == 'var' &&
result is SassString &&
!result.hasQuotes
? SassString('(${result.text})', quotes: false)
: result;
} else if (node is StringExpression) {
assert(!node.hasQuotes);
return CalculationInterpolation(await _performInterpolation(node.text));
Expand Down
11 changes: 9 additions & 2 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: 3bcae71014e2ef5626aae9e5e0a5b49c499a226f
// Checksum: 58efc9d3f1a86c811ca30cfd7dcbeb01a6945d89
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -2279,7 +2279,14 @@ class _EvaluateVisitor
/// Evaluates [node] as a component of a calculation.
Object _visitCalculationValue(Expression node) {
if (node is ParenthesizedExpression) {
return _visitCalculationValue(node.expression);
var inner = node.expression;
var result = _visitCalculationValue(inner);
return inner is FunctionExpression &&
inner.name.toLowerCase() == 'var' &&
result is SassString &&
!result.hasQuotes
? SassString('(${result.text})', quotes: false)
: result;
} else if (node is StringExpression) {
assert(!node.hasQuotes);
return CalculationInterpolation(_performInterpolation(node.text));
Expand Down
4 changes: 4 additions & 0 deletions pkg/sass_api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0-beta.12

* No user-visible changes.

## 1.0.0-beta.11

* No user-visible changes.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Original file line number Diff line number Diff line change
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: 1.0.0-beta.11
version: 1.0.0-beta.12
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
sass: 1.41.0
sass: 1.41.1

dependency_overrides:
sass: {path: ../..}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.41.0
version: 1.41.1
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit 52ef3c6

Please sign in to comment.