Skip to content

Commit

Permalink
fix(dart/transform): Sanitize generated library names
Browse files Browse the repository at this point in the history
Sanitize generated library names by removing unsafe characters and
ensuring that Dart keywords do not appear as library segments.
  • Loading branch information
Tim Blasi committed Oct 6, 2015
1 parent 4ac2962 commit ba6e0e1
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library angular2.transform.common.code.source_module;

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

import 'uri.dart';
Expand All @@ -9,7 +10,8 @@ String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
if (sourceModule == null) return null;
var buf = new StringBuffer();
var sourceWithImports = sourceModule.getSourceWithImports();
var libraryName = _getLibName(sourceModule.moduleUrl);
libraryName = _sanitizeLibName(
libraryName != null ? libraryName : sourceModule.moduleUrl);
buf..writeln('library $libraryName;')..writeln();
sourceWithImports.imports.forEach((import) {
// Format for importLine := [uri, prefix]
Expand All @@ -29,11 +31,11 @@ String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
}

final _unsafeCharsPattern = new RegExp(r'[^a-zA-Z0-9_\.]');
String _getLibName(String moduleUrl) {
// TODO(tbosch): use `.replaceAll('/', '.')` here as well
// Also: replaceAll('asset:', '').
// Right now, this fails in some cases with Dart2Js, e.g.
// (Error 'switch' is a reserved word and can't be used here.
// library angular2_material.lib.src.components.switcher.switch.template.dart;)
return moduleUrl.replaceAll(_unsafeCharsPattern, '_');
String _sanitizeLibName(String moduleUrl) {
var sanitized =
moduleUrl.replaceAll(_unsafeCharsPattern, '_').replaceAll('/', '.');
for (var keyword in Keyword.values) {
sanitized.replaceAll(keyword.syntax, '${keyword.syntax}_');
}
return sanitized;
}

0 comments on commit ba6e0e1

Please sign in to comment.