dynamic-import-expression.js (5324B)
1 // |jit-test| 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 program([ 90 expressionStatement( 91 importCall( 92 ident("import"), 93 [ 94 ident("foo"), 95 objExpr([]) 96 ] 97 ) 98 ) 99 ]).assert(parse("import(foo, {});")); 100 101 program([ 102 expressionStatement( 103 importCall( 104 ident("import"), 105 106 [ 107 ident("foo"), 108 objExpr([ 109 property( 110 ident("assert"), 111 objExpr([] 112 )) 113 ]) 114 ] 115 116 ) 117 ) 118 ]).assert(parse("import(foo, { assert: {} });")); 119 120 program([ 121 expressionStatement( 122 importCall( 123 ident("import"), 124 [ 125 ident("foo"), 126 objExpr([ 127 property( 128 ident("assert"), 129 objExpr([ 130 property( 131 ident("type"), 132 lit('json') 133 ) 134 ] 135 )) 136 ]) 137 ] 138 ) 139 ) 140 ]).assert(parse("import(foo, { assert: { type: 'json' } });")); 141 142 program([ 143 expressionStatement( 144 importCall( 145 ident("import"), 146 [ 147 ident("foo"), 148 objExpr([ 149 property( 150 ident("assert"), 151 objExpr([ 152 property( 153 ident("type"), 154 lit('json') 155 ), 156 property( 157 ident("foo"), 158 lit('bar') 159 ) 160 ] 161 )) 162 ]) 163 ] 164 ) 165 ) 166 ]).assert(parse("import(foo, { assert: { type: 'json', foo: 'bar' } });")); 167 168 program([ 169 expressionStatement( 170 importCall( 171 ident("import"), 172 [ 173 ident("foo"), 174 objExpr([ 175 property( 176 ident("assert"), 177 objExpr([ 178 property( 179 ident("type"), 180 callExpression(ident('getType'), []) 181 ) 182 ] 183 )) 184 ]) 185 ] 186 ) 187 ) 188 ]).assert(parse("import(foo, { assert: { type: getType() } });")); 189 } 190 191 function assertParseThrowsSyntaxError(source) 192 { 193 assertThrowsInstanceOf(() => parseAsClassicScript(source), SyntaxError); 194 assertThrowsInstanceOf(() => parseAsModuleScript(source), SyntaxError); 195 } 196 197 assertParseThrowsSyntaxError("import"); 198 assertParseThrowsSyntaxError("import("); 199 assertParseThrowsSyntaxError("import(1,"); 200 assertParseThrowsSyntaxError("x = import"); 201 assertParseThrowsSyntaxError("x = import("); 202 assertParseThrowsSyntaxError("x = import(1,"); 203 assertParseThrowsSyntaxError("x = import(1, 2"); 204 assertParseThrowsSyntaxError("import(1, 2, 3)");