Skip to content

Commit

Permalink
Add strict unary migrator (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
jathak authored Sep 7, 2022
1 parent ef1de68 commit 874c62f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.6.0

### Strict Unary Migrator

* Add a new migrator for eliminating ambiguous syntax for the `+` and `-`
operators that will soon be deprecated.

## 1.5.6

* No user-visible changes.
Expand Down
47 changes: 47 additions & 0 deletions lib/src/migrators/strict_unary.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:sass_api/sass_api.dart';

import '../migration_visitor.dart';
import '../migrator.dart';
import '../patch.dart';

/// Migrates deprecated `$a -$b` construct to unambiguous `$a - $b`.
class StrictUnaryMigrator extends Migrator {
final name = "strict-unary";
final description = r"Migrates deprecated `$a -$b` syntax (and similar) to "
r"unambiguous `$a - $b`";

@override
Map<Uri, String> migrateFile(
ImportCache importCache, Stylesheet stylesheet, Importer importer) {
var visitor = _UnaryMigrationVisitor(importCache, migrateDependencies);
var result = visitor.run(stylesheet, importer);
missingDependencies.addAll(visitor.missingDependencies);
return result;
}
}

class _UnaryMigrationVisitor extends MigrationVisitor {
_UnaryMigrationVisitor(ImportCache importCache, bool migrateDependencies)
: super(importCache, migrateDependencies);

@override
void visitBinaryOperationExpression(BinaryOperationExpression node) {
if (node.operator == BinaryOperator.plus ||
node.operator == BinaryOperator.minus) {
var betweenOperands = node.span.file
.span(node.left.span.end.offset, node.right.span.start.offset)
.text;
if (betweenOperands.startsWith(RegExp(r'\s')) &&
betweenOperands.endsWith(node.operator.operator)) {
addPatch(Patch.insert(node.right.span.start, ' '));
}
}
super.visitBinaryOperationExpression(node);
}
}
2 changes: 2 additions & 0 deletions lib/src/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'io.dart';
import 'migrators/division.dart';
import 'migrators/module.dart';
import 'migrators/namespace.dart';
import 'migrators/strict_unary.dart';
import 'exception.dart';

/// A command runner that runs a migrator based on provided arguments.
Expand Down Expand Up @@ -55,6 +56,7 @@ class MigratorRunner extends CommandRunner<Map<Uri, String>> {
addCommand(DivisionMigrator());
addCommand(ModuleMigrator());
addCommand(NamespaceMigrator());
addCommand(StrictUnaryMigrator());
}

/// Runs a migrator and then writes the migrated files to disk unless
Expand Down
7 changes: 3 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass_migrator
version: 1.5.7-dev
version: 1.6.0
description: A tool for running migrations on Sass files
homepage: https://github.com/sass/migrator

Expand All @@ -10,13 +10,12 @@ dependencies:
args: ^2.1.0
charcode: ^1.2.0
collection: ^1.16.0
# Workaround until pulyaevskiy/node-interop#110 is resolved
file: '>=6.1.0 <6.1.2'
file: ^6.1.4
glob: ^2.0.1
js: ^0.6.3
meta: ^1.3.0
node_interop: ^2.0.2
node_io: ^2.1.0
node_io: ^2.2.0
path: ^1.8.0
sass_api: ^3.0.0
source_span: ^1.8.1
Expand Down
2 changes: 1 addition & 1 deletion test/migrators/namespace_node_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import '../utils.dart';

main() {
runNodeTests = true;
testMigrator("division");
testMigrator("namespace");
}
31 changes: 31 additions & 0 deletions test/migrators/strict_unary/add_whitespace.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<==> input/entrypoint.scss
$x: 1;
$y: 2;

plus {
a: $x +$y;
b: ($x) +($y);
c: $x +$y +$y;
}

minus {
a: $x -$y;
b: ($x) -($y);
c: $x -$y -$y;
}

<==> output/entrypoint.scss
$x: 1;
$y: 2;

plus {
a: $x + $y;
b: ($x) + ($y);
c: $x + $y + $y;
}

minus {
a: $x - $y;
b: ($x) - ($y);
c: $x - $y - $y;
}
20 changes: 20 additions & 0 deletions test/migrators/strict_unary/no_change.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<==> input/entrypoint.scss
$x: 1;
$y: 2;

plus {
a: $x+$y;
b: $x + $y;
c: $x (+$y);
d: $x+ $y;
}

minus {
a: ($x)-$y;
b: $x - $y;
c: $x (-$y);
d: ($x)- $y;
}

<==> log.txt
Nothing to migrate!
11 changes: 11 additions & 0 deletions test/migrators/strict_unary_dart_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022 Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import '../utils.dart';

main() {
testMigrator("strict_unary");
}
16 changes: 16 additions & 0 deletions test/migrators/strict_unary_node_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

@Tags(["node"])

import 'package:test/test.dart';

import '../utils.dart';

main() {
runNodeTests = true;
testMigrator("strict_unary");
}
2 changes: 1 addition & 1 deletion test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Future<void> _testHrx(File hrxFile, String migrator) async {
await files.unpack();

var process = await runMigrator([
migrator,
migrator.replaceAll('_', '-'),
'--no-unicode',
...files.arguments,
for (var path in files.input.keys)
Expand Down

0 comments on commit 874c62f

Please sign in to comment.