parser_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. package parser
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "testing"
  7. "github.com/dop251/goja/ast"
  8. "github.com/dop251/goja/file"
  9. )
  10. func firstErr(err error) error {
  11. switch err := err.(type) {
  12. case ErrorList:
  13. return err[0]
  14. }
  15. return err
  16. }
  17. var matchBeforeAfterSeparator = regexp.MustCompile(`(?m)^[ \t]*---$`)
  18. func testParse(src string) (parser *_parser, program *ast.Program, err error) {
  19. defer func() {
  20. if tmp := recover(); tmp != nil {
  21. switch tmp := tmp.(type) {
  22. case string:
  23. if strings.HasPrefix(tmp, "SyntaxError:") {
  24. parser = nil
  25. program = nil
  26. err = errors.New(tmp)
  27. return
  28. }
  29. }
  30. panic(tmp)
  31. }
  32. }()
  33. parser = newParser("", src)
  34. program, err = parser.parse()
  35. return
  36. }
  37. func TestParseFile(t *testing.T) {
  38. tt(t, func() {
  39. _, err := ParseFile(nil, "", `/abc/`, 0)
  40. is(err, nil)
  41. _, err = ParseFile(nil, "", `/(?!def)abc/`, IgnoreRegExpErrors)
  42. is(err, nil)
  43. _, err = ParseFile(nil, "", `/(?!def)abc/; return`, IgnoreRegExpErrors)
  44. is(err, "(anonymous): Line 1:15 Illegal return statement")
  45. })
  46. }
  47. func TestParseFunction(t *testing.T) {
  48. tt(t, func() {
  49. test := func(prm, bdy string, expect interface{}) *ast.FunctionLiteral {
  50. function, err := ParseFunction(prm, bdy)
  51. is(firstErr(err), expect)
  52. return function
  53. }
  54. test("a, b,c,d", "", nil)
  55. test("a, b;,c,d", "", "(anonymous): Line 1:15 Unexpected token ;")
  56. test("this", "", "(anonymous): Line 1:11 Unexpected token this")
  57. test("a, b, c, null", "", "(anonymous): Line 1:20 Unexpected token null")
  58. test("a, b,c,d", "return;", nil)
  59. test("a, b,c,d", "break;", "(anonymous): Line 2:1 Illegal break statement")
  60. test("a, b,c,d", "{}", nil)
  61. })
  62. }
  63. func TestParserErr(t *testing.T) {
  64. tt(t, func() {
  65. test := func(input string, expect interface{}) (*ast.Program, *_parser) {
  66. parser := newParser("", input)
  67. program, err := parser.parse()
  68. is(firstErr(err), expect)
  69. return program, parser
  70. }
  71. program, parser := test("", nil)
  72. program, parser = test(`
  73. var abc;
  74. break; do {
  75. } while(true);
  76. `, "(anonymous): Line 3:9 Illegal break statement")
  77. {
  78. stmt := program.Body[1].(*ast.BadStatement)
  79. is(parser.position(stmt.From).Column, 9)
  80. is(parser.position(stmt.To).Column, 16)
  81. is(parser.slice(stmt.From, stmt.To), "break; ")
  82. }
  83. test("{", "(anonymous): Line 1:2 Unexpected end of input")
  84. test("}", "(anonymous): Line 1:1 Unexpected token }")
  85. test("3ea", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  86. test("3in", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  87. test("3in []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  88. test("3e", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  89. test("3e+", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  90. test("3e-", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  91. test("3x", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  92. test("3x0", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  93. test("0x", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  94. test("09", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  95. test("018", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  96. test("01.0", "(anonymous): Line 1:3 Unexpected number")
  97. test("01a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  98. test("0x3in[]", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  99. test("\"Hello\nWorld\"", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  100. test("\u203f = 10", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  101. test("x\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  102. test("x\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  103. test("x\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  104. test("x\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  105. test("x\\\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  106. test("/\n", "(anonymous): Line 1:1 Invalid regular expression: missing /")
  107. test("0 = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  108. test("func() = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  109. test("(1 + 1) = 2", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  110. test("1++", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  111. test("1--", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  112. test("--1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  113. test("for((1 + 1) in abc) def();", "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  114. test("[", "(anonymous): Line 1:2 Unexpected end of input")
  115. test("[,", "(anonymous): Line 1:3 Unexpected end of input")
  116. test("1 + {", "(anonymous): Line 1:6 Unexpected end of input")
  117. test("1 + { abc:abc", "(anonymous): Line 1:14 Unexpected end of input")
  118. test("1 + { abc:abc,", "(anonymous): Line 1:15 Unexpected end of input")
  119. test("var abc = /\n/", "(anonymous): Line 1:11 Invalid regular expression: missing /")
  120. test("var abc = \"\n", "(anonymous): Line 1:11 Unexpected token ILLEGAL")
  121. test("var if = 0", "(anonymous): Line 1:5 Unexpected token if")
  122. test("abc + 0 = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  123. test("+abc = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  124. test("1 + (", "(anonymous): Line 1:6 Unexpected end of input")
  125. test("\n\n\n{", "(anonymous): Line 4:2 Unexpected end of input")
  126. test("\n/* Some multiline\ncomment */\n)", "(anonymous): Line 4:1 Unexpected token )")
  127. // TODO
  128. //{ set 1 }
  129. //{ get 2 }
  130. //({ set: s(if) { } })
  131. //({ set s(.) { } })
  132. //({ set: s() { } })
  133. //({ set: s(a, b) { } })
  134. //({ get: g(d) { } })
  135. //({ get i() { }, i: 42 })
  136. //({ i: 42, get i() { } })
  137. //({ set i(x) { }, i: 42 })
  138. //({ i: 42, set i(x) { } })
  139. //({ get i() { }, get i() { } })
  140. //({ set i(x) { }, set i(x) { } })
  141. test("function abc(if) {}", "(anonymous): Line 1:14 Unexpected token if")
  142. test("function abc(true) {}", "(anonymous): Line 1:14 Unexpected token true")
  143. test("function abc(false) {}", "(anonymous): Line 1:14 Unexpected token false")
  144. test("function abc(null) {}", "(anonymous): Line 1:14 Unexpected token null")
  145. test("function null() {}", "(anonymous): Line 1:10 Unexpected token null")
  146. test("function true() {}", "(anonymous): Line 1:10 Unexpected token true")
  147. test("function false() {}", "(anonymous): Line 1:10 Unexpected token false")
  148. test("function if() {}", "(anonymous): Line 1:10 Unexpected token if")
  149. test("a b;", "(anonymous): Line 1:3 Unexpected identifier")
  150. test("if.a", "(anonymous): Line 1:3 Unexpected token .")
  151. test("a if", "(anonymous): Line 1:3 Unexpected token if")
  152. test("a class", "(anonymous): Line 1:3 Unexpected reserved word")
  153. test("break\n", "(anonymous): Line 1:1 Illegal break statement")
  154. test("break 1;", "(anonymous): Line 1:7 Unexpected number")
  155. test("for (;;) { break 1; }", "(anonymous): Line 1:18 Unexpected number")
  156. test("continue\n", "(anonymous): Line 1:1 Illegal continue statement")
  157. test("continue 1;", "(anonymous): Line 1:10 Unexpected number")
  158. test("for (;;) { continue 1; }", "(anonymous): Line 1:21 Unexpected number")
  159. test("throw", "(anonymous): Line 1:1 Unexpected end of input")
  160. test("throw;", "(anonymous): Line 1:6 Unexpected token ;")
  161. test("throw \n", "(anonymous): Line 1:1 Unexpected end of input")
  162. test("for (var abc, def in {});", "(anonymous): Line 1:19 Unexpected token in")
  163. test("for ((abc in {});;);", nil)
  164. test("for ((abc in {}));", "(anonymous): Line 1:17 Unexpected token )")
  165. test("for (+abc in {});", "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  166. test("if (false)", "(anonymous): Line 1:11 Unexpected end of input")
  167. test("if (false) abc(); else", "(anonymous): Line 1:23 Unexpected end of input")
  168. test("do", "(anonymous): Line 1:3 Unexpected end of input")
  169. test("while (false)", "(anonymous): Line 1:14 Unexpected end of input")
  170. test("for (;;)", "(anonymous): Line 1:9 Unexpected end of input")
  171. test("with (abc)", "(anonymous): Line 1:11 Unexpected end of input")
  172. test("try {}", "(anonymous): Line 1:1 Missing catch or finally after try")
  173. test("try {} catch {}", "(anonymous): Line 1:14 Unexpected token {")
  174. test("try {} catch () {}", "(anonymous): Line 1:15 Unexpected token )")
  175. test("\u203f = 1", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  176. // TODO
  177. // const x = 12, y;
  178. // const x, y = 12;
  179. // const x;
  180. // if(true) let a = 1;
  181. // if(true) const a = 1;
  182. test(`new abc()."def"`, "(anonymous): Line 1:11 Unexpected string")
  183. test("/*", "(anonymous): Line 1:3 Unexpected end of input")
  184. test("/**", "(anonymous): Line 1:4 Unexpected end of input")
  185. test("/*\n\n\n", "(anonymous): Line 4:1 Unexpected end of input")
  186. test("/*\n\n\n*", "(anonymous): Line 4:2 Unexpected end of input")
  187. test("/*abc", "(anonymous): Line 1:6 Unexpected end of input")
  188. test("/*abc *", "(anonymous): Line 1:9 Unexpected end of input")
  189. test("\n]", "(anonymous): Line 2:1 Unexpected token ]")
  190. test("\r\n]", "(anonymous): Line 2:1 Unexpected token ]")
  191. test("\n\r]", "(anonymous): Line 3:1 Unexpected token ]")
  192. test("//\r\n]", "(anonymous): Line 2:1 Unexpected token ]")
  193. test("//\n\r]", "(anonymous): Line 3:1 Unexpected token ]")
  194. test("/abc\\\n/", "(anonymous): Line 1:1 Invalid regular expression: missing /")
  195. test("//\r \n]", "(anonymous): Line 3:1 Unexpected token ]")
  196. test("/*\r\n*/]", "(anonymous): Line 2:3 Unexpected token ]")
  197. test("/*\r \n*/]", "(anonymous): Line 3:3 Unexpected token ]")
  198. test("\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  199. test("\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  200. test("\\abc", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  201. test("\\u0000", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  202. test("\\u200c = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  203. test("\\u200D = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  204. test(`"\`, "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  205. test(`"\u`, "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  206. test("return", "(anonymous): Line 1:1 Illegal return statement")
  207. test("continue", "(anonymous): Line 1:1 Illegal continue statement")
  208. test("break", "(anonymous): Line 1:1 Illegal break statement")
  209. test("switch (abc) { default: continue; }", "(anonymous): Line 1:25 Illegal continue statement")
  210. test("do { abc } *", "(anonymous): Line 1:12 Unexpected token *")
  211. test("while (true) { break abc; }", "(anonymous): Line 1:16 Undefined label 'abc'")
  212. test("while (true) { continue abc; }", "(anonymous): Line 1:16 Undefined label 'abc'")
  213. test("abc: while (true) { (function(){ break abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'")
  214. test("abc: while (true) { (function(){ abc: break abc; }); }", nil)
  215. test("abc: while (true) { (function(){ continue abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'")
  216. test(`abc: if (0) break abc; else {}`, nil)
  217. test(`abc: if (0) { break abc; } else {}`, nil)
  218. test(`abc: if (0) { break abc } else {}`, nil)
  219. test("abc: while (true) { abc: while (true) {} }", "(anonymous): Line 1:21 Label 'abc' already exists")
  220. if false {
  221. // TODO When strict mode is implemented
  222. test("(function () { 'use strict'; delete abc; }())", "")
  223. }
  224. test("_: _: while (true) {]", "(anonymous): Line 1:4 Label '_' already exists")
  225. test("_:\n_:\nwhile (true) {]", "(anonymous): Line 2:1 Label '_' already exists")
  226. test("_:\n _:\nwhile (true) {]", "(anonymous): Line 2:4 Label '_' already exists")
  227. test("function(){}", "(anonymous): Line 1:9 Unexpected token (")
  228. test("\n/*/", "(anonymous): Line 2:4 Unexpected end of input")
  229. test("/*/.source", "(anonymous): Line 1:11 Unexpected end of input")
  230. test("var class", "(anonymous): Line 1:5 Unexpected reserved word")
  231. test("var if", "(anonymous): Line 1:5 Unexpected token if")
  232. test("object Object", "(anonymous): Line 1:8 Unexpected identifier")
  233. test("[object Object]", "(anonymous): Line 1:9 Unexpected identifier")
  234. test("\\u0xyz", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  235. test(`for (var abc, def in {}) {}`, "(anonymous): Line 1:19 Unexpected token in")
  236. test(`for (abc, def in {}) {}`, "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  237. test(`for (var abc=def, ghi=("abc" in {}); true;) {}`, nil)
  238. {
  239. // Semicolon insertion
  240. test("this\nif (1);", nil)
  241. test("while (1) { break\nif (1); }", nil)
  242. test("throw\nif (1);", "(anonymous): Line 1:1 Illegal newline after throw")
  243. test("(function(){ return\nif (1); })", nil)
  244. test("while (1) { continue\nif (1); }", nil)
  245. test("debugger\nif (1);", nil)
  246. }
  247. { // Reserved words
  248. test("class", "(anonymous): Line 1:1 Unexpected reserved word")
  249. test("abc.class = 1", nil)
  250. test("var class;", "(anonymous): Line 1:5 Unexpected reserved word")
  251. test("const", "(anonymous): Line 1:1 Unexpected reserved word")
  252. test("abc.const = 1", nil)
  253. test("var const;", "(anonymous): Line 1:5 Unexpected reserved word")
  254. test("enum", "(anonymous): Line 1:1 Unexpected reserved word")
  255. test("abc.enum = 1", nil)
  256. test("var enum;", "(anonymous): Line 1:5 Unexpected reserved word")
  257. test("export", "(anonymous): Line 1:1 Unexpected reserved word")
  258. test("abc.export = 1", nil)
  259. test("var export;", "(anonymous): Line 1:5 Unexpected reserved word")
  260. test("extends", "(anonymous): Line 1:1 Unexpected reserved word")
  261. test("abc.extends = 1", nil)
  262. test("var extends;", "(anonymous): Line 1:5 Unexpected reserved word")
  263. test("import", "(anonymous): Line 1:1 Unexpected reserved word")
  264. test("abc.import = 1", nil)
  265. test("var import;", "(anonymous): Line 1:5 Unexpected reserved word")
  266. test("super", "(anonymous): Line 1:1 Unexpected reserved word")
  267. test("abc.super = 1", nil)
  268. test("var super;", "(anonymous): Line 1:5 Unexpected reserved word")
  269. }
  270. { // Reserved words (strict)
  271. test(`implements`, nil)
  272. test(`abc.implements = 1`, nil)
  273. test(`var implements;`, nil)
  274. test(`interface`, nil)
  275. test(`abc.interface = 1`, nil)
  276. test(`var interface;`, nil)
  277. test(`let`, nil)
  278. test(`abc.let = 1`, nil)
  279. test(`var let;`, nil)
  280. test(`package`, nil)
  281. test(`abc.package = 1`, nil)
  282. test(`var package;`, nil)
  283. test(`private`, nil)
  284. test(`abc.private = 1`, nil)
  285. test(`var private;`, nil)
  286. test(`protected`, nil)
  287. test(`abc.protected = 1`, nil)
  288. test(`var protected;`, nil)
  289. test(`public`, nil)
  290. test(`abc.public = 1`, nil)
  291. test(`var public;`, nil)
  292. test(`static`, nil)
  293. test(`abc.static = 1`, nil)
  294. test(`var static;`, nil)
  295. test(`yield`, nil)
  296. test(`abc.yield = 1`, nil)
  297. test(`var yield;`, nil)
  298. }
  299. })
  300. }
  301. func TestParser(t *testing.T) {
  302. tt(t, func() {
  303. test := func(source string, chk interface{}) *ast.Program {
  304. _, program, err := testParse(source)
  305. is(firstErr(err), chk)
  306. return program
  307. }
  308. test(`
  309. abc
  310. --
  311. []
  312. `, "(anonymous): Line 3:13 Invalid left-hand side in assignment")
  313. test(`
  314. abc--
  315. []
  316. `, nil)
  317. test("1\n[]\n", "(anonymous): Line 2:2 Unexpected token ]")
  318. test(`
  319. function abc() {
  320. }
  321. abc()
  322. `, nil)
  323. program := test("", nil)
  324. test("//", nil)
  325. test("/* */", nil)
  326. test("/** **/", nil)
  327. test("/*****/", nil)
  328. test("/*", "(anonymous): Line 1:3 Unexpected end of input")
  329. test("#", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  330. test("/**/#", "(anonymous): Line 1:5 Unexpected token ILLEGAL")
  331. test("new +", "(anonymous): Line 1:5 Unexpected token +")
  332. program = test(";", nil)
  333. is(len(program.Body), 1)
  334. is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1))
  335. program = test(";;", nil)
  336. is(len(program.Body), 2)
  337. is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1))
  338. is(program.Body[1].(*ast.EmptyStatement).Semicolon, file.Idx(2))
  339. program = test("1.2", nil)
  340. is(len(program.Body), 1)
  341. is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2")
  342. program = test("/* */1.2", nil)
  343. is(len(program.Body), 1)
  344. is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2")
  345. program = test("\n", nil)
  346. is(len(program.Body), 0)
  347. test(`
  348. if (0) {
  349. abc = 0
  350. }
  351. else abc = 0
  352. `, nil)
  353. test("if (0) abc = 0 else abc = 0", "(anonymous): Line 1:16 Unexpected token else")
  354. test(`
  355. if (0) {
  356. abc = 0
  357. } else abc = 0
  358. `, nil)
  359. test(`
  360. if (0) {
  361. abc = 1
  362. } else {
  363. }
  364. `, nil)
  365. test(`
  366. do {
  367. } while (true)
  368. `, nil)
  369. test(`
  370. try {
  371. } finally {
  372. }
  373. `, nil)
  374. test(`
  375. try {
  376. } catch (abc) {
  377. } finally {
  378. }
  379. `, nil)
  380. test(`
  381. try {
  382. }
  383. catch (abc) {
  384. }
  385. finally {
  386. }
  387. `, nil)
  388. test(`try {} catch (abc) {} finally {}`, nil)
  389. test(`
  390. do {
  391. do {
  392. } while (0)
  393. } while (0)
  394. `, nil)
  395. test(`
  396. (function(){
  397. try {
  398. if (
  399. 1
  400. ) {
  401. return 1
  402. }
  403. return 0
  404. } finally {
  405. }
  406. })()
  407. `, nil)
  408. test("abc = ''\ndef", nil)
  409. test("abc = 1\ndef", nil)
  410. test("abc = Math\ndef", nil)
  411. test(`"\'"`, nil)
  412. test(`
  413. abc = function(){
  414. }
  415. abc = 0
  416. `, nil)
  417. test("abc.null = 0", nil)
  418. test("0x41", nil)
  419. test(`"\d"`, nil)
  420. test(`(function(){return this})`, nil)
  421. test(`
  422. Object.defineProperty(Array.prototype, "0", {
  423. value: 100,
  424. writable: false,
  425. configurable: true
  426. });
  427. abc = [101];
  428. abc.hasOwnProperty("0") && abc[0] === 101;
  429. `, nil)
  430. test(`new abc()`, nil)
  431. test(`new {}`, nil)
  432. test(`
  433. limit = 4
  434. result = 0
  435. while (limit) {
  436. limit = limit - 1
  437. if (limit) {
  438. }
  439. else {
  440. break
  441. }
  442. result = result + 1
  443. }
  444. `, nil)
  445. test(`
  446. while (0) {
  447. if (0) {
  448. continue
  449. }
  450. }
  451. `, nil)
  452. test("var \u0061\u0062\u0063 = 0", nil)
  453. // 7_3_1
  454. test("var test7_3_1\nabc = 66;", nil)
  455. test("var test7_3_1\u2028abc = 66;", nil)
  456. // 7_3_3
  457. test("//\u2028 =;", "(anonymous): Line 2:2 Unexpected token =")
  458. // 7_3_10
  459. test("var abc = \u2029;", "(anonymous): Line 2:1 Unexpected token ;")
  460. test("var abc = \\u2029;", "(anonymous): Line 1:11 Unexpected token ILLEGAL")
  461. test("var \\u0061\\u0062\\u0063 = 0;", nil)
  462. test("'", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  463. test("'\nstr\ning\n'", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  464. // S7.6_A4.3_T1
  465. test(`var $\u0030 = 0;`, nil)
  466. // S7.6.1.1_A1.1
  467. test(`switch = 1`, "(anonymous): Line 1:8 Unexpected token =")
  468. // S7.8.3_A2.1_T1
  469. test(`.0 === 0.0`, nil)
  470. // 7.8.5-1
  471. test("var regExp = /\\\rn/;", "(anonymous): Line 1:14 Invalid regular expression: missing /")
  472. // S7.8.5_A1.1_T2
  473. test("var regExp = /=/;", nil)
  474. // S7.8.5_A1.2_T1
  475. test("/*/", "(anonymous): Line 1:4 Unexpected end of input")
  476. // Sbp_7.9_A9_T3
  477. test(`
  478. do {
  479. ;
  480. } while (false) true
  481. `, nil)
  482. // S7.9_A10_T10
  483. test(`
  484. {a:1
  485. } 3
  486. `, nil)
  487. test(`
  488. abc
  489. ++def
  490. `, nil)
  491. // S7.9_A5.2_T1
  492. test(`
  493. for(false;false
  494. ) {
  495. break;
  496. }
  497. `, "(anonymous): Line 3:13 Unexpected token )")
  498. // S7.9_A9_T8
  499. test(`
  500. do {};
  501. while (false)
  502. `, "(anonymous): Line 2:18 Unexpected token ;")
  503. // S8.4_A5
  504. test(`
  505. "x\0y"
  506. `, nil)
  507. // S9.3.1_A6_T1
  508. test(`
  509. 10e10000
  510. `, nil)
  511. // 10.4.2-1-5
  512. test(`
  513. "abc\
  514. def"
  515. `, nil)
  516. test("'\\\n'", nil)
  517. test("'\\\r\n'", nil)
  518. //// 11.13.1-1-1
  519. test("42 = 42;", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  520. // S11.13.2_A4.2_T1.3
  521. test(`
  522. abc /= "1"
  523. `, nil)
  524. // 12.1-1
  525. test(`
  526. try{};catch(){}
  527. `, "(anonymous): Line 2:13 Missing catch or finally after try")
  528. // 12.1-3
  529. test(`
  530. try{};finally{}
  531. `, "(anonymous): Line 2:13 Missing catch or finally after try")
  532. // S12.6.3_A11.1_T3
  533. test(`
  534. while (true) {
  535. break abc;
  536. }
  537. `, "(anonymous): Line 3:17 Undefined label 'abc'")
  538. // S15.3_A2_T1
  539. test(`var x / = 1;`, "(anonymous): Line 1:7 Unexpected token /")
  540. test(`
  541. function abc() {
  542. if (0)
  543. return;
  544. else {
  545. }
  546. }
  547. `, nil)
  548. test("//\u2028 var =;", "(anonymous): Line 2:6 Unexpected token =")
  549. test(`
  550. throw
  551. {}
  552. `, "(anonymous): Line 2:13 Illegal newline after throw")
  553. // S7.6.1.1_A1.11
  554. test(`
  555. function = 1
  556. `, "(anonymous): Line 2:22 Unexpected token =")
  557. // S7.8.3_A1.2_T1
  558. test(`0e1`, nil)
  559. test("abc = 1; abc\n++", "(anonymous): Line 2:3 Unexpected end of input")
  560. // ---
  561. test("({ get abc() {} })", nil)
  562. test(`for (abc.def in {}) {}`, nil)
  563. test(`while (true) { break }`, nil)
  564. test(`while (true) { continue }`, nil)
  565. test(`abc=/^(?:(\w+:)\/{2}(\w+(?:\.\w+)*\/?)|(.{0,2}\/{1}))?([/.]*?(?:[^?]+)?\/)?((?:[^/?]+)\.(\w+))(?:\?(\S+)?)?$/,def=/^(?:(\w+:)\/{2})|(.{0,2}\/{1})?([/.]*?(?:[^?]+)?\/?)?$/`, nil)
  566. test(`(function() { try {} catch (err) {} finally {} return })`, nil)
  567. test(`0xde0b6b3a7640080.toFixed(0)`, nil)
  568. test(`/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/`, nil)
  569. test(`/[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/`, nil)
  570. test("var abc = 1;\ufeff", nil)
  571. test("\ufeff/* var abc = 1; */", nil)
  572. test(`if (-0x8000000000000000<=abc&&abc<=0x8000000000000000) {}`, nil)
  573. test(`(function(){debugger;return this;})`, nil)
  574. test(`
  575. `, nil)
  576. test(`
  577. var abc = ""
  578. debugger
  579. `, nil)
  580. test(`
  581. var abc = /\[\]$/
  582. debugger
  583. `, nil)
  584. test(`
  585. var abc = 1 /
  586. 2
  587. debugger
  588. `, nil)
  589. })
  590. }
  591. func Test_parseStringLiteral(t *testing.T) {
  592. tt(t, func() {
  593. test := func(have, want string) {
  594. have, err := parseStringLiteral(have)
  595. is(err, nil)
  596. is(have, want)
  597. }
  598. test("", "")
  599. test("1(\\\\d+)", "1(\\d+)")
  600. test("\\u2029", "\u2029")
  601. test("abc\\uFFFFabc", "abc\uFFFFabc")
  602. test("[First line \\\nSecond line \\\n Third line\\\n. ]",
  603. "[First line Second line Third line. ]")
  604. test("\\u007a\\x79\\u000a\\x78", "zy\nx")
  605. // S7.8.4_A4.2_T3
  606. test("\\a", "a")
  607. test("\u0410", "\u0410")
  608. // S7.8.4_A5.1_T1
  609. test("\\0", "\u0000")
  610. // S8.4_A5
  611. test("\u0000", "\u0000")
  612. // 15.5.4.20
  613. test("'abc'\\\n'def'", "'abc''def'")
  614. // 15.5.4.20-4-1
  615. test("'abc'\\\r\n'def'", "'abc''def'")
  616. // Octal
  617. test("\\0", "\000")
  618. test("\\00", "\000")
  619. test("\\000", "\000")
  620. test("\\09", "\0009")
  621. test("\\009", "\0009")
  622. test("\\0009", "\0009")
  623. test("\\1", "\001")
  624. test("\\01", "\001")
  625. test("\\001", "\001")
  626. test("\\0011", "\0011")
  627. test("\\1abc", "\001abc")
  628. test("\\\u4e16", "\u4e16")
  629. // err
  630. test = func(have, want string) {
  631. have, err := parseStringLiteral(have)
  632. is(err.Error(), want)
  633. is(have, "")
  634. }
  635. test(`\u`, `invalid escape: \u: len("") != 4`)
  636. test(`\u0`, `invalid escape: \u: len("0") != 4`)
  637. test(`\u00`, `invalid escape: \u: len("00") != 4`)
  638. test(`\u000`, `invalid escape: \u: len("000") != 4`)
  639. test(`\x`, `invalid escape: \x: len("") != 2`)
  640. test(`\x0`, `invalid escape: \x: len("0") != 2`)
  641. test(`\x0`, `invalid escape: \x: len("0") != 2`)
  642. })
  643. }
  644. func Test_parseNumberLiteral(t *testing.T) {
  645. tt(t, func() {
  646. test := func(input string, expect interface{}) {
  647. result, err := parseNumberLiteral(input)
  648. is(err, nil)
  649. is(result, expect)
  650. }
  651. test("0", 0)
  652. test("0x8000000000000000", float64(9.223372036854776e+18))
  653. })
  654. }
  655. func TestPosition(t *testing.T) {
  656. tt(t, func() {
  657. parser := newParser("", "// Lorem ipsum")
  658. // Out of range, idx0 (error condition)
  659. is(parser.slice(0, 1), "")
  660. is(parser.slice(0, 10), "")
  661. // Out of range, idx1 (error condition)
  662. is(parser.slice(1, 128), "")
  663. is(parser.str[0:0], "")
  664. is(parser.slice(1, 1), "")
  665. is(parser.str[0:1], "/")
  666. is(parser.slice(1, 2), "/")
  667. is(parser.str[0:14], "// Lorem ipsum")
  668. is(parser.slice(1, 15), "// Lorem ipsum")
  669. parser = newParser("", "(function(){ return 0; })")
  670. program, err := parser.parse()
  671. is(err, nil)
  672. var node ast.Node
  673. node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
  674. is(node.Idx0(), file.Idx(2))
  675. is(node.Idx1(), file.Idx(25))
  676. is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
  677. is(parser.slice(node.Idx0(), node.Idx1()+1), "function(){ return 0; })")
  678. is(parser.slice(node.Idx0(), node.Idx1()+2), "")
  679. is(node.(*ast.FunctionLiteral).Source, "function(){ return 0; }")
  680. node = program
  681. is(node.Idx0(), file.Idx(2))
  682. is(node.Idx1(), file.Idx(25))
  683. is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
  684. parser = newParser("", "(function(){ return abc; })")
  685. program, err = parser.parse()
  686. is(err, nil)
  687. node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
  688. is(node.(*ast.FunctionLiteral).Source, "function(){ return abc; }")
  689. })
  690. }