Skip to content

Commit a714f66

Browse files
committed
util: compatibility patch
This patch makes sure no breaking changes from the referenced semver-major PR are backported. Refs: nodejs#27685
1 parent 33552f2 commit a714f66

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed

doc/api/util.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,6 @@ stream.write('With ES6');
391391
<!-- YAML
392392
added: v0.3.0
393393
changes:
394-
- version: REPLACEME
395-
pr-url: https://github.com/nodejs/node/pull/27685
396-
description: Circular references now include a marker to the reference.
397394
- version: v12.0.0
398395
pr-url: https://github.com/nodejs/node/pull/27109
399396
description: The `compact` options default is changed to `3` and the
@@ -516,7 +513,7 @@ util.inspect(new Bar()); // 'Bar {}'
516513
util.inspect(baz); // '[foo] {}'
517514
```
518515

519-
Circular references point to their anchor by using a reference index:
516+
Circular references are marked as `'[Circular]'`:
520517

521518
```js
522519
const { inspect } = require('util');
@@ -528,9 +525,9 @@ obj.b.inner = obj.b;
528525
obj.b.obj = obj;
529526

530527
console.log(inspect(obj));
531-
// <ref *1> {
532-
// a: [ [Circular *1] ],
533-
// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
528+
// {
529+
// a: [ [Circular] ],
530+
// b: { inner: [Circular], obj: [Circular] }
534531
// }
535532
```
536533

lib/internal/util/inspect.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
572572
ctx.circular.set(value, index);
573573
}
574574
}
575-
return ctx.stylize(`[Circular *${index}]`, 'special');
575+
return ctx.stylize('[Circular]', 'special');
576576
}
577577

578578
return formatRaw(ctx, value, recurseTimes, typedArray);
@@ -778,12 +778,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
778778
if (ctx.circular !== undefined) {
779779
const index = ctx.circular.get(value);
780780
if (index !== undefined) {
781-
const reference = ctx.stylize(`<ref *${index}>`, 'special');
782781
// Add reference always to the very beginning of the output.
783782
if (ctx.compact !== true) {
784-
base = base === '' ? reference : `${reference} ${base}`;
783+
base = base === '' ? '' : `${base}`;
785784
} else {
786-
braces[0] = `${reference} ${braces[0]}`;
785+
braces[0] = `${braces[0]}`;
787786
}
788787
}
789788
}

test/parallel/test-assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ testAssertionMessage({}, '{}');
343343
testAssertionMessage([1, 2, 3], '[\n+ 1,\n+ 2,\n+ 3\n+ ]');
344344
testAssertionMessage(function f() {}, '[Function: f]');
345345
testAssertionMessage(circular,
346-
'<ref *1> {\n+ x: [Circular *1],\n+ y: 1\n+ }');
346+
'{\n+ x: [Circular],\n+ y: 1\n+ }');
347347
testAssertionMessage({ a: undefined, b: null },
348348
'{\n+ a: undefined,\n+ b: null\n+ }');
349349
testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },

test/parallel/test-util-format.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ assert.strictEqual(
195195
'{\n' +
196196
' foo: \'bar\',\n' +
197197
' foobar: 1,\n' +
198-
' func: <ref *1> [Function: func] {\n' +
198+
' func: [Function: func] {\n' +
199199
' [length]: 0,\n' +
200200
' [name]: \'func\',\n' +
201-
' [prototype]: func { [constructor]: [Circular *1] }\n' +
201+
' [prototype]: func { [constructor]: [Circular] }\n' +
202202
' }\n' +
203203
'}');
204204
assert.strictEqual(
@@ -208,10 +208,10 @@ assert.strictEqual(
208208
' foobar: 1,\n' +
209209
' func: [\n' +
210210
' {\n' +
211-
' a: <ref *1> [Function: a] {\n' +
211+
' a: [Function: a] {\n' +
212212
' [length]: 0,\n' +
213213
' [name]: \'a\',\n' +
214-
' [prototype]: a { [constructor]: [Circular *1] }\n' +
214+
' [prototype]: a { [constructor]: [Circular] }\n' +
215215
' }\n' +
216216
' },\n' +
217217
' [length]: 1\n' +
@@ -223,10 +223,10 @@ assert.strictEqual(
223223
' foo: \'bar\',\n' +
224224
' foobar: {\n' +
225225
' foo: \'bar\',\n' +
226-
' func: <ref *1> [Function: func] {\n' +
226+
' func: [Function: func] {\n' +
227227
' [length]: 0,\n' +
228228
' [name]: \'func\',\n' +
229-
' [prototype]: func { [constructor]: [Circular *1] }\n' +
229+
' [prototype]: func { [constructor]: [Circular] }\n' +
230230
' }\n' +
231231
' }\n' +
232232
'}');
@@ -235,29 +235,29 @@ assert.strictEqual(
235235
'{\n' +
236236
' foo: \'bar\',\n' +
237237
' foobar: 1,\n' +
238-
' func: <ref *1> [Function: func] {\n' +
238+
' func: [Function: func] {\n' +
239239
' [length]: 0,\n' +
240240
' [name]: \'func\',\n' +
241-
' [prototype]: func { [constructor]: [Circular *1] }\n' +
241+
' [prototype]: func { [constructor]: [Circular] }\n' +
242242
' }\n' +
243243
'} {\n' +
244244
' foo: \'bar\',\n' +
245245
' foobar: 1,\n' +
246-
' func: <ref *1> [Function: func] {\n' +
246+
' func: [Function: func] {\n' +
247247
' [length]: 0,\n' +
248248
' [name]: \'func\',\n' +
249-
' [prototype]: func { [constructor]: [Circular *1] }\n' +
249+
' [prototype]: func { [constructor]: [Circular] }\n' +
250250
' }\n' +
251251
'}');
252252
assert.strictEqual(
253253
util.format('%o %o', obj),
254254
'{\n' +
255255
' foo: \'bar\',\n' +
256256
' foobar: 1,\n' +
257-
' func: <ref *1> [Function: func] {\n' +
257+
' func: [Function: func] {\n' +
258258
' [length]: 0,\n' +
259259
' [name]: \'func\',\n' +
260-
' [prototype]: func { [constructor]: [Circular *1] }\n' +
260+
' [prototype]: func { [constructor]: [Circular] }\n' +
261261
' }\n' +
262262
'} %o');
263263

test/parallel/test-util-inspect.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ if (typeof Symbol !== 'undefined') {
10211021
{
10221022
const set = new Set();
10231023
set.add(set);
1024-
assert.strictEqual(util.inspect(set), '<ref *1> Set { [Circular *1] }');
1024+
assert.strictEqual(util.inspect(set), 'Set { [Circular] }');
10251025
}
10261026

10271027
// Test Map.
@@ -1039,15 +1039,15 @@ if (typeof Symbol !== 'undefined') {
10391039
{
10401040
const map = new Map();
10411041
map.set(map, 'map');
1042-
assert.strictEqual(inspect(map), "<ref *1> Map { [Circular *1] => 'map' }");
1042+
assert.strictEqual(inspect(map), "Map { [Circular] => 'map' }");
10431043
map.set(map, map);
10441044
assert.strictEqual(
10451045
inspect(map),
1046-
'<ref *1> Map { [Circular *1] => [Circular *1] }'
1046+
'Map { [Circular] => [Circular] }'
10471047
);
10481048
map.delete(map);
10491049
map.set('map', map);
1050-
assert.strictEqual(inspect(map), "<ref *1> Map { 'map' => [Circular *1] }");
1050+
assert.strictEqual(inspect(map), "Map { 'map' => [Circular] }");
10511051
}
10521052

10531053
// Test multiple circular references.
@@ -1060,9 +1060,9 @@ if (typeof Symbol !== 'undefined') {
10601060

10611061
assert.strictEqual(
10621062
inspect(obj),
1063-
'<ref *1> {\n' +
1064-
' a: [ [Circular *1] ],\n' +
1065-
' b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }\n' +
1063+
'{\n' +
1064+
' a: [ [Circular] ],\n' +
1065+
' b: { inner: [Circular], obj: [Circular] }\n' +
10661066
'}'
10671067
);
10681068
}
@@ -1262,9 +1262,9 @@ if (typeof Symbol !== 'undefined') {
12621262
arr[0][0][0] = { a: 2 };
12631263
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
12641264
arr[0][0][0] = arr;
1265-
assert.strictEqual(util.inspect(arr), '<ref *1> [ [ [ [Circular *1] ] ] ]');
1265+
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
12661266
arr[0][0][0] = arr[0][0];
1267-
assert.strictEqual(util.inspect(arr), '[ [ <ref *1> [ [Circular *1] ] ] ]');
1267+
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
12681268
}
12691269

12701270
// Corner cases.
@@ -1631,7 +1631,7 @@ util.inspect(process);
16311631
' 2,',
16321632
' [length]: 2',
16331633
' ]',
1634-
' } => <ref *1> [Map Iterator] {',
1634+
' } => [Map Iterator] {',
16351635
' Uint8Array [',
16361636
' [BYTES_PER_ELEMENT]: 1,',
16371637
' [length]: 0,',
@@ -1642,7 +1642,7 @@ util.inspect(process);
16421642
' foo: true',
16431643
' }',
16441644
' ],',
1645-
' [Circular *1]',
1645+
' [Circular]',
16461646
' },',
16471647
' [size]: 2',
16481648
'}'
@@ -1670,15 +1670,15 @@ util.inspect(process);
16701670
' [byteOffset]: 0,',
16711671
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
16721672
' ],',
1673-
' [Set Iterator] { [ 1, 2, [length]: 2 ] } => <ref *1> [Map Iterator] {',
1673+
' [Set Iterator] { [ 1, 2, [length]: 2 ] } => [Map Iterator] {',
16741674
' Uint8Array [',
16751675
' [BYTES_PER_ELEMENT]: 1,',
16761676
' [length]: 0,',
16771677
' [byteLength]: 0,',
16781678
' [byteOffset]: 0,',
16791679
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
16801680
' ],',
1681-
' [Circular *1]',
1681+
' [Circular]',
16821682
' },',
16831683
' [size]: 2',
16841684
'}'
@@ -1710,7 +1710,7 @@ util.inspect(process);
17101710
' [Set Iterator] {',
17111711
' [ 1,',
17121712
' 2,',
1713-
' [length]: 2 ] } => <ref *1> [Map Iterator] {',
1713+
' [length]: 2 ] } => [Map Iterator] {',
17141714
' Uint8Array [',
17151715
' [BYTES_PER_ELEMENT]: 1,',
17161716
' [length]: 0,',
@@ -1719,7 +1719,7 @@ util.inspect(process);
17191719
' [buffer]: ArrayBuffer {',
17201720
' byteLength: 0,',
17211721
' foo: true } ],',
1722-
' [Circular *1] },',
1722+
' [Circular] },',
17231723
' [size]: 2 }'
17241724
].join('\n');
17251725

0 commit comments

Comments
 (0)