MCPcopy
hub / github.com/webpack/webpack / normalizeString

Function normalizeString

test/checkArrayExpectation.js:30–76  ·  view source on GitHub ↗
(str, testDirectory)

Source from the content-addressed store, hash-verified

28 * @returns {string} normalized string
29 */
30const normalizeString = (str, testDirectory) => {
31 if (!str) return str;
32 const root = getWebpackRoot(testDirectory);
33 // Replace more-specific test dir first, then the broader root
34 str = str.replace(new RegExp(quoteMeta(testDirectory), "g"), "<TEST_DIR>");
35 if (root) {
36 str = str.replace(new RegExp(quoteMeta(root), "g"), "<WEBPACK_ROOT>");
37 // Replace ancestor directories above webpack root.
38 // The resolver walks up to the filesystem root looking for
39 // node_modules, producing paths like /Users/x/node_modules.
40 let ancestor = path.dirname(root);
41 while (ancestor !== path.dirname(ancestor)) {
42 str = str.replace(new RegExp(quoteMeta(ancestor), "g"), "<ANCESTOR>");
43 ancestor = path.dirname(ancestor);
44 }
45 }
46 // Normalize the output directory suite name (e.g. test/js/ConfigTestCases/
47 // vs test/js/ConfigCacheTestCases/) so all suites produce identical snapshots.
48 str = str.replace(/(<WEBPACK_ROOT>\/test\/js\/)[^/]+\//g, "$1<OUTPUT>/");
49 // Strip stack trace lines — line numbers vary across Node.js versions
50 // and between runs (e.g. processTicksAndRejections).
51 str = str
52 .split("\n")
53 .filter((line) => !/^\s+at\s/.test(line))
54 .join("\n")
55 .replace(/\n{3,}/g, "\n\n")
56 .trim();
57 // Normalize engine-specific JSON.parse error text (V8 "Unexpected token …
58 // (0x..)" / "… in JSON …" vs JSC/Bun "JSON Parse error: …") so JSON
59 // type-import "Module parse failed" snapshots match across runtimes. The
60 // acorn "(line:col)" parse errors are engine-independent and left as-is.
61 str = str.replace(
62 /Module parse failed: (?:JSON Parse error:|Unexpected token "[^"]*" \(0x|Unexpected \S+ in JSON|Unexpected end of JSON input|Unexpected non-whitespace character after JSON)[\s\S]*?(?=\nYou may need an appropriate loader)/g,
63 "Module parse failed: <JSON parse error>"
64 );
65 // Normalize JSC (Bun) magic-comment parse phrasing to the V8 form: JSC
66 // quotes the token and appends "Expected …", V8 does neither.
67 str = str.replace(/(Unexpected token) '([^']*)'\. Expected[^\n]*/g, "$1 $2");
68 str = str.replace(/(Unexpected identifier)\. Expected[^\n]*/g, "$1");
69 // Normalize "Unexpected token" messages — quoting and detail
70 // format varies across Node.js versions (e.g. with/without quotes,
71 // hex codes, trailing context).
72 str = str.replace(/(Unexpected token) '([^']*)'$/gm, "$1 $2");
73 str = str.replace(/(Unexpected token[^)]*\))[^\n]*(?:\n['"])?/g, "$1");
74 str = str.replace(/\\/g, "/");
75 return str;
76};
77
78/**
79 * Serializes an array of error/warning objects into a normalized form

Callers 1

normalizeForSnapshotFunction · 0.85

Calls 5

getWebpackRootFunction · 0.85
splitMethod · 0.80
quoteMetaFunction · 0.70
replaceMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected