diff options
Diffstat (limited to 'pw_presubmit/py/pw_presubmit/javascript_checks.py')
-rw-r--r-- | pw_presubmit/py/pw_presubmit/javascript_checks.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/pw_presubmit/py/pw_presubmit/javascript_checks.py b/pw_presubmit/py/pw_presubmit/javascript_checks.py new file mode 100644 index 000000000..3dc8b8f68 --- /dev/null +++ b/pw_presubmit/py/pw_presubmit/javascript_checks.py @@ -0,0 +1,44 @@ +# Copyright 2023 The Pigweed Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +"""JavaScript and TypeScript validity check.""" + +from pw_presubmit import presubmit_context +from pw_presubmit.presubmit import ( + Check, + filter_paths, +) +from pw_presubmit.presubmit_context import ( + PresubmitContext, + PresubmitFailure, +) +from pw_presubmit.tools import log_run + + +@filter_paths(endswith=('.js', '.ts')) +@Check +def eslint(ctx: PresubmitContext): + """Presubmit check that ensures JavaScript files are valid.""" + + ctx.paths = presubmit_context.apply_exclusions(ctx) + + # Check if npm deps are installed. + npm_list = log_run(['npm', 'list']) + if npm_list.returncode != 0: + npm_install = log_run(['npm', 'install']) + if npm_install.returncode != 0: + raise PresubmitFailure('npm install failed.') + + result = log_run(['npx', 'eslint@8.47.0', *ctx.paths]) + if result.returncode != 0: + raise PresubmitFailure('eslint identifed issues.') |