Skip to content

Commit

Permalink
Remove protocols + domains from any path passed to the compiler in th…
Browse files Browse the repository at this point in the history
…e output of our commonjs like modules. This is the only way to be consistent between registered paths and imported paths.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205671956
  • Loading branch information
johnplaisted authored and Bradford Smith committed Jul 23, 2018
1 parent 06c5561 commit ecfa286
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Expand Up @@ -93,10 +93,22 @@ private static class LocalQName {
* <p>Absolute import paths need to match the registered path exactly. Some {@link
* ModuleLoader.ModulePath}s will have a leading slash and some won't. So in order to have
* everything line up AND preserve schemes (if they exist) then just strip leading /.
*
* <p>Additionally if any path contains a protocol it will be stripped only the path part will
* remain. This is done heuristically as we cannot use {@link java.net.URL} or {@link
* java.nio.file.Path} due to GWT. As a result of stripping this cross-domain imports are not
* compatible with this pass.
*/
private static String normalizePath(String path) {
if (path.startsWith("/")) {
return path.substring(1);
int indexOfProtocol = path.indexOf("://");
if (indexOfProtocol > -1) {
path = path.substring(indexOfProtocol + 3);
int indexOfSlash = path.indexOf('/');
if (indexOfSlash > -1) {
path = path.substring(indexOfSlash + 1);
}
} else if (path.startsWith("/")) {
path = path.substring(1);
}
return path;
}
Expand Down Expand Up @@ -383,6 +395,12 @@ private void addExport(Node definePropertiesLit, String exportedName, LocalQName
}

private void visitImport(ModuleLoader.ModulePath path, Node importDecl) {
if (importDecl.getLastChild().getString().contains("://")) {
compiler.report(
JSError.make(
importDecl, Es6ToEs3Util.CANNOT_CONVERT, "Module requests with protocols."));
}

// Normalize the import path according to the module resolution scheme so that bundles are
// compatible with the compiler's module loader options.
importRequests.add(
Expand Down
Expand Up @@ -467,7 +467,7 @@ public void testExportWithArguments() {
"}, 'testcode', []);"));
}

public void testFileNameIsPreservedInRegisteredPathWhenNotEscaping() {
public void testProtocolAndDomainAreRemovedInRegisteredPathWhenNotEscaping() {
pathEscaper = PathEscaper.CANONICALIZE_ONLY;
test(
srcs(SourceFile.fromCode("https://example.domain.google.com/test.js", "export var x;")),
Expand All @@ -486,21 +486,11 @@ public void testFileNameIsPreservedInRegisteredPathWhenNotEscaping() {
" },",
" });",
" var x;",
"}, 'https://example.domain.google.com/test.js', []);"))));
"}, 'test.js', []);"))));
}

public void testFileNameIsPreservedInRequiredPathWhenNotEscaping() {
pathEscaper = PathEscaper.CANONICALIZE_ONLY;
test(
srcs(SourceFile.fromCode("test.js", "import 'file://imported.js';")),
expected(
SourceFile.fromCode(
"https://example.domain.google.com/test.js",
lines(
"$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {",
" 'test pragma';",
" var module$file_$$imported = $$require('file://imported.js');",
"}, 'test.js', ['file://imported.js']);"))));
public void testProtocolInImportPathIsError() {
testError("import * as foo from 'file://imported.js';", Es6ToEs3Util.CANNOT_CONVERT);
}

public void testRegisteredPathDoesNotIncludeModuleRoot() {
Expand Down

0 comments on commit ecfa286

Please sign in to comment.