ruff/scripts/update_ambiguous_characters.py
konstin 550b643e33
Add script for ecosystem wide checks of all rules and fixes (#4326)
* Add script for ecosystem wide checks of all rules and fixes

This adds my personal script for checking an entire checkout of ~2.1k packages for
panics, autofix errors and similar problems. It's not really meant to be used by anybody else but i thought it's better if it lives in the repo than if it doesn't.

For reference, this is the current output of failing autofixes: https://gist.github.com/konstin/c3fada0135af6cacec74f166adf87a00. Trimmed down to the useful information: https://gist.github.com/konstin/c864f4c300c7903a24fdda49635c5da9

* Keep github template intact

* Remove the need for ripgrep

* sort output
2023-05-22 15:23:25 +02:00

62 lines
2.2 KiB
Python

"""Generate the confusables.rs file from the VS Code ambiguous.json file."""
import json
import subprocess
from pathlib import Path
CONFUSABLES_RS_PATH = "crates/ruff/src/rules/ruff/rules/confusables.rs"
AMBIGUOUS_JSON_URL = "https://raw.githubusercontent.com/hediet/vscode-unicode-data/main/out/ambiguous.json"
def get_mapping_data() -> dict:
"""
Get the ambiguous character mapping data from the vscode-unicode-data repository.
Uses the system's `curl` command to download the data,
instead of adding a dependency to a Python-native HTTP client.
"""
content = subprocess.check_output(
["curl", "-sSL", AMBIGUOUS_JSON_URL],
encoding="utf-8",
)
# The content is a JSON object literal wrapped in a JSON string, so double decode:
return json.loads(json.loads(content))
def format_confusables_rs(raw_data: dict) -> str:
"""Format the downloaded data into a Rust source file."""
prelude = """
/// This file is auto-generated by scripts/update_ambiguous_characters.py.
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
/// Via: <https://github.com/hediet/vscode-unicode-data/blob/main/out/ambiguous.json>
/// See: <https://github.com/microsoft/vscode/blob/095ddabc52b82498ee7f718a34f9dd11d59099a8/src/vs/base/common/strings.ts#L1094>
pub(crate) static CONFUSABLES: Lazy<FxHashMap<u32, u8>> = Lazy::new(|| {
#[allow(clippy::unreadable_literal)]
FxHashMap::from_iter([
""".lstrip()
tuples = []
for _category, items in raw_data.items():
for i in range(0, len(items), 2):
tuples.append(f"({items[i]}, {items[i + 1]}),")
postlude = """])});"""
print(f"{len(tuples)} confusable tuples.")
return prelude + "\n".join(tuples) + postlude
def main() -> None:
print("Retrieving data...")
mapping_data = get_mapping_data()
formatted_data = format_confusables_rs(mapping_data)
confusables_path = Path(__file__).parent.parent / CONFUSABLES_RS_PATH
confusables_path.write_text(formatted_data, encoding="utf-8")
print("Formatting Rust file with cargo fmt...")
subprocess.check_call(["cargo", "fmt", "--", confusables_path])
print("Done.")
if __name__ == "__main__":
main()