summaryrefslogtreecommitdiffstats
path: root/tools/esmify/static-import.js
blob: e99bfb33800d653d5d597a101875cfaa04b6a5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env node */

const _path = require("path");
const { getESMFiles } = require(_path.resolve(__dirname, "./is-esmified.js"));
const {
  esmifyExtension,
  isString,
  warnForPath,
  isMemberExpressionWithIdentifiers,
} = require(_path.resolve(__dirname, "./utils.js"));

function isTargetESM(resourceURI) {
  if ("ESMIFY_TARGET_PREFIX" in process.env) {
    const files = getESMFiles(resourceURI);
    const targetPrefix = process.env.ESMIFY_TARGET_PREFIX;
    for (const esm of files) {
      if (esm.startsWith(targetPrefix)) {
        return true;
      }
    }

    return false;
  }

  return true;
}

function isImportESModuleCall(node) {
  return isMemberExpressionWithIdentifiers(node.callee, [
    "ChromeUtils",
    "importESModule",
  ]);
}

// Replace `ChromeUtils.import`, `Cu.import`, and `ChromeUtils.importESModule`
// with static import if it's at the top-level of system ESM file.
function tryReplacingWithStaticImport(
  jscodeshift,
  inputFile,
  path,
  resourceURINode,
  alwaysReplace
) {
  if (!alwaysReplace && !inputFile.endsWith(".sys.mjs")) {
    // Static import is available only in system ESM.
    return false;
  }

  // Check if it's at the top-level.
  if (path.parent.node.type !== "VariableDeclarator") {
    return false;
  }

  if (path.parent.parent.node.type !== "VariableDeclaration") {
    return false;
  }

  const decls = path.parent.parent.node;
  if (decls.declarations.length !== 1) {
    return false;
  }

  if (path.parent.parent.parent.node.type !== "Program") {
    return false;
  }

  if (path.node.arguments.length !== 1) {
    return false;
  }

  const resourceURI = resourceURINode.value;

  // Collect imported symbols.
  const specs = [];
  if (path.parent.node.id.type === "Identifier") {
    specs.push(jscodeshift.importNamespaceSpecifier(path.parent.node.id));
  } else if (path.parent.node.id.type === "ObjectPattern") {
    for (const prop of path.parent.node.id.properties) {
      if (prop.shorthand) {
        specs.push(jscodeshift.importSpecifier(prop.key));
      } else if (prop.value.type === "Identifier") {
        specs.push(jscodeshift.importSpecifier(prop.key, prop.value));
      } else {
        return false;
      }
    }
  } else {
    return false;
  }

  // If this is `ChromeUtils.import` or `Cu.import`, replace the extension.
  // no-op for `ChromeUtils.importESModule`.
  resourceURINode.value = esmifyExtension(resourceURI);

  const e = jscodeshift.importDeclaration(specs, resourceURINode);
  e.comments = path.parent.parent.node.comments;
  path.parent.parent.node.comments = [];
  path.parent.parent.replace(e);

  return true;
}

function replaceImportESModuleCall(
  inputFile,
  jscodeshift,
  path,
  alwaysReplace
) {
  if (path.node.arguments.length !== 1) {
    warnForPath(
      inputFile,
      path,
      `importESModule call should have only one argument`
    );
    return;
  }

  const resourceURINode = path.node.arguments[0];
  if (!isString(resourceURINode)) {
    warnForPath(inputFile, path, `resource URI should be a string`);
    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,
    alwaysReplace
  );
}

exports.isImportESModuleCall = isImportESModuleCall;
exports.tryReplacingWithStaticImport = tryReplacingWithStaticImport;
exports.replaceImportESModuleCall = replaceImportESModuleCall;