tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

declarations.js (4899B)


      1 // |reftest| skip-if(!xulRuntime.shell)
      2 function test() {
      3 
      4 // Bug 632056: constant-folding
      5 program([exprStmt(ident("f")),
      6         ifStmt(lit(1),
      7                blockStmt([funDecl(ident("f"), [], blockStmt([]))]),
      8                null)]).assert(Reflect.parse("f; if (1) function f(){}"));
      9 // declarations
     10 
     11 assertDecl("var x = 1, y = 2, z = 3",
     12           varDecl([{ id: ident("x"), init: lit(1) },
     13                    { id: ident("y"), init: lit(2) },
     14                    { id: ident("z"), init: lit(3) }]));
     15 assertDecl("var x, y, z",
     16           varDecl([{ id: ident("x"), init: null },
     17                    { id: ident("y"), init: null },
     18                    { id: ident("z"), init: null }]));
     19 assertDecl("function foo() { }",
     20           funDecl(ident("foo"), [], blockStmt([])));
     21 assertDecl("function foo() { return 42 }",
     22           funDecl(ident("foo"), [], blockStmt([returnStmt(lit(42))])));
     23 
     24 assertDecl("function foo(...rest) { }",
     25           funDecl(ident("foo"), [], blockStmt([]), [], ident("rest")));
     26 
     27 assertDecl("function foo(a=4) { }", funDecl(ident("foo"), [ident("a")], blockStmt([]), [lit(4)]));
     28 assertDecl("function foo(a, b=4) { }", funDecl(ident("foo"), [ident("a"), ident("b")], blockStmt([]), [null, lit(4)]));
     29 assertDecl("function foo(a, b=4, ...rest) { }",
     30           funDecl(ident("foo"), [ident("a"), ident("b")], blockStmt([]), [null, lit(4), null], ident("rest")));
     31 assertDecl("function foo(a=(function () {})) { function a() {} }",
     32           funDecl(ident("foo"), [ident("a")], blockStmt([funDecl(ident("a"), [], blockStmt([]))]),
     33                   [funExpr(null, [], blockStmt([]))]));
     34 
     35 // Bug 1018628: default paremeter for destructuring
     36 assertDecl("function f(a=1, [x,y]=[2,3]) { }",
     37           funDecl(ident("f"),
     38                   [ident("a"), arrPatt([ident("x"), ident("y")])],
     39                   blockStmt([]),
     40                   [lit(1), arrExpr([lit(2), lit(3)])]));
     41 
     42 // Bug 591437: rebound args have their defs turned into uses
     43 assertDecl("function f(a) { function a() { } }",
     44           funDecl(ident("f"), [ident("a")], blockStmt([funDecl(ident("a"), [], blockStmt([]))])));
     45 assertDecl("function f(a,b,c) { function b() { } }",
     46           funDecl(ident("f"), [ident("a"),ident("b"),ident("c")], blockStmt([funDecl(ident("b"), [], blockStmt([]))])));
     47 assertDecl("function f(a,[x,y]) { function a() { } }",
     48           funDecl(ident("f"),
     49                   [ident("a"), arrPatt([assignElem("x"), assignElem("y")])],
     50                   blockStmt([funDecl(ident("a"), [], blockStmt([]))])));
     51 
     52 // Bug 591450: this test was crashing because of a bug in jsparse
     53 assertDecl("function f(a,[x,y],b,[w,z],c) { function b() { } }",
     54           funDecl(ident("f"),
     55                   [ident("a"), arrPatt([ident("x"), ident("y")]), ident("b"), arrPatt([ident("w"), ident("z")]), ident("c")],
     56                   blockStmt([funDecl(ident("b"), [], blockStmt([]))])));
     57 
     58 // redeclarations (TOK_NAME nodes with lexdef)
     59 
     60 assertStmt("function f() { function g() { } function g() { } }",
     61           funDecl(ident("f"), [], blockStmt([funDecl(ident("g"), [], blockStmt([])),
     62                                              funDecl(ident("g"), [], blockStmt([]))])));
     63 
     64 assertStmt("function f() { function g() { } function g() { return 42 } }",
     65          funDecl(ident("f"), [], blockStmt([funDecl(ident("g"), [], blockStmt([])),
     66                                             funDecl(ident("g"), [], blockStmt([returnStmt(lit(42))]))])));
     67 
     68 assertStmt("function f() { var x = 42; var x = 43; }",
     69           funDecl(ident("f"), [], blockStmt([varDecl([{ id: ident("x"), init: lit(42) }]),
     70                                              varDecl([{ id: ident("x"), init: lit(43) }])])));
     71 
     72 
     73 assertDecl("var {x:y} = foo;", varDecl([{ id: objPatt([assignProp("x", ident("y"))]),
     74                                          init: ident("foo") }]));
     75 assertDecl("var {x} = foo;", varDecl([{ id: objPatt([assignProp("x")]),
     76                                        init: ident("foo") }]));
     77 
     78 // Bug 632030: redeclarations between var and funargs, var and function
     79 assertStmt("function g(x) { var x }",
     80           funDecl(ident("g"), [ident("x")], blockStmt([varDecl([{ id: ident("x"), init: null }])])));
     81 assertProg("f.p = 1; var f; f.p; function f(){}",
     82           [exprStmt(aExpr("=", dotExpr(ident("f"), ident("p")), lit(1))),
     83            varDecl([{ id: ident("f"), init: null }]),
     84            exprStmt(dotExpr(ident("f"), ident("p"))),
     85            funDecl(ident("f"), [], blockStmt([]))]);
     86 }
     87 
     88 assertBlockStmt("{ function f(x) {} }",
     89                blockStmt([funDecl(ident("f"), [ident("x")], blockStmt([]))]));
     90 
     91 // Annex B semantics should not change parse tree.
     92 assertBlockStmt("{ let f; { function f(x) {} } }",
     93                blockStmt([letDecl([{ id: ident("f"), init: null }]),
     94                           blockStmt([funDecl(ident("f"), [ident("x")], blockStmt([]))])]));
     95 
     96 runtest(test);