Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/scripts/lint_javascript.py |
| diff --git a/third_party/WebKit/Source/devtools/scripts/lint_javascript.py b/third_party/WebKit/Source/devtools/scripts/lint_javascript.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..5f39b857e78859385c470a346efc1990c6d283c9 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/scripts/lint_javascript.py |
| @@ -0,0 +1,133 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 Google Inc. All rights reserved. |
|
caseq
2016/04/26 01:39:44
Let's bring the 3-line chromium authors copyright?
paulirish
2016/04/26 03:37:43
done
|
| +# |
| +# Redistribution and use in source and binary forms, with or without |
| +# modification, are permitted provided that the following conditions are |
| +# met: |
| +# |
| +# * Redistributions of source code must retain the above copyright |
| +# notice, this list of conditions and the following disclaimer. |
| +# * Redistributions in binary form must reproduce the above |
| +# copyright notice, this list of conditions and the following disclaimer |
| +# in the documentation and/or other materials provided with the |
| +# distribution. |
| +# * Neither the name of Google Inc. nor the names of its |
| +# contributors may be used to endorse or promote products derived from |
| +# this software without specific prior written permission. |
| +# |
| +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + |
| +import os |
| +import os.path as path |
| +import re |
| +import subprocess |
| +import sys |
| + |
| +files_to_lint = None |
| + |
| +if len(sys.argv) == 2: |
| + if sys.argv[1] == '--help': |
|
caseq
2016/04/26 01:39:44
Let's settle on one quote style? Either is good pe
paulirish
2016/04/26 03:37:43
double. you got it.
|
| + print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0])) |
| + print(" [file|dir|glob]* Path or glob to run eslint on.") |
| + print(" If absent, the entire frontend will be checked.") |
| + sys.exit(0) |
| + |
| + else: |
| + print 'Linting only this path:\n %s' % sys.argv[1:] |
| + files_to_lint = sys.argv[1:] |
| + |
| + |
| +is_cygwin = sys.platform == 'cygwin' |
| + |
| + |
| +def popen(arguments): |
| + return subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| + |
| + |
| +def to_platform_path(filepath): |
| + if not is_cygwin: |
| + return filepath |
| + return re.sub(r'^/cygdrive/(\w)', '\\1:', filepath) |
| + |
| + |
| +def to_platform_path_exact(filepath): |
| + if not is_cygwin: |
| + return filepath |
| + output, _ = popen(['cygpath', '-w', filepath]).communicate() |
| + # pylint: disable=E1103 |
| + return output.strip().replace('\\', '\\\\') |
| + |
| +scripts_path = path.dirname(path.abspath(__file__)) |
| +devtools_path = path.dirname(scripts_path) |
| +devtools_frontend_path = path.join(devtools_path, 'front_end') |
| + |
| + |
| +# Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python. |
| +def which(program): |
| + def is_exe(fpath): |
| + return path.isfile(fpath) and os.access(fpath, os.X_OK) |
| + |
| + fpath, fname = path.split(program) |
| + if fpath: |
| + if is_exe(program): |
| + return program |
| + else: |
| + for part in os.environ["PATH"].split(os.pathsep): |
| + part = part.strip('"') |
| + exe_file = path.join(part, program) |
| + if is_exe(exe_file): |
| + return exe_file |
| + return None |
| + |
| + |
| +print 'Linting JavaScript with eslint...\n' |
| +errors_found = False |
|
caseq
2016/04/26 01:39:44
move into js_lint?
paulirish
2016/04/26 03:37:43
Seems good. done.
|
| + |
| + |
| +def js_lint(files_list=None): |
| + eslint_path = which('eslint') |
| + if not eslint_path: |
| + print '!! Skipping JavaScript linting because eslint is not installed.' |
| + print '!! npm install -g eslint' |
| + errors_found = False # Linting is opt-in for now, so this is a soft failure |
| + return errors_found |
| + |
| + if files_list is None: |
| + files_list = [devtools_frontend_path] |
| + |
| + eslintconfig_path = path.join(devtools_path, 'front_end/.eslintrc.js') |
| + eslintignore_path = path.join(devtools_path, 'front_end/.eslintignore') |
| + exec_command = [ |
| + eslint_path, |
| + '--config', to_platform_path_exact(eslintconfig_path), |
| + '--ignore-path', to_platform_path_exact(eslintignore_path), |
| + ' '.join(files_list) |
| + ] |
| + |
| + eslint_proc = popen(exec_command) |
| + (eslint_proc_out, _) = eslint_proc.communicate() |
| + if eslint_proc.returncode != 0: |
| + errors_found = True |
| + print eslint_proc_out |
|
caseq
2016/04/26 01:39:44
move out of conditional?
paulirish
2016/04/26 03:37:43
done
|
| + else: |
| + print 'eslint exited successfully' |
| + print eslint_proc_out |
| + |
| + return errors_found |
| + |
| +errors_found = js_lint(files_to_lint) |
| + |
| +if errors_found: |
| + print 'ERRORS DETECTED' |
| + sys.exit(1) |