tor-browser

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

templateStrings.js (2404B)


      1 // |reftest| skip-if(!xulRuntime.shell)
      2 function test() {
      3 
      4 // template strings
      5 assertStringExpr("`hey there`", literal("hey there"));
      6 assertStringExpr("`hey\nthere`", literal("hey\nthere"));
      7 assertExpr("`hey${\"there\"}`", templateLit([lit("hey"), lit("there"), lit("")]));
      8 assertExpr("`hey${\"there\"}mine`", templateLit([lit("hey"), lit("there"), lit("mine")]));
      9 assertExpr("`hey${a == 5}mine`", templateLit([lit("hey"), binExpr("==", ident("a"), lit(5)), lit("mine")]));
     10 assertExpr("func`hey\\x`", taggedTemplate(ident("func"), template(["hey\\x"], [void 0])));
     11 assertExpr("func`hey${4}\\x`", taggedTemplate(ident("func"), template(["hey","\\x"], ["hey",void 0], lit(4))));
     12 assertExpr("`hey${`there${\"how\"}`}mine`", templateLit([lit("hey"),
     13           templateLit([lit("there"), lit("how"), lit("")]), lit("mine")]));
     14 assertExpr("func`hey`", taggedTemplate(ident("func"), template(["hey"], ["hey"])));
     15 assertExpr("func`hey${\"4\"}there`", taggedTemplate(ident("func"),
     16           template(["hey", "there"], ["hey", "there"], lit("4"))));
     17 assertExpr("func`hey${\"4\"}there${5}`", taggedTemplate(ident("func"),
     18           template(["hey", "there", ""], ["hey", "there", ""],
     19                  lit("4"), lit(5))));
     20 assertExpr("func`hey\r\n`", taggedTemplate(ident("func"), template(["hey\n"], ["hey\n"])));
     21 assertExpr("func`hey${4}``${5}there``mine`",
     22           taggedTemplate(taggedTemplate(taggedTemplate(
     23               ident("func"), template(["hey", ""], ["hey", ""], lit(4))),
     24               template(["", "there"], ["", "there"], lit(5))),
     25               template(["mine"], ["mine"])));
     26 
     27 // multi-line template string - line numbers
     28 var node = Reflect.parse("`\n\n   ${2}\n\n\n`");
     29 Pattern({loc:{start:{line:1, column:1}, end:{line:6, column:2}, source:null}, type:"Program",
     30 body:[{loc:{start:{line:1, column:1}, end:{line:6, column:2}, source:null},
     31 type:"ExpressionStatement", expression:{loc:{start:{line:1, column:1}, end:{line:6, column:2},
     32 source:null}, type:"TemplateLiteral", elements:[{loc:{start:{line:1, column:1}, end:{line:3,
     33 column:6}, source:null}, type:"Literal", value:"\n\n   "}, {loc:{start:{line:3, column:6},
     34 end:{line:3, column:7}, source:null}, type:"Literal", value:2}, {loc:{start:{line:3, column:7},
     35 end:{line:6, column:2}, source:null}, type:"Literal", value:"\n\n\n"}]}}]}).match(node);
     36 
     37 
     38 assertStringExpr("\"hey there\"", literal("hey there"));
     39 
     40 }
     41 
     42 runtest(test);