ruff/crates/ruff_python_formatter/generate.py
Louis Dispa dc072537e5
Fix python_formatter generate.py with rust path (#5475)
## Summary

This PR fix an issue with the `generate.py` file of the python
formatter.
Since https://github.com/astral-sh/ruff/pull/5369 the [node.rs
file](f51dc20497/crates/ruff_python_ast/src/node.rs)
used to generate the types now has `ast::` in the enum.

```rust
pub enum AnyNode {
   ModModule(ModModule),
   ModInteractive(ModInteractive),
   ModExpression(ModExpression),
   ModFunctionType(ModFunctionType),
   ...
```

And now:

```rust
pub enum AnyNode {
   ModModule(ast::ModModule),
   ModInteractive(ast::ModInteractive),
   ModExpression(ast::ModExpression),
   ModFunctionType(ast::ModFunctionType),
   ...
```

The python script was not parsing rust paths. This PR adds the
possibility to have it.

## Test Plan

This was tested locally.

### Script output

Before

```
['ast::ModModule),', 'ast::ModInteractive),', 'ast::ModExpression),', 'ast::ModFunctionType),', 'ast::StmtFunctionDef),', 'ast::StmtAsyncFunctionDef),', 'ast::StmtClassDef),', 'ast::StmtReturn),', 'ast::StmtDelete),', 'ast::StmtAssign),', 'ast::StmtAugAssign),', 'ast::StmtAnnAssign),', 'ast::StmtFor),', 'ast::StmtAsyncFor),', 'ast::StmtWhile),', 'ast::StmtIf),', 'ast::StmtWith),', 'ast::StmtAsyncWith),', 'ast::StmtMatch),', 'ast::StmtRaise),', 'ast::StmtTry),', 'ast::StmtTryStar),', 'ast::StmtAssert),', 'ast::StmtImport),', 'ast::StmtImportFrom),', 'ast::StmtGlobal),', 'ast::StmtNonlocal),', 'ast::StmtExpr),', 'ast::StmtPass),', 'ast::StmtBreak),', 'ast::StmtContinue),', 'ast::ExprBoolOp),', 'ast::ExprNamedExpr),', 'ast::ExprBinOp),', 'ast::ExprUnaryOp),', 'ast::ExprLambda),', 'ast::ExprIfExp),', 'ast::ExprDict),', 'ast::ExprSet),', 'ast::ExprListComp),', 'ast::ExprSetComp),', 'ast::ExprDictComp),', 'ast::ExprGeneratorExp),', 'ast::ExprAwait),', 'ast::ExprYield),', 'ast::ExprYieldFrom),', 'ast::ExprCompare),', 'ast::ExprCall),', 'ast::ExprFormattedValue),', 'ast::ExprJoinedStr),', 'ast::ExprConstant),', 'ast::ExprAttribute),', 'ast::ExprSubscript),', 'ast::ExprStarred),', 'ast::ExprName),', 'ast::ExprList),', 'ast::ExprTuple),', 'ast::ExprSlice),', 'ast::ExceptHandlerExceptHandler),', 'ast::PatternMatchValue),', 'ast::PatternMatchSingleton),', 'ast::PatternMatchSequence),', 'ast::PatternMatchMapping),', 'ast::PatternMatchClass),', 'ast::PatternMatchStar),', 'ast::PatternMatchAs),', 'ast::PatternMatchOr),', 'ast::TypeIgnoreTypeIgnore),', 'Comprehension),', 'Arguments),', 'Arg),', 'ArgWithDefault),', 'Keyword),', 'Alias),', 'WithItem),', 'MatchCase),', 'Decorator),']

error: unexpected closing delimiter: `)`
 --> <stdin>:3:55
  |
2 |             use ruff_formatter::{write, Buffer, FormatResult};
  |                                 - this opening brace...     - ...matches this closing brace
3 |             use rustpython_parser::ast::ast::ModModule),;
  |                                                       ^ unexpected closing delimiter

Traceback (most recent call last):
  File "/Users/ldispa/Documents/perso/ruff/crates/ruff_python_formatter/generate.py", line 100, in <module>
    node_path.write_text(rustfmt(code))
                         ^^^^^^^^^^^^^
  File "/Users/ldispa/Documents/perso/ruff/crates/ruff_python_formatter/generate.py", line 12, in rustfmt
    return check_output(["rustfmt", "--emit=stdout"], input=code, text=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['rustfmt', '--emit=stdout']' returned non-zero exit status 1.
```

After:
```
['ModModule', 'ModInteractive', 'ModExpression', 'ModFunctionType', 'StmtFunctionDef', 'StmtAsyncFunctionDef', 'StmtClassDef', 'StmtReturn', 'StmtDelete', 'StmtAssign', 'StmtAugAssign', 'StmtAnnAssign', 'StmtFor', 'StmtAsyncFor', 'StmtWhile', 'StmtIf', 'StmtWith', 'StmtAsyncWith', 'StmtMatch', 'StmtRaise', 'StmtTry', 'StmtTryStar', 'StmtAssert', 'StmtImport', 'StmtImportFrom', 'StmtGlobal', 'StmtNonlocal', 'StmtExpr', 'StmtPass', 'StmtBreak', 'StmtContinue', 'ExprBoolOp', 'ExprNamedExpr', 'ExprBinOp', 'ExprUnaryOp', 'ExprLambda', 'ExprIfExp', 'ExprDict', 'ExprSet', 'ExprListComp', 'ExprSetComp', 'ExprDictComp', 'ExprGeneratorExp', 'ExprAwait', 'ExprYield', 'ExprYieldFrom', 'ExprCompare', 'ExprCall', 'ExprFormattedValue', 'ExprJoinedStr', 'ExprConstant', 'ExprAttribute', 'ExprSubscript', 'ExprStarred', 'ExprName', 'ExprList', 'ExprTuple', 'ExprSlice', 'ExceptHandlerExceptHandler', 'PatternMatchValue', 'PatternMatchSingleton', 'PatternMatchSequence', 'PatternMatchMapping', 'PatternMatchClass', 'PatternMatchStar', 'PatternMatchAs', 'PatternMatchOr', 'TypeIgnoreTypeIgnore', 'Comprehension', 'Arguments', 'Arg', 'ArgWithDefault', 'Keyword', 'Alias', 'WithItem', 'MatchCase', 'Decorator']
```
2023-07-03 16:07:57 +02:00

159 lines
4.8 KiB
Python

"""See Docs.md"""
# %%
import re
from collections import defaultdict
from pathlib import Path
from subprocess import check_output
def rustfmt(code: str) -> str:
return check_output(["rustfmt", "--emit=stdout"], input=code, text=True)
# %%
# Read nodes
root = Path(
check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip(),
)
nodes_file = (
root.joinpath("crates")
.joinpath("ruff_python_ast")
.joinpath("src")
.joinpath("node.rs")
.read_text()
)
node_lines = (
nodes_file.split("pub enum AnyNode {")[1].split("}")[0].strip().splitlines()
)
nodes = [
node_line.split("(")[1].split(")")[0].split("::")[-1].split("<")[0]
for node_line in node_lines
]
print(nodes)
# %%
# Generate newtypes with dummy FormatNodeRule implementations
out = (
root.joinpath("crates")
.joinpath("ruff_python_formatter")
.joinpath("src")
.joinpath("generated.rs")
)
src = root.joinpath("crates").joinpath("ruff_python_formatter").joinpath("src")
nodes_grouped = defaultdict(list)
# We rename because mod is a keyword in rust
groups = {
"mod": "module",
"expr": "expression",
"stmt": "statement",
"pattern": "pattern",
"other": "other",
}
def group_for_node(node: str) -> str:
for group in groups:
if node.startswith(group.title()):
return group
else:
return "other"
def to_camel_case(node: str) -> str:
"""Converts PascalCase to camel_case"""
return re.sub("([A-Z])", r"_\1", node).lower().lstrip("_")
for node in nodes:
nodes_grouped[group_for_node(node)].append(node)
for group, group_nodes in nodes_grouped.items():
# These conflict with the manually content of the mod.rs files
# src.joinpath(groups[group]).mkdir(exist_ok=True)
# mod_section = "\n".join(
# f"pub(crate) mod {to_camel_case(node)};" for node in group_nodes
# )
# src.joinpath(groups[group]).joinpath("mod.rs").write_text(rustfmt(mod_section))
for node in group_nodes:
node_path = src.joinpath(groups[group]).joinpath(f"{to_camel_case(node)}.rs")
# Don't override existing manual implementations
if node_path.exists():
continue
code = f"""
use crate::{{verbatim_text, FormatNodeRule, PyFormatter}};
use ruff_formatter::{{write, Buffer, FormatResult}};
use rustpython_parser::ast::{node};
#[derive(Default)]
pub struct Format{node};
impl FormatNodeRule<{node}> for Format{node} {{
fn fmt_fields(&self, item: &{node}, f: &mut PyFormatter) -> FormatResult<()> {{
write!(f, [verbatim_text(item.range)])
}}
}}
""".strip() # noqa: E501
node_path.write_text(rustfmt(code))
# %%
# Generate `FormatRule`, `AsFormat` and `IntoFormat`
generated = """//! This is a generated file. Don't modify it by hand! Run `scripts/generate.py` to re-generate the file.
use crate::context::PyFormatContext;
use crate::{AsFormat, FormatNodeRule, IntoFormat};
use ruff_formatter::formatter::Formatter;
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatRule};
use rustpython_parser::ast;
""" # noqa: E501
for node in nodes:
text = f"""
impl FormatRule<ast::{node}, PyFormatContext<'_>>
for crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}
{{
#[inline]
fn fmt(
&self,
node: &ast::{node},
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {{
FormatNodeRule::<ast::{node}>::fmt(self, node, f)
}}
}}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::{node} {{
type Format<'a> = FormatRefWithRule<
'a,
ast::{node},
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node},
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {{
FormatRefWithRule::new(
self,
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}::default(),
)
}}
}}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::{node} {{
type Format = FormatOwnedWithRule<
ast::{node},
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node},
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {{
FormatOwnedWithRule::new(
self,
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}::default(),
)
}}
}}
""" # noqa: E501
generated += text
out.write_text(rustfmt(generated))