summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/workers/parser/tests/mapBindings.spec.js
blob: 3731ac1dd5c4a15b6bd3eeb13e9571edcc9f4b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

import mapExpressionBindings from "../mapBindings";
import { parseConsoleScript } from "../utils/ast";
import cases from "jest-in-case";

const prettier = require("prettier");

function format(code) {
  return prettier.format(code, { semi: false, parser: "babel" });
}

function excludedTest({ expression, bindings = [] }) {
  const safeExpression = mapExpressionBindings(
    expression,
    parseConsoleScript(expression),
    bindings
  );
  expect(format(safeExpression)).toEqual(format(expression));
}

function includedTest({ expression, newExpression, bindings }) {
  const safeExpression = mapExpressionBindings(
    expression,
    parseConsoleScript(expression),
    bindings
  );
  expect(format(safeExpression)).toEqual(format(newExpression));
}

describe("mapExpressionBindings", () => {
  cases("included cases", includedTest, [
    {
      name: "single declaration",
      expression: "const a = 2; let b = 3; var c = 4;",
      newExpression: "self.a = 2; self.b = 3; self.c = 4;",
    },
    {
      name: "multiple declarations",
      expression: "const a = 2, b = 3",
      newExpression: "self.a = 2; self.b = 3",
    },
    {
      name: "declaration with separate assignment",
      expression: "let a; a = 2;",
      newExpression: "self.a = void 0; self.a = 2;",
    },
    {
      name: "multiple declarations with no assignment",
      expression: "let a = 2, b;",
      newExpression: "self.a = 2; self.b = void 0;",
    },
    {
      name: "local bindings become assignments",
      bindings: ["a"],
      expression: "var a = 2;",
      newExpression: "a = 2;",
    },
    {
      name: "assignments",
      expression: "a = 2;",
      newExpression: "self.a = 2;",
    },
    {
      name: "assignments with +=",
      expression: "a += 2;",
      newExpression: "self.a += 2;",
    },
    {
      name: "destructuring (objects)",
      expression: "const { a } = {}; ",
      newExpression: "({ a: self.a } = {})",
    },
    {
      name: "destructuring (arrays)",
      expression: " var [a, ...foo] = [];",
      newExpression: "([self.a, ...self.foo] = [])",
    },
    {
      name: "destructuring (declarations)",
      expression: "var {d,e} = {}, {f} = {}; ",
      newExpression: `({ d: self.d, e: self.e } = {});
        ({ f: self.f } = {})
      `,
    },
    {
      name: "destructuring & declaration",
      expression: "const { a } = {}; var b = 3",
      newExpression: `({ a: self.a } = {});
        self.b = 3
      `,
    },
    {
      name: "destructuring assignment",
      expression: "[a] = [3]",
      newExpression: "[self.a] = [3]",
    },
    {
      name: "destructuring assignment (declarations)",
      expression: "[a] = [3]; var b = 4",
      newExpression: "[self.a] = [3];\n self.b = 4",
    },
  ]);

  cases("excluded cases", excludedTest, [
    { name: "local variables", expression: "function a() { var b = 2}" },
    { name: "functions", expression: "function a() {}" },
    { name: "classes", expression: "class a {}" },

    { name: "with", expression: "with (obj) {var a = 2;}" },
    {
      name: "with & declaration",
      expression: "with (obj) {var a = 2;}; ; var b = 3",
    },
    {
      name: "hoisting",
      expression: "{ const h = 3; }",
    },
    {
      name: "assignments",
      bindings: ["a"],
      expression: "a = 2;",
    },
    {
      name: "identifier",
      expression: "a",
    },
  ]);

  cases("cases that we should map in the future", excludedTest, [
    { name: "blocks (IF)", expression: "if (true) { var a = 3; }" },
    {
      name: "hoisting",
      expression: "{ var g = 5; }",
    },
    {
      name: "for loops bindings",
      expression: "for (let foo = 4; false;){}",
    },
  ]);

  cases("cases that we shouldn't map in the future", includedTest, [
    {
      name: "window properties",
      expression: "var innerHeight = 3; var location = 5;",
      newExpression: "self.innerHeight = 3; self.location = 5;",
    },
    {
      name: "self declaration",
      expression: "var self = 3",
      newExpression: "self.self = 3",
    },
    {
      name: "self assignment",
      expression: "self = 3",
      newExpression: "self.self = 3",
    },
  ]);
});