parser_test.go 28 KB

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