From 7f87bdb2134604c8ca5d3d54e5064eeb5a219f50 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Tue, 4 Aug 2020 12:55:11 -0300 Subject: [PATCH] Improve sourcemap validator console report (#9131) 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. --- development/sourcemap-validator.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/development/sourcemap-validator.js b/development/sourcemap-validator.js index 2fda2a114..d4d9b8293 100644 --- a/development/sourcemap-validator.js +++ b/development/sourcemap-validator.js @@ -99,10 +99,7 @@ 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)}`) } }) }) @@ -110,6 +107,20 @@ async function validateSourcemapForFile ({ buildName }) { 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