|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Use of this source code is governed by an MIT-style |
| 4 | +// license that can be found in the LICENSE file or at |
| 5 | +// https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +import 'package:sass_api/sass_api.dart'; |
| 8 | + |
| 9 | +import '../migration_visitor.dart'; |
| 10 | +import '../migrator.dart'; |
| 11 | +import '../patch.dart'; |
| 12 | + |
| 13 | +/// Migrates deprecated `$a -$b` construct to unambiguous `$a - $b`. |
| 14 | +class StrictUnaryMigrator extends Migrator { |
| 15 | + final name = "strict-unary"; |
| 16 | + final description = r"Migrates deprecated `$a -$b` syntax (and similar) to " |
| 17 | + r"unambiguous `$a - $b`"; |
| 18 | + |
| 19 | + @override |
| 20 | + Map<Uri, String> migrateFile( |
| 21 | + ImportCache importCache, Stylesheet stylesheet, Importer importer) { |
| 22 | + var visitor = _UnaryMigrationVisitor(importCache, migrateDependencies); |
| 23 | + var result = visitor.run(stylesheet, importer); |
| 24 | + missingDependencies.addAll(visitor.missingDependencies); |
| 25 | + return result; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class _UnaryMigrationVisitor extends MigrationVisitor { |
| 30 | + _UnaryMigrationVisitor(ImportCache importCache, bool migrateDependencies) |
| 31 | + : super(importCache, migrateDependencies); |
| 32 | + |
| 33 | + @override |
| 34 | + void visitBinaryOperationExpression(BinaryOperationExpression node) { |
| 35 | + if (node.operator == BinaryOperator.plus || |
| 36 | + node.operator == BinaryOperator.minus) { |
| 37 | + var betweenOperands = node.span.file |
| 38 | + .span(node.left.span.end.offset, node.right.span.start.offset) |
| 39 | + .text; |
| 40 | + if (betweenOperands.startsWith(RegExp(r'\s')) && |
| 41 | + betweenOperands.endsWith(node.operator.operator)) { |
| 42 | + addPatch(Patch.insert(node.right.span.start, ' ')); |
| 43 | + } |
| 44 | + } |
| 45 | + super.visitBinaryOperationExpression(node); |
| 46 | + } |
| 47 | +} |
0 commit comments