| 1 |
load(libdir + "match.js"); |
| 2 |
load(libdir + "asserts.js"); |
| 3 |
|
| 4 |
var { Pattern, MatchError } = Match; |
| 5 |
|
| 6 |
program = (elts) => Pattern({ |
| 7 |
type: "Program", |
| 8 |
body: elts |
| 9 |
}); |
| 10 |
expressionStatement = (expression) => Pattern({ |
| 11 |
type: "ExpressionStatement", |
| 12 |
expression: expression |
| 13 |
}); |
| 14 |
assignmentExpression = (left, operator, right) => Pattern({ |
| 15 |
type: "AssignmentExpression", |
| 16 |
operator: operator, |
| 17 |
left: left, |
| 18 |
right: right |
| 19 |
}); |
| 20 |
ident = (name) => Pattern({ |
| 21 |
type: "Identifier", |
| 22 |
name: name |
| 23 |
}); |
| 24 |
metaProperty = (meta, property) => Pattern({ |
| 25 |
type: "MetaProperty", |
| 26 |
meta: meta, |
| 27 |
property: property |
| 28 |
}); |
| 29 |
memberExpression = (object, property) => Pattern({ |
| 30 |
type: "MemberExpression", |
| 31 |
object: object, |
| 32 |
property: property |
| 33 |
}); |
| 34 |
|
| 35 |
function parseAsModule(source) |
| 36 |
{ |
| 37 |
return Reflect.parse(source, {target: "module"}); |
| 38 |
} |
| 39 |
|
| 40 |
program([ |
| 41 |
expressionStatement( |
| 42 |
metaProperty( |
| 43 |
ident("import"), |
| 44 |
ident("meta") |
| 45 |
) |
| 46 |
) |
| 47 |
]).assert(parseAsModule("import.meta;")); |
| 48 |
|
| 49 |
program([ |
| 50 |
expressionStatement( |
| 51 |
assignmentExpression( |
| 52 |
ident("x"), |
| 53 |
"=", |
| 54 |
metaProperty( |
| 55 |
ident("import"), |
| 56 |
ident("meta") |
| 57 |
) |
| 58 |
) |
| 59 |
) |
| 60 |
]).assert(parseAsModule("x = import.meta;")); |
| 61 |
|
| 62 |
program([ |
| 63 |
expressionStatement( |
| 64 |
assignmentExpression( |
| 65 |
memberExpression( |
| 66 |
metaProperty( |
| 67 |
ident("import"), |
| 68 |
ident("meta") |
| 69 |
), |
| 70 |
ident("foo") |
| 71 |
), |
| 72 |
"=", |
| 73 |
ident("x"), |
| 74 |
) |
| 75 |
) |
| 76 |
]).assert(parseAsModule("import.meta.foo = x;")); |
| 77 |
|
| 78 |
function assertModuleParseThrowsSyntaxError(source) |
| 79 |
{ |
| 80 |
assertThrowsInstanceOf(() => parseAsModule(source), SyntaxError); |
| 81 |
} |
| 82 |
|
| 83 |
assertModuleParseThrowsSyntaxError("import"); |
| 84 |
assertModuleParseThrowsSyntaxError("import."); |
| 85 |
assertModuleParseThrowsSyntaxError("import.met"); |
| 86 |
assertModuleParseThrowsSyntaxError("import.metaa"); |
| 87 |
assertModuleParseThrowsSyntaxError("x = import"); |
| 88 |
assertModuleParseThrowsSyntaxError("x = import."); |
| 89 |
assertModuleParseThrowsSyntaxError("x = import.met"); |
| 90 |
assertModuleParseThrowsSyntaxError("x = import.metaa"); |
| 91 |
assertModuleParseThrowsSyntaxError("import.meta = x"); |