Skip to content

Commit c80f327

Browse files
Restrict PATH_ONLY mode imports to files - don't attempt to use Node's directory loading algorithm.
1 parent 28060bb commit c80f327

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

src/com/google/javascript/jscomp/Compiler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,9 @@ Node parseInputs() {
14831483
this.moduleLoader = new ModuleLoader(this, options.moduleRoots, inputs, ModuleLoader.PathResolver.RELATIVE,
14841484
options.moduleResolutionMode);
14851485

1486-
this.moduleLoader.setPackageJsonMainEntries(processJsonInputs(inputs));
1486+
if (options.moduleResolutionMode == ModuleLoader.ResolutionMode.NODE) {
1487+
this.moduleLoader.setPackageJsonMainEntries(processJsonInputs(inputs));
1488+
}
14871489

14881490
if (options.lowerFromEs6()) {
14891491
processEs6Modules();

src/com/google/javascript/jscomp/deps/ModuleLoader.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void setPackageJsonMainEntries(Map<String, String> packageJsonMainEntries
125125
*/
126126
public class ModulePath {
127127
private final String path;
128+
private final String[] fileExtensionsToSearch = {"", ".js", ".json"};
128129

129130
private ModulePath(String path) {
130131
this.path = path;
@@ -160,13 +161,17 @@ public String toModuleName() {
160161
* @return The normalized module URI, or {@code null} if not found.
161162
*/
162163
public ModulePath resolveJsModule(String moduleAddress) {
163-
String loadAddress;
164+
String loadAddress = null;
164165

165166
// * the immediate name require'd
166-
if (resolutionMode == ResolutionMode.PATH_ONLY || isAbsoluteIdentifier(moduleAddress) ||
167+
if (isAbsoluteIdentifier(moduleAddress) ||
167168
isRelativeIdentifier(moduleAddress)) {
168-
loadAddress = resolveJsModuleFileOrDirectory(moduleAddress);
169-
} else {
169+
if (resolutionMode == ResolutionMode.PATH_ONLY) {
170+
loadAddress = resolveJsModuleFile(moduleAddress);
171+
} else {
172+
loadAddress = resolveJsModuleFileOrDirectory(moduleAddress);
173+
}
174+
} else if (resolutionMode != ResolutionMode.PATH_ONLY) {
170175
loadAddress = resolveJsModuleFromRegistry(moduleAddress);
171176
}
172177
if (loadAddress != null) {
@@ -185,11 +190,13 @@ public ModulePath resolveCommonJsModule(String requireName) {
185190
}
186191

187192
private String resolveJsModuleFile(String moduleAddress) {
188-
String[] extensions = {"", ".js", ".json"};
189-
190193
// Load as a file
191-
for (int i = 0; i < extensions.length; i++) {
192-
String loadAddress = locate(moduleAddress + extensions[i]);
194+
for (int i = 0; i < fileExtensionsToSearch.length; i++) {
195+
if (resolutionMode == ResolutionMode.PATH_ONLY && fileExtensionsToSearch[i].equals(".json")) {
196+
continue;
197+
}
198+
199+
String loadAddress = locate(moduleAddress + fileExtensionsToSearch[i]);
193200
if (loadAddress != null) {
194201
return loadAddress;
195202
}

test/com/google/javascript/jscomp/IntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ public void testES6Modules() {
618618
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
619619
test(options,
620620
new String[] {
621-
"import {x} from 'i1'; alert(x);",
621+
"import {x} from './i1'; alert(x);",
622622
"export var x = 5;",
623623
},
624624
new String[] {
@@ -637,7 +637,7 @@ public void testES6Modules_missing() {
637637
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
638638
test(options,
639639
new String[] {
640-
"import {x} from 'i2'; alert(x);",
640+
"import {x} from './i2'; alert(x);",
641641
"export var x = 5;",
642642
},
643643
ModuleLoader.LOAD_WARNING);

test/com/google/javascript/jscomp/ProcessEs6ModulesTest.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,40 @@ private void testModules(String input, DiagnosticType error) {
106106

107107
public void testImport() {
108108
testModules(
109-
"import name from 'other'; use(name);",
109+
"import name from './other'; use(name);",
110110
"goog.require('module$other'); use(module$other.default);");
111111

112-
testModules("import {n as name} from 'other';", "goog.require('module$other');");
112+
testModules("import {n as name} from './other';", "goog.require('module$other');");
113113

114114
testModules(
115-
"import x, {f as foo, b as bar} from 'other'; use(x);",
115+
"import x, {f as foo, b as bar} from './other'; use(x);",
116116
"goog.require('module$other'); use(module$other.default);");
117117

118118
testModules(
119-
"import {default as name} from 'other'; use(name);",
119+
"import {default as name} from './other'; use(name);",
120120
"goog.require('module$other'); use(module$other.default);");
121121

122122
testModules(
123-
"import {class as name} from 'other'; use(name);",
123+
"import {class as name} from './other'; use(name);",
124124
"goog.require('module$other'); use(module$other.class);");
125125
}
126126

127127
public void testImport_missing() {
128128
setExpectParseWarningsThisTest(); // JSC_ES6_MODULE_LOAD_WARNING
129129
testModules(
130-
"import name from 'does_not_exist'; use(name);",
130+
"import name from './does_not_exist'; use(name);",
131131
"goog.require('module$does_not_exist'); use(module$does_not_exist.default);");
132132
}
133133

134134
public void testImportStar() {
135135
testModules(
136-
"import * as name from 'other'; use(name.foo);",
136+
"import * as name from './other'; use(name.foo);",
137137
"goog.require('module$other'); use(module$other.foo)");
138138
}
139139

140140
public void testTypeNodeRewriting() {
141141
testModules(
142-
"import * as name from 'other'; /** @type {name.foo} */ var x;",
142+
"import * as name from './other'; /** @type {name.foo} */ var x;",
143143
"goog.require('module$other');"
144144
+ "/** @type {module$other.foo} */ var x$$module$testcode;");
145145
}
@@ -244,7 +244,7 @@ public void testExportWithJsDoc() {
244244

245245
public void testImportAndExport() {
246246
testModules(
247-
LINE_JOINER.join("import {name as n} from 'other';", "use(n);", "export {n as name};"),
247+
LINE_JOINER.join("import {name as n} from './other';", "use(n);", "export {n as name};"),
248248
LINE_JOINER.join(
249249
"goog.provide('module$testcode');",
250250
"goog.require('module$other');",
@@ -255,9 +255,9 @@ public void testImportAndExport() {
255255
public void testExportFrom() {
256256
testModules(
257257
LINE_JOINER.join(
258-
"export {name} from 'other';",
259-
"export {default} from 'other';",
260-
"export {class} from 'other';"),
258+
"export {name} from './other';",
259+
"export {default} from './other';",
260+
"export {class} from './other';"),
261261
LINE_JOINER.join(
262262
"goog.provide('module$testcode');",
263263
"goog.require('module$other');",
@@ -266,7 +266,7 @@ public void testExportFrom() {
266266
"module$testcode.class = module$other.class;"));
267267

268268
testModules(
269-
"export {a, b as c, d} from 'other';",
269+
"export {a, b as c, d} from './other';",
270270
LINE_JOINER.join(
271271
"goog.provide('module$testcode');",
272272
"goog.require('module$other');",
@@ -275,7 +275,7 @@ public void testExportFrom() {
275275
"module$testcode.d = module$other.d;"));
276276

277277
testModules(
278-
"export {a as b, b as a} from 'other';",
278+
"export {a as b, b as a} from './other';",
279279
LINE_JOINER.join(
280280
"goog.provide('module$testcode');",
281281
"goog.require('module$other');",
@@ -284,9 +284,9 @@ public void testExportFrom() {
284284

285285
testModules(
286286
LINE_JOINER.join(
287-
"export {default as a} from 'other';",
288-
"export {a as a2, default as b} from 'other';",
289-
"export {class as switch} from 'other';"),
287+
"export {default as a} from './other';",
288+
"export {a as a2, default as b} from './other';",
289+
"export {class as switch} from './other';"),
290290
LINE_JOINER.join(
291291
"goog.provide('module$testcode');",
292292
"goog.require('module$other');",
@@ -348,7 +348,7 @@ public void testExportDefault_anonymous() {
348348
public void testExtendImportedClass() {
349349
testModules(
350350
LINE_JOINER.join(
351-
"import {Parent} from 'other';",
351+
"import {Parent} from './other';",
352352
"class Child extends Parent {",
353353
" /** @param {Parent} parent */",
354354
" useParent(parent) {}",
@@ -362,7 +362,7 @@ public void testExtendImportedClass() {
362362

363363
testModules(
364364
LINE_JOINER.join(
365-
"import {Parent} from 'other';",
365+
"import {Parent} from './other';",
366366
"class Child extends Parent {",
367367
" /** @param {./other.Parent} parent */",
368368
" useParent(parent) {}",
@@ -376,7 +376,7 @@ public void testExtendImportedClass() {
376376

377377
testModules(
378378
LINE_JOINER.join(
379-
"import {Parent} from 'other';",
379+
"import {Parent} from './other';",
380380
"export class Child extends Parent {",
381381
" /** @param {Parent} parent */",
382382
" useParent(parent) {}",
@@ -448,7 +448,7 @@ public void testReferenceToTypeFromOtherModule() {
448448
public void testRenameTypedef() {
449449
testModules(
450450
LINE_JOINER.join(
451-
"import 'other';", "/** @typedef {string|!Object} */", "export var UnionType;"),
451+
"import './other';", "/** @typedef {string|!Object} */", "export var UnionType;"),
452452
LINE_JOINER.join(
453453
"goog.provide('module$testcode');",
454454
"goog.require('module$other');",
@@ -461,8 +461,8 @@ public void testRenameTypedef() {
461461
public void testRenameImportedReference() {
462462
testModules(
463463
LINE_JOINER.join(
464-
"import {f} from 'other';",
465-
"import {b as bar} from 'other';",
464+
"import {f} from './other';",
465+
"import {b as bar} from './other';",
466466
"f();",
467467
"function g() {",
468468
" f();",
@@ -506,11 +506,11 @@ public void testGoogRequires_noChange() {
506506
"module$testcode.x = x$$module$testcode"));
507507

508508
testModules(
509-
"import * as s from 'other'; goog.require('foo.bar');",
509+
"import * as s from './other'; goog.require('foo.bar');",
510510
"goog.require('module$other'); goog.require('foo.bar');");
511511

512512
testModules(
513-
"goog.require('foo.bar'); import * as s from 'other';",
513+
"goog.require('foo.bar'); import * as s from './other';",
514514
"goog.require('module$other'); goog.require('foo.bar'); ");
515515
}
516516

@@ -534,14 +534,14 @@ public void testGoogRequires_rewrite() {
534534
"module$testcode.x = x$$module$testcode"));
535535

536536
testModules(
537-
"import * as s from 'other'; const bar = goog.require('foo.bar');",
537+
"import * as s from './other'; const bar = goog.require('foo.bar');",
538538
LINE_JOINER.join(
539539
"goog.require('module$other');",
540540
"goog.require('foo.bar');",
541541
"const bar$$module$testcode = foo.bar;"));
542542

543543
testModules(
544-
"const bar = goog.require('foo.bar'); import * as s from 'other';",
544+
"const bar = goog.require('foo.bar'); import * as s from './other';",
545545
LINE_JOINER.join(
546546
"goog.require('module$other');",
547547
"goog.require('foo.bar');",
@@ -558,18 +558,18 @@ public void testGoogRequires_nonConst() {
558558
LHS_OF_GOOG_REQUIRE_MUST_BE_CONST);
559559

560560
testModules(
561-
"import * as s from 'other'; var bar = goog.require('foo.bar');",
561+
"import * as s from './other'; var bar = goog.require('foo.bar');",
562562
LHS_OF_GOOG_REQUIRE_MUST_BE_CONST);
563563

564564
testModules(
565-
"var bar = goog.require('foo.bar'); import * as s from 'other';",
565+
"var bar = goog.require('foo.bar'); import * as s from './other';",
566566
LHS_OF_GOOG_REQUIRE_MUST_BE_CONST);
567567
}
568568

569569
public void testGoogRequiresDestructuring_rewrite() {
570570
testModules(
571571
LINE_JOINER.join(
572-
"import * as s from 'other';",
572+
"import * as s from './other';",
573573
"const {foo, bar} = goog.require('some.name.space');",
574574
"use(foo, bar);"),
575575
LINE_JOINER.join(
@@ -583,14 +583,14 @@ public void testGoogRequiresDestructuring_rewrite() {
583583

584584
testModules(
585585
LINE_JOINER.join(
586-
"import * as s from 'other';",
586+
"import * as s from './other';",
587587
"var {foo, bar} = goog.require('some.name.space');",
588588
"use(foo, bar);"),
589589
LHS_OF_GOOG_REQUIRE_MUST_BE_CONST);
590590

591591
testModules(
592592
LINE_JOINER.join(
593-
"import * as s from 'other';",
593+
"import * as s from './other';",
594594
"let {foo, bar} = goog.require('some.name.space');",
595595
"use(foo, bar);"),
596596
LHS_OF_GOOG_REQUIRE_MUST_BE_CONST);
@@ -628,7 +628,7 @@ public void testNamespaceImports() {
628628
public void testObjectDestructuringAndObjLitShorthand() {
629629
testModules(
630630
LINE_JOINER.join(
631-
"import {f} from 'other';",
631+
"import {f} from './other';",
632632
"const foo = 1;",
633633
"const {a, b} = f({foo});",
634634
"use(a, b);"),
@@ -643,11 +643,11 @@ public void testObjectDestructuringAndObjLitShorthand() {
643643
}
644644

645645
public void testImportWithoutReferences() {
646-
testModules("import 'other';", "goog.require('module$other');");
646+
testModules("import './other';", "goog.require('module$other');");
647647
// GitHub issue #1819: https://github.com/google/closure-compiler/issues/1819
648648
// Need to make sure the order of the goog.requires matches the order of the imports.
649649
testModules(
650-
"import 'other'; import 'yet_another';",
650+
"import './other'; import './yet_another';",
651651
"goog.require('module$other'); goog.require('module$yet_another');");
652652
}
653653

test/com/google/javascript/jscomp/RewriteJsonToModuleTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.javascript.jscomp.deps.ModuleLoader;
2122
import com.google.javascript.rhino.Node;
2223

2324
/** Unit tests for {@link RewriteJsonToModule} */
@@ -38,6 +39,7 @@ protected CompilerOptions getOptions() {
3839
CompilerOptions options = super.getOptions();
3940
// Trigger module processing after parsing.
4041
options.setProcessCommonJSModules(true);
42+
options.setModuleResolutionMode(ModuleLoader.ResolutionMode.NODE);
4143
return options;
4244
}
4345

0 commit comments

Comments
 (0)