Skip to content

Commit 618bf67

Browse files
author
Colin McNeil
committed
Finalize 2
1 parent 02393a9 commit 618bf67

File tree

4 files changed

+54
-53
lines changed

4 files changed

+54
-53
lines changed

functions/tree_sitter/main.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
const TreeSitter = require('tree-sitter');
22
const Python = require('tree-sitter-python');
3+
const TypeScript = require('tree-sitter-typescript');
4+
const JavaScript = require('tree-sitter-javascript');
35
const fs = require('fs');
46

57
const args = JSON.parse(process.argv[2])
68

79
// Load the Python parser
810
const parser = new TreeSitter();
911
// Set the language to the parser
10-
parser.setLanguage(Python);
12+
const ext = args.path.split('.').pop();
13+
14+
let language
15+
16+
if (ext === 'py') {
17+
language = Python
18+
}
19+
else if (ext === 'ts') {
20+
language = TypeScript.typescript
21+
}
22+
else if (ext === 'tsx') {
23+
language = TypeScript.tsx
24+
}
25+
else if (ext === 'js' || ext === 'jsx') {
26+
language = JavaScript
27+
}
28+
29+
parser.setLanguage(language);
30+
1131
// Read the code file content
1232
const codeContent = fs.readFileSync(args.path, 'utf8');
1333
// Parse the code using the chosen parser
1434
const parsed = parser.parse(codeContent);
1535

16-
const line_to_grab = args.line
36+
const line_to_grab = args.line + 1
37+
38+
if (line_to_grab === undefined) {
39+
console.log('No line number provided')
40+
process.exit(1)
41+
}
42+
43+
if (line_to_grab > codeContent.split('\n').length) {
44+
console.log('Line number provided is greater than the number of lines in the file')
45+
process.exit(1)
46+
}
1747

1848
// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
1949
const search_node = (node) => {
@@ -31,16 +61,23 @@ const search_node = (node) => {
3161

3262
const line_node = search_node(parsed.rootNode)
3363

64+
if (!line_node) {
65+
console.log('No node found for line', line_to_grab)
66+
console.log('Code:', codeContent.split('\n').slice(line_to_grab - 5, line_to_grab + 5).join('\n'))
67+
process.exit(1)
68+
}
69+
3470
const parent = line_node.parent
3571

36-
if (parent) {
37-
const start_line = parent.startPosition.row
38-
const end_line = parent.endPosition.row
39-
// Return codeContent from start_line to end_line
40-
const lines = codeContent.split('\n').slice(start_line, end_line + 1)
41-
parent.content = lines.join('\n')
72+
if (parent.text.length > 256) {
73+
console.log('Parent node text is too long, truncating')
74+
const index = parent.text.indexOf(line_node.text)
75+
const start = index - 128
76+
const end = index + 128
77+
parent.text = parent.text.slice(start, end)
4278
}
4379

80+
4481
console.log({
4582
offending_line: line_node.text,
4683
line_node: line_node,

prompts/eslint/020_user_prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Lint my project's JS/TS files.
1+
Lint my project's JS/TS files using `summary` output.
22

33
Report what you did, broken down by each tool.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Fix any lint violations you can using the tools provided.
2+
3+
Please only fix `no-unused-vars`

prompts/eslint_fix/fixing.md

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
1. **Get the line**: Use read_eslint with the `path` arg to get all of the violations for a file.
2-
2. **Get the code**: Use tree_sitter with the path and line to read the surrounding code if necessary. Repeat for all violations in the file.
3-
3. **Make the correction** Respond in the following format:
2+
2. **Get the code**: Use tree_sitter with the path and line to read the surrounding code if necessary.
3+
3. **Make the correction**: Suggest an edit in the following format:
44

55
```json
66
{
7-
"start": [1, 4],
8-
"end": [2, 4],
9-
"edit": "Lorem ipsum"
7+
"start": [1, 4], // row,col for start character
8+
"end": [2, 4], // row,col for end character
9+
"edit": "Lorem ipsum" // The edit to make
1010
}
11-
```
12-
13-
Once you have fixed one file, move on to the next.
14-
15-
You are able to fix the following violations:
16-
17-
constructor-super:
18-
no-compare-neg-0:
19-
no-const-assign
20-
no-control-regex
21-
no-debugger
22-
no-empty-character-classes
23-
no-empty-pattern
24-
no-new-native-constructors
25-
no-obj-calls
26-
no-self-assign
27-
getter-return
28-
no-async-promise-executer
29-
no-class-assign
30-
no-constant-binary-expression
31-
no-dupe-args
32-
no-dupe-else-if
33-
no-dupe-keys
34-
no-duplicate-case
35-
no-ex-assign
36-
no-func-assign
37-
no-import-assign
38-
no-irregular-whitespace
39-
no-loss-of-precision
40-
no-misleading-character-class
41-
no-this-before-super
42-
no-undef
43-
no-unexpected-multiline
44-
no-unreachable
45-
no-unsafe-finally
46-
no-unsafe-negation
47-
no-unused-private-class-members
48-
no-unused-vars
49-
no-useless-backreference
11+
```

0 commit comments

Comments
 (0)