MCPcopy
hub / github.com/webpack/webpack / renameInlinedModuleVariable

Function renameInlinedModuleVariable

lib/javascript/JavascriptModulesPlugin.js:362–416  ·  view source on GitHub ↗
(
	source,
	ast,
	variable,
	moduleIdentifier,
	readableIdentifier,
	allUsedNames
)

Source from the content-addressed store, hash-verified

360 * @returns {void}
361 */
362const renameInlinedModuleVariable = (
363 source,
364 ast,
365 variable,
366 moduleIdentifier,
367 readableIdentifier,
368 allUsedNames
369) => {
370 /** @type {UsedNamesInScopeInfo} */
371 const usedNamesInScopeInfo = new Map();
372 /** @type {ScopeSet} */
373 const ignoredScopes = new Set();
374
375 const name = variable.name;
376 const { usedNames, alreadyCheckedScopes } = getUsedNamesInScopeInfo(
377 usedNamesInScopeInfo,
378 moduleIdentifier,
379 name
380 );
381
382 if (allUsedNames.has(name) || usedNames.has(name)) {
383 const references = getAllReferences(variable);
384 const allIdentifiers = new Set([
385 ...references.map((r) => r.identifier),
386 ...variable.identifiers
387 ]);
388 for (const ref of references) {
389 addScopeSymbols(ref.from, usedNames, alreadyCheckedScopes, ignoredScopes);
390 }
391
392 const newName = findNewName(
393 name,
394 allUsedNames,
395 usedNames,
396 readableIdentifier
397 );
398 allUsedNames.add(newName);
399 for (const identifier of allIdentifiers) {
400 const r = /** @type {Range} */ (identifier.range);
401 const path = getPathInAst(ast, identifier);
402 if (path && path.length > 1) {
403 const maybeProperty =
404 path[1].type === "AssignmentPattern" && path[1].left === path[0]
405 ? path[2]
406 : path[1];
407 if (maybeProperty.type === "Property" && maybeProperty.shorthand) {
408 source.insert(r[1], `: ${newName}`);
409 continue;
410 }
411 }
412 source.replace(r[0], r[1] - 1, newName);
413 }
414 }
415 allUsedNames.add(name);
416};
417
418const PLUGIN_NAME = "JavascriptModulesPlugin";
419

Callers 1

Calls 8

getUsedNamesInScopeInfoFunction · 0.85
getAllReferencesFunction · 0.85
addScopeSymbolsFunction · 0.85
findNewNameFunction · 0.85
getPathInAstFunction · 0.85
hasMethod · 0.45
addMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected