Last active
April 23, 2022 08:40
-
-
Save pygy/7a19085d48bcd24fa76e0c9642f511cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Never forget a stray console.log() | |
// ---------------------------------- | |
// | |
// This one is for you printf debuggers | |
// | |
// This spies on console.*() calls and reports the call sites | |
// ordered by file name then by position, all at once at the | |
// end of the report (while making the suite fail, this counts | |
// as a failed assertion). | |
import o from 'ospec' | |
function calledFrom(n=0) { | |
try {throw new Error()} catch(e) {return e.stack.split("\n")[2+n].replace(/^\s*/, "")} | |
} | |
// the ospec spy doesn't cut it here, we want the call sites | |
function spy(f) { | |
function spied() { | |
spied.callSites.add(calledFrom(1)) | |
return f.apply(this, arguments) | |
} | |
return Object.assign(spied, {callSites: new Set}) | |
} | |
function strip(s) { | |
// normalize the format | |
return (s.indexOf("(") === -1) ? s.slice(3) : s.replace(/^[^\(]+\(|\)$/g, '') | |
} | |
function sort(ary) { | |
return ary.map(strip).sort(function(a, b) { | |
const [nameA, lineA, colA] = a.split(':') | |
const [nameB, lineB, colB] = b.split(':') | |
// const [name, line, col] = partsA | |
return nameA.localeCompare(nameB) | |
|| Number(lineA) - Number(lineB) | |
|| Number(colA) - Number(colB) | |
}) | |
} | |
Object.entries(console).forEach(([k, v]) => { | |
console[k] = spy(v) | |
}) | |
o.after(function(){ | |
Object.entries(console).forEach(([k, v]) => { | |
o(v.callSites).satisfies( | |
set => ( | |
set.size === 0 | |
? {pass: true} | |
: {pass: false, message: | |
`console.${k}() was called from ${set.size} site${set.size === 1 ? '' : 's'} | |
${sort([...v.callSites]).map(s => "... " + s).join('\n')} | |
` | |
} | |
) | |
) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
o.after( ): | |
console.log() was called from 5 sites | |
... file:///Users/pygy/dev/compose-regexp.js/src/core.js:40:12 | |
... file:///Users/pygy/dev/compose-regexp.js/src/core.js:40:31 | |
... file:///Users/pygy/dev/compose-regexp.js/src/core.js:254:9 | |
... file:///Users/pygy/dev/compose-regexp.js/src/core.js:318:9 | |
... file:///Users/pygy/dev/compose-regexp.js/tests/tests.js:661:9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment