Exemple #1
0
def get_formatted_reports_for_path(
    path: Path,
    opts: LintOpts,
    metadata_cache: Optional[Mapping["ProviderT", object]] = None,
) -> Iterable[str]:
    with open(path, "rb") as f:
        source = f.read()

    try:
        cst_wrapper = None
        if metadata_cache is not None:
            cst_wrapper = MetadataWrapper(parse_module(source), True, metadata_cache)
        raw_reports = lint_file(
            path,
            source,
            rules=opts.rules,
            use_ignore_byte_markers=opts.use_ignore_byte_markers,
            use_ignore_comments=opts.use_ignore_comments,
            cst_wrapper=cst_wrapper,
            find_unused_suppressions=True,
        )
    except (SyntaxError, ParserSyntaxError) as e:
        print_red(
            f"Encountered the following error while parsing source code in file {path}:"
        )
        print(e)
        return []

    # linter completed successfully
    return [opts.formatter.format(rr) for rr in raw_reports]
Exemple #2
0
def apply_fix_operation(
    path: Path,
    opts: LintOpts,
    metadata_cache: Optional[Mapping["ProviderT", object]] = None,
) -> Iterable[str]:
    with open(path, "rb") as f:
        source = f.read()
    patched_files_list = opts.patched_files_list
    try:
        if patched_files_list is None:
            lint_result = lint_file_and_apply_patches(
                path,
                source,
                rules=opts.rules,
                use_ignore_byte_markers=opts.use_ignore_byte_markers,
                use_ignore_comments=opts.use_ignore_comments,
                find_unused_suppressions=True,
            )
            raw_reports = lint_result.reports
            updated_source = lint_result.patched_source
            if updated_source != source:
                if not opts.skip_autoformatter:
                    # Format the code using the config file's formatter.
                    updated_source = invoke_formatter(
                        get_lint_config().formatter, updated_source)
                with open(path, "wb") as f:
                    f.write(updated_source)
        else:
            lint_result = get_one_patchable_report_for_path(
                path,
                source,
                opts.rules,
                opts.use_ignore_byte_markers,
                opts.use_ignore_comments,
                metadata_cache,
            )
            # Will either be a single patchable report, or a collection of non-patchable reports.
            raw_reports = lint_result.reports
            updated_source = lint_result.patched_source
            if updated_source != source:
                # We don't do any formatting here as it's wasteful. The caller should handle formatting all files at the end.
                with open(path, "wb") as f:
                    f.write(updated_source)

                patched_files_list.append(str(path))
                # Return only the report that was used in the patched source.
                return [next(opts.formatter.format(rr) for rr in raw_reports)]
    except (SyntaxError, ParserSyntaxError) as e:
        print_red(
            f"Encountered the following error while parsing source code in file {path}:"
        )
        print(e)
        return []

    return [opts.formatter.format(rr) for rr in raw_reports]
def get_formatted_reports_for_path(
    path: Path,
    opts: InsertSuppressionsOpts,
    metadata_cache: Optional[Mapping["ProviderT", object]] = None,
) -> Iterable[str]:
    with open(path, "rb") as f:
        source = f.read()

    try:
        cst_wrapper = None
        if metadata_cache is not None:
            cst_wrapper = MetadataWrapper(
                parse_module(source),
                True,
                metadata_cache,
            )
        raw_reports = lint_file(
            path, source, rules={opts.rule}, cst_wrapper=cst_wrapper
        )
    except (SyntaxError, ParserSyntaxError) as e:
        print_red(
            f"Encountered the following error while parsing source code in file {path}:"
        )
        print(e)
        return []

    opts_message = opts.message
    comments = []
    for rr in raw_reports:
        if isinstance(opts_message, str):
            message = opts_message
        elif opts_message == MessageKind.USE_LINT_REPORT:
            message = rr.message
        else:  # opts_message == MessageKind.NO_MESSAGE
            message = None
        comments.append(
            SuppressionComment(opts.kind, rr.line, rr.code, message, opts.max_lines)
        )
    insert_suppressions_result = insert_suppressions(source, comments)
    updated_source = insert_suppressions_result.updated_source
    assert (
        not insert_suppressions_result.failed_insertions
    ), "Failed to insert some comments. This should not be possible."

    if updated_source != source:
        if not opts.skip_autoformatter:
            # Format the code using the config file's formatter.
            updated_source = invoke_formatter(
                get_lint_config().formatter, updated_source
            )
        with open(path, "wb") as f:
            f.write(updated_source)

    # linter completed successfully
    return [opts.formatter.format(rr) for rr in raw_reports]