Last active
February 3, 2023 20:42
-
-
Save dallasread/edd111a3ef1a298afbffd9f75d2e24b7 to your computer and use it in GitHub Desktop.
GitHub review improvements
This file contains hidden or 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
// ==UserScript== | |
// @name GitHubReviewSpecMover | |
// @version 7 | |
// @grant none | |
// @match https://github.com/* | |
// ==/UserScript== | |
((attr) => { | |
const backoff = (func, startingDelay = 500, factor = 1.3) => { | |
func() | |
setTimeout(() => backoff(func, startingDelay * factor, factor), startingDelay, factor) | |
} | |
const simpleName = (filePath) => { | |
return filePath.slice(filePath.lastIndexOf('/'), filePath.length).split('.')[0] | |
} | |
const isSpec = (filePath, srcFile) => { | |
if (srcFile) { | |
const matcher = new RegExp(`${simpleName(srcFile)}[\._]spec`) | |
if (!matcher.test(filePath)) { | |
return false | |
} | |
} | |
return filePath && filePath.indexOf('spec') !== -1 | |
} | |
const findSpecMatch = (filePaths, filePath) => { | |
return filePaths.find((fp) => fp !== filePath && isSpec(fp, filePath) && simpleName(fp).indexOf(simpleName(filePath)) === 0) | |
} | |
const moveSpecNodes = () => { | |
const checkedAttr = `checked-${attr}` | |
const els = [...document.querySelectorAll(`[${attr}]`)] | |
const filePaths = els.map((el) => el.getAttribute(attr)) | |
const specsPaths = filePaths.filter((path) => isSpec(path)) | |
const nonSpecsPaths = filePaths.filter((path) => !isSpec(path)) | |
const pathsWithMissingSpecs = nonSpecsPaths.filter((path) => !findSpecMatch(specsPaths, path)) | |
const pathsWithSpecs = nonSpecsPaths.filter((path) => findSpecMatch(specsPaths, path)) | |
const missingNodes = [...document.querySelectorAll(`.missing-${checkedAttr}`)] | |
missingNodes.forEach((el) => el.parentNode.removeChild(el)) | |
pathsWithMissingSpecs.forEach((path) => { | |
const filePathNode = els.find((el) => el.getAttribute(attr) === path) | |
filePathNode.querySelector('.file-info').innerHTML += `<span class="missing-${checkedAttr}" style="color: red">Missing spec</span>` | |
}) | |
pathsWithSpecs.forEach((path) => { | |
const filePathNode = els.find((el) => el.getAttribute(attr) === path) | |
const matchedFilePath = findSpecMatch(filePaths, path) | |
const matchedFilePathNode = els.find((el) => el.getAttribute(attr) === matchedFilePath) | |
filePathNode.parentNode.insertBefore(matchedFilePathNode, filePathNode.nextSibling); | |
}) | |
} | |
if (document.readyState == 'complete') { | |
moveSpecNodes() | |
} else { | |
document.addEventListener('turbo:load', moveSpecNodes) | |
} | |
backoff(moveSpecNodes) | |
})('data-tagsearch-path') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment