Skip to content

Commit

Permalink
Throw an error when generating different results for the same file
Browse files Browse the repository at this point in the history
This error was already thrown when different results were generated
due to different entrypoints, but not for the same entrypoint. In
practice I don't think this can come up with the module migrator,
though, due to #99.
  • Loading branch information
nex3 committed Oct 1, 2019
1 parent 6799df6 commit 24ed9b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/src/migration_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import 'dart:collection';

import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';

// The sass package's API is not necessarily stable. It is being imported with
// the Sass team's explicit knowledge and approval. See
// https://github.com/sass/dart-sass/issues/236.
Expand All @@ -14,9 +18,7 @@ import 'package:sass/src/importer.dart';
import 'package:sass/src/import_cache.dart';
import 'package:sass/src/visitor/recursive_ast.dart';

import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';

import 'exception.dart';
import 'patch.dart';

/// A visitor that migrates a stylesheet.
Expand Down Expand Up @@ -87,10 +89,20 @@ abstract class MigrationVisitor extends RecursiveAstVisitor {
_patches = [];
_currentUrl = node.span.sourceUrl;
super.visitStylesheet(node);

var results = getMigratedContents();
if (results != null) {
_migrated[node.span.sourceUrl] = results;
var existingResults = _migrated[_currentUrl];
if (existingResults != null && existingResults != results) {
throw MigrationException(
"The migrator has found multiple possible migrations for "
"${p.prettyUri(_currentUrl)}, depending on the context in which "
"it's loaded.");
}

_migrated[_currentUrl] = results;
}

_patches = oldPatches;
_currentUrl = oldUrl;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/migrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ abstract class Migrator extends Command<Map<Uri, String>> {
if (allMigrated.containsKey(file) &&
migrated[file] != allMigrated[file]) {
throw MigrationException(
"$file is migrated in more than one way by these entrypoints.");
"The migrator has found multiple possible migrations for $file, "
"depending on the context in which it's loaded.");
}
allMigrated[file] = migrated[file];
}
Expand Down

0 comments on commit 24ed9b4

Please sign in to comment.