-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esprima_5_scope_differ_v2.js
executable file
·266 lines (218 loc) · 9.77 KB
/
esprima_5_scope_differ_v2.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
// -------------------------------------------------------------------------------------------------
// TODO: The variable scope parsing/rewriting is pretty horribly broken in this currently I think..
// it might be easier to try and write it in something other than esprima...
// -------------------------------------------------------------------------------------------------
// Ref: https://replit.com/@0xdevalias/Rewriting-JavaScript-Variables-via-AST-Examples#esprima_5_scope_extractor.js
// Ref:
// https://chat.openai.com/c/07a322fd-ff60-4250-8e9c-cca0a732afce
// https://chat.openai.com/c/482911c5-6dd4-4e67-8531-c17f786887d1
//
// Note: This is based off the implementation in esprima_5.js, but this version is designed to extract the variableMapping from an existing piece of code; which can then be used later to rename those mappings
// TODO: Save these scripts somewhere useful:
// GITREF='HEAD' FILEREF='167'; git show ${GITREF}:../unpacked/_next/static/chunks/${FILEREF}.js | ./esprima_5_scope_extractor.js > variableMapping.${FILEREF}-${GITREF}.json
// git diff --no-index --patch -- variableMapping.167-HEAD\^1.json variableMapping.167-HEAD.json
// TODO: Explore using estools/escope instead of the hacky implementations within this:
// https://github.com/estools/escope
const fs = require('fs');
const {
readAndParseVariableMappingFile,
// makeDebugLog,
} = require('./lib/esprimaHelpers');
function diffVariableMappings(mapping1, mapping2) {
const diff = {
scopeAdded: {},
scopeRemoved: {},
scopeModified: {}
};
// Iterate through each scope in mapping1
for (const scope in mapping1) {
const scopeMapping1 = mapping1[scope];
const scopeMapping2 = mapping2[scope];
if (scope in mapping2) {
// Create objects to hold the added, removed, and changed keys
const added = {};
const removed = {};
const changed = {};
// Check for keys in scopeMapping1 but not in scopeMapping2
for (const key in scopeMapping1) {
if (!(key in scopeMapping2)) {
removed[key] = scopeMapping1[key];
} else if (scopeMapping1[key] !== scopeMapping2[key]) {
// Check for changed keys
changed[key] = { from: scopeMapping1[key], to: scopeMapping2[key] };
}
}
// Check for keys in scopeMapping2 but not in scopeMapping1
for (const key in scopeMapping2) {
if (!(key in scopeMapping1)) {
added[key] = scopeMapping2[key];
}
}
// Create an object to store the differences for this scope
const scopeDiff = {};
// Conditionally add added, removed, and changed to scopeDiff if non-empty
if (Object.keys(added).length > 0) scopeDiff.added = added;
if (Object.keys(removed).length > 0) scopeDiff.removed = removed;
if (Object.keys(changed).length > 0) scopeDiff.changed = changed;
// If there are differences, add them to the scopeModified under the scope
if (Object.keys(scopeDiff).length > 0) {
diff.scopeModified[scope] = scopeDiff;
}
} else {
// If the scope is not in mapping2, it is entirely removed
diff.scopeRemoved[scope] = scopeMapping1;
}
}
// Check for scopes in mapping2 that are not in mapping1
for (const scope in mapping2) {
if (!(scope in mapping1)) {
// If the scope is not in mapping1, it is entirely added
diff.scopeAdded[scope] = mapping2[scope];
}
}
// Remove empty sections
if (Object.keys(diff.scopeAdded).length === 0) delete diff.scopeAdded;
if (Object.keys(diff.scopeRemoved).length === 0) delete diff.scopeRemoved;
if (Object.keys(diff.scopeModified).length === 0) delete diff.scopeModified;
return diff;
}
// function diffVariableMappings(mapping1, mapping2) {
// const diff = {};
// // Iterate through each scope in mapping1
// for (const scope in mapping1) {
// const scopeMapping1 = mapping1[scope];
// const scopeMapping2 = mapping2[scope];
// if (scope in mapping2) {
// // Create objects to hold the added, removed, and changed keys
// const added = {};
// const removed = {};
// const changed = {};
// // Check for keys in scopeMapping1 but not in scopeMapping2
// for (const key in scopeMapping1) {
// if (!(key in scopeMapping2)) {
// removed[key] = scopeMapping1[key];
// } else if (scopeMapping1[key] !== scopeMapping2[key]) {
// // Check for changed keys
// changed[key] = { from: scopeMapping1[key], to: scopeMapping2[key] };
// }
// }
// // Check for keys in scopeMapping2 but not in scopeMapping1
// for (const key in scopeMapping2) {
// if (!(key in scopeMapping1)) {
// added[key] = scopeMapping2[key];
// }
// }
// // Create an object to store the differences for this scope
// const scopeDiff = {};
// // Conditionally add added, removed, and changed to scopeDiff if non-empty
// if (Object.keys(added).length > 0) scopeDiff.added = added;
// if (Object.keys(removed).length > 0) scopeDiff.removed = removed;
// if (Object.keys(changed).length > 0) scopeDiff.changed = changed;
// // If there are differences, add them to the diff under the scope
// if (Object.keys(scopeDiff).length > 0) {
// diff[scope] = scopeDiff;
// }
// } else {
// // If the scope is not in mapping2, consider it as entirely removed
// diff[scope] = { removed: scopeMapping1 };
// }
// }
// // Check for scopes in mapping2 that are not in mapping1
// for (const scope in mapping2) {
// if (!(scope in mapping1)) {
// diff[scope] = { added: mapping2[scope] };
// }
// }
// return diff;
// }
// function diffVariableMappings(mapping1, mapping2) {
// const diff = {};
// // Iterate through each scope in mapping1
// for (const scope in mapping1) {
// // If the scope is also in mapping2, compare the mappings
// if (scope in mapping2) {
// const scopeDiff = {};
// const scopeMapping1 = mapping1[scope];
// const scopeMapping2 = mapping2[scope];
// // Check for keys in scopeMapping1 but not in scopeMapping2
// for (const key in scopeMapping1) {
// if (!(key in scopeMapping2)) {
// scopeDiff[key] = scopeMapping1[key];
// }
// }
// // Check for keys in scopeMapping2 but not in scopeMapping1
// for (const key in scopeMapping2) {
// if (!(key in scopeMapping1)) {
// scopeDiff[key] = scopeMapping2[key];
// }
// }
// // If there are differences, add them to the diff under the scope
// if (Object.keys(scopeDiff).length > 0) {
// diff[scope] = scopeDiff;
// }
// } else {
// // If the scope is not in mapping2, add it to the diff
// diff[scope] = mapping1[scope];
// }
// }
// // Check for scopes in mapping2 that are not in mapping1
// for (const scope in mapping2) {
// if (!(scope in mapping1)) {
// if (scope in diff) {
// console.warn("WARNING: Scope from mapping2 overriding scope already in diff")
// }
// diff[scope] = mapping2[scope];
// }
// }
// return diff;
// }
// function diffVariableMappings(mapping1, mapping2) {
// const diffResult = {};
// console.error("Mapping 1:", mapping1["global.(anonymous_330)"]);
// console.error("Mapping 2:", mapping2["global.(anonymous_330)"]);
// // Iterate through the scopes in mapping1
// for (const [scope, variables] of Object.entries(mapping1)) {
// // Check if the scope exists in mapping2
// if (mapping2.hasOwnProperty(scope)) {
// // Temporary store for variable differences in current scope
// const scopeDiff = {};
// // Compare variables within the scope
// for (const [variable, mapping] of Object.entries(variables)) {
// if (mapping2[scope][variable] !== mapping) {
// // Add the different mapping to the scopeDiff
// scopeDiff[variable] = mapping2[scope][variable];
// }
// }
// // If there are differences in the scope, add them to diffResult
// if (Object.keys(scopeDiff).length > 0) {
// diffResult[scope] = scopeDiff;
// }
// }
// }
// // Iterate through the scopes in mapping2 to find scopes not present in mapping1
// for (const [scope, variables] of Object.entries(mapping2)) {
// if (!mapping1.hasOwnProperty(scope)) {
// // If the scope does not exist in mapping1, we add it to the result.
// diffResult[scope] = variables;
// }
// }
// return diffResult;
// }
async function main() {
if (process.argv.length !== 4) {
console.error('Usage: node diffVariableMappings.js <file1.json> <file2.json>');
process.exit(1);
}
const [file1Path, file2Path] = process.argv.slice(2);
try {
const mapping1 = await readAndParseVariableMappingFile(file1Path);
const mapping2 = await readAndParseVariableMappingFile(file2Path);
const diffResult = diffVariableMappings(mapping1, mapping2);
console.log(JSON.stringify(diffResult, null, 2));
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main();