Instagram / Instagram/LibCST

Parsing Code with Syntax Errors

Open
#310 2 comments 0 reactions 0 assignees View on GitHub
enhancement parsing
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

Is it possible to parse code that has syntax errors in part of the code? I'd like to refactor code that might still be incomplete (and this, might have syntax errors).

For example:
`print(x)
def f(x,y,z):`
is not valid code by itself (because the function needs a body). When I call libcst.parse_expression('print(x)\ndef :'), it throws an error. While this is an understandable response, I was wondering if it would be possible to not throw the baby out with the bathwater; that is to say is it possible to recover the print(x) in the output (instead of just throwing an error).

Why I want this:
When editing a python file, usually in between edits, I have invalid syntax (like in writing the above example; before adding a function body). But I'd like to be able to run refactorings anyway, like PyCharm does. Is this possible with this library?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.