@@ -42,7 +42,9 @@ const _calcFunctions = {
4242/// Migrates stylesheets that use the `/` operator for division to use the
4343/// `divide` function instead.
4444class DivisionMigrator extends Migrator {
45+ @override
4546 final name = "division" ;
47+ @override
4648 final description = """
4749Use the math.div() function instead of the / division operator
4850
@@ -88,17 +90,12 @@ More info: https://sass-lang.com/d/slash-div""";
8890/// The set of constant divisors that should be migrated to multiplication.
8991const _allowedDivisors = {2 , 4 , 5 , 8 , 10 , 20 , 40 , 50 , 80 , 100 , 1000 };
9092
91- class _DivisionMigrationVisitor extends MigrationVisitor {
92- final bool isPessimistic;
93- final bool useMultiplication;
94-
95- _DivisionMigrationVisitor (
96- super .importCache,
97- this .isPessimistic,
98- this .useMultiplication, {
99- required super .migrateDependencies,
100- });
101-
93+ class _DivisionMigrationVisitor (
94+ super .importCache,
95+ final bool isPessimistic,
96+ final bool useMultiplication, {
97+ required super .migrateDependencies,
98+ }) extends MigrationVisitor {
10299 /// True when division is allowed by the context the current node is in.
103100 var _isDivisionAllowed = false ;
104101
@@ -196,7 +193,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
196193 /// operands.
197194 @override
198195 void visitBinaryOperationExpression (BinaryOperationExpression node) {
199- if (node.operator == BinaryOperator .dividedBy) {
196+ if (node.operator == .dividedBy) {
200197 _visitSlashOperation (node);
201198 } else {
202199 _withContext (
@@ -255,7 +252,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
255252 }) {
256253 _withContext (() {
257254 if (node.expression
258- case BinaryOperationExpression (operator : BinaryOperator .dividedBy) &&
255+ case BinaryOperationExpression (operator : .dividedBy) &&
259256 var expression) {
260257 if (_visitSlashOperation (expression) && ! negated) {
261258 addPatch (patchDelete (node.span, end: 1 ));
@@ -272,7 +269,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
272269 @override
273270 void visitUnaryOperationExpression (UnaryOperationExpression node) {
274271 if (node case UnaryOperationExpression (
275- operator : UnaryOperator .minus,
272+ operator : .minus,
276273 : ParenthesizedExpression operand,
277274 )) {
278275 visitParenthesizedExpression (operand, negated: true );
@@ -316,7 +313,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
316313
317314 if (channels case ListExpression (
318315 hasBrackets: false ,
319- separator: ListSeparator .space,
316+ separator: .space,
320317 contents: [_, _, BinaryOperationExpression last],
321318 )) {
322319 // Handles cases like `rgb(10 20 30/2 / 0.5)`, since converting `30/2`
@@ -355,8 +352,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
355352 }
356353 var status = _NumberStatus .of (node);
357354
358- if ((! _isDivisionAllowed && _onlySlash (node)) ||
359- status == _NumberStatus .no) {
355+ if ((! _isDivisionAllowed && _onlySlash (node)) || status == .no) {
360356 // Definitely not division
361357 if (_isDivisionAllowed || _containsInterpolation (node)) {
362358 // We only want to convert a non-division slash operation to a
@@ -368,9 +364,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
368364 }
369365 return true ;
370366 }
371- if (_expectsNumericResult ||
372- status == _NumberStatus .yes ||
373- ! isPessimistic) {
367+ if (_expectsNumericResult || status == .yes || ! isPessimistic) {
374368 // Definitely division
375369 _withContext (
376370 () => super .visitBinaryOperationExpression (node),
@@ -397,10 +391,8 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
397391 /// Returns true if patched and false otherwise.
398392 bool _tryMultiplication (BinaryOperationExpression node) {
399393 if (! useMultiplication) return false ;
400- if (node.right case NumberExpression (
401- unit: null ,
402- value: var divisor,
403- ) when _allowedDivisors.contains (divisor)) {
394+ if (node.right case NumberExpression (unit: null , value: var divisor)
395+ when _allowedDivisors.contains (divisor)) {
404396 var operatorSpan = node.left.span
405397 .extendThroughWhitespace ()
406398 .end
@@ -418,7 +410,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
418410 /// unnecessary interpolation.
419411 void _visitSlashListArguments (Expression node) {
420412 switch (node) {
421- case BinaryOperationExpression (operator : BinaryOperator .dividedBy):
413+ case BinaryOperationExpression (operator : .dividedBy):
422414 _visitSlashListArguments (node.left);
423415 _patchOperatorToComma (node);
424416 _visitSlashListArguments (node.right);
@@ -435,24 +427,20 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
435427 /// Returns true if we assume that [operator] always operators on numbers.
436428 ///
437429 /// This is true for `*` , `%` , `<` , `<=` , `>` , and `>=` .
438- bool _operatesOnNumbers (BinaryOperator operator ) => {
439- BinaryOperator .times,
440- BinaryOperator .modulo,
441- BinaryOperator .lessThan,
442- BinaryOperator .lessThanOrEquals,
443- BinaryOperator .greaterThan,
444- BinaryOperator .greaterThanOrEquals,
430+ bool _operatesOnNumbers (BinaryOperator operator ) => < BinaryOperator > {
431+ .times,
432+ .modulo,
433+ .lessThan,
434+ .lessThanOrEquals,
435+ .greaterThan,
436+ .greaterThanOrEquals,
445437 }.contains (operator );
446438
447439 /// Returns true if [node] is entirely composed of number literals and slash
448440 /// operations.
449441 bool _onlySlash (Expression node) => switch (node) {
450442 NumberExpression () => true ,
451- BinaryOperationExpression (
452- operator : BinaryOperator .dividedBy,
453- : var left,
454- : var right,
455- ) =>
443+ BinaryOperationExpression (operator : .dividedBy, : var left, : var right) =>
456444 _onlySlash (left) && _onlySlash (right),
457445 _ => false ,
458446 };
@@ -490,9 +478,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
490478 void _patchParensIfAny (SassNode node) {
491479 switch (node) {
492480 case ParenthesizedExpression (
493- expression: BinaryOperationExpression (
494- operator : BinaryOperator .dividedBy,
495- ),
481+ expression: BinaryOperationExpression (operator : .dividedBy),
496482 ):
497483 return ;
498484 case ParenthesizedExpression ():
@@ -503,7 +489,7 @@ class _DivisionMigrationVisitor extends MigrationVisitor {
503489
504490 /// Runs [operation] with the given context.
505491 void _withContext (
506- void operation () , {
492+ void Function () operation , {
507493 bool ? isDivisionAllowed,
508494 bool ? expectsNumericResult,
509495 bool ? inCalcContext,
@@ -532,9 +518,7 @@ enum _NumberStatus {
532518 /// definitely not a number, and [maybe] otherwise.
533519 static _NumberStatus of (Expression node) => switch (node) {
534520 NumberExpression () ||
535- BinaryOperationExpression (
536- operator : BinaryOperator .times || BinaryOperator .modulo,
537- ) => yes,
521+ BinaryOperationExpression (operator : .times || .modulo) => yes,
538522 BooleanExpression () ||
539523 ColorExpression () ||
540524 ListExpression () ||
0 commit comments