| 1 |
// |jit-test| --enable-import-attributes |
| 2 |
|
| 3 |
load(libdir + "match.js"); |
| 4 |
load(libdir + "asserts.js"); |
| 5 |
|
| 6 |
var { Pattern, MatchError } = Match; |
| 7 |
|
| 8 |
program = (elts) => Pattern({ |
| 9 |
type: "Program", |
| 10 |
body: elts |
| 11 |
}); |
| 12 |
expressionStatement = (expression) => Pattern({ |
| 13 |
type: "ExpressionStatement", |
| 14 |
expression: expression |
| 15 |
}); |
| 16 |
assignmentExpression = (left, operator, right) => Pattern({ |
| 17 |
type: "AssignmentExpression", |
| 18 |
operator: operator, |
| 19 |
left: left, |
| 20 |
right: right |
| 21 |
}); |
| 22 |
ident = (name) => Pattern({ |
| 23 |
type: "Identifier", |
| 24 |
name: name |
| 25 |
}); |
| 26 |
importCall = (ident, args) => Pattern({ |
| 27 |
type: "CallImport", |
| 28 |
ident: ident, |
| 29 |
arguments: args |
| 30 |
}); |
| 31 |
|
| 32 |
objExpr = (elts) => Pattern({ |
| 33 |
type: "ObjectExpression", |
| 34 |
properties: elts |
| 35 |
}); |
| 36 |
property = (key, value) => Pattern({ |
| 37 |
type: "Property", |
| 38 |
kind: "init", |
| 39 |
key: key, |
| 40 |
value: value, |
| 41 |
}); |
| 42 |
lit = (val) => Pattern({ |
| 43 |
type: "Literal", |
| 44 |
value: val |
| 45 |
}); |
| 46 |
callExpression = (callee, args) => Pattern({ |
| 47 |
type: "CallExpression", |
| 48 |
callee: callee, |
| 49 |
arguments: args |
| 50 |
}); |
| 51 |
|
| 52 |
function parseAsClassicScript(source) |
| 53 |
{ |
| 54 |
return Reflect.parse(source); |
| 55 |
} |
| 56 |
|
| 57 |
function parseAsModuleScript(source) |
| 58 |
{ |
| 59 |
return Reflect.parse(source, {target: "module"}); |
| 60 |
} |
| 61 |
|
| 62 |
for (let parse of [parseAsModuleScript, parseAsClassicScript]) { |
| 63 |
program([ |
| 64 |
expressionStatement( |
| 65 |
importCall( |
| 66 |
ident("import"), |
| 67 |
[ |
| 68 |
ident("foo") |
| 69 |
] |
| 70 |
) |
| 71 |
) |
| 72 |
]).assert(parse("import(foo);")); |
| 73 |
|
| 74 |
program([ |
| 75 |
expressionStatement( |
| 76 |
assignmentExpression( |
| 77 |
ident("x"), |
| 78 |
"=", |
| 79 |
importCall( |
| 80 |
ident("import"), |
| 81 |
[ |
| 82 |
ident("foo") |
| 83 |
] |
| 84 |
) |
| 85 |
) |
| 86 |
) |
| 87 |
]).assert(parse("x = import(foo);")); |
| 88 |
|
| 89 |
if (getRealmConfiguration("importAttributes")) { |
| 90 |
program([ |
| 91 |
expressionStatement( |
| 92 |
importCall( |
| 93 |
ident("import"), |
| 94 |
[ |
| 95 |
ident("foo"), |
| 96 |
objExpr([]) |
| 97 |
] |
| 98 |
) |
| 99 |
) |
| 100 |
]).assert(parse("import(foo, {});")); |
| 101 |
|
| 102 |
program([ |
| 103 |
expressionStatement( |
| 104 |
importCall( |
| 105 |
ident("import"), |
| 106 |
|
| 107 |
[ |
| 108 |
ident("foo"), |
| 109 |
objExpr([ |
| 110 |
property( |
| 111 |
ident("assert"), |
| 112 |
objExpr([] |
| 113 |
)) |
| 114 |
]) |
| 115 |
] |
| 116 |
|
| 117 |
) |
| 118 |
) |
| 119 |
]).assert(parse("import(foo, { assert: {} });")); |
| 120 |
|
| 121 |
program([ |
| 122 |
expressionStatement( |
| 123 |
importCall( |
| 124 |
ident("import"), |
| 125 |
[ |
| 126 |
ident("foo"), |
| 127 |
objExpr([ |
| 128 |
property( |
| 129 |
ident("assert"), |
| 130 |
objExpr([ |
| 131 |
property( |
| 132 |
ident("type"), |
| 133 |
lit('json') |
| 134 |
) |
| 135 |
] |
| 136 |
)) |
| 137 |
]) |
| 138 |
] |
| 139 |
) |
| 140 |
) |
| 141 |
]).assert(parse("import(foo, { assert: { type: 'json' } });")); |
| 142 |
|
| 143 |
program([ |
| 144 |
expressionStatement( |
| 145 |
importCall( |
| 146 |
ident("import"), |
| 147 |
[ |
| 148 |
ident("foo"), |
| 149 |
objExpr([ |
| 150 |
property( |
| 151 |
ident("assert"), |
| 152 |
objExpr([ |
| 153 |
property( |
| 154 |
ident("type"), |
| 155 |
lit('json') |
| 156 |
), |
| 157 |
property( |
| 158 |
ident("foo"), |
| 159 |
lit('bar') |
| 160 |
) |
| 161 |
] |
| 162 |
)) |
| 163 |
]) |
| 164 |
] |
| 165 |
) |
| 166 |
) |
| 167 |
]).assert(parse("import(foo, { assert: { type: 'json', foo: 'bar' } });")); |
| 168 |
|
| 169 |
program([ |
| 170 |
expressionStatement( |
| 171 |
importCall( |
| 172 |
ident("import"), |
| 173 |
[ |
| 174 |
ident("foo"), |
| 175 |
objExpr([ |
| 176 |
property( |
| 177 |
ident("assert"), |
| 178 |
objExpr([ |
| 179 |
property( |
| 180 |
ident("type"), |
| 181 |
callExpression(ident('getType'), []) |
| 182 |
) |
| 183 |
] |
| 184 |
)) |
| 185 |
]) |
| 186 |
] |
| 187 |
) |
| 188 |
) |
| 189 |
]).assert(parse("import(foo, { assert: { type: getType() } });")); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
function assertParseThrowsSyntaxError(source) |
| 194 |
{ |
| 195 |
assertThrowsInstanceOf(() => parseAsClassicScript(source), SyntaxError); |
| 196 |
assertThrowsInstanceOf(() => parseAsModuleScript(source), SyntaxError); |
| 197 |
} |
| 198 |
|
| 199 |
assertParseThrowsSyntaxError("import"); |
| 200 |
assertParseThrowsSyntaxError("import("); |
| 201 |
assertParseThrowsSyntaxError("import(1,"); |
| 202 |
assertParseThrowsSyntaxError("x = import"); |
| 203 |
assertParseThrowsSyntaxError("x = import("); |
| 204 |
assertParseThrowsSyntaxError("x = import(1,"); |
| 205 |
assertParseThrowsSyntaxError("x = import(1, 2"); |
| 206 |
|
| 207 |
if (!getRealmConfiguration("importAttributes")) { |
| 208 |
assertParseThrowsSyntaxError("import(1, 2"); |
| 209 |
assertParseThrowsSyntaxError("import(1, 2)"); |
| 210 |
assertParseThrowsSyntaxError("x = import(1, 2)"); |
| 211 |
} |
| 212 |
else { |
| 213 |
assertParseThrowsSyntaxError("import(1, 2, 3)"); |
| 214 |
} |