summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/workers/parser/mapExpression.js
blob: 3a34397ba6e0ea4fb644254a077fb1c52490b0d4 (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
/* 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/>. */

// @flow

import { parseConsoleScript } from "./utils/ast";
import mapOriginalExpression from "./mapOriginalExpression";
import mapExpressionBindings from "./mapBindings";
import mapTopLevelAwait from "./mapAwaitExpression";

export default function mapExpression(
  expression: string,
  mappings: {
    [string]: string | null,
  } | null,
  bindings: string[],
  shouldMapBindings: boolean = true,
  shouldMapAwait: boolean = true
): {
  expression: string,
  mapped: {
    await: boolean,
    bindings: boolean,
    originalExpression: boolean,
  },
} {
  const mapped = {
    await: false,
    bindings: false,
    originalExpression: false,
  };

  const ast = parseConsoleScript(expression);
  try {
    if (mappings && ast) {
      const beforeOriginalExpression = expression;
      expression = mapOriginalExpression(expression, ast, mappings);
      mapped.originalExpression = beforeOriginalExpression !== expression;
    }

    if (shouldMapBindings && ast) {
      const beforeBindings = expression;
      expression = mapExpressionBindings(expression, ast, bindings);
      mapped.bindings = beforeBindings !== expression;
    }

    if (shouldMapAwait) {
      const beforeAwait = expression;
      expression = mapTopLevelAwait(expression, ast);
      mapped.await = beforeAwait !== expression;
    }
  } catch (e) {
    console.warn(`Error when mapping ${expression} expression:`, e);
  }

  return {
    expression,
    mapped,
  };
}