1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

add option to lint and format only staged files (#5172)

This commit is contained in:
Michał Zdunek 2020-05-09 12:22:27 +02:00 committed by GitHub
parent 4e5e6da348
commit 9790399bce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 51 deletions

View file

@ -9,6 +9,8 @@
// deno_typescript/typescript/lib/typescript.d.ts. Ideally we could simply point // deno_typescript/typescript/lib/typescript.d.ts. Ideally we could simply point
// to that in this import specifier, but "cargo package" is very strict and // to that in this import specifier, but "cargo package" is very strict and
// requires all files to be present in a crate's subtree. // requires all files to be present in a crate's subtree.
// to get proper editor intellisense, you can substitute "$asset$" with
// "../../deno_typescript/typescript/lib" - remember to revert before committing
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as ts_ from "$asset$/typescript.d.ts"; import * as ts_ from "$asset$/typescript.d.ts";

View file

@ -4,17 +4,37 @@ import os
import sys import sys
import argparse import argparse
from third_party import python_env from third_party import python_env
from util import git_ls_files, third_party_path, root_path, run from util import git_ls_files, git_staged, third_party_path, root_path
from util import print_command, run
cmd_args = None
def main(): def get_cmd_args():
os.chdir(root_path) global cmd_args
if cmd_args:
return cmd_args
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--js", help="run prettier", action="store_true") parser.add_argument("--js", help="run prettier", action="store_true")
parser.add_argument("--py", help="run yapf", action="store_true") parser.add_argument("--py", help="run yapf", action="store_true")
parser.add_argument("--rs", help="run rustfmt", action="store_true") parser.add_argument("--rs", help="run rustfmt", action="store_true")
args = parser.parse_args() parser.add_argument(
"--staged", help="run only on staged files", action="store_true")
cmd_args = parser.parse_args()
return cmd_args
def get_sources(*args):
getter = git_staged if get_cmd_args().staged else git_ls_files
return getter(*args)
def main():
os.chdir(root_path)
args = get_cmd_args()
did_fmt = False did_fmt = False
if args.js: if args.js:
@ -34,29 +54,34 @@ def main():
def prettier(): def prettier():
print "prettier"
script = os.path.join(third_party_path, "node_modules", "prettier", script = os.path.join(third_party_path, "node_modules", "prettier",
"bin-prettier.js") "bin-prettier.js")
source_files = git_ls_files(root_path, ["*.js", "*.json", "*.ts", "*.md"]) source_files = get_sources(root_path, ["*.js", "*.json", "*.ts", "*.md"])
run(["node", script, "--write", "--loglevel=error", "--"] + source_files, if source_files:
print_command("prettier", source_files)
run(["node", script, "--write", "--loglevel=error", "--"] +
source_files,
shell=False, shell=False,
quiet=True) quiet=True)
def yapf(): def yapf():
print "yapf"
script = os.path.join(third_party_path, "python_packages", "bin", "yapf") script = os.path.join(third_party_path, "python_packages", "bin", "yapf")
source_files = git_ls_files(root_path, ["*.py"]) source_files = get_sources(root_path, ["*.py"])
run([sys.executable, script, "-i", "--"] + source_files, if source_files:
print_command("yapf", source_files)
run([sys.executable, script, "-i", "--style=pep8", "--"] +
source_files,
env=python_env(), env=python_env(),
shell=False, shell=False,
quiet=True) quiet=True)
def rustfmt(): def rustfmt():
print "rustfmt"
config_file = os.path.join(root_path, ".rustfmt.toml") config_file = os.path.join(root_path, ".rustfmt.toml")
source_files = git_ls_files(root_path, ["*.rs"]) source_files = get_sources(root_path, ["*.rs"])
if source_files:
print_command("rustfmt", source_files)
run([ run([
"rustfmt", "rustfmt",
"--config-path=" + config_file, "--config-path=" + config_file,

View file

@ -5,20 +5,39 @@
import os import os
import sys import sys
import argparse import argparse
from util import enable_ansi_colors, git_ls_files, root_path, run from util import enable_ansi_colors, git_ls_files, git_staged, root_path, run
from util import third_party_path, build_mode from util import third_party_path, build_mode, print_command
from third_party import python_env from third_party import python_env
cmd_args = None
def get_cmd_args():
global cmd_args
if cmd_args:
return cmd_args
parser = argparse.ArgumentParser()
parser.add_argument("--js", help="run eslint", action="store_true")
parser.add_argument("--py", help="run pylint", action="store_true")
parser.add_argument("--rs", help="run clippy", action="store_true")
parser.add_argument(
"--staged", help="run only on staged files", action="store_true")
cmd_args = parser.parse_args()
return cmd_args
def get_sources(*args):
getter = git_staged if get_cmd_args().staged else git_ls_files
return getter(*args)
def main(): def main():
enable_ansi_colors() enable_ansi_colors()
os.chdir(root_path) os.chdir(root_path)
parser = argparse.ArgumentParser() args = get_cmd_args()
parser.add_argument("--js", help="run eslint", action="store_true")
parser.add_argument("--py", help="run pylint", action="store_true")
parser.add_argument("--rs", help="run clippy", action="store_true")
args = parser.parse_args()
did_fmt = False did_fmt = False
if args.js: if args.js:
@ -38,33 +57,36 @@ def main():
def eslint(): def eslint():
print "eslint"
script = os.path.join(third_party_path, "node_modules", "eslint", "bin", script = os.path.join(third_party_path, "node_modules", "eslint", "bin",
"eslint") "eslint")
# Find all *directories* in the main repo that contain .ts/.js files. # Find all *directories* in the main repo that contain .ts/.js files.
source_files = git_ls_files(root_path, [ source_files = get_sources(root_path, [
"*.js", "*.ts", ":!:std/**/testdata/*", ":!:std/**/node_modules/*", "*.js", "*.ts", ":!:std/**/testdata/*", ":!:std/**/node_modules/*",
":!:cli/compilers/*" ":!:cli/compilers/wasm_wrap.js", ":!:cli/tests/error_syntax.js"
]) ])
source_dirs = set([os.path.dirname(f) for f in source_files]) if source_files:
# Within the source dirs, eslint does its own globbing, taking into account print_command("eslint", source_files)
# the exclusion rules listed in '.eslintignore'.
source_globs = ["%s/*.{js,ts}" % d for d in source_dirs]
# Set NODE_PATH so we don't have to maintain a symlink in root_path. # Set NODE_PATH so we don't have to maintain a symlink in root_path.
env = os.environ.copy() env = os.environ.copy()
env["NODE_PATH"] = os.path.join(root_path, "third_party", "node_modules") env["NODE_PATH"] = os.path.join(root_path, "third_party",
run(["node", script, "--max-warnings=0", "--"] + source_globs, "node_modules")
run(["node", script, "--max-warnings=0", "--"] + source_files,
shell=False, shell=False,
env=env, env=env,
quiet=True) quiet=True)
def pylint(): def pylint():
print "pylint"
script = os.path.join(third_party_path, "python_packages", "pylint") script = os.path.join(third_party_path, "python_packages", "pylint")
rcfile = os.path.join(root_path, "tools", "pylintrc") rcfile = os.path.join(root_path, "tools", "pylintrc")
source_files = git_ls_files(root_path, ["*.py"]) msg_template = "{path}({line}:{column}) {category}: {msg} ({symbol})"
run([sys.executable, script, "--rcfile=" + rcfile, "--"] + source_files, source_files = get_sources(root_path, ["*.py"])
if source_files:
print_command("pylint", source_files)
run([
sys.executable, script, "--rcfile=" + rcfile,
"--msg-template=" + msg_template, "--"
] + source_files,
env=python_env(), env=python_env(),
shell=False, shell=False,
quiet=True) quiet=True)

View file

@ -193,6 +193,23 @@ def git_ls_files(base_dir, patterns=None):
return files return files
# list all files staged for commit
def git_staged(base_dir, patterns=None):
base_dir = os.path.abspath(base_dir)
args = [
"git", "-C", base_dir, "diff", "--staged", "--diff-filter=ACMR",
"--name-only", "-z"
]
if patterns:
args += ["--"] + patterns
output = subprocess.check_output(args)
files = [
os.path.normpath(os.path.join(base_dir, f)) for f in output.split("\0")
if f != ""
]
return files
# The Python equivalent of `rm -rf`. # The Python equivalent of `rm -rf`.
def rmtree(directory): def rmtree(directory):
# On Windows, shutil.rmtree() won't delete files that have a readonly bit. # On Windows, shutil.rmtree() won't delete files that have a readonly bit.
@ -406,3 +423,8 @@ def tty_capture(cmd, bytes_input, timeout=5):
os.close(fd) # can't do it sooner: it leads to errno.EIO error os.close(fd) # can't do it sooner: it leads to errno.EIO error
p.wait() p.wait()
return p.returncode, res['stdout'], res['stderr'] return p.returncode, res['stdout'], res['stderr']
def print_command(cmd, files):
noun = "file" if len(files) == 1 else "files"
print "%s (%d %s)" % (cmd, len(files), noun)