Skip to content

Commit 809f9f5

Browse files
authored
Stop migrating division inside calc expressions (#217)
Fixes #216.
1 parent 7fe3ce1 commit 809f9f5

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.5.3
2+
3+
### Division Migrator
4+
5+
* Fix a bug where division inside calc expressions was unnecessarily migrated.
6+
17
## 1.5.2
28

39
* No user-visible changes.

lib/src/migrators/division.dart

+36-6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
6969
/// True when the current node is expected to evaluate to a number.
7070
var _expectsNumericResult = false;
7171

72+
/// True when the current node is in calc context, so division should be
73+
/// left unchanged.
74+
var _inCalcContext = false;
75+
7276
/// The namespaces that already exist in the current stylesheet.
7377
Map<Uri, String?> get _existingNamespaces =>
7478
assertInStylesheet(__existingNamespaces, '_existingNamespaces');
@@ -140,7 +144,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
140144
@override
141145
void visitArgumentInvocation(ArgumentInvocation invocation) {
142146
_withContext(() => super.visitArgumentInvocation(invocation),
143-
isDivisionAllowed: true);
147+
isDivisionAllowed: true, inCalcContext: false);
144148
}
145149

146150
/// If this is a division operation, migrates it.
@@ -159,6 +163,14 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
159163
}
160164
}
161165

166+
/// Visits calculations with [_inCalcContext] set to true.
167+
@override
168+
void visitCalculationExpression(CalculationExpression node) {
169+
_withContext(() {
170+
super.visitCalculationExpression(node);
171+
}, inCalcContext: true);
172+
}
173+
162174
/// Allows division within a function call's arguments, with special handling
163175
/// for new-syntax color functions.
164176
@override
@@ -167,6 +179,15 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
167179
visitArgumentInvocation(node.arguments);
168180
}
169181

182+
/// Visits interpolation with [_isDivisionAllowed] and [_inCalcContext] set
183+
/// to false.
184+
@override
185+
void visitInterpolation(Interpolation node) {
186+
_withContext(() {
187+
super.visitInterpolation(node);
188+
}, isDivisionAllowed: false, inCalcContext: false);
189+
}
190+
170191
/// Disallows division within this list.
171192
@override
172193
void visitListExpression(ListExpression node) {
@@ -271,6 +292,12 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
271292
/// Returns true the `/` was migrated to either function call (indicating that
272293
/// parentheses surrounding this operation should be removed).
273294
bool _visitSlashOperation(BinaryOperationExpression node) {
295+
if (_inCalcContext) {
296+
node.left.accept(this);
297+
node.right.accept(this);
298+
return false;
299+
}
300+
274301
if ((!_isDivisionAllowed && _onlySlash(node)) ||
275302
_isDefinitelyNotNumber(node)) {
276303
// Definitely not division
@@ -446,15 +473,18 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
446473

447474
/// Runs [operation] with the given context.
448475
void _withContext(void operation(),
449-
{bool? isDivisionAllowed, bool? expectsNumericResult}) {
476+
{bool? isDivisionAllowed,
477+
bool? expectsNumericResult,
478+
bool? inCalcContext}) {
450479
var previousDivisionAllowed = _isDivisionAllowed;
451480
var previousNumericResult = _expectsNumericResult;
452-
if (isDivisionAllowed != null) _isDivisionAllowed = isDivisionAllowed;
453-
if (expectsNumericResult != null) {
454-
_expectsNumericResult = expectsNumericResult;
455-
}
481+
var previousCalcContext = _inCalcContext;
482+
_isDivisionAllowed = isDivisionAllowed ?? _isDivisionAllowed;
483+
_expectsNumericResult = expectsNumericResult ?? _expectsNumericResult;
484+
_inCalcContext = inCalcContext ?? _inCalcContext;
456485
operation();
457486
_isDivisionAllowed = previousDivisionAllowed;
458487
_expectsNumericResult = previousNumericResult;
488+
_inCalcContext = previousCalcContext;
459489
}
460490
}

pubspec.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name: sass_migrator
2-
version: 1.5.2
2+
version: 1.5.3
33
description: A tool for running migrations on Sass files
4-
author: Jennifer Thakar <[email protected]>
54
homepage: https://github.com/sass/migrator
65

76
environment:
@@ -18,7 +17,7 @@ dependencies:
1817
node_interop: ^2.0.2
1918
node_io: ^2.1.0
2019
path: ^1.8.0
21-
sass: ^1.38.1
20+
sass: ^1.44.0
2221
source_span: ^1.8.1
2322
string_scanner: ^1.1.0
2423
term_glyph: ^1.2.0
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<==> input/entrypoint.scss
2+
a {
3+
$x: 300px;
4+
$y: 100%;
5+
b: calc($x / 2);
6+
c: clamp($x / 10, $y / 4, $x / 2);
7+
d: min($x / 2, $y / 2);
8+
e: calc(max($x / 2, $y / 2) / 2);
9+
f: calc(#{$x / 2});
10+
g: calc(fn($x / 2));
11+
}
12+
13+
<==> output/entrypoint.scss
14+
a {
15+
$x: 300px;
16+
$y: 100%;
17+
b: calc($x / 2);
18+
c: clamp($x / 10, $y / 4, $x / 2);
19+
d: min($x / 2, $y / 2);
20+
e: calc(max($x / 2, $y / 2) / 2);
21+
f: calc(#{$x * 0.5});
22+
g: calc(fn($x * 0.5));
23+
}

0 commit comments

Comments
 (0)