Replace verbatim text with NOT_YET_IMPLEMENTED (#4904)

<!--
Thank you for contributing to Ruff! To help us out with reviewing, please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR replaces the `verbatim_text` builder with a `not_yet_implemented` builder that emits `NOT_YET_IMPLEMENTED_<NodeKind>` for not yet implemented nodes. 

The motivation for this change is that partially formatting compound statements can result in incorrectly indented code, which is a syntax error:

```python
def func_no_args():
  a; b; c
  if True: raise RuntimeError
  if False: ...
  for i in range(10):
    print(i)
    continue
```

Get's reformatted to

```python
def func_no_args():
    a; b; c
    if True: raise RuntimeError
    if False: ...
    for i in range(10):
    print(i)
    continue
```

because our formatter does not yet support `for` statements and just inserts the text from the source. 

## Downsides

Using an identifier will not work in all situations. For example, an identifier is invalid in an `Arguments ` position. That's why I kept `verbatim_text` around and e.g. use it in the `Arguments` formatting logic where incorrect indentations are impossible (to my knowledge). Meaning, `verbatim_text` we can opt in to `verbatim_text` when we want to iterate quickly on nodes that we don't want to provide a full implementation yet and using an identifier would be invalid. 

## Upsides

Running this on main discovered stability issues with the newline handling that were previously "hidden" because of the verbatim formatting. I guess that's an upside :)

## Test Plan

None?
This commit is contained in:
Micha Reiser 2023-06-07 14:57:25 +02:00 committed by GitHub
parent 2f125f4019
commit bcf745c5ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
134 changed files with 5308 additions and 4385 deletions

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAnnAssign;
@ -7,6 +7,6 @@ pub struct FormatStmtAnnAssign;
impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
fn fmt_fields(&self, item: &StmtAnnAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAssert;
@ -7,6 +7,6 @@ pub struct FormatStmtAssert;
impl FormatNodeRule<StmtAssert> for FormatStmtAssert {
fn fmt_fields(&self, item: &StmtAssert, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAssign;
@ -7,6 +7,6 @@ pub struct FormatStmtAssign;
impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
fn fmt_fields(&self, item: &StmtAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAsyncFor;
@ -7,6 +7,6 @@ pub struct FormatStmtAsyncFor;
impl FormatNodeRule<StmtAsyncFor> for FormatStmtAsyncFor {
fn fmt_fields(&self, item: &StmtAsyncFor, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAsyncFunctionDef;
@ -7,6 +7,6 @@ pub struct FormatStmtAsyncFunctionDef;
impl FormatNodeRule<StmtAsyncFunctionDef> for FormatStmtAsyncFunctionDef {
fn fmt_fields(&self, item: &StmtAsyncFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAsyncWith;
@ -7,6 +7,6 @@ pub struct FormatStmtAsyncWith;
impl FormatNodeRule<StmtAsyncWith> for FormatStmtAsyncWith {
fn fmt_fields(&self, item: &StmtAsyncWith, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAugAssign;
@ -7,6 +7,6 @@ pub struct FormatStmtAugAssign;
impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
fn fmt_fields(&self, item: &StmtAugAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtBreak;
@ -7,6 +7,6 @@ pub struct FormatStmtBreak;
impl FormatNodeRule<StmtBreak> for FormatStmtBreak {
fn fmt_fields(&self, item: &StmtBreak, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtClassDef;
@ -7,6 +7,6 @@ pub struct FormatStmtClassDef;
impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
fn fmt_fields(&self, item: &StmtClassDef, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtContinue;
@ -7,6 +7,6 @@ pub struct FormatStmtContinue;
impl FormatNodeRule<StmtContinue> for FormatStmtContinue {
fn fmt_fields(&self, item: &StmtContinue, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtDelete;
@ -7,6 +7,6 @@ pub struct FormatStmtDelete;
impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
fn fmt_fields(&self, item: &StmtDelete, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtFor;
@ -7,6 +7,6 @@ pub struct FormatStmtFor;
impl FormatNodeRule<StmtFor> for FormatStmtFor {
fn fmt_fields(&self, item: &StmtFor, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtFunctionDef;
@ -7,6 +7,6 @@ pub struct FormatStmtFunctionDef;
impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
fn fmt_fields(&self, item: &StmtFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtGlobal;
@ -7,6 +7,6 @@ pub struct FormatStmtGlobal;
impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
fn fmt_fields(&self, item: &StmtGlobal, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtIf;
@ -7,6 +7,6 @@ pub struct FormatStmtIf;
impl FormatNodeRule<StmtIf> for FormatStmtIf {
fn fmt_fields(&self, item: &StmtIf, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtImport;
@ -7,6 +7,6 @@ pub struct FormatStmtImport;
impl FormatNodeRule<StmtImport> for FormatStmtImport {
fn fmt_fields(&self, item: &StmtImport, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtImportFrom;
@ -7,6 +7,6 @@ pub struct FormatStmtImportFrom;
impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
fn fmt_fields(&self, item: &StmtImportFrom, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtMatch;
@ -7,6 +7,6 @@ pub struct FormatStmtMatch;
impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
fn fmt_fields(&self, item: &StmtMatch, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtNonlocal;
@ -7,6 +7,6 @@ pub struct FormatStmtNonlocal;
impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
fn fmt_fields(&self, item: &StmtNonlocal, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtPass;
@ -7,6 +7,6 @@ pub struct FormatStmtPass;
impl FormatNodeRule<StmtPass> for FormatStmtPass {
fn fmt_fields(&self, item: &StmtPass, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtRaise;
@ -7,6 +7,6 @@ pub struct FormatStmtRaise;
impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
fn fmt_fields(&self, item: &StmtRaise, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtReturn;
@ -7,6 +7,6 @@ pub struct FormatStmtReturn;
impl FormatNodeRule<StmtReturn> for FormatStmtReturn {
fn fmt_fields(&self, item: &StmtReturn, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtTry;
@ -7,6 +7,6 @@ pub struct FormatStmtTry;
impl FormatNodeRule<StmtTry> for FormatStmtTry {
fn fmt_fields(&self, item: &StmtTry, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtTryStar;
@ -7,6 +7,6 @@ pub struct FormatStmtTryStar;
impl FormatNodeRule<StmtTryStar> for FormatStmtTryStar {
fn fmt_fields(&self, item: &StmtTryStar, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -1,4 +1,4 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtWith;
@ -7,6 +7,6 @@ pub struct FormatStmtWith;
impl FormatNodeRule<StmtWith> for FormatStmtWith {
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item.range)])
write!(f, [not_yet_implemented(item)])
}
}

View file

@ -234,31 +234,28 @@ def trailing_func():
assert_eq!(
formatted,
r#"a = 10
r#"NOT_YET_IMPLEMENTED_StmtAssign
three_leading_newlines = 80
NOT_YET_IMPLEMENTED_StmtAssign
two_leading_newlines = 20
NOT_YET_IMPLEMENTED_StmtAssign
one_leading_newline = 10
no_leading_newline = 30
NOT_YET_IMPLEMENTED_StmtAssign
NOT_YET_IMPLEMENTED_StmtAssign
class InTheMiddle:
pass
NOT_YET_IMPLEMENTED_StmtClassDef
trailing_statement = 1
NOT_YET_IMPLEMENTED_StmtAssign
def func():
pass
NOT_YET_IMPLEMENTED_StmtFunctionDef
def trailing_func():
pass"#
NOT_YET_IMPLEMENTED_StmtFunctionDef"#
);
}
@ -268,25 +265,22 @@ def trailing_func():
assert_eq!(
formatted,
r#"a = 10
r#"NOT_YET_IMPLEMENTED_StmtAssign
three_leading_newlines = 80
NOT_YET_IMPLEMENTED_StmtAssign
two_leading_newlines = 20
NOT_YET_IMPLEMENTED_StmtAssign
one_leading_newline = 10
no_leading_newline = 30
NOT_YET_IMPLEMENTED_StmtAssign
NOT_YET_IMPLEMENTED_StmtAssign
class InTheMiddle:
pass
NOT_YET_IMPLEMENTED_StmtClassDef
trailing_statement = 1
NOT_YET_IMPLEMENTED_StmtAssign
def func():
pass
NOT_YET_IMPLEMENTED_StmtFunctionDef
def trailing_func():
pass"#
NOT_YET_IMPLEMENTED_StmtFunctionDef"#
);
}
}