From 24ed9b4c70006a16bdb52dcdcfb36c794e9b45b3 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Mon, 30 Sep 2019 17:46:27 -0700 Subject: [PATCH] Throw an error when generating different results for the same file 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. --- lib/src/migration_visitor.dart | 20 ++++++++++++++++---- lib/src/migrator.dart | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/src/migration_visitor.dart b/lib/src/migration_visitor.dart index 49beb877..af774a38 100644 --- a/lib/src/migration_visitor.dart +++ b/lib/src/migration_visitor.dart @@ -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. @@ -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. @@ -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; } diff --git a/lib/src/migrator.dart b/lib/src/migrator.dart index 1ad45ae3..9d96dbd0 100644 --- a/lib/src/migrator.dart +++ b/lib/src/migrator.dart @@ -87,7 +87,8 @@ abstract class Migrator extends Command> { 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]; }