Skip to content

Commit

Permalink
Use latest Dart Sass and release migrator 2.0.0 (#251)
Browse files Browse the repository at this point in the history
- Deleted the obsolete `media-logic` migrator (breaking change bumps
  version to 2.0.0).
- Moved off of the tuple package to native Dart tuples.
- Fixed the `calc-interpolation` and `division` migrators to eliminate
  their use of the removed `CalculationExpression` AST node.
- Created a new `ScopedAstVisitor` that uses `Scope` to track Sass
  member declarations. `MigrationVisitor` and `_ReferenceVisitor`
  now extend this.
- Refactored the `division` migrator to use patterns.
  • Loading branch information
jathak authored Jan 4, 2024
1 parent 31ea60a commit 387292f
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 386 deletions.
29 changes: 24 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
## 1.8.2
## 2.0.0

* **Breaking change**: The `media-logic` migrator has been removed as the
[corresponding breaking change][media logic] has been completed in Dart Sass.
If you still need to migrate legacy code, use migrator version 1.8.1.

[media logic]: https://sass-lang.com/documentation/breaking-changes/media-logic/

* Update to be compatible with the latest version of the Dart Sass AST.

### Calc Functions Interpolation Migrator

* Add parentheses in place of interpolation when necessary to preserve the evaluation order.
* Add parentheses in place of interpolation when necessary to preserve the
evaluation order.

### Division Migrator

* `/` division should now be left untouched in all CSS calculation functions.
This was already the case for `calc`, `clamp`, `min`, and `max`, but it now
applies to the new functions that Dart Sass 1.67.0 added support for.

## 1.8.1

### Calc Functions Interpolation Migrator

* Migration for more than one interpolation or expressions in a calc function parameter.
* Migration for more than one interpolation or expressions in a calc function
parameter.

## 1.8.0

### Calc Functions Interpolation Migrator

* Removes interpolation in calculation functions `calc()`, `clamp()`, `min()`, and `max()`.
See the [scss/function-calculation-no-interpolation](https://github.com/stylelint-scss/stylelint-scss/tree/master/src/rules/function-calculation-no-interpolation) rule for more information.
* Removes interpolation in calculation functions `calc()`, `clamp()`, `min()`,
and `max()`. See the [scss/function-calculation-no-interpolation] rule for
more information.

[scss/function-calculation-no-interpolation]: https://github.com/stylelint-scss/stylelint-scss/tree/master/src/rules/function-calculation-no-interpolation

## 1.7.3

Expand Down
18 changes: 12 additions & 6 deletions lib/src/migration_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'package:source_span/source_span.dart';

import 'exception.dart';
import 'patch.dart';
import 'util/scope.dart';
import 'util/scoped_ast_visitor.dart';

/// A visitor that migrates a stylesheet.
///
Expand All @@ -24,8 +26,7 @@ import 'patch.dart';
/// If [migrateDependencies] is enabled, this visitor will construct and run a
/// new instance of itself (using [newInstance]) each time it encounters an
/// `@import` or `@use` rule.
abstract class MigrationVisitor
with RecursiveStatementVisitor, RecursiveAstVisitor {
abstract class MigrationVisitor extends ScopedAstVisitor {
/// A mapping from URLs to migrated contents for stylesheets already migrated.
final _migrated = <Uri, String>{};

Expand Down Expand Up @@ -117,26 +118,31 @@ abstract class MigrationVisitor

/// Visits the stylesheet at [dependency], resolved based on the current
/// stylesheet's URL and importer.
///
/// When [forImport] is true, this preserves the [currentScope]. Otherwise,
/// the dependency is visited with a new global scope for the new module.
@protected
void visitDependency(Uri dependency, FileSpan context,
{bool forImport = false}) {
if (dependency.scheme == 'sass') return;
var result = importCache.import(dependency,
baseImporter: _importer, baseUrl: _currentUrl, forImport: forImport);
if (result != null) {
if (result case (var newImporter, var stylesheet)) {
// If [dependency] comes from a non-relative import, don't migrate it,
// because it's likely to be outside the user's repository and may even be
// authored by a different person.
//
// TODO(nweiz): Add a flag to override this behavior for load paths
// (#104).
if (result.item1 != _importer) return;
if (newImporter != _importer) return;

var oldImporter = _importer;
_importer = result.item1;
var stylesheet = result.item2;
var oldScope = currentScope;
_importer = newImporter;
if (!forImport) currentScope = Scope();
visitStylesheet(stylesheet);
_importer = oldImporter;
currentScope = oldScope;
} else {
_missingDependencies.putIfAbsent(
context.sourceUrl!.resolveUri(dependency), () => context);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/migrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ abstract class Migrator extends Command<Map<Uri, String>> {
throw MigrationException("Could not find Sass file at '$entrypoint'.");
}

var migrated = migrateFile(importCache, tuple.item2, tuple.item1);
var migrated = migrateFile(importCache, tuple.$2, tuple.$1);
migrated.forEach((file, contents) {
if (allMigrated.containsKey(file) && contents != allMigrated[file]) {
throw MigrationException(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/migrators/calc_interpolation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
: super(importCache, migrateDependencies);

@override
void visitCalculationExpression(CalculationExpression node) {
void visitFunctionExpression(FunctionExpression node) {
const calcFunctions = ['calc', 'clamp', 'min', 'max'];
final interpolation = RegExp(r'\#{\s*[^}]+\s*}');
final hasOperation = RegExp(r'[-+*/]+');
if (calcFunctions.contains(node.name)) {
for (var arg in node.arguments) {
for (var arg in node.arguments.positional) {
var newArg = arg.toString();
for (var match in interpolation.allMatches(arg.toString())) {
var noInterpolation =
Expand All @@ -58,6 +58,6 @@ class _CalculationInterpolationVisitor extends MigrationVisitor {
}
}
}
super.visitCalculationExpression(node);
super.visitFunctionExpression(node);
}
}
Loading

0 comments on commit 387292f

Please sign in to comment.