summaryrefslogtreecommitdiffstats
path: root/devtools/shared/acorn/tests/unit/head_acorn.js
blob: 3e06ca3c1a90149caf67d0a0a7d51729e4b7751b (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
"use strict";
var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});


function isObject(value) {
  return typeof value === "object" && value !== null;
}

function intersect(a, b) {
  const seen = new Set(a);
  return b.filter(value => seen.has(value));
}

function checkEquivalentASTs(expected, actual, prop = []) {
  do_print("Checking: " + prop.join(" "));

  if (!isObject(expected)) {
    return void do_check_eq(expected, actual);
  }

  do_check_true(isObject(actual));

  if (Array.isArray(expected)) {
    do_check_true(Array.isArray(actual));
    do_check_eq(expected.length, actual.length);
    for (let i = 0; i < expected.length; i++) {
      checkEquivalentASTs(expected[i], actual[i], prop.concat(i));
    }
  } else {
    // We must intersect the keys since acorn and Reflect have different
    // extraneous properties on their AST nodes.
    const keys = intersect(Object.keys(expected), Object.keys(actual));
    for (let key of keys) {
      checkEquivalentASTs(expected[key], actual[key], prop.concat(key));
    }
  }
}


// Register a console listener, so console messages don't just disappear
// into the ether.
var errorCount = 0;
var listener = {
  observe: function (aMessage) {
    errorCount++;
    try {
      // If we've been given an nsIScriptError, then we can print out
      // something nicely formatted, for tools like Emacs to pick up.
      var scriptError = aMessage.QueryInterface(Ci.nsIScriptError);
      dump(aMessage.sourceName + ":" + aMessage.lineNumber + ": " +
           scriptErrorFlagsToKind(aMessage.flags) + ": " +
           aMessage.errorMessage + "\n");
      var string = aMessage.errorMessage;
    } catch (x) {
      // Be a little paranoid with message, as the whole goal here is to lose
      // no information.
      try {
        var string = "" + aMessage.message;
      } catch (x) {
        var string = "<error converting error message to string>";
      }
    }

    // Ignored until they are fixed in bug 1242968.
    if (string.includes("JavaScript Warning")) {
      return;
    }

    do_throw("head_acorn.js got console message: " + string + "\n");
  }
};

var consoleService = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
consoleService.registerListener(listener);