Skip to content

Commit b86e018

Browse files
domenicmoz-wptsync-bot
authored andcommitted
Bug 1771008 [wpt PR 34188] - Implement import.meta.resolve(), a=testonly
Automatic update from web-platform-tests Implement import.meta.resolve() See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <kouhei@chromium.org> Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Commit-Queue: Domenic Denicola <domenic@chromium.org> Cr-Commit-Position: refs/heads/main@{#1021529} -- wpt-commits: edcc51d740e71ace04d1fe1a4ae107ea3e640c2a wpt-pr: 34188
1 parent dfbe637 commit b86e018

File tree

9 files changed

+202
-20
lines changed

9 files changed

+202
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// META: global=dedicatedworker-module,sharedworker-module,serviceworker-module
2+
3+
test(() => {
4+
assert_equals(typeof import.meta, "object");
5+
assert_not_equals(import.meta, null);
6+
}, "import.meta is an object");
7+
8+
test(() => {
9+
import.meta.newProperty = 1;
10+
assert_true(Object.isExtensible(import.meta));
11+
}, "import.meta is extensible");
12+
13+
test(() => {
14+
for (const name of Reflect.ownKeys(import.meta)) {
15+
const desc = Object.getOwnPropertyDescriptor(import.meta, name);
16+
assert_equals(desc.writable, true);
17+
assert_equals(desc.enumerable, true);
18+
assert_equals(desc.configurable, true);
19+
}
20+
}, "import.meta's properties are writable, configurable, and enumerable");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
5+
<!--
6+
More extensive tests of import maps and import.meta.resolve() will be
7+
located in the import maps test suite. This contains some basic tests plus
8+
tests some tricky parts of the import.meta.resolve() algorithm around string
9+
conversion which are only testable with import maps.
10+
-->
11+
12+
<script type="importmap">
13+
{
14+
"imports": {
15+
"bare": "https://example.com/",
16+
"https://example.com/rewrite": "https://example.com/rewritten",
17+
18+
"1": "https://example.com/PASS-1",
19+
"null": "https://example.com/PASS-null",
20+
"undefined": "https://example.com/PASS-undefined",
21+
"[object Object]": "https://example.com/PASS-object",
22+
23+
"./start": "./resources/export-1.mjs",
24+
"./resources/export-1.mjs": "./resources/export-2.mjs"
25+
}
26+
}
27+
</script>
28+
29+
<script type="module">
30+
test(() => {
31+
assert_equals(import.meta.resolve("bare"), "https://example.com/");
32+
}, "import.meta.resolve() given an import mapped bare specifier");
33+
34+
test(() => {
35+
assert_equals(import.meta.resolve("https://example.com/rewrite"), "https://example.com/rewritten");
36+
}, "import.meta.resolve() given an import mapped URL-like specifier");
37+
38+
test(() => {
39+
assert_equals(import.meta.resolve(), "https://example.com/PASS-undefined", "no-arg case");
40+
41+
assert_equals(import.meta.resolve(1), "https://example.com/PASS-1");
42+
assert_equals(import.meta.resolve(null), "https://example.com/PASS-null");
43+
assert_equals(import.meta.resolve(undefined), "https://example.com/PASS-undefined");
44+
45+
// Only toString() methods are consulted by ToString, not valueOf() ones.
46+
// So this becomes "[object Object]".
47+
assert_equals(import.meta.resolve({ valueOf() { return "./x"; } }), "https://example.com/PASS-object");
48+
}, "Testing the ToString() step of import.meta.resolve() via import maps");
49+
50+
promise_test(async () => {
51+
const one = (await import("./start")).default;
52+
assert_equals(one, 1);
53+
54+
const two = (await import(import.meta.resolve("./start"))).default;
55+
assert_equals(two, 2);
56+
}, "import(import.meta.resolve(x)) can be different from import(x)");
57+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
5+
<iframe src="resources/store-import-meta.html"></iframe>
6+
7+
<script type="module">
8+
import * as otherImportMeta from "./resources/export-import-meta.mjs";
9+
setup({ explicit_done: true });
10+
11+
window.onload = () => {
12+
test(() => {
13+
assert_not_equals(frames[0].importMetaURL, import.meta.url,
14+
"Precondition check: we've set things up so that the other script has a different import.meta.url");
15+
16+
const expected = (new URL("resources/x", location.href)).href;
17+
assert_equals(frames[0].importMetaResolve("./x"), expected);
18+
}, "import.meta.resolve resolves URLs relative to the import.meta.url, not relative to the active script when it is called: another global's inline script");
19+
20+
test(() => {
21+
const otherFrameImportMetaResolve = frames[0].importMetaResolve;
22+
23+
document.querySelector("iframe").remove();
24+
25+
const expected = (new URL("resources/x", location.href)).href;
26+
assert_equals(otherFrameImportMetaResolve("./x"), expected);
27+
}, "import.meta.resolve still works if its global has been destroyed (by detaching the iframe)");
28+
29+
test(() => {
30+
assert_not_equals(otherImportMeta.url, import.meta.url,
31+
"Precondition check: we've set things up so that the other script has a different import.meta.url");
32+
33+
const expected = (new URL("resources/x", location.href)).href;
34+
assert_equals(otherImportMeta.resolve("./x"), expected);
35+
}, "import.meta.resolve resolves URLs relative to the import.meta.url, not relative to the active script when it is called: another module script");
36+
37+
done();
38+
};
39+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// META: global=dedicatedworker-module,sharedworker-module,serviceworker-module
2+
3+
import { importMetaOnRootModule, importMetaOnDependentModule }
4+
from "./import-meta-root.js";
5+
6+
test(() => {
7+
assert_equals(typeof import.meta.resolve, "function");
8+
assert_equals(import.meta.resolve.name, "resolve");
9+
assert_equals(import.meta.resolve.length, 1);
10+
assert_equals(Object.getPrototypeOf(import.meta.resolve), Function.prototype);
11+
}, "import.meta.resolve is a function with the right properties");
12+
13+
test(() => {
14+
assert_false(isConstructor(import.meta.resolve));
15+
16+
assert_throws_js(TypeError, () => new import.meta.resolve("./x"));
17+
}, "import.meta.resolve is not a constructor");
18+
19+
test(() => {
20+
// See also tests in ./import-meta-resolve-importmap.html.
21+
22+
assert_equals(import.meta.resolve({ toString() { return "./x"; } }), resolveURL("x"));
23+
assert_throws_js(TypeError, () => import.meta.resolve(Symbol("./x")),
24+
"symbol");
25+
assert_throws_js(TypeError, () => import.meta.resolve(),
26+
"no argument (which is treated like \"undefined\")");
27+
}, "import.meta.resolve ToString()s its argument");
28+
29+
test(() => {
30+
assert_equals(import.meta.resolve("./x"), resolveURL("x"),
31+
"current module import.meta");
32+
assert_equals(importMetaOnRootModule.resolve("./x"), resolveURL("x"),
33+
"sibling module import.meta");
34+
assert_equals(importMetaOnDependentModule.resolve("./x"), resolveURL("x"),
35+
"dependency module import.meta");
36+
}, "Relative URL-like specifier resolution");
37+
38+
test(() => {
39+
assert_equals(import.meta.resolve("https://example.com/"), "https://example.com/",
40+
"current module import.meta");
41+
assert_equals(importMetaOnRootModule.resolve("https://example.com/"), "https://example.com/",
42+
"sibling module import.meta");
43+
assert_equals(importMetaOnDependentModule.resolve("https://example.com/"), "https://example.com/",
44+
"dependency module import.meta");
45+
}, "Absolute URL-like specifier resolution");
46+
47+
test(() => {
48+
const invalidSpecifiers = [
49+
"https://eggplant:b/c",
50+
"pumpkins.js",
51+
".tomato",
52+
"..zuccini.mjs",
53+
".\\yam.es"
54+
];
55+
56+
for (const specifier of invalidSpecifiers) {
57+
assert_throws_js(TypeError, () => import.meta.resolve(specifier), specifier);
58+
}
59+
}, "Invalid module specifiers");
60+
61+
test(() => {
62+
const { resolve } = import.meta;
63+
assert_equals(resolve("https://example.com/"), "https://example.com/", "current module import.meta");
64+
}, "Works fine with no this value");
65+
66+
function resolveURL(urlRelativeToThisTest) {
67+
return (new URL(urlRelativeToThisTest, location.href)).href;
68+
}
69+
70+
function isConstructor(o) {
71+
try {
72+
new (new Proxy(o, { construct: () => ({}) }));
73+
return true;
74+
} catch {
75+
return false;
76+
}
77+
}

testing/web-platform/tests/html/semantics/scripting-1/the-script-element/module/import-meta/import-meta-url.any.js

-20
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ test(() => {
1515
base + "/import-meta-dependent.js");
1616
}, "import.meta.url in a dependent external script");
1717

18-
test(() => {
19-
assert_equals(typeof importMetaOnRootModule, "object");
20-
assert_not_equals(importMetaOnRootModule, null);
21-
}, "import.meta is an object");
22-
23-
test(() => {
24-
importMetaOnRootModule.newProperty = 1;
25-
assert_true(Object.isExtensible(importMetaOnRootModule));
26-
}, "import.meta is extensible");
27-
28-
test(() => {
29-
const names = new Set(Reflect.ownKeys(importMetaOnRootModule));
30-
for (const name of names) {
31-
var desc = Object.getOwnPropertyDescriptor(importMetaOnRootModule, name);
32-
assert_equals(desc.writable, true);
33-
assert_equals(desc.enumerable, true);
34-
assert_equals(desc.configurable, true);
35-
}
36-
}, "import.meta's properties are writable, configurable, and enumerable");
37-
3818

3919
import { importMetaOnRootModule as hashedImportMetaOnRootModule1,
4020
importMetaOnDependentModule as hashedImportMetaOnDependentModule1 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const url = import.meta.url;
2+
export const resolve = import.meta.resolve;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<script type="module">
3+
window.importMetaURL = import.meta.url;
4+
window.importMetaResolve = import.meta.resolve;
5+
</script>

0 commit comments

Comments
 (0)