jarbin-c-coding-style 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
File without changes
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import hashlib
|
|
3
|
+
from collections import Counter
|
|
4
|
+
from collections.abc import Callable, Iterable
|
|
5
|
+
|
|
6
|
+
from sources.diagnostics.model import Diagnostic
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Number of hexadecimal characters kept from the SHA-256 digest (plan 06, DIAG-03).
|
|
10
|
+
FINGERPRINT_LENGTH: int = 32
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def normalize_line_text(text: str) -> str:
|
|
14
|
+
"""
|
|
15
|
+
Normalise the text of the offending line: leading/trailing whitespace trimmed, internal whitespace
|
|
16
|
+
collapsed to one space (plan 06, section 6).
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
text : str
|
|
21
|
+
Raw line text, without its end of line.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
----------
|
|
25
|
+
str
|
|
26
|
+
The normalised text.
|
|
27
|
+
"""
|
|
28
|
+
return " ".join(text.split())
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def compute_fingerprint(rule_id: str, path: str, line_text: str, occurrence: int) -> str:
|
|
32
|
+
"""
|
|
33
|
+
Fingerprint of one diagnostic: sha256(rule_id \\0 path \\0 normalized_line_text \\0 occurrence_index),
|
|
34
|
+
truncated (plan 06, DIAG-03). Line numbers are excluded on purpose, so the fingerprint survives
|
|
35
|
+
unrelated edits.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
rule_id : str
|
|
40
|
+
Rule identifier (`<category>/<name>` or `jccs/*`).
|
|
41
|
+
path : str
|
|
42
|
+
File, relative POSIX path.
|
|
43
|
+
line_text : str
|
|
44
|
+
Normalised text of the first line of the span.
|
|
45
|
+
occurrence : int
|
|
46
|
+
Index of this (rule, path, text) triple among identical ones, in file order.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
----------
|
|
50
|
+
str
|
|
51
|
+
32 hexadecimal characters.
|
|
52
|
+
"""
|
|
53
|
+
payload: bytes = "\0".join((rule_id, path, line_text, str(occurrence))).encode("utf-8", "surrogatepass")
|
|
54
|
+
return hashlib.sha256(payload).hexdigest()[:FINGERPRINT_LENGTH]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def assign_fingerprints(diagnostics: Iterable[Diagnostic], line_text: Callable[[str, int], str]) -> tuple[Diagnostic, ...]:
|
|
58
|
+
"""
|
|
59
|
+
Give every diagnostic its fingerprint. The diagnostics must be in their final, sorted order: the
|
|
60
|
+
occurrence index counts identical triples in that order.
|
|
61
|
+
|
|
62
|
+
Parameters
|
|
63
|
+
----------
|
|
64
|
+
diagnostics : Iterable[Diagnostic]
|
|
65
|
+
Sorted diagnostics.
|
|
66
|
+
line_text : Callable[[str, int], str]
|
|
67
|
+
Raw text of a line, from (path, 1-based line number); "" when unknown.
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
----------
|
|
71
|
+
tuple[Diagnostic, ...]
|
|
72
|
+
The same diagnostics, in the same order, with `fingerprint` set.
|
|
73
|
+
"""
|
|
74
|
+
seen: Counter[tuple[str, str, str]] = Counter()
|
|
75
|
+
result: list[Diagnostic] = []
|
|
76
|
+
for diagnostic in diagnostics:
|
|
77
|
+
text: str = normalize_line_text(line_text(diagnostic.location.path, diagnostic.location.line))
|
|
78
|
+
triple: tuple[str, str, str] = (diagnostic.rule_id, diagnostic.location.path, text)
|
|
79
|
+
fingerprint: str = compute_fingerprint(*triple, seen[triple])
|
|
80
|
+
seen[triple] += 1
|
|
81
|
+
result.append(dataclasses.replace(diagnostic, fingerprint=fingerprint))
|
|
82
|
+
return tuple(result)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from types import MappingProxyType
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from sources.enums import Detection, FixApplicability, SeverityLevel
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Order of severities, from least to most severe.
|
|
10
|
+
SEVERITY_RANK: Mapping[SeverityLevel, int] = MappingProxyType({
|
|
11
|
+
SeverityLevel.INFO: 0,
|
|
12
|
+
SeverityLevel.MINOR: 1,
|
|
13
|
+
SeverityLevel.MAJOR: 2,
|
|
14
|
+
SeverityLevel.FATAL: 3,
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
# Code of the diagnostic emitted when a check or an analysis step crashes.
|
|
18
|
+
INTERNAL_ERROR_CODE: str = "jccs/internal-error"
|
|
19
|
+
|
|
20
|
+
# Code of the diagnostic emitted once per file that could only be partially parsed.
|
|
21
|
+
SYNTAX_ERROR_CODE: str = "jccs/syntax-error"
|
|
22
|
+
|
|
23
|
+
# Code of the diagnostic emitted for files that are not valid UTF-8.
|
|
24
|
+
ENCODING_CODE: str = "jccs/encoding"
|
|
25
|
+
|
|
26
|
+
# Code of the diagnostic emitted for files whose content was not loaded.
|
|
27
|
+
FILE_SKIPPED_CODE: str = "jccs/file-skipped"
|
|
28
|
+
|
|
29
|
+
# Code of the diagnostic emitted for a fix the verification rejected (plan 07, section 4).
|
|
30
|
+
FIX_REJECTED_CODE: str = "jccs/fix-rejected"
|
|
31
|
+
|
|
32
|
+
# Internal codes that never count toward failure, whatever the threshold (plan 06, section 4).
|
|
33
|
+
NEVER_COUNTED_CODES: frozenset[str] = frozenset({FILE_SKIPPED_CODE, FIX_REJECTED_CODE})
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, slots=True)
|
|
37
|
+
class Location:
|
|
38
|
+
"""
|
|
39
|
+
Where a diagnostic is (plan 06, section 2).
|
|
40
|
+
(dataclass)
|
|
41
|
+
|
|
42
|
+
Attributes
|
|
43
|
+
----------
|
|
44
|
+
path : str
|
|
45
|
+
File, relative to the project root, POSIX separators.
|
|
46
|
+
line : int
|
|
47
|
+
1-based line.
|
|
48
|
+
column : int
|
|
49
|
+
1-based column, in code points.
|
|
50
|
+
end_line : int | None
|
|
51
|
+
1-based line of the end of the span.
|
|
52
|
+
end_column : int | None
|
|
53
|
+
1-based column just after the span.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
path: str
|
|
57
|
+
line: int
|
|
58
|
+
column: int
|
|
59
|
+
end_line: int | None = None
|
|
60
|
+
end_column: int | None = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True, slots=True)
|
|
64
|
+
class Diagnostic:
|
|
65
|
+
"""
|
|
66
|
+
One finding reported to the user (plan 06, section 2).
|
|
67
|
+
(dataclass)
|
|
68
|
+
|
|
69
|
+
Attributes
|
|
70
|
+
----------
|
|
71
|
+
rule_id : str
|
|
72
|
+
`<category>/<name>`, or an internal `jccs/*` code.
|
|
73
|
+
check_id : str | None
|
|
74
|
+
Check that produced the finding.
|
|
75
|
+
code : str
|
|
76
|
+
Display code (e.g. "C-F4").
|
|
77
|
+
severity : SeverityLevel
|
|
78
|
+
Effective severity.
|
|
79
|
+
message : str
|
|
80
|
+
Rendered message.
|
|
81
|
+
location : Location
|
|
82
|
+
Primary location.
|
|
83
|
+
detection : Detection
|
|
84
|
+
Detection level of the check.
|
|
85
|
+
review : bool
|
|
86
|
+
Whether the item needs a human review (heuristic or manual checks, uncertain findings).
|
|
87
|
+
data : Mapping[str, Any]
|
|
88
|
+
Structured payload (value, limit...).
|
|
89
|
+
fix : FixApplicability
|
|
90
|
+
Fix applicability of the check.
|
|
91
|
+
fix_kind : str | None
|
|
92
|
+
Fix primitive of the check (plan 07).
|
|
93
|
+
tags : tuple[str, ...]
|
|
94
|
+
Tags of the rule.
|
|
95
|
+
fingerprint : str
|
|
96
|
+
Stable identity across unrelated edits (plan 06, DIAG-03); "" until assigned by the runner.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
rule_id: str
|
|
100
|
+
check_id: str | None
|
|
101
|
+
code: str
|
|
102
|
+
severity: SeverityLevel
|
|
103
|
+
message: str
|
|
104
|
+
location: Location
|
|
105
|
+
detection: Detection = Detection.AUTOMATIC
|
|
106
|
+
review: bool = False
|
|
107
|
+
data: Mapping[str, Any] = field(default_factory=lambda: MappingProxyType({}))
|
|
108
|
+
fix: FixApplicability = FixApplicability.NONE
|
|
109
|
+
fix_kind: str | None = None
|
|
110
|
+
tags: tuple[str, ...] = ()
|
|
111
|
+
fingerprint: str = field(default="", compare=False)
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def is_manual(self) -> bool:
|
|
115
|
+
"""
|
|
116
|
+
Whether the item comes from a manual check (never counts toward failure).
|
|
117
|
+
|
|
118
|
+
Returns
|
|
119
|
+
----------
|
|
120
|
+
bool
|
|
121
|
+
True for a manual review item.
|
|
122
|
+
"""
|
|
123
|
+
return self.detection is Detection.MANUAL
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def display_code(self) -> str:
|
|
127
|
+
"""
|
|
128
|
+
Code shown in reports: the rule code, followed by the check ID when there is one.
|
|
129
|
+
|
|
130
|
+
Returns
|
|
131
|
+
----------
|
|
132
|
+
str
|
|
133
|
+
e.g. "C-L1:assignment_in_condition".
|
|
134
|
+
"""
|
|
135
|
+
return f"{self.code}:{self.check_id}" if self.check_id else self.code
|
|
136
|
+
|
|
137
|
+
def sort_key(self) -> tuple[str, int, int, str, str, str]:
|
|
138
|
+
"""
|
|
139
|
+
Deterministic ordering key (plan 06, DIAG-06).
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
----------
|
|
143
|
+
tuple[str, int, int, str, str, str]
|
|
144
|
+
(path, line, column, rule, check, message).
|
|
145
|
+
"""
|
|
146
|
+
return (self.location.path, self.location.line, self.location.column, self.rule_id, self.check_id or "", self.message)
|
|
File without changes
|
sources/engine/api.py
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from collections.abc import Iterable, Mapping
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from types import MappingProxyType
|
|
6
|
+
|
|
7
|
+
from sources.analysis.project import ProjectContext, build_project_context
|
|
8
|
+
from sources.configs.project import (
|
|
9
|
+
ProjectConfig,
|
|
10
|
+
ensure_project_directory,
|
|
11
|
+
find_project_root,
|
|
12
|
+
load_project_config,
|
|
13
|
+
load_project_config_file,
|
|
14
|
+
resolve_config_argument,
|
|
15
|
+
)
|
|
16
|
+
from sources.engine.outcome import Outcome, describe_outcome
|
|
17
|
+
from sources.engine.plan import ActivePlan, build_plan
|
|
18
|
+
from sources.engine.runner import RunResult, run_plan
|
|
19
|
+
from sources.engine.scanner import LoadedFiles, discover_files, load_files
|
|
20
|
+
from sources.enums import ExitCode, SeverityLevel
|
|
21
|
+
from sources.errors.exceptions import ProjectConfigError, ProjectEnvironmentError
|
|
22
|
+
from sources.rules.catalog import RuleCatalog
|
|
23
|
+
from sources.utils.globs import match_any_glob
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True, slots=True)
|
|
27
|
+
class CheckOptions:
|
|
28
|
+
"""
|
|
29
|
+
Settings of a check run that do not come from the project config (command line, API callers).
|
|
30
|
+
(dataclass)
|
|
31
|
+
|
|
32
|
+
Attributes
|
|
33
|
+
----------
|
|
34
|
+
targets : tuple[Path, ...]
|
|
35
|
+
Files or directories to analyse (empty: the whole project); the context always holds every file.
|
|
36
|
+
only : tuple[str, ...]
|
|
37
|
+
Selectors replacing the active rule set.
|
|
38
|
+
argument_overrides : Mapping[str, Mapping[str, str]]
|
|
39
|
+
Raw rule argument values by rule ID or alias.
|
|
40
|
+
config_file : Path | None
|
|
41
|
+
Config file or directory replacing the discovered `.JCCS/.jccs.ini`.
|
|
42
|
+
include : tuple[str, ...]
|
|
43
|
+
Globs added to the config `include` list.
|
|
44
|
+
exclude : tuple[str, ...]
|
|
45
|
+
Globs added to the config `exclude` list.
|
|
46
|
+
fail_on : SeverityLevel | None
|
|
47
|
+
Overrides the config failure threshold.
|
|
48
|
+
max_errors : int | None
|
|
49
|
+
Overrides the config error budget.
|
|
50
|
+
strict_heuristics : bool | None
|
|
51
|
+
Overrides the config value.
|
|
52
|
+
create_project_directory : bool
|
|
53
|
+
Whether `.JCCS` is created when missing.
|
|
54
|
+
restrict_to : tuple[Path, ...] | None
|
|
55
|
+
When set, only these files are analysed (e.g. files changed since a git ref); combined with `targets`.
|
|
56
|
+
buffer : tuple[Path, bytes] | None
|
|
57
|
+
A file whose content comes from the caller instead of the disk (unsaved editor buffer); it is the only
|
|
58
|
+
file analysed.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
targets: tuple[Path, ...] = ()
|
|
62
|
+
only: tuple[str, ...] = ()
|
|
63
|
+
argument_overrides: Mapping[str, Mapping[str, str]] = field(default_factory=lambda: MappingProxyType({}))
|
|
64
|
+
config_file: Path | None = None
|
|
65
|
+
include: tuple[str, ...] = ()
|
|
66
|
+
exclude: tuple[str, ...] = ()
|
|
67
|
+
fail_on: SeverityLevel | None = None
|
|
68
|
+
max_errors: int | None = None
|
|
69
|
+
strict_heuristics: bool | None = None
|
|
70
|
+
create_project_directory: bool = True
|
|
71
|
+
restrict_to: tuple[Path, ...] | None = None
|
|
72
|
+
buffer: tuple[Path, bytes] | None = None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass(frozen=True, slots=True)
|
|
76
|
+
class FixRecord:
|
|
77
|
+
"""
|
|
78
|
+
One fix applied by `check --fix` (reported in the summary and the JSON `fixes` array).
|
|
79
|
+
(dataclass)
|
|
80
|
+
|
|
81
|
+
Attributes
|
|
82
|
+
----------
|
|
83
|
+
path : str
|
|
84
|
+
File, relative POSIX path.
|
|
85
|
+
rule_id : str
|
|
86
|
+
Rule of the fixed diagnostic.
|
|
87
|
+
check_id : str | None
|
|
88
|
+
Check of the fixed diagnostic.
|
|
89
|
+
code : str
|
|
90
|
+
Display code of the rule.
|
|
91
|
+
kind : str
|
|
92
|
+
Fix primitive.
|
|
93
|
+
line : int
|
|
94
|
+
Line of the diagnostic before fixing.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
path: str
|
|
98
|
+
rule_id: str
|
|
99
|
+
check_id: str | None
|
|
100
|
+
code: str
|
|
101
|
+
kind: str
|
|
102
|
+
line: int
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True, slots=True)
|
|
106
|
+
class CheckReport:
|
|
107
|
+
"""
|
|
108
|
+
Everything a `check` run produced, for the output layer.
|
|
109
|
+
(dataclass)
|
|
110
|
+
|
|
111
|
+
Attributes
|
|
112
|
+
----------
|
|
113
|
+
root : Path
|
|
114
|
+
Project root.
|
|
115
|
+
config : ProjectConfig
|
|
116
|
+
Configuration used.
|
|
117
|
+
catalog : RuleCatalog
|
|
118
|
+
Known rules.
|
|
119
|
+
plan : ActivePlan
|
|
120
|
+
Rules that ran.
|
|
121
|
+
project : ProjectContext
|
|
122
|
+
The project, as loaded in memory.
|
|
123
|
+
result : RunResult
|
|
124
|
+
Diagnostics and counters.
|
|
125
|
+
outcome : Outcome
|
|
126
|
+
Exit code and its explanation.
|
|
127
|
+
fail_on : SeverityLevel
|
|
128
|
+
Failure threshold used.
|
|
129
|
+
max_errors : int
|
|
130
|
+
Error budget used.
|
|
131
|
+
strict_heuristics : bool
|
|
132
|
+
Whether heuristic findings counted.
|
|
133
|
+
timing : Mapping[str, float]
|
|
134
|
+
Duration of each phase, in milliseconds.
|
|
135
|
+
fixing : bool
|
|
136
|
+
Whether the report describes the state after `--fix`.
|
|
137
|
+
fixes : tuple[FixRecord, ...]
|
|
138
|
+
Fixes applied, sorted by path, line and rule.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
root: Path
|
|
142
|
+
config: ProjectConfig
|
|
143
|
+
catalog: RuleCatalog
|
|
144
|
+
plan: ActivePlan
|
|
145
|
+
project: ProjectContext
|
|
146
|
+
result: RunResult
|
|
147
|
+
outcome: Outcome
|
|
148
|
+
fail_on: SeverityLevel
|
|
149
|
+
max_errors: int
|
|
150
|
+
strict_heuristics: bool
|
|
151
|
+
timing: Mapping[str, float]
|
|
152
|
+
fixing: bool = False
|
|
153
|
+
fixes: tuple[FixRecord, ...] = ()
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def exit_code(self) -> ExitCode:
|
|
157
|
+
"""
|
|
158
|
+
Exit code of the run.
|
|
159
|
+
|
|
160
|
+
Returns
|
|
161
|
+
----------
|
|
162
|
+
ExitCode
|
|
163
|
+
The exit code.
|
|
164
|
+
"""
|
|
165
|
+
return self.outcome.exit_code
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _relative_targets(root: Path, targets: Iterable[Path]) -> list[str]:
|
|
169
|
+
"""
|
|
170
|
+
Turn target paths (files or directories) into project-relative POSIX prefixes.
|
|
171
|
+
|
|
172
|
+
Parameters
|
|
173
|
+
----------
|
|
174
|
+
root : Path
|
|
175
|
+
Project root.
|
|
176
|
+
targets : Iterable[Path]
|
|
177
|
+
Paths given by the user.
|
|
178
|
+
|
|
179
|
+
Returns
|
|
180
|
+
----------
|
|
181
|
+
list[str]
|
|
182
|
+
Relative paths ("" for the root itself).
|
|
183
|
+
|
|
184
|
+
Raises
|
|
185
|
+
----------
|
|
186
|
+
ProjectEnvironmentError
|
|
187
|
+
A target does not exist or is outside the project.
|
|
188
|
+
"""
|
|
189
|
+
relative: list[str] = []
|
|
190
|
+
for target in targets:
|
|
191
|
+
resolved: Path = target.resolve()
|
|
192
|
+
if not resolved.exists():
|
|
193
|
+
raise ProjectEnvironmentError(f"'{target}' does not exist.")
|
|
194
|
+
try:
|
|
195
|
+
part: str = resolved.relative_to(root).as_posix()
|
|
196
|
+
except ValueError:
|
|
197
|
+
raise ProjectEnvironmentError(f"'{target}' is outside the project root '{root}'.") from None
|
|
198
|
+
relative.append("" if part == "." else part)
|
|
199
|
+
return relative
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _relative_file(root: Path, path: Path) -> str:
|
|
203
|
+
"""
|
|
204
|
+
Project-relative POSIX path of a file that may not exist on disk (the `--stdin-filename` of a buffer).
|
|
205
|
+
|
|
206
|
+
Parameters
|
|
207
|
+
----------
|
|
208
|
+
root : Path
|
|
209
|
+
Project root.
|
|
210
|
+
path : Path
|
|
211
|
+
Absolute path of the file.
|
|
212
|
+
|
|
213
|
+
Returns
|
|
214
|
+
----------
|
|
215
|
+
str
|
|
216
|
+
The relative path.
|
|
217
|
+
|
|
218
|
+
Raises
|
|
219
|
+
----------
|
|
220
|
+
ProjectEnvironmentError
|
|
221
|
+
The path is outside the project.
|
|
222
|
+
"""
|
|
223
|
+
resolved: Path = path.parent.resolve() / path.name
|
|
224
|
+
try:
|
|
225
|
+
relative: str = resolved.relative_to(root).as_posix()
|
|
226
|
+
except ValueError:
|
|
227
|
+
raise ProjectEnvironmentError(f"'{path}' is outside the project root '{root}'.") from None
|
|
228
|
+
if relative in ("", "."):
|
|
229
|
+
raise ProjectEnvironmentError(f"'{path}' is not a file name.")
|
|
230
|
+
return relative
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class _Stopwatch:
|
|
234
|
+
"""
|
|
235
|
+
Measures the duration of the phases of a run.
|
|
236
|
+
|
|
237
|
+
Attributes
|
|
238
|
+
----------
|
|
239
|
+
phases : dict[str, float]
|
|
240
|
+
Duration of each finished phase, in milliseconds.
|
|
241
|
+
"""
|
|
242
|
+
|
|
243
|
+
def __init__(self) -> None:
|
|
244
|
+
"""
|
|
245
|
+
Start measuring.
|
|
246
|
+
|
|
247
|
+
Returns
|
|
248
|
+
----------
|
|
249
|
+
None
|
|
250
|
+
Nothing.
|
|
251
|
+
"""
|
|
252
|
+
self.phases: dict[str, float] = {}
|
|
253
|
+
self._last: float = time.perf_counter()
|
|
254
|
+
|
|
255
|
+
def lap(self, phase: str) -> None:
|
|
256
|
+
"""
|
|
257
|
+
End a phase.
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
phase : str
|
|
262
|
+
Name of the phase.
|
|
263
|
+
|
|
264
|
+
Returns
|
|
265
|
+
----------
|
|
266
|
+
None
|
|
267
|
+
Nothing.
|
|
268
|
+
"""
|
|
269
|
+
now: float = time.perf_counter()
|
|
270
|
+
self.phases[phase] = round((now - self._last) * 1000, 3)
|
|
271
|
+
self._last = now
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def check_project(start: Path, options: CheckOptions | None = None) -> CheckReport:
|
|
275
|
+
"""
|
|
276
|
+
Check a project end to end: find the root, create `.JCCS` if needed, load the config and the rules,
|
|
277
|
+
load every project file in memory (project context), run the active rules on the files to analyse and
|
|
278
|
+
compute the outcome.
|
|
279
|
+
|
|
280
|
+
Parameters
|
|
281
|
+
----------
|
|
282
|
+
start : Path
|
|
283
|
+
Directory inside the project.
|
|
284
|
+
options : CheckOptions | None
|
|
285
|
+
Run settings (defaults when None).
|
|
286
|
+
|
|
287
|
+
Returns
|
|
288
|
+
----------
|
|
289
|
+
CheckReport
|
|
290
|
+
The report.
|
|
291
|
+
|
|
292
|
+
Raises
|
|
293
|
+
----------
|
|
294
|
+
ProjectConfigError
|
|
295
|
+
The project config is invalid, or no rule is active.
|
|
296
|
+
RuleDefinitionError
|
|
297
|
+
A rule file is invalid.
|
|
298
|
+
UnknownSelectorError
|
|
299
|
+
A selector matches nothing.
|
|
300
|
+
ProjectEnvironmentError
|
|
301
|
+
The project cannot be read or `.JCCS` cannot be created.
|
|
302
|
+
"""
|
|
303
|
+
settings: CheckOptions = options or CheckOptions()
|
|
304
|
+
watch: _Stopwatch = _Stopwatch()
|
|
305
|
+
if not start.is_dir():
|
|
306
|
+
raise ProjectEnvironmentError(f"Project directory '{start}' does not exist.")
|
|
307
|
+
root: Path = find_project_root(start)
|
|
308
|
+
builtin: RuleCatalog = RuleCatalog.load()
|
|
309
|
+
directory: Path | None = ensure_project_directory(root, builtin.rules.values()) if settings.create_project_directory else None
|
|
310
|
+
if settings.config_file is not None:
|
|
311
|
+
config: ProjectConfig = load_project_config_file(resolve_config_argument(settings.config_file))
|
|
312
|
+
else:
|
|
313
|
+
config = load_project_config(directory)
|
|
314
|
+
custom: Path | None = config.custom_rules_directory
|
|
315
|
+
catalog: RuleCatalog = RuleCatalog.load([custom]) if custom is not None and custom.is_dir() else builtin
|
|
316
|
+
plan: ActivePlan = build_plan(catalog, config, only=settings.only, argument_overrides=settings.argument_overrides)
|
|
317
|
+
if not plan.rules and not plan.dormant:
|
|
318
|
+
raise ProjectConfigError("No active rule.", "Check the [rules] selection of the config and --only.", path=config.source)
|
|
319
|
+
watch.lap("configure")
|
|
320
|
+
exclude: tuple[str, ...] = config.exclude + settings.exclude
|
|
321
|
+
include: tuple[str, ...] = config.include + settings.include
|
|
322
|
+
paths: list[str] = discover_files(root, exclude=exclude, use_gitignore=config.use_gitignore)
|
|
323
|
+
loaded: LoadedFiles = load_files(root, paths, max_file_size=config.max_file_size)
|
|
324
|
+
contents: dict[str, bytes] = dict(loaded.contents)
|
|
325
|
+
buffered: str | None = None
|
|
326
|
+
if settings.buffer is not None:
|
|
327
|
+
buffered = _relative_file(root, settings.buffer[0])
|
|
328
|
+
contents[buffered] = settings.buffer[1]
|
|
329
|
+
watch.lap("discover")
|
|
330
|
+
project: ProjectContext = build_project_context(contents, root=str(root), skipped={path: reason for path, reason in loaded.skipped.items() if path != buffered})
|
|
331
|
+
watch.lap("analyze")
|
|
332
|
+
prefixes: list[str] = _relative_targets(root, settings.targets)
|
|
333
|
+
allowed: set[str] | None = None
|
|
334
|
+
if settings.restrict_to is not None:
|
|
335
|
+
allowed = set()
|
|
336
|
+
for item in settings.restrict_to:
|
|
337
|
+
try:
|
|
338
|
+
allowed.add(item.resolve().relative_to(root).as_posix())
|
|
339
|
+
except ValueError:
|
|
340
|
+
continue
|
|
341
|
+
analysed: list[str] = [
|
|
342
|
+
path for path in project.files
|
|
343
|
+
if (not prefixes or any(not prefix or path == prefix or path.startswith(prefix + "/") for prefix in prefixes))
|
|
344
|
+
and (not include or match_any_glob(path, include))
|
|
345
|
+
and (allowed is None or path in allowed)
|
|
346
|
+
]
|
|
347
|
+
if buffered is not None:
|
|
348
|
+
analysed = [buffered]
|
|
349
|
+
result: RunResult = run_plan(plan, project, analysed)
|
|
350
|
+
watch.lap("rules")
|
|
351
|
+
fail_on: SeverityLevel = settings.fail_on or config.fail_on
|
|
352
|
+
max_errors: int = config.max_errors if settings.max_errors is None else settings.max_errors
|
|
353
|
+
strict: bool = config.strict_heuristics if settings.strict_heuristics is None else settings.strict_heuristics
|
|
354
|
+
outcome: Outcome = describe_outcome(result.diagnostics, fail_on=fail_on, max_errors=max_errors, strict_heuristics=strict)
|
|
355
|
+
return CheckReport(root, config, catalog, plan, project, result, outcome, fail_on, max_errors, strict, MappingProxyType(watch.phases))
|