@vitest/utils 2.0.0-beta.1 → 2.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ast.js CHANGED
@@ -14,8 +14,9 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
14
14
  const identifiers = [];
15
15
  const setScope = (node, name) => {
16
16
  let scopeIds = scopeMap.get(node);
17
- if (scopeIds && scopeIds.has(name))
17
+ if (scopeIds && scopeIds.has(name)) {
18
18
  return;
19
+ }
19
20
  if (!scopeIds) {
20
21
  scopeIds = /* @__PURE__ */ new Set();
21
22
  scopeMap.set(node, scopeIds);
@@ -35,15 +36,17 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
35
36
  handlePattern(p.argument, parentScope);
36
37
  } else if (p.type === "ObjectPattern") {
37
38
  p.properties.forEach((property) => {
38
- if (property.type === "RestElement")
39
+ if (property.type === "RestElement") {
39
40
  setScope(parentScope, property.argument.name);
40
- else
41
+ } else {
41
42
  handlePattern(property.value, parentScope);
43
+ }
42
44
  });
43
45
  } else if (p.type === "ArrayPattern") {
44
46
  p.elements.forEach((element) => {
45
- if (element)
47
+ if (element) {
46
48
  handlePattern(element, parentScope);
49
+ }
47
50
  });
48
51
  } else if (p.type === "AssignmentPattern") {
49
52
  handlePattern(p.left, parentScope);
@@ -53,18 +56,23 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
53
56
  }
54
57
  walk(root, {
55
58
  enter(node, parent) {
56
- if (node.type === "ImportDeclaration")
59
+ if (node.type === "ImportDeclaration") {
57
60
  return this.skip();
58
- if (parent && !(parent.type === "IfStatement" && node === parent.alternate))
61
+ }
62
+ if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
59
63
  parentStack.unshift(parent);
60
- if (node.type === "VariableDeclaration")
64
+ }
65
+ if (node.type === "VariableDeclaration") {
61
66
  varKindStack.unshift(node.kind);
62
- if (node.type === "CallExpression")
67
+ }
68
+ if (node.type === "CallExpression") {
63
69
  onCallExpression == null ? void 0 : onCallExpression(node);
64
- if (node.type === "MetaProperty" && node.meta.name === "import")
70
+ }
71
+ if (node.type === "MetaProperty" && node.meta.name === "import") {
65
72
  onImportMeta == null ? void 0 : onImportMeta(node);
66
- else if (node.type === "ImportExpression")
73
+ } else if (node.type === "ImportExpression") {
67
74
  onDynamicImport == null ? void 0 : onDynamicImport(node);
75
+ }
68
76
  if (node.type === "Identifier") {
69
77
  if (!isInScope(node.name, parentStack) && isRefIdentifier(node, parent, parentStack)) {
70
78
  identifiers.push([node, parentStack.slice(0)]);
@@ -72,8 +80,9 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
72
80
  } else if (isFunctionNode(node)) {
73
81
  if (node.type === "FunctionDeclaration") {
74
82
  const parentScope = findParentScope(parentStack);
75
- if (parentScope)
83
+ if (parentScope) {
76
84
  setScope(parentScope, node.id.name);
85
+ }
77
86
  }
78
87
  node.params.forEach((p) => {
79
88
  if (p.type === "ObjectPattern" || p.type === "ArrayPattern") {
@@ -82,14 +91,18 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
82
91
  }
83
92
  walk(p.type === "AssignmentPattern" ? p.left : p, {
84
93
  enter(child, parent2) {
85
- if ((parent2 == null ? void 0 : parent2.type) === "AssignmentPattern" && (parent2 == null ? void 0 : parent2.right) === child)
94
+ if ((parent2 == null ? void 0 : parent2.type) === "AssignmentPattern" && (parent2 == null ? void 0 : parent2.right) === child) {
86
95
  return this.skip();
87
- if (child.type !== "Identifier")
96
+ }
97
+ if (child.type !== "Identifier") {
88
98
  return;
89
- if (isStaticPropertyKey(child, parent2))
99
+ }
100
+ if (isStaticPropertyKey(child, parent2)) {
90
101
  return;
91
- if ((parent2 == null ? void 0 : parent2.type) === "TemplateLiteral" && (parent2 == null ? void 0 : parent2.expressions.includes(child)) || (parent2 == null ? void 0 : parent2.type) === "CallExpression" && (parent2 == null ? void 0 : parent2.callee) === child)
102
+ }
103
+ if ((parent2 == null ? void 0 : parent2.type) === "TemplateLiteral" && (parent2 == null ? void 0 : parent2.expressions.includes(child)) || (parent2 == null ? void 0 : parent2.type) === "CallExpression" && (parent2 == null ? void 0 : parent2.callee) === child) {
92
104
  return;
105
+ }
93
106
  setScope(node, child.name);
94
107
  }
95
108
  });
@@ -101,17 +114,20 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
101
114
  parentStack,
102
115
  varKindStack[0] === "var"
103
116
  );
104
- if (parentFunction)
117
+ if (parentFunction) {
105
118
  handlePattern(node.id, parentFunction);
119
+ }
106
120
  } else if (node.type === "CatchClause" && node.param) {
107
121
  handlePattern(node.param, node);
108
122
  }
109
123
  },
110
124
  leave(node, parent) {
111
- if (parent && !(parent.type === "IfStatement" && node === parent.alternate))
125
+ if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
112
126
  parentStack.shift();
113
- if (node.type === "VariableDeclaration")
127
+ }
128
+ if (node.type === "VariableDeclaration") {
114
129
  varKindStack.shift();
130
+ }
115
131
  }
116
132
  });
117
133
  identifiers.forEach(([node, stack]) => {
@@ -121,37 +137,51 @@ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallEx
121
137
  const hasBindingShortcut = isStaticProperty(parent) && parent.shorthand && (!isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack));
122
138
  const classDeclaration = parent.type === "PropertyDefinition" && (grandparent == null ? void 0 : grandparent.type) === "ClassBody" || parent.type === "ClassDeclaration" && node === parent.superClass;
123
139
  const classExpression = parent.type === "ClassExpression" && node === parent.id;
124
- onIdentifier == null ? void 0 : onIdentifier(node, {
125
- hasBindingShortcut,
126
- classDeclaration,
127
- classExpression
128
- }, stack);
140
+ onIdentifier == null ? void 0 : onIdentifier(
141
+ node,
142
+ {
143
+ hasBindingShortcut,
144
+ classDeclaration,
145
+ classExpression
146
+ },
147
+ stack
148
+ );
129
149
  }
130
150
  });
131
151
  }
132
152
  function isRefIdentifier(id, parent, parentStack) {
133
- if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id)
153
+ if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id) {
134
154
  return false;
155
+ }
135
156
  if (isFunctionNode(parent)) {
136
- if (parent.id === id)
157
+ if (parent.id === id) {
137
158
  return false;
138
- if (parent.params.includes(id))
159
+ }
160
+ if (parent.params.includes(id)) {
139
161
  return false;
162
+ }
140
163
  }
141
- if (parent.type === "MethodDefinition" && !parent.computed)
164
+ if (parent.type === "MethodDefinition" && !parent.computed) {
142
165
  return false;
143
- if (isStaticPropertyKey(id, parent))
166
+ }
167
+ if (isStaticPropertyKey(id, parent)) {
144
168
  return false;
145
- if (isNodeInPattern(parent) && parent.value === id)
169
+ }
170
+ if (isNodeInPattern(parent) && parent.value === id) {
146
171
  return false;
147
- if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack))
172
+ }
173
+ if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack)) {
148
174
  return false;
149
- if (parent.type === "MemberExpression" && parent.property === id && !parent.computed)
175
+ }
176
+ if (parent.type === "MemberExpression" && parent.property === id && !parent.computed) {
150
177
  return false;
151
- if (parent.type === "ExportSpecifier")
178
+ }
179
+ if (parent.type === "ExportSpecifier") {
152
180
  return false;
153
- if (id.name === "arguments")
181
+ }
182
+ if (id.name === "arguments") {
154
183
  return false;
184
+ }
155
185
  return true;
156
186
  }
157
187
  function isStaticProperty(node) {
@@ -172,8 +202,9 @@ function findParentScope(parentStack, isVar = false) {
172
202
  return parentStack.find(isVar ? isFunctionNode : isBlock);
173
203
  }
174
204
  function isInDestructuringAssignment(parent, parentStack) {
175
- if (parent && (parent.type === "Property" || parent.type === "ArrayPattern"))
205
+ if (parent && (parent.type === "Property" || parent.type === "ArrayPattern")) {
176
206
  return parentStack.some((i) => i.type === "AssignmentExpression");
207
+ }
177
208
  return false;
178
209
  }
179
210
 
@@ -33,10 +33,13 @@ function string(str) {
33
33
  }
34
34
  string.open = "";
35
35
  string.close = "";
36
- const defaultColors = /* @__PURE__ */ colorsEntries.reduce((acc, [key]) => {
37
- acc[key] = string;
38
- return acc;
39
- }, { isColorSupported: false });
36
+ const defaultColors = /* @__PURE__ */ colorsEntries.reduce(
37
+ (acc, [key]) => {
38
+ acc[key] = string;
39
+ return acc;
40
+ },
41
+ { isColorSupported: false }
42
+ );
40
43
  function getDefaultColors() {
41
44
  return { ...defaultColors };
42
45
  }
@@ -46,10 +49,14 @@ function getColors() {
46
49
  function createColors(isTTY = false) {
47
50
  const enabled = typeof process !== "undefined" && !("NO_COLOR" in process.env || process.argv.includes("--no-color")) && !("GITHUB_ACTIONS" in process.env) && ("FORCE_COLOR" in process.env || process.argv.includes("--color") || process.platform === "win32" || isTTY && process.env.TERM !== "dumb" || "CI" in process.env);
48
51
  const replaceClose = (string2, close, replace, index) => {
49
- const start = string2.substring(0, index) + replace;
50
- const end = string2.substring(index + close.length);
51
- const nextIndex = end.indexOf(close);
52
- return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end;
52
+ let result = "";
53
+ let cursor = 0;
54
+ do {
55
+ result += string2.substring(cursor, index) + replace;
56
+ cursor = index + close.length;
57
+ index = string2.indexOf(close, cursor);
58
+ } while (~index);
59
+ return result + string2.substring(cursor);
53
60
  };
54
61
  const formatter = (open, close, replace = open) => {
55
62
  const fn = (input) => {
@@ -41,43 +41,51 @@ function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
41
41
  return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(maxDepth / 2)) : result;
42
42
  }
43
43
 
44
- const formatRegExp = /%[sdjifoOcj%]/g;
44
+ const formatRegExp = /%[sdjifoOc%]/g;
45
45
  function format(...args) {
46
46
  if (typeof args[0] !== "string") {
47
47
  const objects = [];
48
- for (let i2 = 0; i2 < args.length; i2++)
48
+ for (let i2 = 0; i2 < args.length; i2++) {
49
49
  objects.push(inspect(args[i2], { depth: 0, colors: false }));
50
+ }
50
51
  return objects.join(" ");
51
52
  }
52
53
  const len = args.length;
53
54
  let i = 1;
54
55
  const template = args[0];
55
56
  let str = String(template).replace(formatRegExp, (x) => {
56
- if (x === "%%")
57
+ if (x === "%%") {
57
58
  return "%";
58
- if (i >= len)
59
+ }
60
+ if (i >= len) {
59
61
  return x;
62
+ }
60
63
  switch (x) {
61
64
  case "%s": {
62
65
  const value = args[i++];
63
- if (typeof value === "bigint")
66
+ if (typeof value === "bigint") {
64
67
  return `${value.toString()}n`;
65
- if (typeof value === "number" && value === 0 && 1 / value < 0)
68
+ }
69
+ if (typeof value === "number" && value === 0 && 1 / value < 0) {
66
70
  return "-0";
67
- if (typeof value === "object" && value !== null)
71
+ }
72
+ if (typeof value === "object" && value !== null) {
68
73
  return inspect(value, { depth: 0, colors: false });
74
+ }
69
75
  return String(value);
70
76
  }
71
77
  case "%d": {
72
78
  const value = args[i++];
73
- if (typeof value === "bigint")
79
+ if (typeof value === "bigint") {
74
80
  return `${value.toString()}n`;
81
+ }
75
82
  return Number(value).toString();
76
83
  }
77
84
  case "%i": {
78
85
  const value = args[i++];
79
- if (typeof value === "bigint")
86
+ if (typeof value === "bigint") {
80
87
  return `${value.toString()}n`;
88
+ }
81
89
  return Number.parseInt(String(value)).toString();
82
90
  }
83
91
  case "%f":
@@ -98,8 +106,9 @@ function format(...args) {
98
106
  if (
99
107
  // chromium
100
108
  m.includes("circular structure") || m.includes("cyclic structures") || m.includes("cyclic object")
101
- )
109
+ ) {
102
110
  return "[Circular]";
111
+ }
103
112
  throw err;
104
113
  }
105
114
  default:
@@ -107,21 +116,24 @@ function format(...args) {
107
116
  }
108
117
  });
109
118
  for (let x = args[i]; i < len; x = args[++i]) {
110
- if (x === null || typeof x !== "object")
119
+ if (x === null || typeof x !== "object") {
111
120
  str += ` ${x}`;
112
- else
121
+ } else {
113
122
  str += ` ${inspect(x)}`;
123
+ }
114
124
  }
115
125
  return str;
116
126
  }
117
127
  function inspect(obj, options = {}) {
118
- if (options.truncate === 0)
128
+ if (options.truncate === 0) {
119
129
  options.truncate = Number.POSITIVE_INFINITY;
130
+ }
120
131
  return loupe.inspect(obj, options);
121
132
  }
122
133
  function objDisplay(obj, options = {}) {
123
- if (typeof options.truncate === "undefined")
134
+ if (typeof options.truncate === "undefined") {
124
135
  options.truncate = 40;
136
+ }
125
137
  const str = inspect(obj, options);
126
138
  const type = Object.prototype.toString.call(obj);
127
139
  if (options.truncate && str.length >= options.truncate) {