michael@0: load(libdir + "match.js"); michael@0: load(libdir + "asserts.js"); michael@0: michael@0: var { Pattern, MatchError } = Match; michael@0: michael@0: program = (elts) => Pattern({ michael@0: type: "Program", michael@0: body: elts michael@0: }) michael@0: importDeclaration = (specifiers, source) => Pattern({ michael@0: type: "ImportDeclaration", michael@0: specifiers: specifiers, michael@0: source: source michael@0: }); michael@0: importSpecifier = (id, name) => Pattern({ michael@0: type: "ImportSpecifier", michael@0: id: id, michael@0: name: name michael@0: }); michael@0: ident = (name) => Pattern({ michael@0: type: "Identifier", michael@0: name: name michael@0: }) michael@0: lit = (val) => Pattern({ michael@0: type: "Literal", michael@0: value: val michael@0: }) michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("default"), michael@0: ident("a") michael@0: ) michael@0: ], michael@0: lit("b") michael@0: ) michael@0: ]).assert(Reflect.parse("import a from 'b'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [], michael@0: lit("a") michael@0: ) michael@0: ]).assert(Reflect.parse("import {} from 'a'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("a"), michael@0: ident("a") michael@0: ) michael@0: ], michael@0: lit("b") michael@0: ) michael@0: ]).assert(Reflect.parse("import { a } from 'b'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("a"), michael@0: ident("a") michael@0: ) michael@0: ], michael@0: lit("b") michael@0: ) michael@0: ]).assert(Reflect.parse("import { a, } from 'b'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("a"), michael@0: ident("b") michael@0: ) michael@0: ], michael@0: lit("c") michael@0: ) michael@0: ]).assert(Reflect.parse("import { a as b } from 'c'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("as"), michael@0: ident("as") michael@0: ) michael@0: ], michael@0: lit("a") michael@0: ) michael@0: ]).assert(Reflect.parse("import { as as as } from 'a'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("true"), michael@0: ident("a") michael@0: ) michael@0: ], michael@0: lit("b") michael@0: ) michael@0: ]).assert(Reflect.parse("import { true as a } from 'b'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("a"), michael@0: ident("a") michael@0: ), michael@0: importSpecifier( michael@0: ident("b"), michael@0: ident("b") michael@0: ), michael@0: ], michael@0: lit("c") michael@0: ) michael@0: ]).assert(Reflect.parse("import { a, b } from 'c'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [ michael@0: importSpecifier( michael@0: ident("a"), michael@0: ident("b") michael@0: ), michael@0: importSpecifier( michael@0: ident("c"), michael@0: ident("d") michael@0: ), michael@0: ], michael@0: lit("e") michael@0: ) michael@0: ]).assert(Reflect.parse("import { a as b, c as d } from 'e'")); michael@0: michael@0: program([ michael@0: importDeclaration( michael@0: [], michael@0: lit("a") michael@0: ) michael@0: ]).assert(Reflect.parse("import 'a'")); michael@0: michael@0: var loc = Reflect.parse("import { a as b } from 'c'", { michael@0: loc: true michael@0: }).body[0].loc; michael@0: michael@0: assertEq(loc.start.line, 1); michael@0: assertEq(loc.start.column, 0); michael@0: assertEq(loc.start.line, 1); michael@0: assertEq(loc.end.column, 26); michael@0: michael@0: assertThrowsInstanceOf(function () { michael@0: Reflect.parse("function f() { import a from 'b' }"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function () { michael@0: Reflect.parse("if (true) import a from 'b'"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import {"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import {}"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import {} from"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import {,} from 'a'"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import { a as true } from 'b'"); michael@0: }, SyntaxError); michael@0: michael@0: assertThrowsInstanceOf(function() { michael@0: Reflect.parse("import { true } from 'a'"); michael@0: }, SyntaxError);