ruff/crates
Charlie Marsh fd1dfc3bfa
Add support for global and nonlocal symbol renames (#5134)
## Summary

In #5074, we introduced an abstraction to support local symbol renames
("local" here refers to "within a module"). However, that abstraction
didn't support `global` and `nonlocal` symbols. This PR extends it to
those cases.

Broadly, there are considerations.

First, if we're renaming a symbol in a scope in which it is declared
`global` or `nonlocal`. For example, given:

```python
x = 1

def foo():
    global x
```

Then when renaming `x` in `foo`, we need to detect that it's `global`
and instead perform the rename starting from the module scope.

Second, when renaming a symbol, we need to determine the scopes in which
it is declared `global` or `nonlocal`. This is effectively the inverse
of the above: when renaming `x` in the module scope, we need to detect
that we should _also_ rename `x` in `foo`.

To support these cases, the renaming algorithm was adjusted as follows:

- When we start a rename in a scope, determine whether the symbol is
declared `global` or `nonlocal` by looking for a `global` or `nonlocal`
binding. If it is, start the rename in the defining scope. (This
requires storing the defining scope on the `nonlocal` binding, which is
new.)
- We then perform the rename in the defining scope.
- We then check whether the symbol was declared as `global` or
`nonlocal` in any scopes, and perform the rename in those scopes too.
(Thankfully, this doesn't need to be done recursively.)

Closes #5092.

## Test Plan

Added some additional snapshot tests.
2023-06-16 14:35:10 +00:00
..
flake8_to_ruff Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff Add support for global and nonlocal symbol renames (#5134) 2023-06-16 14:35:10 +00:00
ruff_benchmark Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_cache Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00
ruff_cli Remove FixMode::None (#5087) 2023-06-14 11:17:09 -04:00
ruff_dev Add roundtrip support for Jupyter notebook (#5028) 2023-06-12 23:27:45 +05:30
ruff_diagnostics Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_formatter Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00
ruff_index Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00
ruff_macros Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_python_ast Use matches! for insecure hash rule (#5141) 2023-06-16 04:18:32 +00:00
ruff_python_formatter Always use identifier ranges to store bindings (#5110) 2023-06-15 18:43:19 +00:00
ruff_python_semantic Add support for global and nonlocal symbol renames (#5134) 2023-06-16 14:35:10 +00:00
ruff_python_stdlib Avoid allocations in lowercase comparisons (#5137) 2023-06-16 08:57:43 -04:00
ruff_python_whitespace Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_rustpython Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_testing_macros Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_textwrap Use consistent Cargo.toml metadata in all crates (#5015) 2023-06-12 00:02:40 +00:00
ruff_wasm Add support for auto-fix in Jupyter notebooks (#4665) 2023-06-12 14:14:15 +00:00