diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index eedcd5ea005..f3cf89afddb 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -1714,7 +1714,8 @@ Node parseInputs() { options.moduleRoots, inputs, moduleResolverFactory, - ModuleLoader.PathResolver.RELATIVE); + ModuleLoader.PathResolver.RELATIVE, + options.getPathEscaper()); } else { // Use an empty module loader if we're not actually dealing with modules. this.moduleLoader = ModuleLoader.EMPTY; diff --git a/src/com/google/javascript/jscomp/CompilerOptions.java b/src/com/google/javascript/jscomp/CompilerOptions.java index 0f1cb709e29..e1f300f77d1 100644 --- a/src/com/google/javascript/jscomp/CompilerOptions.java +++ b/src/com/google/javascript/jscomp/CompilerOptions.java @@ -1197,6 +1197,8 @@ public void setWrapGoogModulesForWhitespaceOnly(boolean enable) { */ private ImmutableMap browserResolverPrefixReplacements; + private ModuleLoader.PathEscaper pathEscaper; + /** Which entries to look for in package.json files when processing modules */ List packageJsonEntryNames; @@ -1226,6 +1228,7 @@ public CompilerOptions() { // Modules moduleResolutionMode = ModuleLoader.ResolutionMode.BROWSER; packageJsonEntryNames = ImmutableList.of("browser", "module", "main"); + pathEscaper = ModuleLoader.PathEscaper.ESCAPE; // Checks skipNonTranspilationPasses = false; @@ -2863,6 +2866,14 @@ public void setBrowserResolverPrefixReplacements( this.browserResolverPrefixReplacements = browserResolverPrefixReplacements; } + public void setPathEscaper(ModuleLoader.PathEscaper pathEscaper) { + this.pathEscaper = pathEscaper; + } + + public ModuleLoader.PathEscaper getPathEscaper() { + return pathEscaper; + } + public List getPackageJsonEntryNames() { return this.packageJsonEntryNames; } @@ -2909,6 +2920,7 @@ public String toString() { .add("appNameStr", appNameStr) .add("assumeClosuresOnlyCaptureReferences", assumeClosuresOnlyCaptureReferences) .add("assumeStrictThis", assumeStrictThis()) + .add("browserResolverPrefixReplacements", browserResolverPrefixReplacements) .add("brokenClosureRequiresLevel", brokenClosureRequiresLevel) .add("checkDeterminism", getCheckDeterminism()) .add("checkGlobalNamesLevel", checkGlobalNamesLevel) @@ -3014,6 +3026,7 @@ public String toString() { "parentChunkCanSeeSymbolsDeclaredInChildren", parentChunkCanSeeSymbolsDeclaredInChildren) .add("parseJsDocDocumentation", isParseJsDocDocumentation()) + .add("pathEscaper", pathEscaper) .add("polymerVersion", polymerVersion) .add("preferLineBreakAtEndOfFile", preferLineBreakAtEndOfFile) .add("preferSingleQuotes", preferSingleQuotes) diff --git a/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java b/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java index 0a50c6fdd15..f15b9b02d72 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java +++ b/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java @@ -20,6 +20,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; +import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.ModuleLoader.ModulePath; import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature; import com.google.javascript.rhino.IR; @@ -85,6 +86,20 @@ private static class LocalQName { } } + /** + * Normalizes a registered or import path. + * + *

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 /. + */ + private static String normalizePath(String path) { + if (path.startsWith("/")) { + return path.substring(1); + } + return path; + } + /** * Rewrites a single ES6 module into a CommonJS like module designed to be loaded in the * compiler's module runtime. @@ -113,7 +128,7 @@ private class Rewriter extends AbstractPostOrderCallback { public void visit(NodeTraversal t, Node n, Node parent) { switch (n.getToken()) { case IMPORT: - visitImport(n); + visitImport(t.getInput().getPath(), n); break; case EXPORT: visitExport(t, n, parent); @@ -296,11 +311,10 @@ private void registerAndLoadModule(NodeTraversal t) { IR.call( IR.getprop(IR.name("$jscomp"), IR.string("registerAndLoadModule")), moduleFunction, - // Specifically use the input's name rather than modulePath.toString(). The former - // is the raw path and the latter is encoded (special characters are replaced). - // This is designed to run in a web browser and we want to preserve the URL given - // to us. But the encodings will replace : with - due to windows. - IR.string(t.getInput().getName()), + // Resolving this path enables removing module roots from this path. + IR.string( + normalizePath( + compiler.getModuleLoader().resolve(t.getInput().getName()).toString())), shallowDeps)); script.addChildToBack(exprResult.useSourceInfoIfMissingFromForTree(script)); @@ -344,8 +358,12 @@ private void addExport(Node definePropertiesLit, String exportedName, LocalQName compiler.reportChangeToChangeScope(getterFunction); } - private void visitImport(Node importDecl) { - importRequests.add(importDecl.getLastChild().getString()); + private void visitImport(ModuleLoader.ModulePath path, Node importDecl) { + // Normalize the import path according to the module resolution scheme so that bundles are + // compatible with the compiler's module loader options. + importRequests.add( + normalizePath( + path.resolveModuleAsPath(importDecl.getLastChild().getString()).toString())); imports.add(importDecl); } diff --git a/src/com/google/javascript/jscomp/deps/BrowserModuleResolver.java b/src/com/google/javascript/jscomp/deps/BrowserModuleResolver.java index fd5d50d5606..18a6c99c8c0 100644 --- a/src/com/google/javascript/jscomp/deps/BrowserModuleResolver.java +++ b/src/com/google/javascript/jscomp/deps/BrowserModuleResolver.java @@ -22,6 +22,7 @@ import com.google.javascript.jscomp.ErrorHandler; import com.google.javascript.jscomp.JSError; import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import javax.annotation.Nullable; /** @@ -36,8 +37,9 @@ public class BrowserModuleResolver extends ModuleResolver { public BrowserModuleResolver( ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler) { - super(modulePaths, moduleRootPaths, errorHandler); + ErrorHandler errorHandler, + PathEscaper pathEscaper) { + super(modulePaths, moduleRootPaths, errorHandler, pathEscaper); } @Override diff --git a/src/com/google/javascript/jscomp/deps/BrowserWithTransformedPrefixesModuleResolver.java b/src/com/google/javascript/jscomp/deps/BrowserWithTransformedPrefixesModuleResolver.java index 765abac7fdf..2465d44a745 100644 --- a/src/com/google/javascript/jscomp/deps/BrowserWithTransformedPrefixesModuleResolver.java +++ b/src/com/google/javascript/jscomp/deps/BrowserWithTransformedPrefixesModuleResolver.java @@ -27,6 +27,7 @@ import com.google.javascript.jscomp.ErrorHandler; import com.google.javascript.jscomp.JSError; import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import java.util.Comparator; import java.util.Set; import javax.annotation.Nullable; @@ -56,9 +57,10 @@ public Factory( public ModuleResolver create( ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler) { + ErrorHandler errorHandler, + PathEscaper pathEscaper) { return new BrowserWithTransformedPrefixesModuleResolver( - modulePaths, moduleRootPaths, errorHandler, prefixReplacements); + modulePaths, moduleRootPaths, errorHandler, pathEscaper, prefixReplacements); } } @@ -83,8 +85,9 @@ public BrowserWithTransformedPrefixesModuleResolver( ImmutableSet modulePaths, ImmutableList moduleRootPaths, ErrorHandler errorHandler, + PathEscaper pathEscaper, ImmutableMap prefixReplacements) { - super(modulePaths, moduleRootPaths, errorHandler); + super(modulePaths, moduleRootPaths, errorHandler, pathEscaper); Set p = prefixReplacements .entrySet() diff --git a/src/com/google/javascript/jscomp/deps/ModuleLoader.java b/src/com/google/javascript/jscomp/deps/ModuleLoader.java index dc0f93daff4..fdffab14062 100644 --- a/src/com/google/javascript/jscomp/deps/ModuleLoader.java +++ b/src/com/google/javascript/jscomp/deps/ModuleLoader.java @@ -72,31 +72,51 @@ public final class ModuleLoader { /** Used to canonicalize paths before resolution. */ private final PathResolver pathResolver; + private final PathEscaper pathEscaper; + private final ModuleResolver moduleResolver; /** * Creates an instance of the module loader which can be used to locate ES6 and CommonJS modules. * - * @param inputs All inputs to the compilation process. + * @param moduleRoots path prefixes to strip from module paths + * @param inputs all inputs to the compilation process. Used to ensure that resolved paths + * references an valid input. + * @param factory creates a module resolver, which determines how module identifiers are resolved + * @param pathResolver determines how to sanitize paths before resolving + * @param pathEscaper determines if / how paths should be escaped */ public ModuleLoader( @Nullable ErrorHandler errorHandler, Iterable moduleRoots, Iterable inputs, ModuleResolverFactory factory, - PathResolver pathResolver) { + PathResolver pathResolver, + PathEscaper pathEscaper) { checkNotNull(moduleRoots); checkNotNull(inputs); checkNotNull(pathResolver); + checkNotNull(pathEscaper); this.pathResolver = pathResolver; + this.pathEscaper = pathEscaper; this.errorHandler = errorHandler == null ? new NoopErrorHandler() : errorHandler; - this.moduleRootPaths = createRootPaths(moduleRoots, pathResolver); + this.moduleRootPaths = createRootPaths(moduleRoots, pathResolver, pathEscaper); this.modulePaths = resolvePaths( Iterables.transform(Iterables.transform(inputs, DependencyInfo::getName), pathResolver), - moduleRootPaths); + moduleRootPaths, + pathEscaper); this.moduleResolver = - factory.create(this.modulePaths, this.moduleRootPaths, this.errorHandler); + factory.create(this.modulePaths, this.moduleRootPaths, this.errorHandler, this.pathEscaper); + } + + public ModuleLoader( + @Nullable ErrorHandler errorHandler, + Iterable moduleRoots, + Iterable inputs, + ModuleResolverFactory factory, + PathResolver pathResolver) { + this(errorHandler, moduleRoots, inputs, factory, pathResolver, PathEscaper.ESCAPE); } public ModuleLoader( @@ -104,7 +124,7 @@ public ModuleLoader( Iterable moduleRoots, Iterable inputs, ModuleResolverFactory factory) { - this(errorHandler, moduleRoots, inputs, factory, PathResolver.RELATIVE); + this(errorHandler, moduleRoots, inputs, factory, PathResolver.RELATIVE, PathEscaper.ESCAPE); } @VisibleForTesting @@ -197,14 +217,7 @@ public ModulePath resolveModuleAsPath(String moduleAddress) { /** Resolves a path into a {@link ModulePath}. */ public ModulePath resolve(String path) { - return new ModulePath( - normalize(ModuleNames.escapePath(pathResolver.apply(path)), moduleRootPaths)); - } - - /** Resolves a path into a {@link ModulePath}. */ - public ModulePath resolveWithoutEscapingPath(String path) { - return new ModulePath( - normalize(pathResolver.apply(path), moduleRootPaths)); + return new ModulePath(normalize(pathEscaper.escape(pathResolver.apply(path)), moduleRootPaths)); } /** Whether this is relative to the current file, or a top-level identifier. */ @@ -228,14 +241,14 @@ public static boolean isPathIdentifier(String name) { } /** - * @param roots List of module root paths. This path prefix will be removed from module paths when - * resolved. + * Normalizes the given root paths, which are path prefixes to be removed from a module path when + * resolved. */ private static ImmutableList createRootPaths( - Iterable roots, PathResolver resolver) { + Iterable roots, PathResolver resolver, PathEscaper escaper) { ImmutableList.Builder builder = ImmutableList.builder(); for (String root : roots) { - String rootModuleName = ModuleNames.escapePath(resolver.apply(root)); + String rootModuleName = escaper.escape(resolver.apply(root)); if (isAmbiguousIdentifier(rootModuleName)) { rootModuleName = MODULE_SLASH + rootModuleName; } @@ -251,11 +264,11 @@ private static ImmutableList createRootPaths( * @return List of normalized modules which always have a leading slash */ private static ImmutableSet resolvePaths( - Iterable modulePaths, Iterable roots) { + Iterable modulePaths, Iterable roots, PathEscaper escaper) { ImmutableSet.Builder resolved = ImmutableSet.builder(); Set knownPaths = new HashSet<>(); for (String name : modulePaths) { - String canonicalizedPath = ModuleNames.escapePath(name); + String canonicalizedPath = escaper.escape(name); if (!knownPaths.add(normalize(canonicalizedPath, roots))) { // Having root paths "a" and "b" and source files "a/f.js" and "b/f.js" is ambiguous. throw new IllegalArgumentException( @@ -304,6 +317,33 @@ public ErrorHandler getErrorHandler() { return this.errorHandler; } + /** Indicates whether to escape characters in paths. */ + public enum PathEscaper { + /** + * Escapes characters in paths according to {@link ModuleNames#escapePath(String)} and then + * canonicalizes it. + */ + ESCAPE { + @Override + public String escape(String path) { + return ModuleNames.escapePath(path); + } + }, + + /** + * Does not escaped characters in paths, but does canonicalize it according to {@link + * ModuleNames#canonicalizePath(String)}. + */ + CANONICALIZE_ONLY { + @Override + public String escape(String path) { + return ModuleNames.canonicalizePath(path); + } + }; + + public abstract String escape(String path); + } + /** An enum indicating whether to absolutize paths. */ public enum PathResolver implements Function { RELATIVE { @@ -328,7 +368,8 @@ public interface ModuleResolverFactory { ModuleResolver create( ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler); + ErrorHandler errorHandler, + PathEscaper pathEscaper); } /** A trivial module loader with no roots. */ diff --git a/src/com/google/javascript/jscomp/deps/ModuleResolver.java b/src/com/google/javascript/jscomp/deps/ModuleResolver.java index c364c061851..9fe5d80898a 100644 --- a/src/com/google/javascript/jscomp/deps/ModuleResolver.java +++ b/src/com/google/javascript/jscomp/deps/ModuleResolver.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.ErrorHandler; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import java.util.Map; import javax.annotation.Nullable; @@ -32,14 +33,17 @@ public abstract class ModuleResolver { protected final ImmutableList moduleRootPaths; protected ErrorHandler errorHandler; + private final PathEscaper pathEscaper; public ModuleResolver( ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler) { + ErrorHandler errorHandler, + ModuleLoader.PathEscaper pathEscaper) { this.modulePaths = modulePaths; this.moduleRootPaths = moduleRootPaths; this.errorHandler = errorHandler; + this.pathEscaper = pathEscaper; } Map getPackageJsonMainEntries() { @@ -54,7 +58,7 @@ public String resolveModuleAsPath(String scriptAddress, String moduleAddress) { if (!moduleAddress.endsWith(".js")) { moduleAddress += ".js"; } - String path = ModuleNames.escapePath(moduleAddress); + String path = pathEscaper.escape(moduleAddress); if (ModuleLoader.isRelativeIdentifier(moduleAddress)) { String ourPath = scriptAddress; int lastIndex = ourPath.lastIndexOf(ModuleLoader.MODULE_SLASH); @@ -103,7 +107,7 @@ protected String locate(String scriptAddress, String name) { * relative paths to absolute references. */ protected String canonicalizePath(String scriptAddress, String moduleAddress) { - String path = ModuleNames.escapePath(moduleAddress); + String path = pathEscaper.escape(moduleAddress); if (ModuleLoader.isRelativeIdentifier(moduleAddress)) { String ourPath = scriptAddress; int lastIndex = ourPath.lastIndexOf(ModuleLoader.MODULE_SLASH); diff --git a/src/com/google/javascript/jscomp/deps/NodeModuleResolver.java b/src/com/google/javascript/jscomp/deps/NodeModuleResolver.java index 3352d7db86a..23bbb221d56 100644 --- a/src/com/google/javascript/jscomp/deps/NodeModuleResolver.java +++ b/src/com/google/javascript/jscomp/deps/NodeModuleResolver.java @@ -24,6 +24,7 @@ import com.google.javascript.jscomp.ErrorHandler; import com.google.javascript.jscomp.JSError; import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; @@ -114,10 +115,13 @@ public Factory(@Nullable Map packageJsonMainEntries) { } @Override - public ModuleResolver create(ImmutableSet modulePaths, - ImmutableList moduleRootPaths, ErrorHandler errorHandler) { + public ModuleResolver create( + ImmutableSet modulePaths, + ImmutableList moduleRootPaths, + ErrorHandler errorHandler, + PathEscaper pathEscaper) { return new NodeModuleResolver( - modulePaths, moduleRootPaths, packageJsonMainEntries, errorHandler); + modulePaths, moduleRootPaths, packageJsonMainEntries, errorHandler, pathEscaper); } } @@ -125,8 +129,9 @@ public NodeModuleResolver( ImmutableSet modulePaths, ImmutableList moduleRootPaths, Map packageJsonMainEntries, - ErrorHandler errorHandler) { - super(modulePaths, moduleRootPaths, errorHandler); + ErrorHandler errorHandler, + PathEscaper pathEscaper) { + super(modulePaths, moduleRootPaths, errorHandler, pathEscaper); this.nodeModulesFolders = buildNodeModulesFoldersRegistry(modulePaths); if (packageJsonMainEntries == null) { diff --git a/src/com/google/javascript/jscomp/deps/WebpackModuleResolver.java b/src/com/google/javascript/jscomp/deps/WebpackModuleResolver.java index 7cffeeb64b7..454636ae635 100644 --- a/src/com/google/javascript/jscomp/deps/WebpackModuleResolver.java +++ b/src/com/google/javascript/jscomp/deps/WebpackModuleResolver.java @@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.ErrorHandler; import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -49,18 +50,19 @@ public Factory(Map lookupMap) { public ModuleResolver create( ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler) { + ErrorHandler errorHandler, + PathEscaper pathEscaper) { Map normalizedPathsById = new HashMap<>(); for (Entry moduleEntry : lookupMap.entrySet()) { String canonicalizedPath = - ModuleLoader.normalize(ModuleNames.escapePath(moduleEntry.getValue()), moduleRootPaths); + ModuleLoader.normalize(pathEscaper.escape(moduleEntry.getValue()), moduleRootPaths); if (ModuleLoader.isAmbiguousIdentifier(canonicalizedPath)) { canonicalizedPath = ModuleLoader.MODULE_SLASH + canonicalizedPath; } normalizedPathsById.put(moduleEntry.getKey(), canonicalizedPath); } return new WebpackModuleResolver( - modulePaths, moduleRootPaths, normalizedPathsById, errorHandler); + modulePaths, moduleRootPaths, normalizedPathsById, errorHandler, pathEscaper); } } @@ -68,8 +70,14 @@ public WebpackModuleResolver( ImmutableSet modulePaths, ImmutableList moduleRootPaths, Map modulesById, - ErrorHandler errorHandler) { - super(modulePaths, moduleRootPaths, null, errorHandler); + ErrorHandler errorHandler, + PathEscaper pathEscaper) { + super( + modulePaths, + moduleRootPaths, + /* packageJsonMainEntries= */ null, + errorHandler, + pathEscaper); this.modulesById = ImmutableMap.copyOf(modulesById); } diff --git a/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java b/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java index 3638399c57f..bd46ea22c35 100644 --- a/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java +++ b/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java @@ -35,6 +35,7 @@ import com.google.javascript.jscomp.VariableRenamingPolicy; import com.google.javascript.jscomp.bundle.TranspilationException; import com.google.javascript.jscomp.deps.ModuleLoader; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import com.google.javascript.jscomp.deps.ModuleLoader.ResolutionMode; import com.google.javascript.rhino.Node; import java.io.IOException; @@ -177,6 +178,8 @@ protected void setOptions(CompilerOptions options) { options.setModuleResolutionMode(moduleResolution); options.setModuleRoots(moduleRoots); options.setBrowserResolverPrefixReplacements(prefixReplacements); + // Don't escape module paths when bundling in the event paths are URLs. + options.setPathEscaper(PathEscaper.CANONICALIZE_ONLY); options.setSourceMapOutputPath("/dev/null"); options.setSourceMapIncludeSourcesContent(true); diff --git a/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java b/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java index ffe628e6e0a..f50d400a23b 100644 --- a/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java +++ b/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java @@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableMap; import com.google.javascript.jscomp.CompilerOptions.LanguageMode; import com.google.javascript.jscomp.deps.ModuleLoader; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; import java.util.List; @@ -26,6 +27,7 @@ public final class Es6RewriteModulesToCommonJsModulesTest extends CompilerTestCa private List moduleRoots; private ModuleLoader.ResolutionMode resolutionMode; private ImmutableMap prefixReplacements; + private PathEscaper pathEscaper; @Override protected void setUp() throws Exception { @@ -36,6 +38,7 @@ protected void setUp() throws Exception { moduleRoots = ImmutableList.of(); resolutionMode = ModuleLoader.ResolutionMode.BROWSER; prefixReplacements = ImmutableMap.of(); + pathEscaper = PathEscaper.ESCAPE; } @Override @@ -47,6 +50,7 @@ protected CompilerOptions getOptions() { options.setModuleRoots(moduleRoots); options.setModuleResolutionMode(resolutionMode); options.setBrowserResolverPrefixReplacements(prefixReplacements); + options.setPathEscaper(pathEscaper); return options; } @@ -454,7 +458,8 @@ public void testExportWithArguments() { "}, 'testcode', []);")); } - public void testFileNameIsPreserved() { + public void testFileNameIsPreservedInRegisteredPathWhenNotEscaping() { + pathEscaper = PathEscaper.CANONICALIZE_ONLY; test( srcs(SourceFile.fromCode("https://example.domain.google.com/test.js", "export var x;")), expected( @@ -475,8 +480,21 @@ public void testFileNameIsPreserved() { "}, 'https://example.domain.google.com/test.js', []);")))); } - // TODO(johnplaisted): This should strip module roots - public void testRegisteredPathDoesIncludeModuleRoot() { + 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 testRegisteredPathDoesNotIncludeModuleRoot() { moduleRoots = ImmutableList.of("module/root/"); test( @@ -487,11 +505,10 @@ public void testRegisteredPathDoesIncludeModuleRoot() { lines( "$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {", " 'test pragma';", - "}, 'module/root/test.js', []);")))); + "}, 'test.js', []);")))); } - // TODO(johnplaisted): This should strip module roots - public void testImportPathDoesIncludeModuleRoot() { + public void testImportPathDoesNotIncludeModuleRoot() { moduleRoots = ImmutableList.of("module/root/"); test( @@ -502,11 +519,10 @@ public void testImportPathDoesIncludeModuleRoot() { lines( "$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {", " 'test pragma';", - " var module$foo = $$require('module/root/foo.js');", - "}, 'not/root/test.js', ['module/root/foo.js']);")))); + " var module$foo = $$require('foo.js');", + "}, 'not/root/test.js', ['foo.js']);")))); } - // TODO(johnplaisted): This should respect different browser resolutions. public void testImportPathWithBrowserPrefixReplacementResolution() { resolutionMode = ModuleLoader.ResolutionMode.BROWSER_WITH_TRANSFORMED_PREFIXES; prefixReplacements = ImmutableMap.of("@root/", ""); @@ -519,7 +535,7 @@ public void testImportPathWithBrowserPrefixReplacementResolution() { lines( "$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {", " 'test pragma';", - " var module$foo = $$require('@root/foo.js');", - "}, 'not/root/test.js', ['@root/foo.js']);")))); + " var module$foo = $$require('foo.js');", + "}, 'not/root/test.js', ['foo.js']);")))); } } diff --git a/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java b/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java index 9b81f36b32c..5639935feb6 100644 --- a/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java +++ b/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java @@ -162,11 +162,11 @@ public void testEs6Module() throws IOException { + " }}, y:{enumerable:true, get:function() {\n" + " return module$nested$path$other.x;\n" + " }}});\n" - + " var module$nested$path$other = $$require(\"./other.js\");\n" + + " var module$nested$path$other = $$require(\"nested/path/other.js\");\n" + " var local;\n" + " function foo() {\n" + " return local;\n" + " }\n" - + "}, \"nested/path/foo.js\", [\"./other.js\"]);\n"); + + "}, \"nested/path/foo.js\", [\"nested/path/other.js\"]);\n"); } } diff --git a/test/com/google/javascript/jscomp/deps/ModuleLoaderTest.java b/test/com/google/javascript/jscomp/deps/ModuleLoaderTest.java index 8a927217ee0..2c37066a630 100644 --- a/test/com/google/javascript/jscomp/deps/ModuleLoaderTest.java +++ b/test/com/google/javascript/jscomp/deps/ModuleLoaderTest.java @@ -24,6 +24,8 @@ import com.google.javascript.jscomp.CompilerInput; import com.google.javascript.jscomp.ErrorHandler; import com.google.javascript.jscomp.SourceFile; +import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper; +import com.google.javascript.jscomp.deps.ModuleLoader.PathResolver; import javax.annotation.Nullable; import junit.framework.TestCase; @@ -241,6 +243,38 @@ public void testCanonicalizePath() throws Exception { assertEquals("/", ModuleNames.canonicalizePath("/a/..")); } + public void testEscapePath() { + ModuleLoader loader = + new ModuleLoader( + /* errorHandler= */ null, + /* moduleRoots= */ ImmutableList.of(), + inputs("/has:special:chars.js"), + BrowserModuleResolver.FACTORY, + PathResolver.RELATIVE, + PathEscaper.ESCAPE); + + // : is escaped to - + assertThat(loader.resolve("file://my/file.js").toString()).isEqualTo("file-//my/file.js"); + assertThat(loader.resolve("c").resolveJsModule("/has:special:chars.js").toString()) + .isEqualTo("/has-special-chars.js"); + } + + public void testDoNoEscapePath() { + // : is a character that is escaped + ModuleLoader loader = + new ModuleLoader( + /* errorHandler= */ null, + /* moduleRoots= */ ImmutableList.of(), + inputs("/has:special:chars.js"), + BrowserModuleResolver.FACTORY, + PathResolver.RELATIVE, + PathEscaper.CANONICALIZE_ONLY); + + assertThat(loader.resolve("file://my/file.js").toString()).isEqualTo("file://my/file.js"); + assertThat(loader.resolve("c").resolveJsModule("/has:special:chars.js").toString()) + .isEqualTo("/has:special:chars.js"); + } + ImmutableList inputs(String... names) { ImmutableList.Builder builder = ImmutableList.builder(); for (String name : names) { @@ -454,8 +488,9 @@ public void testCustomResolution() { inputs("A/index.js", "B/index.js", "app.js"), (ImmutableSet modulePaths, ImmutableList moduleRootPaths, - ErrorHandler errorHandler) -> - new ModuleResolver(modulePaths, moduleRootPaths, errorHandler) { + ErrorHandler errorHandler, + PathEscaper pathEscaper) -> + new ModuleResolver(modulePaths, moduleRootPaths, errorHandler, pathEscaper) { @Nullable @Override public String resolveJsModule( diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/decl_leg_name_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/decl_leg_name_test.js similarity index 80% rename from test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/decl_leg_name_test.js rename to test/com/google/javascript/jscomp/runtime_tests/module_tests/decl_leg_name_test.js index bada03bc32c..efb114958dc 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/decl_leg_name_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/decl_leg_name_test.js @@ -17,11 +17,14 @@ goog.provide("decl.leg.name.Test"); goog.setTestOnly(); - goog.require("decl.leg.name.A"); +goog.require('goog.testing.testSuite'); +/** @type {decl.leg.name.A} */ +var a; -function test_Main() { - /** @type {decl.leg.name.A} */ - var a = new decl.leg.name.A; -} +goog.testing.testSuite({ + test_Main: function() { + a = new decl.leg.name.A; + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/dot_dot_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/dot_dot_test.js index 2a35b2d208c..fd069926190 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/dot_dot_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/dot_dot_test.js @@ -16,6 +16,10 @@ import s from '../module_tests/module_test_resources/exportDefault.js'; -function testDefault() { - assertEquals('this is the default export', s); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testDefault() { + assertEquals('this is the default export', s); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_from_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_from_test.js index 881d79d3355..6dce9ffdfd2 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_from_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_from_test.js @@ -17,9 +17,15 @@ /** * @fileoverview Tests for export statement with a "from" clause. */ -import {alpha, gamma} from './module_test_resources/exportFrom.js'; +import {alpha, assertVarsNotAddedToThisScope, gamma} from './module_test_resources/exportFrom.js'; -function testTransitiveImport() { - assertEquals(3, alpha); - assertEquals(4, gamma); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testTransitiveImport() { + assertEquals(3, alpha); + assertEquals(4, gamma); + }, + + testVarsNotAddedToThisScope: assertVarsNotAddedToThisScope, +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_spec_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_spec_test.js index 66bd9022660..f7f907a7658 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_spec_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/export_spec_test.js @@ -16,6 +16,10 @@ import {x} from './module_test_resources/exportSpec.js'; -function testX() { - assertEquals(5, x); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testX() { + assertEquals(5, x); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_class_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_class_test.js index a3bd80b7f83..adabf37e376 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_class_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_class_test.js @@ -22,6 +22,8 @@ import {Parent} from './module_test_resources/exportClass.js'; +const testSuite = goog.require('goog.testing.testSuite'); + export class Child extends Parent { /** * @param {Parent} parent The parent. @@ -32,11 +34,13 @@ export class Child extends Parent { export class GrandChild extends Child {} -function testClass() { - new Child().useParent(new Parent()); -} +testSuite({ + testClass() { + new Child().useParent(new Parent()); + }, -function testStaticInheritanceAcrossModules() { - assertEquals('Parent.staticFunction', Child.staticFunction()); - assertEquals('Parent.staticFunction', GrandChild.staticFunction()); -} + testStaticInheritanceAcrossModules() { + assertEquals('Parent.staticFunction', Child.staticFunction()); + assertEquals('Parent.staticFunction', GrandChild.staticFunction()); + }, +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_default_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_default_test.js index 64730044e83..57ff6ddc72f 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_default_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_default_test.js @@ -22,12 +22,16 @@ import s from './module_test_resources/exportDefault.js'; import ExampleClass from './module_test_resources/exportDefaultClass.js'; import ExampleCtor from './module_test_resources/exportDefaultOldStyleClass.js'; -function testDefault() { - assertEquals('this is the default export', s); - assertEquals(0, (new ExampleClass).foo()); -} +const testSuite = goog.require('goog.testing.testSuite'); -function testOldStyleClass() { - assertEquals('staticMethod', ExampleCtor.staticMethod()); - assertEquals('method', (new ExampleCtor).method()); -} +testSuite({ + testDefault() { + assertEquals('this is the default export', s); + assertEquals(0, (new ExampleClass).foo()); + }, + + testOldStyleClass() { + assertEquals('staticMethod', ExampleCtor.staticMethod()); + assertEquals('method', (new ExampleCtor).method()); + }, +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_mixed_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_mixed_test.js index 95a422e7c87..0939bbac5b1 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_mixed_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_mixed_test.js @@ -15,11 +15,15 @@ */ import defaultStr, * as mod from './module_test_resources/exportDefault.js'; -import defaultStr, {nonDefaultExport} from './module_test_resources/exportDefault.js'; +import {nonDefaultExport} from './module_test_resources/exportDefault.js'; -function testImportMixed() { - assertEquals('this is the default export', defaultStr); - assertEquals('this is the default export', mod.default); - assertEquals(2, mod.nonDefaultExport); - assertEquals(2, nonDefaultExport); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testImportMixed() { + assertEquals('this is the default export', defaultStr); + assertEquals('this is the default export', mod.default); + assertEquals(2, mod.nonDefaultExport); + assertEquals(2, nonDefaultExport); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_namespace_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_namespace_test.js index e19585061a6..8d7166a3bf8 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_namespace_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_namespace_test.js @@ -14,19 +14,23 @@ * limitations under the License. */ -import Foo from 'goog:test.ns.Foo'; -import {staticBar} from 'goog:test.ns.Foo'; -import {foo, bar} from 'goog:test.goog.module'; +import {bar, foo} from 'goog:data.goog.module'; +import Foo from 'goog:data.ns.Foo'; +import {staticBar} from 'goog:data.ns.Foo'; -function testImportDefault() { - assertEquals(42, new Foo().bar); -} +const testSuite = goog.require('goog.testing.testSuite'); -function testImportDestructuring() { - assertEquals(23, staticBar); -} +testSuite({ + testImportDefault() { + assertEquals(42, new Foo().bar); + }, -function testImportDestructuring_googModule() { - assertEquals(23, foo); - assertEquals(42, bar); -} + testImportDestructuring() { + assertEquals(23, staticBar); + }, + + testImportDestructuring_googModule() { + assertEquals(23, foo); + assertEquals(42, bar); + }, +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_star_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_star_test.js index 4bc2a5fea25..08e9b168936 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_star_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_star_test.js @@ -16,12 +16,16 @@ import * as m from './module_test_resources/simpleExport.js'; -function testImportStar() { - assertEquals('g', m.g()); -} +const testSuite = goog.require('goog.testing.testSuite'); -/** @suppress {checkTypes} */ -function testUnexportedProperty() { - // Module-scoped variable, not exported with this name. - assertEquals(undefined, m.a); -} +testSuite({ + testImportStar() { + assertEquals('g', m.g()); + }, + + /** @suppress {checkTypes} */ + testUnexportedProperty() { + // Module-scoped variable, not exported with this name. + assertEquals(undefined, m.a); + }, +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_with_js_extension_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_with_js_extension_test.js index 48f608d61aa..bd353f25681 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_with_js_extension_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/import_with_js_extension_test.js @@ -17,6 +17,10 @@ // Tests import statement with a ".js" extension in the module specifier. import * as m from './module_test_resources/simpleExport.js'; -function testImportStar() { - assertEquals('g', m.g()); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testImportStar() { + assertEquals('g', m.g()); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_order_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_order_test.js index e97a16cbc81..2f50a3833db 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_order_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_order_test.js @@ -14,18 +14,22 @@ * limitations under the License. */ -import { record } from './module_test_resources/moduleOrderRecorder.js'; -import { A } from './module_test_resources/moduleOrderA.js'; -import { B } from './module_test_resources/moduleOrderB.js'; -import { C } from './module_test_resources/moduleOrderC.js'; +import {A} from './module_test_resources/moduleOrderA.js'; +import {B} from './module_test_resources/moduleOrderB.js'; +import {C} from './module_test_resources/moduleOrderC.js'; +import {record} from './module_test_resources/moduleOrderRecorder.js'; -function testModuleOrder() { - assertEquals(record[0], 'processed module C'); - assertEquals(record[1], 'processed module A'); - assertEquals(record[2], 'processed module B'); +const testSuite = goog.require('goog.testing.testSuite'); - // Verify the imported aliases are usable. - new A().create(); - new B(); - new C(); -} +testSuite({ + testModuleOrder() { + assertEquals(record[0], 'processed module C'); + assertEquals(record[1], 'processed module A'); + assertEquals(record[2], 'processed module B'); + + // Verify the imported aliases are usable. + new A().create(); + new B(); + new C(); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/exportFrom.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/exportFrom.js index 631c8f0474e..b8ee3ab88df 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/exportFrom.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/exportFrom.js @@ -14,15 +14,19 @@ * limitations under the License. */ -goog.require('goog.testing.asserts'); +// TODO(johnplaisted): This should goog.require asserts, but there's a type +// error. Why? -var alpha = 'a'; -var beta = 'b'; -var gamma = 'c'; +const alpha = 'a'; +const beta = 'b'; +const gamma = 'c'; export {alpha, beta as gamma} from './simpleExport.js'; -function testVarsNotAddedToThisScope() { +/** + * Asserts that exported vars are not in this scope. + */ +export function assertVarsNotAddedToThisScope() { assertEquals('a', alpha); assertEquals('b', beta); assertEquals('c', gamma); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/goog_module.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/goog_module.js index ecb214f62b4..613efec3dc2 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/goog_module.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/goog_module.js @@ -17,7 +17,7 @@ /** * @fileoverview A file exporting properties as a goog.module(). */ -goog.module('test.goog.module'); +goog.module('data.goog.module'); goog.module.declareLegacyNamespace(); // This file is imported from an ES6 module and the ES6 module handling at the // moment can only successfully dep upon on legacy namespaces (or other ES6 diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/namespace_export.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/namespace_export.js index c19477ad23e..1beec694008 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/namespace_export.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/module_test_resources/namespace_export.js @@ -17,13 +17,13 @@ /** * @fileoverview A file exporting symbols as namespaced properties. */ -goog.provide('test.ns.Foo'); +goog.provide('data.ns.Foo'); /** @constructor */ -test.ns.Foo = function() { +data.ns.Foo = function() { /** @public @type {number} */ this.bar = 42; }; /** @type {number} */ -test.ns.Foo.staticBar = 23; +data.ns.Foo.staticBar = 23; diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/mutable_exports_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/mutable_exports_test.js index 4b7509e0f83..9ddd2cc91eb 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/mutable_exports_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/mutable_exports_test.js @@ -14,19 +14,22 @@ * limitations under the License. */ -import {a, b, set} from './module_test_resources/mutable_exports.js'; import * as Mutable from './module_test_resources/mutable_exports.js'; +import {a, b, set} from './module_test_resources/mutable_exports.js'; -goog.require('goog.testing.asserts'); +const asserts = goog.require('goog.testing.asserts'); +const testSuite = goog.require('goog.testing.testSuite'); -function testMutableExportS() { - assertEquals(0, a); - assertEquals(1, b); - assertEquals(2, Mutable.c); +testSuite({ + testMutableExportS() { + asserts.assertEquals(0, a); + asserts.assertEquals(1, b); + asserts.assertEquals(2, Mutable.c); - set(1, 2, 3); + set(1, 2, 3); - assertEquals(1, a); - assertEquals(2, b); - assertEquals(3, Mutable.c); -} + asserts.assertEquals(1, a); + asserts.assertEquals(2, b); + asserts.assertEquals(3, Mutable.c); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/reexport_mutable_exports_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/reexport_mutable_exports_test.js index 257dc069e58..8865a8ae385 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/reexport_mutable_exports_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/reexport_mutable_exports_test.js @@ -17,22 +17,25 @@ import * as Mutable from './module_test_resources/mutable_exports.js'; import * as Reexported from './module_test_resources/reexport_mutable_exports.js'; -goog.require('goog.testing.asserts'); +const asserts = goog.require('goog.testing.asserts'); +const testSuite = goog.require('goog.testing.testSuite'); -function testMutableExportS() { - assertEquals(Mutable.a, Reexported.A); - assertEquals(Mutable.b, Reexported.B); - assertEquals(Mutable.c, Reexported.C); +testSuite({ + testMutableExportS() { + asserts.assertEquals(Mutable.a, Reexported.A); + asserts.assertEquals(Mutable.b, Reexported.B); + asserts.assertEquals(Mutable.c, Reexported.C); - Mutable.set(1, 2, 3); + Mutable.set(1, 2, 3); - assertEquals(Mutable.a, Reexported.A); - assertEquals(Mutable.b, Reexported.B); - assertEquals(Mutable.c, Reexported.C); + asserts.assertEquals(Mutable.a, Reexported.A); + asserts.assertEquals(Mutable.b, Reexported.B); + asserts.assertEquals(Mutable.c, Reexported.C); - Reexported.set(4, 5, 6); + Reexported.set(4, 5, 6); - assertEquals(Mutable.a, Reexported.A); - assertEquals(Mutable.b, Reexported.B); - assertEquals(Mutable.c, Reexported.C); -} + asserts.assertEquals(Mutable.a, Reexported.A); + asserts.assertEquals(Mutable.b, Reexported.B); + asserts.assertEquals(Mutable.c, Reexported.C); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/require_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/require_test.js index 824e62bfa8a..181c9a5cb07 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/require_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/require_test.js @@ -16,13 +16,17 @@ import s from './module_test_resources/exportDefault.js'; -const {assertInstanceof} = goog.require('goog.asserts'); const EventHandler = goog.require('goog.events.EventHandler'); +const testSuite = goog.require('goog.testing.testSuite'); +const {assertInstanceof} = goog.require('goog.asserts'); + // Just check to make sure the goog.require worked and the required class can // be used. -function testRequire() { - assertEquals(s, s); // This is used to avoid the extra require warning - var eh = new EventHandler(); - assertInstanceof(eh, EventHandler); -} +testSuite({ + testRequire() { + assertEquals(s, s); // This is used to avoid the extra require warning + var eh = new EventHandler(); + assertInstanceof(eh, EventHandler); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/simple_module_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/simple_module_test.js index 307987a2f49..147d71fdc85 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/simple_module_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/simple_module_test.js @@ -20,15 +20,18 @@ * @author moz@google.com (Michael Zhou) */ -import {foo as f} from './module_test_resources/simpleExport.js'; -import {bar as b, alpha, beta} from './module_test_resources/simpleExport.js'; import {a} from './module_test_resources/+subdir/module_es6.js'; +import {foo as f} from './module_test_resources/simpleExport.js'; +import {alpha, bar as b, beta} from './module_test_resources/simpleExport.js'; -goog.require('goog.testing.asserts'); +const asserts = goog.require('goog.testing.asserts'); +const testSuite = goog.require('goog.testing.testSuite'); -function testBasic() { - assertEquals(2, f + 1); - assertEquals(3, f + b); - assertEquals(12, alpha * beta); - assertEquals(1, a); -} +testSuite({ + testBasic() { + asserts.assertEquals(2, f + 1); + asserts.assertEquals(3, f + b); + asserts.assertEquals(12, alpha * beta); + asserts.assertEquals(1, a); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/transitive_import_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/transitive_import_test.js index e7ccf44ffb5..fe7d2b1d0f5 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/transitive_import_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/transitive_import_test.js @@ -20,7 +20,11 @@ */ import {alpha, gamma} from './module_test_resources/importAndExport.js'; -function testTransitiveImport() { - assertEquals(3, alpha); - assertEquals(4, gamma); -} +const testSuite = goog.require('goog.testing.testSuite'); + +testSuite({ + testTransitiveImport() { + assertEquals(3, alpha); + assertEquals(4, gamma); + } +}); diff --git a/test/com/google/javascript/jscomp/runtime_tests/module_tests/typedef_test.js b/test/com/google/javascript/jscomp/runtime_tests/module_tests/typedef_test.js index eec2a1559d5..4539531919a 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/module_tests/typedef_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/module_tests/typedef_test.js @@ -16,9 +16,13 @@ import * as t from './module_test_resources/typedef.js'; +const testSuite = goog.require('goog.testing.testSuite'); + /** @type {t.StringOrNumber} */ var x = 1; -function testNothing() { - // Nothing to do. Just making sure the typedef above is understood. -} +testSuite({ + testNothing() { + // Nothing to do. Just making sure the typedef above is understood. + } +});