예제 #1
0
def runESLint(print_func, files):
    """Runs ESLint on the files that are passed.

    Keyword arguments:
    print_func -- A function to call to print the output.
    files -- A list of files to be checked.
    """
    try:
        basepath = setup_helper.get_project_root()

        if not basepath:
            return False

        if not setup_helper.check_node_executables_valid():
            return False

        if setup_helper.eslint_module_needs_setup():
            setup_helper.eslint_setup()

        dir = os.path.join(basepath, "node_modules", ".bin")

        eslint_path = os.path.join(dir, "eslint")
        if os.path.exists(os.path.join(dir, "eslint.cmd")):
            eslint_path = os.path.join(dir, "eslint.cmd")
        output = check_output(
            [eslint_path, "--format", "json", "--plugin", "html"] + files,
            cwd=basepath)
        display(print_func, output)
        return True
    except CalledProcessError as ex:
        display(print_func, ex.output)
        return False
예제 #2
0
def runESLint(print_func, files):
    """Runs ESLint on the files that are passed.

    Keyword arguments:
    print_func -- A function to call to print the output.
    files -- A list of files to be checked.
    """
    try:
        basepath = setup_helper.get_project_root()

        if not basepath:
            return False

        if not setup_helper.check_node_executables_valid():
            return False

        if setup_helper.eslint_module_needs_setup():
            setup_helper.eslint_setup()

        dir = os.path.join(basepath, "node_modules", ".bin")

        eslint_path = os.path.join(dir, "eslint")
        if os.path.exists(os.path.join(dir, "eslint.cmd")):
            eslint_path = os.path.join(dir, "eslint.cmd")
        output = check_output([eslint_path,
                               "--format", "json", "--plugin", "html"] + files,
                              cwd=basepath)
        display(print_func, output)
        return True
    except CalledProcessError as ex:
        display(print_func, ex.output)
        return False
예제 #3
0
def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):
    """Run eslint."""
    setup_helper.set_project_root(lintargs['root'])
    module_path = setup_helper.get_project_root()

    # Valid binaries are:
    #  - Any provided by the binary argument.
    #  - Any pointed at by the ESLINT environmental variable.
    #  - Those provided by |mach lint --setup|.

    if not binary:
        binary, _ = find_node_executable()

    if not binary:
        print(ESLINT_NOT_FOUND_MESSAGE)
        return 1

    extra_args = lintargs.get('extra_args') or []
    exclude_args = []
    for path in config.get('exclude', []):
        exclude_args.extend(
            ['--ignore-pattern',
             os.path.relpath(path, lintargs['root'])])

    cmd_args = [
        binary,
        os.path.join(module_path, "node_modules", "eslint", "bin",
                     "eslint.js"),
        # Enable the HTML plugin.
        # We can't currently enable this in the global config file
        # because it has bad interactions with the SublimeText
        # ESLint plugin (bug 1229874).
        '--plugin',
        'html',
        # This keeps ext as a single argument.
        '--ext',
        '[{}]'.format(','.join(config['extensions'])),
        '--format',
        'json',
    ] + extra_args + exclude_args + paths

    # eslint requires that --fix be set before the --ext argument.
    if fix:
        cmd_args.insert(2, '--fix')

    shell = False
    if os.environ.get('MSYSTEM') in ('MINGW32', 'MINGW64'):
        # The eslint binary needs to be run from a shell with msys
        shell = True

    orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
    proc = ProcessHandler(cmd_args, env=os.environ, stream=None, shell=shell)
    proc.run()
    signal.signal(signal.SIGINT, orig)

    try:
        proc.wait()
    except KeyboardInterrupt:
        proc.kill()
        return []

    if not proc.output:
        return []  # no output means success

    try:
        jsonresult = json.loads(proc.output[0])
    except ValueError:
        print(ESLINT_ERROR_MESSAGE.format("\n".join(proc.output)))
        return 1

    results = []
    for obj in jsonresult:
        errors = obj['messages']

        for err in errors:
            err.update({
                'hint': err.get('fix'),
                'level': 'error' if err['severity'] == 2 else 'warning',
                'lineno': err.get('line') or 0,
                'path': obj['filePath'],
                'rule': err.get('ruleId'),
            })
            results.append(result.from_config(config, **err))

    return results
예제 #4
0
def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):
    """Run eslint."""
    setup_helper.set_project_root(lintargs['root'])

    module_path = setup_helper.get_project_root()

    if not setup_helper.check_node_executables_valid():
        return 1

    if setup:
        return setup_helper.eslint_setup()

    setup_helper.eslint_maybe_setup()

    # Valid binaries are:
    #  - Any provided by the binary argument.
    #  - Any pointed at by the ESLINT environmental variable.
    #  - Those provided by mach eslint --setup.
    #
    #  eslint --setup installs some mozilla specific plugins and installs
    #  all node modules locally. This is the preferred method of
    #  installation.

    if not binary:
        binary = os.environ.get('ESLINT', None)

        if not binary:
            binary = os.path.join(module_path, "node_modules", ".bin", "eslint")
            if not os.path.isfile(binary):
                binary = None

    if not binary:
        print(ESLINT_NOT_FOUND_MESSAGE)
        return 1

    extra_args = lintargs.get('extra_args') or []
    cmd_args = [binary,
                # Enable the HTML plugin.
                # We can't currently enable this in the global config file
                # because it has bad interactions with the SublimeText
                # ESLint plugin (bug 1229874).
                '--plugin', 'html',
                # This keeps ext as a single argument.
                '--ext', '[{}]'.format(','.join(config['extensions'])),
                '--format', 'json',
                ] + extra_args + paths

    # eslint requires that --fix be set before the --ext argument.
    if fix:
        cmd_args.insert(1, '--fix')

    shell = False
    if os.environ.get('MSYSTEM') in ('MINGW32', 'MINGW64'):
        # The eslint binary needs to be run from a shell with msys
        shell = True

    orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
    proc = ProcessHandler(cmd_args, env=os.environ, stream=None, shell=shell)
    proc.run()
    signal.signal(signal.SIGINT, orig)

    try:
        proc.wait()
    except KeyboardInterrupt:
        proc.kill()
        return []

    if not proc.output:
        return []  # no output means success

    try:
        jsonresult = json.loads(proc.output[0])
    except ValueError:
        print(ESLINT_ERROR_MESSAGE.format("\n".join(proc.output)))
        return 1

    results = []
    for obj in jsonresult:
        errors = obj['messages']

        for err in errors:
            err.update({
                'hint': err.get('fix'),
                'level': 'error' if err['severity'] == 2 else 'warning',
                'lineno': err.get('line'),
                'path': obj['filePath'],
                'rule': err.get('ruleId'),
            })
            results.append(result.from_config(config, **err))

    return results