-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc.js
executable file
·403 lines (346 loc) · 12.2 KB
/
sc.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env node
/**
sourcecrop - cat functions out of source code with autocomplete
Copyright (C) 2024 Cris (@o0101) and Dosyago Corp.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
import fs from 'fs';
import path from 'path';
import { spawnSync, execSync } from 'child_process';
import * as acorn from 'acorn';
const listed = new Set();
const uniqueNames = new Set();
const inverseIndex = {}; // Map callees to sets of callers
// Function to expand tilde (~) in file paths
const expandTilde = (filepath) => {
if (filepath.startsWith('~')) {
return path.join(process.env.HOME, filepath.slice(1));
}
return filepath;
};
// Ensure the correct number of arguments
if (process.argv.length < 3) {
console.error('Usage: sc <file> [name|calls-<name>|by-<name>]');
process.exit(1);
}
// Expand tilde in the provided file path
const filePath = path.resolve(expandTilde(process.argv[2]));
const name = process.argv[3] || null;
// Check if bat is installed
const isBatInstalled = () => {
try {
execSync('bat --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
};
const getAnonymousContext = (node) => {
let name = `untitled-${node.start}`;
if (node.parent) {
if (node.parent.type === 'VariableDeclarator' && node.parent.id) {
name = `${node.parent.id.name}`;
} else if (node.parent.type === 'Property' && node.parent.key) {
name = `${node.parent.key.name}`;
} else if (node.parent.type === 'AssignmentExpression' && node.parent.left) {
name = `${node.parent.left.name}`;
} else if (node.parent.type === 'CallExpression' && node.parent.callee) {
name = `${getCalleeName(node.parent)}`;
} else if (node.parent?.parent?.type == 'CallExpression' && node.parent?.parent?.callee) {
name = `${name}-in-${getCalleeName(node.parent.parent)}`;
}
}
return name;
};
// Stack-based walker to add parent links
const addParentLinks = (ast) => {
const stack = [];
const visitNode = (node) => {
if (stack.length > 0) {
node.parent = stack[stack.length - 1];
} else {
node.parent = null;
}
stack.push(node);
// Recursively visit child nodes
for (const key in node) {
if (node[key] && typeof node[key] === 'object' && key !== 'parent') {
visitNode(node[key]);
}
}
stack.pop();
};
visitNode(ast);
};
// Helper function to get the callee name from a CallExpression node
const getCalleeName = (node) => {
if (node.callee.type === 'Identifier') {
return node.callee.name;
} else if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier') {
return `${node.callee.object.name}.${node.callee.property.name}`;
}
return null;
};
// Function to add to the inverse index
const addToInverseIndex = (callee, caller) => {
if (!callee || !caller) return;
if (!inverseIndex[callee]) {
inverseIndex[callee] = new Set();
}
inverseIndex[callee].add(caller);
};
// Function to list all items (functions, classes, imports, variables, etc.) and build the inverse index
const listItems = (ast, fileName) => {
const items = [];
const stack = []; // Stack to track our current path in the AST
const pushItem = item => {
const key = `${fileName}:${item.start}:${item.end}`;
if (!listed.has(key)) {
items.push(item);
listed.add(key);
}
};
const visitNode = (node) => {
stack.push(node);
// Handle different types of nodes
switch (node.type) {
case 'FunctionDeclaration':
if (node.id) {
pushItem({ name: node.id.name, type: 'FunctionDeclaration', start: node.start, end: node.end });
}
break;
case 'VariableDeclarator':
if (node.init && node.init.type === 'ArrowFunctionExpression') {
pushItem({ name: node.id.name, type: 'ArrowFunction', start: node.start, end: node.end });
} else if (node.init && node.init.type === 'FunctionExpression') {
pushItem({ name: node.id.name, type: 'FunctionExpression', start: node.start, end: node.end });
}
break;
case 'MethodDefinition':
if (node.key && node.key.name) {
pushItem({ name: node.key.name, type: 'MethodDefinition', start: node.start, end: node.end });
}
break;
case 'FunctionExpression':
case 'ArrowFunctionExpression':
// Handle anonymous functions
if (!node.id) {
const context = getAnonymousContext(node);
pushItem({ name: context, type: 'AnonymousFunction', start: node.start, end: node.end });
}
break;
case 'CallExpression':
const calleeName = getCalleeName(node);
let callerName = null;
// Traverse the stack to find the closest function/method context
for (let i = stack.length - 1; i >= 0; i--) {
const currentNode = stack[i];
if (
currentNode.type === 'FunctionDeclaration' ||
currentNode.type === 'FunctionExpression' ||
currentNode.type === 'ArrowFunctionExpression' ||
currentNode.type === 'MethodDefinition'
) {
callerName = currentNode.id ? currentNode.id.name : getAnonymousContext(currentNode);
break;
}
}
addToInverseIndex(calleeName, callerName);
break;
}
// Recursively visit child nodes
for (const key in node) {
if (node[key] && typeof node[key] === 'object' && key !== 'parent') {
visitNode(node[key]);
}
}
stack.pop();
};
visitNode(ast);
return items;
};
// Function to list all functions called in the file
const listCallees = (ast) => {
const callees = new Set();
const visitNode = (node) => {
if (node.type === 'CallExpression') {
const calleeName = getCalleeName(node);
if (calleeName) {
callees.add(calleeName);
}
}
// Recursively visit child nodes
for (const key in node) {
if (node[key] && typeof node[key] === 'object' && key !== 'parent') {
visitNode(node[key]);
}
}
};
visitNode(ast);
return Array.from(callees);
};
// Function to list all functions that call other functions (for by- selector)
const listCallers = (ast) => {
const callers = new Set();
for (const callee in inverseIndex) {
inverseIndex[callee].forEach(caller => callers.add(caller));
}
return Array.from(callers);
};
// Function to display the matched function's code
const displayFunctionCode = (data, matchedItems, batInstalled) => {
console.log('');
matchedItems.forEach((item) => {
const snippet = normalizeIndent(data.slice(item.start, item.end));
if (batInstalled) {
const bat = spawnSync('bat', ['--theme=TwoDark', '--style=plain', '--color=always', '--language=javascript'], {
input: snippet,
encoding: 'utf8',
});
console.log(bat.stdout);
} else {
console.log(snippet);
}
});
console.log('');
};
// Function to display a single function's code by name without using `walk.simple`
const displaySingleFunctionCode = (ast, functionName, data, batInstalled) => {
let found = false;
const stack = [];
const visitNode = (node) => {
stack.push(node);
switch (node.type) {
case 'FunctionDeclaration':
case 'FunctionExpression':
case 'ArrowFunctionExpression':
const nodeName = node.id ? node.id.name : getAnonymousContext(node);
if (nodeName === functionName) {
found = true;
displayFunctionCode(data, [{ start: node.start, end: node.end }], batInstalled);
}
break;
case 'MethodDefinition':
if (node.key && node.key.name === functionName) {
found = true;
displayFunctionCode(data, [{ start: node.start, end: node.end }], batInstalled);
}
break;
}
// Recursively visit child nodes
for (const key in node) {
if (node[key] && typeof node[key] === 'object' && key !== 'parent') {
visitNode(node[key]);
}
}
stack.pop();
};
visitNode(ast);
if (!found) {
console.error(`Function '${functionName}' not found in the provided file.`);
}
};
// Read and parse the file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}:`, err);
process.exit(1);
}
try {
const ast = acorn.parse(data, { ecmaVersion: 'latest', sourceType: 'module' });
// Add parent links using the stack-based walker
addParentLinks(ast);
// List items and build the inverse index
const items = listItems(ast, filePath);
const batInstalled = isBatInstalled();
if (name) {
if (name === '-listCallees') {
const callees = listCallees(ast);
callees.forEach(callee => console.log(callee));
process.exit(0);
} else if (name === '-listCallers') {
const callers = listCallers(ast);
callers.forEach(caller => console.log(caller));
process.exit(0);
} else if (name.startsWith('calls-')) {
const calleeName = name.slice(6);
const callers = inverseIndex[calleeName] || new Set();
if (callers.size > 0) {
if (process.argv.length >= 5) {
const selectedCaller = process.argv[4];
if (callers.has(selectedCaller)) {
displaySingleFunctionCode(ast, selectedCaller, data, batInstalled);
process.exit(0);
}
}
console.log(`Functions that call '${calleeName}':`);
callers.forEach(caller => console.log(caller));
} else {
console.error(`No functions call '${calleeName}' in the provided file.`);
}
} else if (name.startsWith('by-')) {
const callerName = name.slice(3);
const callees = [];
for (const callee in inverseIndex) {
if (inverseIndex[callee].has(callerName)) {
callees.push(callee);
}
}
if (callees.length > 0) {
if (process.argv.length >= 5) {
const selectedCallee = process.argv[4];
if (callees.includes(selectedCallee)) {
displaySingleFunctionCode(ast, selectedCallee, data, batInstalled);
process.exit(0);
}
}
console.log(`Functions called by '${callerName}':`);
callees.forEach(callee => console.log(callee));
} else {
console.error(`No functions are called by '${callerName}' in the provided file.`);
}
} else {
// Handle exact match for declared functions
const matchedItems = items.filter(item => item.name === name);
if (matchedItems.length) {
displayFunctionCode(data, matchedItems, batInstalled);
} else {
console.error(`Element ${name} not found in file ${filePath}`);
}
}
process.exit(0);
} else {
// List all declared functions for autocompletion
items.forEach(item => console.log(item.name));
process.exit(0);
}
} catch (parseError) {
console.error('Error parsing JavaScript file:', parseError);
process.exit(1);
}
});
function normalizeIndent(code) {
// Split the code into lines
const lines = code.split('\n');
let first = '';
if (!lines[0].match(/^\s+/)) first = lines.shift();
// skip first line if it doesn't start with indent
// Find the minimum indent (ignoring empty lines)
const minIndent = lines.reduce((min, line) => {
const trimmed = line.trimStart();
if (trimmed.length === 0) return min; // Skip empty lines
const indent = line.length - trimmed.length;
return Math.min(min, indent);
}, Infinity);
// Normalize all lines by removing the minimum indent
return first + '\n' + lines.map(line => line.slice(minIndent)).join('\n');
}