tor-browser

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

mapBindings.spec.js (4397B)


      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 mapExpressionBindings from "../mapBindings";
      6 import { parseConsoleScript } from "../utils/ast";
      7 import cases from "jest-in-case";
      8 
      9 const prettier = require("prettier");
     10 
     11 function format(code) {
     12  return prettier.format(code, { semi: false, parser: "babel" });
     13 }
     14 
     15 function excludedTest({ expression, bindings = [] }) {
     16  const safeExpression = mapExpressionBindings(
     17    expression,
     18    parseConsoleScript(expression),
     19    bindings
     20  );
     21  expect(format(safeExpression)).toEqual(format(expression));
     22 }
     23 
     24 function includedTest({ expression, newExpression, bindings }) {
     25  const safeExpression = mapExpressionBindings(
     26    expression,
     27    parseConsoleScript(expression),
     28    bindings
     29  );
     30  expect(format(safeExpression)).toEqual(format(newExpression));
     31 }
     32 
     33 describe("mapExpressionBindings", () => {
     34  cases("included cases", includedTest, [
     35    {
     36      name: "single declaration",
     37      expression: "const a = 2; let b = 3; var c = 4;",
     38      newExpression: "self.a = 2; self.b = 3; self.c = 4;",
     39    },
     40    {
     41      name: "multiple declarations",
     42      expression: "const a = 2, b = 3",
     43      newExpression: "self.a = 2; self.b = 3",
     44    },
     45    {
     46      name: "declaration with separate assignment",
     47      expression: "let a; a = 2;",
     48      newExpression: "self.a = void 0; self.a = 2;",
     49    },
     50    {
     51      name: "multiple declarations with no assignment",
     52      expression: "let a = 2, b;",
     53      newExpression: "self.a = 2; self.b = void 0;",
     54    },
     55    {
     56      name: "local bindings become assignments",
     57      bindings: ["a"],
     58      expression: "var a = 2;",
     59      newExpression: "a = 2;",
     60    },
     61    {
     62      name: "assignments",
     63      expression: "a = 2;",
     64      newExpression: "self.a = 2;",
     65    },
     66    {
     67      name: "assignments with +=",
     68      expression: "a += 2;",
     69      newExpression: "self.a += 2;",
     70    },
     71    {
     72      name: "destructuring (objects)",
     73      expression: "const { a } = {}; ",
     74      newExpression: "({ a: self.a } = {})",
     75    },
     76    {
     77      name: "destructuring (arrays)",
     78      expression: " var [a, ...foo] = [];",
     79      newExpression: "([self.a, ...self.foo] = [])",
     80    },
     81    {
     82      name: "destructuring (declarations)",
     83      expression: "var {d,e} = {}, {f} = {}; ",
     84      newExpression: `({ d: self.d, e: self.e } = {});
     85        ({ f: self.f } = {})
     86      `,
     87    },
     88    {
     89      name: "destructuring & declaration",
     90      expression: "const { a } = {}; var b = 3",
     91      newExpression: `({ a: self.a } = {});
     92        self.b = 3
     93      `,
     94    },
     95    {
     96      name: "destructuring assignment",
     97      expression: "[a] = [3]",
     98      newExpression: "[self.a] = [3]",
     99    },
    100    {
    101      name: "destructuring assignment (declarations)",
    102      expression: "[a] = [3]; var b = 4",
    103      newExpression: "[self.a] = [3];\n self.b = 4",
    104    },
    105  ]);
    106 
    107  cases("excluded cases", excludedTest, [
    108    { name: "local variables", expression: "function a() { var b = 2}" },
    109    { name: "functions", expression: "function a() {}" },
    110    { name: "classes", expression: "class a {}" },
    111 
    112    { name: "with", expression: "with (obj) {var a = 2;}" },
    113    {
    114      name: "with & declaration",
    115      expression: "with (obj) {var a = 2;}; ; var b = 3",
    116    },
    117    {
    118      name: "hoisting",
    119      expression: "{ const h = 3; }",
    120    },
    121    {
    122      name: "assignments",
    123      bindings: ["a"],
    124      expression: "a = 2;",
    125    },
    126    {
    127      name: "identifier",
    128      expression: "a",
    129    },
    130  ]);
    131 
    132  cases("cases that we should map in the future", excludedTest, [
    133    { name: "blocks (IF)", expression: "if (true) { var a = 3; }" },
    134    {
    135      name: "hoisting",
    136      expression: "{ var g = 5; }",
    137    },
    138    {
    139      name: "for loops bindings",
    140      expression: "for (let foo = 4; false;){}",
    141    },
    142  ]);
    143 
    144  cases("cases that we shouldn't map in the future", includedTest, [
    145    {
    146      name: "window properties",
    147      expression: "var innerHeight = 3; var location = 5;",
    148      newExpression: "self.innerHeight = 3; self.location = 5;",
    149    },
    150    {
    151      name: "self declaration",
    152      expression: "var self = 3",
    153      newExpression: "self.self = 3",
    154    },
    155    {
    156      name: "self assignment",
    157      expression: "self = 3",
    158      newExpression: "self.self = 3",
    159    },
    160  ]);
    161 });