Skip to content

Commit

Permalink
Sort __proto__ field before other properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed May 25, 2020
1 parent 354c2ca commit a152001
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
18 changes: 9 additions & 9 deletions print.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default function print(value, ...args){

const linesBefore = [];
const linesAfter = [];
const propLines = [];
const recurse = (v, k, p, f) => print(v, k, opts, refs, p || path, f);
const isArrayBuffer = value instanceof ArrayBuffer || "function" === typeof SharedArrayBuffer && value instanceof SharedArrayBuffer;
let isArrayLike = false;
Expand Down Expand Up @@ -342,8 +343,15 @@ export default function print(value, ...args){
// Identify Number/Math globals so we know when not to identify “magic” numbers
flags = (Math === value) << 2 | (Number === value) << 1;

// Show the `__proto__` object if possible (and requested)
if(opts.proto){
let proto;
try{ proto = value.__proto__; }
catch(e){ proto = e; opts = {...opts, proto: false}; }
propLines.push(recurse(proto, "__proto__", 0, flags));
}

// Inspect each property we're interested in displaying
const propLines = [];
for(let prop of props){
const desc = Object.getOwnPropertyDescriptor(value, prop);

Expand All @@ -368,14 +376,6 @@ export default function print(value, ...args){
else propLines.push(recurse(desc.value, prop, 0, flags));
}

// Show the `__proto__` object if possible (and requested)
if(opts.proto){
let proto;
try{ proto = value.__proto__; }
catch(e){ proto = e; opts = {...opts, proto: false}; }
propLines.push(recurse(proto, "__proto__", 0, flags));
}

// Pick an appropriate pair of brackets
value = isArrayLike
? [punct + "[" + off, "\n", punct + "]" + off]
Expand Down
26 changes: 13 additions & 13 deletions test/02-properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ describe("Property fields", () => {
const bar = {name: "Bar"};
bar.__proto__ = foo;
expect({foo, bar}).to.print(`{
__proto__: {
Null prototype
__proto__: null
}
foo: {
__proto__: -> {root}.__proto__
name: "Foo"
__proto__: {
Null prototype
__proto__: null
}
}
bar: {
name: "Bar"
__proto__: -> {root}.foo
name: "Bar"
}
__proto__: -> {root}.foo.__proto__
}`, {proto: true});
});

Expand All @@ -142,17 +142,17 @@ describe("Property fields", () => {
},
});
expect({foo}).to.print(`{
foo: {
name: "Foo"
__proto__: Error {
no: "Really"
}
}
__proto__: {
Null prototype
__proto__: null
}
foo: {
__proto__: Error {
no: "Really"
}
name: "Foo"
}
}`, {proto: true});
});
});
Expand Down

0 comments on commit a152001

Please sign in to comment.