11library angular2.transform.common.code.source_module;
22
3+ import 'package:analyzer/src/generated/scanner.dart' show Keyword;
34import 'package:angular2/src/core/compiler/source_module.dart' ;
45
56import 'uri.dart' ;
@@ -9,7 +10,8 @@ String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
910 if (sourceModule == null ) return null ;
1011 var buf = new StringBuffer ();
1112 var sourceWithImports = sourceModule.getSourceWithImports ();
12- var libraryName = _getLibName (sourceModule.moduleUrl);
13+ libraryName = _sanitizeLibName (
14+ libraryName != null ? libraryName : sourceModule.moduleUrl);
1315 buf..writeln ('library $libraryName ;' )..writeln ();
1416 sourceWithImports.imports.forEach ((import) {
1517 // Format for importLine := [uri, prefix]
@@ -29,11 +31,11 @@ String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
2931}
3032
3133final _unsafeCharsPattern = new RegExp (r'[^a-zA-Z0-9_\.]' );
32- String _getLibName (String moduleUrl) {
33- // TODO(tbosch): use `.replaceAll('/', '.')` here as well
34- // Also: replaceAll('asset:' , '').
35- // Right now, this fails in some cases with Dart2Js, e.g.
36- // (Error 'switch' is a reserved word and can't be used here.
37- // library angular2_material.lib.src.components.switcher.switch.template.dart;)
38- return moduleUrl. replaceAll (_unsafeCharsPattern, '_' ) ;
34+ String _sanitizeLibName (String moduleUrl) {
35+ var sanitized =
36+ moduleUrl. replaceAll (_unsafeCharsPattern, '_' ). replaceAll ( '/' , '.' );
37+ for ( var keyword in Keyword .values) {
38+ sanitized. replaceAll (keyword.syntax, '${ keyword . syntax }_' );
39+ }
40+ return sanitized ;
3941}
0 commit comments