Skip to content

Commit

Permalink
Improve sourcemap validator console report (#9131)
Browse files Browse the repository at this point in the history
The report printed to the console for invalid source map samples has
been improved in a few ways:

* The entire message is now printed using `console.error`, so the
contents aren't split between STDERR and STDOUT
* The code fence is now guaranteed to be a set length, rather than it
varying depending on the filename
* The code fence is no longer padded on the inside with newlines, which
results in a more compact output that is (in my opinion) just as
readable.
  • Loading branch information
Gudahtt committed Aug 7, 2020
1 parent c5ce4a1 commit e17c18f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions development/sourcemap-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,28 @@ async function validateSourcemapForFile ({ buildName }) {
const isMaybeValid = portion.includes(targetString)
if (!isMaybeValid) {
valid = false
console.error('Sourcemap seems invalid:')
console.log(`\n========================== ${result.source} ====================================\n`)
console.log(line)
console.log(`\n==============================================================================\n`)
console.error(`Sourcemap seems invalid:\n${getFencedCode(result.source, line)}`)
}
})
})
console.log(` checked ${sampleCount} samples`)
return valid
}

const CODE_FENCE_LENGTH = 80
const TITLE_PADDING_LENGTH = 1

function getFencedCode (filename, code) {
const title = `${' '.repeat(TITLE_PADDING_LENGTH)}${filename}${' '.repeat(TITLE_PADDING_LENGTH)}`
const openingFenceLength = Math.max(CODE_FENCE_LENGTH - (filename.length + (TITLE_PADDING_LENGTH * 2)), 0)
const startOpeningFenceLength = Math.floor(openingFenceLength / 2)
const endOpeningFenceLength = Math.ceil(openingFenceLength / 2)
const openingFence = `${'='.repeat(startOpeningFenceLength)}${title}${'='.repeat(endOpeningFenceLength)}`
const closingFence = '='.repeat(CODE_FENCE_LENGTH)

return `${openingFence}\n${code}\n${closingFence}\n`
}

function indicesOf (substring, string) {
const a = []
let i = -1
Expand Down

0 comments on commit e17c18f

Please sign in to comment.