tor-browser

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

mapOriginalExpression.spec.js (2532B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 import mapExpression from "../mapExpression";
      6 import { format } from "prettier";
      7 
      8 const formatOutput = output =>
      9  format(output, {
     10    parser: "babel",
     11  });
     12 
     13 const mapOriginalExpression = (expression, mappings) =>
     14  mapExpression(expression, mappings, [], false, false).expression;
     15 
     16 describe("mapOriginalExpression", () => {
     17  it("simple", () => {
     18    const generatedExpression = mapOriginalExpression("a + b;", {
     19      a: "foo",
     20      b: "bar",
     21    });
     22    expect(generatedExpression).toEqual("foo + bar;");
     23  });
     24 
     25  it("this", () => {
     26    const generatedExpression = mapOriginalExpression("this.prop;", {
     27      this: "_this",
     28    });
     29    expect(generatedExpression).toEqual("_this.prop;");
     30  });
     31 
     32  it("member expressions", () => {
     33    const generatedExpression = mapOriginalExpression("a + b", {
     34      a: "_mod.foo",
     35      b: "_mod.bar",
     36    });
     37    expect(generatedExpression).toEqual("_mod.foo + _mod.bar;");
     38  });
     39 
     40  it("block", () => {
     41    // todo: maybe wrap with parens ()
     42    const generatedExpression = mapOriginalExpression("{a}", {
     43      a: "_mod.foo",
     44      b: "_mod.bar",
     45    });
     46    expect(generatedExpression).toEqual("{\n  _mod.foo;\n}");
     47  });
     48 
     49  it("skips codegen with no mappings", () => {
     50    const generatedExpression = mapOriginalExpression("a + b", {
     51      a: "a",
     52      c: "_c",
     53    });
     54    expect(generatedExpression).toEqual("a + b");
     55  });
     56 
     57  it("object destructuring", () => {
     58    const generatedExpression = mapOriginalExpression("({ a } = { a: 4 })", {
     59      a: "_mod.foo",
     60    });
     61 
     62    expect(formatOutput(generatedExpression)).toEqual(
     63      formatOutput("({ a: _mod.foo } = {\n a: 4 \n})")
     64    );
     65  });
     66 
     67  it("nested object destructuring", () => {
     68    const generatedExpression = mapOriginalExpression(
     69      "({ a: { b, c } } = { a: 4 })",
     70      {
     71        a: "_mod.foo",
     72        b: "_mod.bar",
     73      }
     74    );
     75 
     76    expect(formatOutput(generatedExpression)).toEqual(
     77      formatOutput("({ a: { b: _mod.bar, c } } = {\n a: 4 \n})")
     78    );
     79  });
     80 
     81  it("shadowed bindings", () => {
     82    const generatedExpression = mapOriginalExpression(
     83      "window.thing = function fn(){ var a; a; b; }; a; b; ",
     84      {
     85        a: "_a",
     86        b: "_b",
     87      }
     88    );
     89    expect(generatedExpression).toEqual(
     90      "window.thing = function fn() {\n  var a;\n  a;\n  _b;\n};\n_a;\n_b;"
     91    );
     92  });
     93 });