Bug 1780301 - Part 3: Add option to skip filename filter. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D152227
This commit is contained in:
Tooru Fujisawa 2022-07-20 11:34:32 +00:00
Родитель d62c324450
Коммит 44f5fff2b9
2 изменённых файлов: 29 добавлений и 9 удалений

Просмотреть файл

@ -100,7 +100,13 @@ function replaceImportCall(inputFile, jscodeshift, path) {
}
if (
!tryReplacingWithStaticImport(jscodeshift, inputFile, path, resourceURINode)
!tryReplacingWithStaticImport(
jscodeshift,
inputFile,
path,
resourceURINode,
false
)
) {
path.node.callee.object.name = "ChromeUtils";
path.node.callee.property.name = "importESModule";
@ -420,7 +426,7 @@ function doTranslate(inputFile, jscodeshift, root) {
if (isImportCall(path.node)) {
replaceImportCall(inputFile, jscodeshift, path);
} else if (isImportESModuleCall(path.node)) {
replaceImportESModuleCall(inputFile, jscodeshift, path);
replaceImportESModuleCall(inputFile, jscodeshift, path, false);
} else if (isLazyGetterCall(path.node)) {
replaceLazyGetterCall(inputFile, jscodeshift, path);
} else if (isLazyGettersCall(path.node)) {

Просмотреть файл

@ -40,9 +40,10 @@ function tryReplacingWithStaticImport(
jscodeshift,
inputFile,
path,
resourceURINode
resourceURINode,
alwaysReplace
) {
if (!inputFile.endsWith(".sys.mjs")) {
if (!alwaysReplace && !inputFile.endsWith(".sys.mjs")) {
// Static import is available only in system ESM.
return false;
}
@ -101,7 +102,12 @@ function tryReplacingWithStaticImport(
return true;
}
function replaceImportESModuleCall(inputFile, jscodeshift, path) {
function replaceImportESModuleCall(
inputFile,
jscodeshift,
path,
alwaysReplace
) {
if (path.node.arguments.length !== 1) {
warnForPath(
inputFile,
@ -117,13 +123,21 @@ function replaceImportESModuleCall(inputFile, jscodeshift, path) {
return;
}
const resourceURI = resourceURINode.value;
if (!isTargetESM(resourceURI)) {
return;
if (!alwaysReplace) {
const resourceURI = resourceURINode.value;
if (!isTargetESM(resourceURI)) {
return;
}
}
// If this cannot be replaced with static import, do nothing.
tryReplacingWithStaticImport(jscodeshift, inputFile, path, resourceURINode);
tryReplacingWithStaticImport(
jscodeshift,
inputFile,
path,
resourceURINode,
alwaysReplace
);
}
exports.isImportESModuleCall = isImportESModuleCall;