-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathImportMeta.js
112 lines (106 loc) · 4.52 KB
/
ImportMeta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
var tests = [
{
name: "Shape of import.meta object",
body: function() {
WScript.RegisterModuleSource('t1.js', `
assert.areEqual('object', typeof import.meta, 'typeof import.meta === "object"');
import.meta.prop = 'prop';
assert.areEqual('prop', import.meta.prop, 'import.meta is mutable');
`);
WScript.LoadScriptFile('t1.js', 'module');
}
},
{
name: "Each module has a unique import.meta object",
body: function() {
WScript.RegisterModuleSource('t2.js', `
export function getImportMeta() {
return import.meta;
}
`);
WScript.RegisterModuleSource('t3.js', `
export let getImportMetaArrow = () => import.meta;
`);
WScript.RegisterModuleSource('t4.js', `
export let importmeta = import.meta;
`);
WScript.RegisterModuleSource('t5.js', `
function foo() {
return import.meta;
}
assert.areEqual(import.meta, foo(), "Each module has a unique import.meta object");
import { getImportMeta } from 't2.js';
let t2_import_meta = getImportMeta();
import { getImportMetaArrow } from 't3.js';
let t3_import_meta = getImportMetaArrow();
import { importmeta as t4_import_meta } from 't4.js';
assert.areEqual('object', typeof t2_import_meta, 'typeof t2_import_meta === "object"');
assert.isTrue(t2_import_meta !== import.meta, "t2_import_meta !== import.meta");
assert.areEqual('object', typeof t3_import_meta, 'typeof t3_import_meta === "object"');
assert.isTrue(t3_import_meta !== import.meta, "t3_import_meta !== import.meta");
assert.areEqual('object', typeof t4_import_meta, 'typeof t4_import_meta === "object"');
assert.isTrue(t4_import_meta !== import.meta, "t4_import_meta !== import.meta");
`);
WScript.LoadScriptFile('t5.js', 'module');
}
},
{
name: "The only metaproperty on import is meta",
body: function() {
WScript.RegisterModuleSource('t6.js', `
function foo() {
return import.notmeta;
}
`);
assert.throws(()=>WScript.LoadScriptFile('t6.js', 'module'));
}
},
{
name: "Non-module goal symbol throws early syntax error",
body: function() {
assert.throws(()=>WScript.LoadScript('import.meta', 'samethread'));
assert.throws(()=>WScript.LoadScript('function foo() { return import.meta; }', 'samethread'));
}
},
{
name: "import.meta is not an L value",
body: function() {
WScript.RegisterModuleSource('t7.js', `
import.meta = {};
`);
WScript.RegisterModuleSource('t8.js', `
import.meta++;
`);
assert.throws(()=>WScript.LoadScriptFile('t7.js', 'module'));
assert.throws(()=>WScript.LoadScriptFile('t8.js', 'module'));
}
},
{
name: "import.meta is not valid inside eval",
body: function() {
WScript.RegisterModuleSource('t9.js', `
assert.throws(()=>eval('import.meta'));
`);
WScript.LoadScriptFile('t9.js', 'module');
}
},
{
name: "import.meta is not valid when located in parsed Function body or param list",
body: function() {
WScript.RegisterModuleSource('t10.js', `
assert.throws(()=>Function('import.meta'));
`);
WScript.LoadScriptFile('t10.js', 'module');
WScript.RegisterModuleSource('t11.js', `
assert.throws(()=>Function('a = import.meta', ''));
`);
WScript.LoadScriptFile('t11.js', 'module');
}
},
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });