-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
62 lines (48 loc) · 1.92 KB
/
test.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
const fs = require('fs');
const path = require('path');
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');
const parser = new Parser();
parser.setLanguage(JavaScript);
const filePath = path.resolve('/Users/ravirawat/Desktop/nodeApp/index.js');
console.log(filePath)
let sourceCode = fs.readFileSync(filePath, 'utf8');
console.log(sourceCode)
const comments = [];
const tree = parser.parse(sourceCode);
console.log(tree.rootNode);
const commentInsertions = [];
tree.rootNode.descendantsOfType('function_declaration').forEach(node => {
const functionName = node.childForFieldName('name').text;
console.log(functionName)
comments.push({
functionName :functionName,
jsDoc: `/**\n This is the ${functionName} function\n */`
});
const commentIndex = comments.findIndex(comment => comment.functionName === functionName);
console.log(commentIndex)
if (commentIndex !== -1) {
const newJsDoc = comments[commentIndex].jsDoc;
const previousNode = node.previousNamedSibling;
if (previousNode && previousNode.type === 'comment') {
// Update existing comment
const startIndex = previousNode.startIndex;
const endIndex = previousNode.endIndex;
sourceCode = sourceCode.slice(0, startIndex) + newJsDoc + sourceCode.slice(endIndex);
} else {
// Note the position for new comment
const functionStartLine = node.startPosition.row;
commentInsertions.push({ line: functionStartLine, comment: newJsDoc });
}
}
});
// Add new comments at the noted positions
if (commentInsertions.length > 0) {
const lines = sourceCode.split('\n');
commentInsertions.sort((a, b) => b.line - a.line); // Sort in reverse order to avoid messing up line numbers
commentInsertions.forEach(insertion => {
lines.splice(insertion.line, 0, insertion.comment);
});
sourceCode = lines.join('\n');
}
// fs.writeFileSync(filePath, sourceCode, 'utf8');