-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpython-parser.js
More file actions
62 lines (62 loc) · 2.3 KB
/
python-parser.js
File metadata and controls
62 lines (62 loc) · 2.3 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PythonParser = void 0;
var PythonParser = /** @class */ (function () {
function PythonParser() {
}
/**
* Finds the enclosing context (function or class) of a given line range.
* @param file - The Python file content as a string.
* @param lineStart - The starting line number.
* @param lineEnd - The ending line number.
* @returns The enclosing context, or null if not found.
*/
PythonParser.prototype.findEnclosingContext = function (file, lineStart, lineEnd) {
var lines = file.split("\n");
var context = null;
// Search upwards from the starting line to find the enclosing context
for (var i = lineStart - 1; i >= 0; i--) {
var line = lines[i].trim();
if (line.startsWith("def ")) {
context = {
type: "function",
name: line.split(" ")[1].split("(")[0], // Extract function name
};
break;
}
else if (line.startsWith("class ")) {
context = {
type: "class",
name: line.split(" ")[1].split("(")[0], // Extract class name
};
break;
}
}
return context; // Returns null if no context is found
};
/**
* Performs a dry run to check if the Python file is valid syntax.
* @param file - The Python file content as a string.
* @returns An object indicating validity and any error message.
*/
PythonParser.prototype.dryRun = function (file) {
try {
var spawnSync = require("child_process").spawnSync;
var result = spawnSync("python3", ["-m", "py_compile", "-"], {
input: file,
encoding: "utf-8",
});
if (result.status === 0) {
return { valid: true, error: "" }; // File is valid
}
else {
return { valid: false, error: result.stderr.toString() }; // Syntax error
}
}
catch (e) {
return { valid: false, error: e.message }; // Handle unexpected errors
}
};
return PythonParser;
}());
exports.PythonParser = PythonParser;