X Tutup
Skip to content

Commit ba6e0e1

Browse files
author
Tim Blasi
committed
fix(dart/transform): Sanitize generated library names
Sanitize generated library names by removing unsafe characters and ensuring that Dart keywords do not appear as library segments.
1 parent 4ac2962 commit ba6e0e1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

modules_dart/transform/lib/src/transform/common/code/source_module.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library angular2.transform.common.code.source_module;
22

3+
import 'package:analyzer/src/generated/scanner.dart' show Keyword;
34
import 'package:angular2/src/core/compiler/source_module.dart';
45

56
import '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

3133
final _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

Comments
 (0)
X Tutup