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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/* 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/. */
// Shared utility functions.
/* eslint-env node */
function warnForPath(inputFile, path, message) {
const loc = path.node.loc;
console.log(
`WARNING: ${inputFile}:${loc.start.line}:${loc.start.column} : ${message}`
);
}
// Get the previous statement of `path.node` in `Program`.
function getPrevStatement(path) {
const parent = path.parent;
if (parent.node.type !== "Program") {
return null;
}
const index = parent.node.body.findIndex(n => n == path.node);
if (index === -1) {
return null;
}
if (index === 0) {
return null;
}
return parent.node.body[index - 1];
}
// Get the next statement of `path.node` in `Program`.
function getNextStatement(path) {
const parent = path.parent;
if (parent.node.type !== "Program") {
return null;
}
const index = parent.node.body.findIndex(n => n == path.node);
if (index === -1) {
return null;
}
if (index + 1 == parent.node.body.length) {
return null;
}
return parent.node.body[index + 1];
}
function isIdentifier(node, name) {
if (node.type !== "Identifier") {
return false;
}
if (node.name !== name) {
return false;
}
return true;
}
function isString(node) {
return node.type === "Literal" && typeof node.value === "string";
}
const jsmExtPattern = /\.(jsm|js|jsm\.js)$/;
function esmifyExtension(path) {
return path.replace(jsmExtPattern, ".sys.mjs");
}
// Given possible member expression, return the list of Identifier nodes in
// the source order.
//
// Returns an empty array if:
// * not a simple MemberExpression tree with Identifiers
// * there's computed property
function memberExpressionsToIdentifiers(memberExpr) {
let ids = [];
function f(node) {
if (node.type !== "MemberExpression" || node.computed) {
return false;
}
if (node.object.type === "Identifier") {
ids.push(node.object);
ids.push(node.property);
return true;
}
if (!f(node.object)) {
return false;
}
ids.push(node.property);
return true;
}
if (!f(memberExpr)) {
return [];
}
return ids;
}
// Returns true if the node is a simple MemberExpression tree with Identifiers
// matches expectedIDs.
function isMemberExpressionWithIdentifiers(node, expectedIDs) {
const actualIDs = memberExpressionsToIdentifiers(node);
if (actualIDs.length !== expectedIDs.length) {
return false;
}
for (let i = 0; i < expectedIDs.length; i++) {
if (actualIDs[i].name !== expectedIDs[i]) {
return false;
}
}
return true;
}
// Rewrite the Identifiers of MemberExpression tree to toIDs.
// `node` must be a simple MemberExpression tree with Identifiers, and
// the length of Identifiers should match.
function rewriteMemberExpressionWithIdentifiers(node, toIDs) {
const actualIDs = memberExpressionsToIdentifiers(node);
for (let i = 0; i < toIDs.length; i++) {
actualIDs[i].name = toIDs[i];
}
}
// Create a simple MemberExpression tree with given Identifiers.
function createMemberExpressionWithIdentifiers(jscodeshift, ids) {
if (ids.length < 2) {
throw new Error("Unexpected length of ids for member expression");
}
if (ids.length > 2) {
return jscodeshift.memberExpression(
createMemberExpressionWithIdentifiers(jscodeshift, ids.slice(0, -1)),
jscodeshift.identifier(ids[ids.length - 1])
);
}
return jscodeshift.memberExpression(
jscodeshift.identifier(ids[0]),
jscodeshift.identifier(ids[1])
);
}
exports.warnForPath = warnForPath;
exports.getPrevStatement = getPrevStatement;
exports.getNextStatement = getNextStatement;
exports.isIdentifier = isIdentifier;
exports.isString = isString;
exports.jsmExtPattern = jsmExtPattern;
exports.esmifyExtension = esmifyExtension;
exports.isMemberExpressionWithIdentifiers = isMemberExpressionWithIdentifiers;
exports.rewriteMemberExpressionWithIdentifiers =
rewriteMemberExpressionWithIdentifiers;
exports.createMemberExpressionWithIdentifiers =
createMemberExpressionWithIdentifiers;
|