1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/modules/export-declaration.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,298 @@ 1.4 +load(libdir + "match.js"); 1.5 +load(libdir + "asserts.js"); 1.6 + 1.7 +var { Pattern, MatchError } = Match; 1.8 + 1.9 +program = (elts) => Pattern({ 1.10 + type: "Program", 1.11 + body: elts 1.12 +}) 1.13 +exportDeclaration = (declaration, specifiers, source) => Pattern({ 1.14 + type: "ExportDeclaration", 1.15 + declaration: declaration, 1.16 + specifiers: specifiers, 1.17 + source: source 1.18 +}); 1.19 +exportSpecifier = (id, name) => Pattern({ 1.20 + type: "ExportSpecifier", 1.21 + id: id, 1.22 + name: name 1.23 +}); 1.24 +exportBatchSpecifier = () => Pattern({ 1.25 + type: "ExportBatchSpecifier" 1.26 +}); 1.27 +blockStatement = (body) => Pattern({ 1.28 + type: "BlockStatement", 1.29 + body: body 1.30 +}); 1.31 +functionDeclaration = (id, params, body) => Pattern({ 1.32 + type: "FunctionDeclaration", 1.33 + id: id, 1.34 + params: params, 1.35 + defaults: [], 1.36 + body: body, 1.37 + rest: null, 1.38 + generator: false 1.39 +}); 1.40 +variableDeclaration = (decls) => Pattern({ 1.41 + type: "VariableDeclaration", 1.42 + kind: "var", 1.43 + declarations: decls 1.44 +}); 1.45 +constDeclaration = (decls) => Pattern({ 1.46 + type: "VariableDeclaration", 1.47 + kind: "const", 1.48 + declarations: decls 1.49 +}); 1.50 +letDeclaration = (decls) => Pattern({ 1.51 + type: "VariableDeclaration", 1.52 + kind: "let", 1.53 + declarations: decls 1.54 +}); 1.55 +ident = (name) => Pattern({ 1.56 + type: "Identifier", 1.57 + name: name 1.58 +}); 1.59 +lit = (val) => Pattern({ 1.60 + type: "Literal", 1.61 + value: val 1.62 +}); 1.63 + 1.64 +program([ 1.65 + exportDeclaration( 1.66 + null, 1.67 + [], 1.68 + null 1.69 + ) 1.70 +]).assert(Reflect.parse("export {}")); 1.71 + 1.72 +program([ 1.73 + exportDeclaration( 1.74 + null, 1.75 + [ 1.76 + exportSpecifier( 1.77 + ident("a"), 1.78 + ident("a") 1.79 + ) 1.80 + ], 1.81 + null 1.82 + ) 1.83 +]).assert(Reflect.parse("export { a }")); 1.84 + 1.85 +program([ 1.86 + exportDeclaration( 1.87 + null, 1.88 + [ 1.89 + exportSpecifier( 1.90 + ident("a"), 1.91 + ident("b") 1.92 + ) 1.93 + ], 1.94 + null 1.95 + ) 1.96 +]).assert(Reflect.parse("export { a as b }")); 1.97 + 1.98 +program([ 1.99 + exportDeclaration( 1.100 + null, 1.101 + [ 1.102 + exportSpecifier( 1.103 + ident("as"), 1.104 + ident("as") 1.105 + ) 1.106 + ], 1.107 + null 1.108 + ) 1.109 +]).assert(Reflect.parse("export { as as as }")); 1.110 + 1.111 +program([ 1.112 + exportDeclaration( 1.113 + null, 1.114 + [ 1.115 + exportSpecifier( 1.116 + ident("a"), 1.117 + ident("true") 1.118 + ) 1.119 + ], 1.120 + null 1.121 + ) 1.122 +]).assert(Reflect.parse("export { a as true }")); 1.123 + 1.124 +program([ 1.125 + exportDeclaration( 1.126 + null, 1.127 + [ 1.128 + exportSpecifier( 1.129 + ident("a"), 1.130 + ident("a") 1.131 + ), 1.132 + exportSpecifier( 1.133 + ident("b"), 1.134 + ident("b") 1.135 + ), 1.136 + ], 1.137 + null 1.138 + ) 1.139 +]).assert(Reflect.parse("export { a, b }")); 1.140 + 1.141 +program([ 1.142 + exportDeclaration( 1.143 + null, 1.144 + [ 1.145 + exportSpecifier( 1.146 + ident("a"), 1.147 + ident("b") 1.148 + ), 1.149 + exportSpecifier( 1.150 + ident("c"), 1.151 + ident("d") 1.152 + ), 1.153 + ], 1.154 + null 1.155 + ) 1.156 +]).assert(Reflect.parse("export { a as b, c as d }")); 1.157 + 1.158 +program([ 1.159 + exportDeclaration( 1.160 + null, 1.161 + [ 1.162 + exportBatchSpecifier() 1.163 + ], 1.164 + null 1.165 + ) 1.166 +]).assert(Reflect.parse("export *")); 1.167 + 1.168 +program([ 1.169 + exportDeclaration( 1.170 + null, 1.171 + [ 1.172 + exportBatchSpecifier() 1.173 + ], 1.174 + lit("a") 1.175 + ) 1.176 +]).assert(Reflect.parse("export * from 'a'")); 1.177 + 1.178 +program([ 1.179 + exportDeclaration( 1.180 + functionDeclaration( 1.181 + ident("f"), 1.182 + [], 1.183 + blockStatement([]) 1.184 + ), 1.185 + null, 1.186 + null 1.187 + ) 1.188 +]).assert(Reflect.parse("export function f() {}")); 1.189 + 1.190 +program([ 1.191 + exportDeclaration( 1.192 + variableDeclaration([ 1.193 + { 1.194 + id: ident("a"), 1.195 + init: lit(1) 1.196 + }, { 1.197 + id: ident("b"), 1.198 + init: lit(2) 1.199 + } 1.200 + ]), 1.201 + null, 1.202 + null 1.203 + ) 1.204 +]).assert(Reflect.parse("export var a = 1, b = 2;")); 1.205 + 1.206 +program([ 1.207 + exportDeclaration( 1.208 + constDeclaration([ 1.209 + { 1.210 + id: ident("a"), 1.211 + init: lit(1) 1.212 + }, { 1.213 + id: ident("b"), 1.214 + init: lit(2) 1.215 + } 1.216 + ]), 1.217 + null, 1.218 + null 1.219 + ) 1.220 +]).assert(Reflect.parse("export const a = 1, b = 2;")); 1.221 + 1.222 +// FIXME: In scripts, top level lets are converted back to vars. Fix this when 1.223 +// we implement compiling scripts as modules. 1.224 +program([ 1.225 + exportDeclaration( 1.226 + variableDeclaration([ 1.227 + { 1.228 + id: ident("a"), 1.229 + init: lit(1) 1.230 + }, { 1.231 + id: ident("b"), 1.232 + init: lit(2) 1.233 + } 1.234 + ]), 1.235 + null, 1.236 + null 1.237 + ) 1.238 +]).assert(Reflect.parse("export let a = 1, b = 2;")); 1.239 + 1.240 +// NOTE: binding lists are treated as if they were let declarations by esprima, 1.241 +// so we follow that convention. 1.242 +program([ 1.243 + exportDeclaration( 1.244 + variableDeclaration([ 1.245 + { 1.246 + id: ident("a"), 1.247 + init: lit(1) 1.248 + }, { 1.249 + id: ident("b"), 1.250 + init: lit(2) 1.251 + } 1.252 + ]), 1.253 + null, 1.254 + null 1.255 + ) 1.256 +]).assert(Reflect.parse("export a = 1, b = 2;")); 1.257 + 1.258 +var loc = Reflect.parse("export { a as b } from 'c'", { 1.259 + loc: true 1.260 +}).body[0].loc; 1.261 + 1.262 +assertEq(loc.start.line, 1); 1.263 +assertEq(loc.start.column, 0); 1.264 +assertEq(loc.start.line, 1); 1.265 +assertEq(loc.end.column, 26); 1.266 + 1.267 +assertThrowsInstanceOf(function () { 1.268 + Reflect.parse("function f() { export a }"); 1.269 +}, SyntaxError); 1.270 + 1.271 +assertThrowsInstanceOf(function () { 1.272 + Reflect.parse("if (true) export a"); 1.273 +}, SyntaxError); 1.274 + 1.275 +assertThrowsInstanceOf(function() { 1.276 + Reflect.parse("export {"); 1.277 +}, SyntaxError); 1.278 + 1.279 +assertThrowsInstanceOf(function() { 1.280 + Reflect.parse("export {} from"); 1.281 +}, SyntaxError); 1.282 + 1.283 +assertThrowsInstanceOf(function() { 1.284 + Reflect.parse("export {,} from 'a'"); 1.285 +}, SyntaxError); 1.286 + 1.287 +assertThrowsInstanceOf(function() { 1.288 + Reflect.parse("export { true as a } from 'b'"); 1.289 +}, SyntaxError); 1.290 + 1.291 +assertThrowsInstanceOf(function () { 1.292 + Reflect.parse("export { a } from 'b' f();"); 1.293 +}, SyntaxError); 1.294 + 1.295 +assertThrowsInstanceOf(function () { 1.296 + Reflect.parse("export * from 'b' f();"); 1.297 +}, SyntaxError); 1.298 + 1.299 +assertThrowsInstanceOf(function() { 1.300 + Reflect.parse("export {}\nfrom ()"); 1.301 +}, SyntaxError);