GeezerVison is showing github.com — util: escape symbol and non-enumerable keys · nodejs/node@f763ac7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/util.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1208,9 +1208,10 @@ function formatProperty(ctx, value, recurseTimes, key, array) { | 1208 | 1208 | return str; | 1209 | 1209 | } | 1210 | 1210 | if (typeof key === 'symbol') { | 1211 | | - name = `[${ctx.stylize(key.toString(), 'symbol')}]`; | | 1211 | + const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn); | | 1212 | + name = `[${ctx.stylize(tmp, 'symbol')}]`; | 1212 | 1213 | } else if (desc.enumerable === false) { | 1213 | | - name = `[${key}]`; | | 1214 | + name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`; | 1214 | 1215 | } else if (keyStrRegExp.test(key)) { | 1215 | 1216 | name = ctx.stylize(key, 'name'); | 1216 | 1217 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +15-5Lines changed: 15 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -890,22 +890,22 @@ if (typeof Symbol !== 'undefined') { | 890 | 890 | const options = { showHidden: true }; | 891 | 891 | let subject = {}; | 892 | 892 |
| 893 | | - subject[Symbol('symbol')] = 42; | | 893 | + subject[Symbol('sym\nbol')] = 42; | 894 | 894 |
| 895 | | - assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }'); | | 895 | + assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }'); | 896 | 896 | assert.strictEqual( | 897 | 897 | util.inspect(subject, options), | 898 | | - '{ [Symbol(symbol)]: 42 }' | | 898 | + '{ [Symbol(sym\\nbol)]: 42 }' | 899 | 899 | ); | 900 | 900 |
| 901 | 901 | Object.defineProperty( | 902 | 902 | subject, | 903 | 903 | Symbol(), | 904 | 904 | { enumerable: false, value: 'non-enum' }); | 905 | | - assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }'); | | 905 | + assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }'); | 906 | 906 | assert.strictEqual( | 907 | 907 | util.inspect(subject, options), | 908 | | - "{ [Symbol(symbol)]: 42, [Symbol()]: 'non-enum' }" | | 908 | + "{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }" | 909 | 909 | ); | 910 | 910 |
| 911 | 911 | subject = [1, 2, 3]; | @@ -1678,3 +1678,13 @@ util.inspect(process); | 1678 | 1678 | assert.strictEqual(inspect(1n), '1n'); | 1679 | 1679 | assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]'); | 1680 | 1680 | assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]'); | | 1681 | + | | 1682 | +// Verify non-enumerable keys get escaped. | | 1683 | +{ | | 1684 | + const obj = {}; | | 1685 | + Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true }); | | 1686 | + assert.strictEqual( | | 1687 | + util.inspect(obj, { showHidden: true }), | | 1688 | + '{ [Non\\nenumerable\\tkey]: true }' | | 1689 | + ); | | 1690 | +} |
|