xref: /aosp_15_r20/external/pigweed/pw_presubmit/py/pw_presubmit/javascript_checks.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""JavaScript and TypeScript validity check."""
15
16from pw_presubmit import presubmit_context
17from pw_presubmit.presubmit import (
18    Check,
19    filter_paths,
20)
21from pw_presubmit.presubmit_context import (
22    PresubmitContext,
23    PresubmitFailure,
24)
25from pw_presubmit.tools import log_run
26
27
28@filter_paths(endswith=('.js', '.ts'))
29@Check
30def eslint(ctx: PresubmitContext):
31    """Presubmit check that ensures JavaScript files are valid."""
32
33    ctx.paths = presubmit_context.apply_exclusions(ctx)
34
35    # Check if npm deps are installed.
36    npm_list = log_run(['npm', 'list'])
37    if npm_list.returncode != 0:
38        npm_install = log_run(['npm', 'install'])
39        if npm_install.returncode != 0:
40            raise PresubmitFailure('npm install failed.')
41
42    result = log_run(['npm', 'exec', 'eslint', *ctx.paths])
43    if result.returncode != 0:
44        raise PresubmitFailure('eslint identifed issues.')
45