MCPcopy
hub / github.com/ampproject/amphtml / sanitize

Function sanitize

build-system/compile/sanitize.js:16–34  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

14 * @return {Promise<void>}
15 */
16async 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
36module.exports = {sanitize};

Callers

nothing calls this directly

Calls 4

resolveConfigMethod · 0.80
StringFunction · 0.50
createMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected