@oclif/core 4.0.28 → 4.0.29

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.
@@ -2,7 +2,7 @@ type Options = {
2
2
  pretty?: boolean | undefined;
3
3
  theme?: Record<string, string> | undefined;
4
4
  };
5
- export declare function removeCycles(object: unknown): unknown;
5
+ export declare function stringifyInput(json?: unknown, options?: Options): string;
6
6
  export declare function tokenize(json?: unknown, options?: Options): {
7
7
  type: string;
8
8
  value: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeCycles = removeCycles;
3
+ exports.stringifyInput = stringifyInput;
4
4
  exports.tokenize = tokenize;
5
5
  exports.default = colorizeJson;
6
6
  const theme_1 = require("./theme");
@@ -16,46 +16,48 @@ const tokenTypes = [
16
16
  { regex: /^true|^false/, tokenType: 'boolean' },
17
17
  { regex: /^null/, tokenType: 'null' },
18
18
  ];
19
- function removeCycles(object) {
20
- // Keep track of seen objects.
21
- const seenObjects = new WeakMap();
22
- const _removeCycles = (obj) => {
23
- // Use object prototype to get around type and null checks
24
- if (Object.prototype.toString.call(obj) === '[object Object]') {
25
- // We know it is a "Record<string, unknown>" because of the conditional
26
- const dictionary = obj;
27
- // Seen, return undefined to remove.
28
- if (seenObjects.has(dictionary))
29
- return;
30
- seenObjects.set(dictionary, undefined);
31
- for (const key in dictionary) {
32
- // Delete the duplicate object if cycle found.
33
- if (_removeCycles(dictionary[key]) === undefined) {
34
- delete dictionary[key];
35
- }
36
- }
37
- }
38
- else if (Array.isArray(obj)) {
39
- for (const i in obj) {
40
- if (_removeCycles(obj[i]) === undefined) {
41
- // We don't want to delete the array, but we can replace the element with null.
42
- obj[i] = null;
43
- }
44
- }
19
+ function stringify(value, replacer, spaces) {
20
+ return JSON.stringify(value, serializer(replacer, replacer), spaces);
21
+ }
22
+ // Inspired by https://github.com/moll/json-stringify-safe
23
+ function serializer(replacer, cycleReplacer) {
24
+ const stack = [];
25
+ const keys = [];
26
+ if (!cycleReplacer)
27
+ cycleReplacer = function (key, value) {
28
+ if (stack[0] === value)
29
+ return '[Circular ~]';
30
+ return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
31
+ };
32
+ return function (key, value) {
33
+ if (stack.length > 0) {
34
+ // @ts-expect-error because `this` is not typed
35
+ const thisPos = stack.indexOf(this);
36
+ // @ts-expect-error because `this` is not typed
37
+ // eslint-disable-next-line no-bitwise
38
+ ~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
39
+ // eslint-disable-next-line no-bitwise
40
+ ~thisPos ? keys.splice(thisPos, Number.POSITIVE_INFINITY, key) : keys.push(key);
41
+ // @ts-expect-error because `this` is not typed
42
+ if (stack.includes(value))
43
+ value = cycleReplacer.call(this, key, value);
45
44
  }
46
- return obj;
45
+ else
46
+ stack.push(value);
47
+ // @ts-expect-error because `this` is not typed
48
+ return replacer ? replacer.call(this, key, value) : value;
47
49
  };
48
- return _removeCycles(object);
49
50
  }
50
- function formatInput(json, options) {
51
- return options?.pretty
52
- ? JSON.stringify(typeof json === 'string' ? JSON.parse(json) : json, null, 2)
51
+ function stringifyInput(json, options) {
52
+ const str = options?.pretty
53
+ ? stringify(typeof json === 'string' ? JSON.parse(json) : json, undefined, 2)
53
54
  : typeof json === 'string'
54
55
  ? json
55
- : JSON.stringify(json);
56
+ : stringify(json);
57
+ return str;
56
58
  }
57
59
  function tokenize(json, options) {
58
- let input = formatInput(removeCycles(json), options);
60
+ let input = stringifyInput(json, options);
59
61
  const tokens = [];
60
62
  let foundToken = false;
61
63
  do {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/core",
3
3
  "description": "base library for oclif CLIs",
4
- "version": "4.0.28",
4
+ "version": "4.0.29",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/core/issues",
7
7
  "dependencies": {
@@ -37,7 +37,7 @@
37
37
  "@types/debug": "^4.1.10",
38
38
  "@types/ejs": "^3.1.5",
39
39
  "@types/indent-string": "^4.0.1",
40
- "@types/mocha": "^10.0.8",
40
+ "@types/mocha": "^10.0.9",
41
41
  "@types/node": "^18",
42
42
  "@types/pnpapi": "^0.0.5",
43
43
  "@types/sinon": "^17.0.3",