-
-
Save jakearchibald/ceb666e0a9271b55cd48d07dac2b74b6 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
const definitionsInOl = new WeakMap(); | |
function isDefinition(varNode) { | |
// Likely to be args | |
if (!varNode.closest('ol')) return true; | |
const previousNode = varNode.previousSibling; | |
if (!previousNode) return false; | |
if (previousNode.nodeType !== 3) return false; | |
if (previousNode.nodeValue.trim().toLowerCase().endsWith('let')) return true; | |
const previousEl = varNode.previousElementSibling; | |
if (!previousEl) return false; | |
return previousEl.textContent.trim().toLowerCase() === 'for each'; | |
} | |
function getDefsForList(list) { | |
let defs = definitionsInOl.get(list); | |
if (!defs) { | |
defs = []; | |
definitionsInOl.set(list, defs); | |
} | |
return defs; | |
} | |
function main() { | |
const vars = Array.from(document.querySelectorAll('var')); | |
const definitions = vars.filter(v => isDefinition(v)); | |
loopDefs: for (const definition of definitions) { | |
const name = definition.textContent.trim(); | |
let list = definition.closest('ol'); | |
if (!list) { | |
const p = definition.closest('p'); | |
let nextEl = p && p.nextElementSibling; | |
while (nextEl) { | |
if (!nextEl.classList.contains('note')) break; | |
nextEl = nextEl.nextElementSibling; | |
} | |
if (nextEl && nextEl.tagName === 'OL') { | |
list = nextEl; | |
} | |
else { | |
continue; | |
} | |
} | |
const defs = getDefsForList(list); | |
if (defs.includes(name)) { | |
definition.after('(redefinition)'); | |
definition.style.background = '#f00'; | |
console.log(definition); | |
continue; | |
} | |
defs.push(name); | |
} | |
const nondefs = vars.filter(v => !isDefinition(v)); | |
loopVars: for (const v of nondefs) { | |
const name = v.textContent.trim(); | |
let list = v.closest('ol'); | |
while (list) { | |
if (getDefsForList(list).includes(name)) continue loopVars; | |
list = list.parentElement.closest('ol'); | |
} | |
v.after('(definition not found)'); | |
v.style.background = '#f00'; | |
console.log(v); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment