Last active
April 2, 2025 11:20
-
-
Save richi0/404a837748012ba86a2ff3cfd77733cc to your computer and use it in GitHub Desktop.
Annotate TS Code for Strict Mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import re | |
import os | |
ROOT = Path(__file__).parent.resolve() | |
ERRORS = ROOT / "errors.txt" | |
class TSErrror(): | |
def __init__(self, file, line, message, id): | |
self.file = Path(ROOT, file) | |
self.line = line | |
self.message = message | |
self.errorId = id | |
self.fixed = False | |
def fix(self, errors, onlyJSXErrors=False): | |
lines = self.file.read_text().splitlines() | |
errorLine = self.line | |
startBracket = "" | |
endBracket = "" | |
self.fixed = True | |
if onlyJSXErrors: | |
try: | |
line = lines[self.line-1] | |
if not line.startswith("{") and "@ts-expect-error" in line: | |
lines[self.line-1] = "{" + line + "}" | |
except Exception as e: | |
print(self.file, self.line) | |
print(e) | |
else : | |
lines.insert(errorLine - 1, f"{startBracket}/* @ts-expect-error TODO: {self.errorId} -> {self.message[:120]} */{endBracket}") | |
for error in errors: | |
if error.file == self.file and error.fixed == False: | |
error.increment() | |
self.file.write_text("\n".join(lines) + "\n") | |
def increment(self): | |
self.line += 1 | |
def parse_errors(onlyJSXErrors=False): | |
errors = [] | |
errorsLines = ERRORS.read_text().splitlines() | |
for error in errorsLines: | |
pattern = re.compile(r".*error TS\d\d\d\d\d?:") | |
match = pattern.match(error) | |
if match: | |
parts = error.split(":") | |
file = parts[0].split("(")[0] | |
line = int(parts[0].split("(")[1].split(",")[0]) | |
errorId = error[:match.end()].split("error")[1][:-1].strip() | |
errors.append(TSErrror(file, line, parts[2], errorId)) | |
for error in errors: | |
error.fix(errors, onlyJSXErrors) | |
def generate_error_file(): | |
os.system("yarn tsc --noEmit --skipLibCheck > errors.txt") | |
if __name__ == "__main__": | |
generate_error_file() | |
parse_errors() | |
generate_error_file() | |
parse_errors(True) | |
os.system("yarn prettier -w src") | |
ERRORS.unlink() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment