Пример #1
0
def _CheckFormat(input_api, output_api):
    def popen(args):
        return input_api.subprocess.Popen(args=args,
                                          stdout=input_api.subprocess.PIPE,
                                          stderr=input_api.subprocess.STDOUT)

    affected_files = _getAffectedJSFiles(input_api)
    if len(affected_files) == 0:
        return []
    original_sys_path = sys.path
    try:
        sys.path = sys.path + [
            input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts")
        ]
        import local_node
    finally:
        sys.path = original_sys_path

    ignore_files = []
    eslint_ignore_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
                                                '.eslintignore')
    with open(eslint_ignore_path, 'r') as ignore_manifest:
        for line in ignore_manifest:
            ignore_files.append(line.strip())
    formattable_files = [
        affected_file for affected_file in affected_files if all(
            ignore_file not in affected_file for ignore_file in ignore_files)
    ]

    check_formatting_process = popen(
        ['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files)
    check_formatting_process.communicate()
    if check_formatting_process.returncode == 0:
        return []

    format_args = ['git', 'cl', 'format', '--js'] + formattable_files
    format_process = popen(format_args)
    format_out, _ = format_process.communicate()
    if format_process.returncode != 0:
        return [output_api.PresubmitError(format_out)]

    # Use eslint to autofix the braces.
    # Also fix semicolon to avoid confusing clang-format.
    eslint_process = popen([
        local_node.node_path(),
        local_node.eslint_path(), '--no-eslintrc', '--fix', '--env=es6',
        '--rule={"curly": [2, "multi-or-nest", "consistent"], "semi": 2}'
    ] + affected_files)
    eslint_process.communicate()

    # Need to run clang-format again to align the braces
    popen(format_args).communicate()

    return [
        output_api.PresubmitError(
            "ERROR: Found formatting violations in third_party/WebKit/Source/devtools.\n"
            "Ran clang-format on diff\n"
            "Use git status to check the formatting changes"),
        output_api.PresubmitError(format_out),
    ]
Пример #2
0
def js_lint(files_list=None):
    eslint_errors_found = False

    if files_list is None:
        files_list = [devtools_frontend_path]
    files_list = [file_name for file_name in files_list if not file_name.endswith(".eslintrc.js")]

    eslintconfig_path = path.join(devtools_path, ".eslintrc.js")
    eslintignore_path = path.join(devtools_path, ".eslintignore")
    exec_command = [
        local_node.node_path(),
        local_node.eslint_path(),
        "--config",
        to_platform_path_exact(eslintconfig_path),
        "--ignore-path",
        to_platform_path_exact(eslintignore_path),
    ] + files_list

    eslint_proc = popen(exec_command, cwd=devtools_path)
    (eslint_proc_out, _) = eslint_proc.communicate()
    if eslint_proc.returncode != 0:
        eslint_errors_found = True
    else:
        print("eslint exited successfully")

    print(eslint_proc_out)
    return eslint_errors_found