check improvements works
This commit is contained in:
@@ -254,24 +254,38 @@ def compile(file: Path, output: str | None):
|
|||||||
out_path.write_text(transpiled, encoding="utf-8")
|
out_path.write_text(transpiled, encoding="utf-8")
|
||||||
click.echo(f"Compiled: {out_path}")
|
click.echo(f"Compiled: {out_path}")
|
||||||
|
|
||||||
|
|
||||||
@main.command(context_settings=CONTEXT_SETTINGS)
|
@main.command(context_settings=CONTEXT_SETTINGS)
|
||||||
@click.argument("file", type=click.Path(exists=True, path_type=Path))
|
@click.argument("files", nargs=-1, required=True, type=click.Path(exists=True, path_type=Path))
|
||||||
def check(file: Path):
|
def check(files: tuple):
|
||||||
"""Validate a foreign-language file without running it."""
|
"""Validate one or more foreign-language files without running them."""
|
||||||
import ast
|
import ast
|
||||||
|
import io
|
||||||
|
import tokenize as _tokenize
|
||||||
|
|
||||||
detected_lang = _lang_from_file(file)
|
from .transpiler import _build_mapping, transpile_file
|
||||||
try:
|
|
||||||
pack = _load_effective_pack(file, detected_lang)
|
failed = False
|
||||||
transpiled = transpile_file(file, pack=pack)
|
for file in files:
|
||||||
ast.parse(transpiled)
|
detected_lang = _lang_from_file(file)
|
||||||
click.echo(f"✓ {file.name} looks good.")
|
try:
|
||||||
except SyntaxError as e:
|
pack = _load_effective_pack(file, detected_lang)
|
||||||
click.echo(f"✗ Syntax error: {e}", err=True)
|
transpiled = transpile_file(file, pack=pack)
|
||||||
sys.exit(1)
|
ast.parse(transpiled)
|
||||||
except Exception as e:
|
|
||||||
click.echo(f"✗ {e}", err=True)
|
source = file.read_text(encoding="utf-8")
|
||||||
|
mapping = _build_mapping(pack)
|
||||||
|
tokens = list(_tokenize.generate_tokens(io.StringIO(source).readline))
|
||||||
|
count = sum(1 for t in tokens if t.type == _tokenize.NAME and t.string in mapping)
|
||||||
|
|
||||||
|
click.echo(f"✓ {file.name} looks good. ({count} tokens translated)")
|
||||||
|
except SyntaxError as e:
|
||||||
|
click.echo(f"✗ {file.name}: Syntax error: {e}", err=True)
|
||||||
|
failed = True
|
||||||
|
except Exception as e:
|
||||||
|
click.echo(f"✗ {file.name}: {e}", err=True)
|
||||||
|
failed = True
|
||||||
|
|
||||||
|
if failed:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
@main.command(context_settings=CONTEXT_SETTINGS)
|
@main.command(context_settings=CONTEXT_SETTINGS)
|
||||||
|
|||||||
Reference in New Issue
Block a user