Debian Codegraph @

packages/babel-parser/test/helpers/difference.js

raw
1 /* eslint-disable no-confusing-arrow */
2
3 import { isIdentifierName } from "@babel/helper-validator-identifier";
4
5 const { isArray } = Array;
6 const { isInteger } = Number;
7 const { hasOwnProperty } = Object;
8
9 export default class Difference {
10 constructor(adjust, expected, actual) {
11 const woundDifference = compare(adjust, expected, actual);
12
13 if (!woundDifference) {
14 return Difference.None;
15 }
16
17 const [path, reason] = toUnwoundDifference(woundDifference);
18 const message = `${toExplanationString(reason)} in ${toPathString(path)}`;
19
20 return Object.assign(this, { ...reason, path, message });
21 }
22 }
23
24 Difference.None = Object.freeze(
25 Object.setPrototypeOf({}, Difference.prototype),
26 );
27
28 const toType = value =>
29 value === null
30 ? "null"
31 : typeof value !== "object"
32 ? typeof value
33 : isArray(value)
34 ? "Array"
35 : value instanceof RegExp
36 ? "RegExp"
37 : value instanceof Error
38 ? "Error"
39 : "Object";
40
41 function compare(adjust, expected, actual) {
42 // easy.
43 if (Object.is(expected, actual)) {
44 return false;
45 }
46
47 const typeExpected = toType(expected);
48 const typeActual = toType(actual);
49
50 if (typeExpected !== typeActual) {
51 return { discrepancy: "value", expected, actual };
52 }
53
54 // Just ignore functions (AKA, assume they're equal).
55 if (typeActual === "function") {
56 return false;
57 }
58
59 if (typeActual === "RegExp" && expected + "" === actual + "") {
60 return false;
61 }
62
63 if (typeActual === "Error") {
64 return compare(
65 adjust,
66 { message: expected.message },
67 { message: actual.message },
68 );
69 }
70
71 if (typeActual !== "Object" && typeActual !== "Array") {
72 return { discrepancy: "value", expected, actual };
73 }
74
75 const keysExpected = Object.keys(expected);
76 const keysActual = Object.keys(actual).filter(
77 key => actual[key] !== void 0 && typeof actual[key] !== "function",
78 );
79 const lengthExpected = keysExpected.length;
80 const lengthActual = keysActual.length;
81
82 if (lengthExpected !== lengthActual && typeActual === "Array") {
83 return {
84 discrepancy: "length",
85 expected: lengthExpected,
86 actual: lengthActual,
87 };
88 }
89
90 if (lengthExpected < lengthActual) {
91 const keysExpectedSet = new Set(keysExpected);
92 const key = keysActual.find(key => !keysExpectedSet.has(key));
93
94 if (key !== void 0) {
95 return { discrepancy: "unexpected-key", key };
96 }
97 }
98
99 for (const key of keysExpected) {
100 if (!hasOwnProperty.call(actual, key)) {
101 return { discrepancy: "missing-key", key, actual };
102 }
103
104 const original = expected[key];
105 const adjusted = adjust
106 ? adjust(adjust, original, key, expected)
107 : original;
108 const difference = compare(adjust, adjusted, actual[key]);
109
110 if (difference) {
111 return [key, difference];
112 }
113 }
114
115 return false;
116 }
117
118 const toUnwoundDifference = compiled =>
119 !isArray(compiled)
120 ? [[], compiled]
121 : toUnwoundDifference(compiled[1]).map((item, index) =>
122 index === 0 ? [compiled[0], ...item] : item,
123 );
124
125 const toValueString = (value, type = toType(value)) =>
126 type === "string"
127 ? JSON.stringify(value)
128 : type === "symbol"
129 ? value.toString()
130 : type === "bigint"
131 ? `${value}n`
132 : Object.is(value, -0)
133 ? "-0"
134 : value + "";
135
136 const toExplanationString = ({ discrepancy, expected, actual, key }) =>
137 discrepancy === "length"
138 ? `Array of wrong size, expected length of ${expected}, but got ${actual}`
139 : discrepancy === "unexpected-key"
140 ? `Did not expect a property ${toValueString(key)}`
141 : discrepancy === "missing-key"
142 ? `${toType(actual)} is missing property ${toValueString(key)}`
143 : `${toValueString(expected)} != ${toValueString(actual)}`;
144
145 const isInt = key => isInteger(+key);
146 const toAccess = key =>
147 isInt(key) ? `[${key}]` : isIdentifierName(key) ? `.${key}` : `["${key}"]`;
148
149 const toPathString = path => path.map(toAccess).join("");