throw transform error on invalid package names
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f364ff1..4630daf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
#### 0.12.0+4
- * Support package names with hyphens (even though it violates dart style).
+ * The transformer will now give an informative error on package names with
+ hyphens.
#### 0.12.0+3
* Update analyzer dependency to `<0.27.0` and fix up some tests.
diff --git a/lib/build/script_compactor.dart b/lib/build/script_compactor.dart
index 56ae7e7..f177638 100644
--- a/lib/build/script_compactor.dart
+++ b/lib/build/script_compactor.dart
@@ -108,7 +108,7 @@
primaryInput.path.replaceFirst('.html', '.bootstrap.dart'));
var buffer = new StringBuffer();
- buffer.writeln('library ${_libraryNameFor(bootstrapId)};');
+ buffer.writeln('library ${_libraryNameFor(bootstrapId, logger)};');
buffer.writeln();
var i = 0;
for (var script in importScripts) {
@@ -147,7 +147,7 @@
// TODO(sigmund): ensure this path is unique (dartbug.com/12618).
var newId = primaryInput.addExtension('.$count.dart');
if (!_hasLibraryDirective(code)) {
- var libName = _libraryNameFor(primaryInput, count);
+ var libName = _libraryNameFor(primaryInput, logger, count);
code = "library $libName;\n$code";
}
@@ -228,7 +228,12 @@
}
/// Generate a library name for an asset.
-String _libraryNameFor(AssetId id, [int suffix]) {
+String _libraryNameFor(AssetId id, BuildLogger logger, [int suffix]) {
+ if (id.package.contains(_invalidLibCharsRegex)) {
+ logger.error('Invalid package name `${id.package}`. Package names should '
+ 'be valid dart identifiers, as indicated at '
+ 'https://www.dartlang.org/tools/pub/pubspec.html#name.');
+ }
var name = '${path.withoutExtension(id.path)}_'
'${path.extension(id.path).substring(1)}';
if (name.startsWith('lib/')) name = name.substring(4);
@@ -239,7 +244,7 @@
}
name = name.split('/').map(validLibName).join(".");
var suffixString = suffix != null ? '_$suffix' : '';
- return '${validLibName(id.package)}.${name}$suffixString';
+ return '${id.package}.${name}$suffixString';
}
/// Parse [code] and determine whether it has a library directive.
diff --git a/test/build/script_compactor_test.dart b/test/build/script_compactor_test.dart
index e59140d..a72f3e6 100644
--- a/test/build/script_compactor_test.dart
+++ b/test/build/script_compactor_test.dart
@@ -174,29 +174,43 @@
'a|web/%05_test.html.0.dart': 'library a.web._05_test_html_0;\n/*5*/',
}, [], StringFormatter.noNewlinesOrSurroundingWhitespace);
- testPhases('package and file names with hyphens', phases, {
- 'a-b|web/a-b.html': '''
+ testPhases('file names with hyphens are ok', phases, {
+ 'a|web/a-b.html': '''
<!DOCTYPE html><html><head></head><body>
<script type="application/dart" src="a-b.dart"></script>
</body></html>''',
- 'a-b|web/a-b.dart': '''
- library a_b.a_b;
+ 'a|web/a-b.dart': '''
+ library a.a_b;
main(){}''',
}, {
- 'a-b|web/a-b.html': '''
+ 'a|web/a-b.html': '''
<!DOCTYPE html><html><head></head><body>
<script type="application/dart" src="a-b.bootstrap.dart"></script>
</body></html>''',
- 'a-b|web/a-b.bootstrap.dart': '''
- library a_b.web.a_b_bootstrap_dart;
+ 'a|web/a-b.bootstrap.dart': '''
+ library a.web.a_b_bootstrap_dart;
import 'a-b.dart' as i0;
main() => i0.main();''',
- 'a-b|web/a-b.dart': '''
- library a_b.a_b;
+ 'a|web/a-b.dart': '''
+ library a.a_b;
main(){}''',
}, [], StringFormatter.noNewlinesOrSurroundingWhitespace);
+
+ testPhases('package names with hyphens give an error', phases, {
+ 'a-b|web/a.html': '''
+ <!DOCTYPE html><html><head></head><body>
+ <script type="application/dart" src="a.dart"></script>
+ </body></html>''',
+ 'a-b|web/a.dart': '''
+ library a.a;
+ main(){}''',
+ }, {}, [
+ 'error: Invalid package name `a-b`. Package names should be '
+ 'valid dart identifiers, as indicated at '
+ 'https://www.dartlang.org/tools/pub/pubspec.html#name.'
+ ], StringFormatter.noNewlinesOrSurroundingWhitespace);
}
void codeExtractorTests() {