* Sanitizes variable names in minified output to aid in debugging. * 1. Normalizes the length of all jscomp variables, so that prettier will * format it the same. * 2. Strips numbers from the sanitized jscomp variables so that a single extra * variable doesn't cause thousands of diffs. *
(file)
| 14 | * @return {Promise<void>} |
| 15 | */ |
| 16 | async function sanitize(file) { |
| 17 | if (!argv.sanitize_vars_for_diff) { |
| 18 | return; |
| 19 | } |
| 20 | const contents = await fs.readFile(file, 'utf-8'); |
| 21 | const config = await prettier.resolveConfig(file); |
| 22 | const options = {filepath: file, parser: 'babel', ...config}; |
| 23 | const replaced = Object.create(null); |
| 24 | let count = 0; |
| 25 | const presanitize = contents.replace( |
| 26 | /(?:[a-zA-Z$_][a-zA-Z$_0-9]*)?(?:JSCompiler|jscomp)[a-zA-Z$_0-9]*/g, |
| 27 | (match) => |
| 28 | replaced[match] || |
| 29 | (replaced[match] = `___${String(count++).padStart(6, '0')}___`) |
| 30 | ); |
| 31 | const formatted = await prettier.format(presanitize, options); |
| 32 | const sanitized = formatted.replace(/___\d+___/g, '______'); |
| 33 | await fs.outputFile(file, sanitized); |
| 34 | } |
| 35 | |
| 36 | module.exports = {sanitize}; |
nothing calls this directly
no test coverage detected