blob: ae7daf2a014a8f99cbeb069c2744ed58631881b4 [file] [log] [blame]
#!/usr/bin/env python3
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Lint our source files."""
import glob
import os
import sys
import nassh # pylint: disable=wrong-import-order
import libdot
JS_DIR = nassh.DIR / "js"
WASSH_DIR = libdot.LIBAPPS_DIR / "wassh"
WJB_DIR = libdot.LIBAPPS_DIR / "wasi-js-bindings"
# Path to generated deps file that terminal uses for libdot/hterm/etc...
DEPS_FILE = os.path.join(JS_DIR, "deps_local.concat.js")
def _get_default_paths(basedir):
"""Get list of paths to lint by default."""
most_files = sorted(
x
for x in libdot.lint.get_known_sources(basedir)
if x.suffix not in {".js"}
)
# All files in js/*.js excluding generated files.
# Use relpath for nicer default output.
# Sort to ensure nasftp.js comes before nasftp_cli.js, etc.
js_files = sorted(
x for x in JS_DIR.glob("*.js") if not x.name.startswith("deps_")
)
return [os.path.relpath(x) for x in most_files + js_files]
def mkdeps(_opts):
"""Build the deps we might use when linting."""
if not os.path.exists(DEPS_FILE):
libdot.run([os.path.join(nassh.DIR, "bin", "mkdeps")])
def main(argv):
"""The main func!"""
# We need to use an absolute path with the module root to workaround
# https://github.com/google/closure-compiler/issues/3580
for i, arg in enumerate(argv):
if arg and arg[0] != "-" and os.path.exists(arg):
argv[i] = os.path.relpath(os.path.realpath(arg), nassh.DIR)
os.chdir(nassh.DIR)
wasi_externs = WJB_DIR / "externs"
externs = ["--js_module_root", JS_DIR] + [
f"--externs={os.path.relpath(x)}"
for x in sorted(glob.glob(os.path.join(nassh.DIR, "externs", "*.js")))
]
closure_args = (
list(libdot.lint.DEFAULT_CLOSURE_ARGS)
+ externs
+ [
# TODO(vapier): We want to turn this on at some point.
"--jscomp_off=strictMissingProperties",
os.path.relpath(DEPS_FILE),
# Let closure compiler itself do the expansion.
WJB_DIR / "js" / "**.js",
WASSH_DIR / "js" / "**.js",
]
+ [
f"--externs={os.path.relpath(x)}"
for x in sorted(wasi_externs.glob("*.js"))
]
)
return libdot.lint.main(
argv,
basedir=nassh.DIR,
get_default_paths=_get_default_paths,
mkdeps=mkdeps,
closure_args=closure_args,
)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))