Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): keep the Java package line out of the clone token stream (#2948)

Yang Bai5bcc872ad04b
diff --git a/packages/core/src/repowise/core/analysis/health/duplication/tokenizer.py b/packages/core/src/repowise/core/analysis/health/duplication/tokenizer.py
index 90bbd492e..efff679a5 100644
--- a/packages/core/src/repowise/core/analysis/health/duplication/tokenizer.py
+++ b/packages/core/src/repowise/core/analysis/health/duplication/tokenizer.py
@@ -93,6 +93,15 @@ _LITERAL_KINDS = frozenset(
_NOT_IMPORT_KINDS = frozenset({"call", "command", "function_call", "extends_statement"})
+# Statements that are not imports to the resolvers but tokenize the same in
+# every file, so they are kept out of the hash stream here and not added to the
+# shared language config. Java: every file of a package opens with the same
+# ``package a.b;`` tokens, which anchored a clone at line 2.
+_SKIPPED_WITH_IMPORTS: dict[str, frozenset[str]] = {
+ "java": frozenset({"package_declaration"}),
+}
+
+
@dataclass(frozen=True, slots=True)
class Token:
"""One AST token with the source location of its origin node."""
@@ -106,13 +115,15 @@ class Token:
def import_node_kinds(language: str) -> frozenset[str]:
- """The language's import statement node kinds, minus ones that are real code."""
+ """The language's import statement node kinds, minus ones that are real
+ code, plus the statements skipped along with them."""
from repowise.core.ingestion.language_configs import LANGUAGE_CONFIGS
config = LANGUAGE_CONFIGS.get(language)
if config is None:
return frozenset()
- return frozenset(config.import_node_types) - _NOT_IMPORT_KINDS
+ kinds = frozenset(config.import_node_types) - _NOT_IMPORT_KINDS
+ return kinds | _SKIPPED_WITH_IMPORTS.get(language, frozenset())
_NEWLINE = re.compile(rb"\n")
diff --git a/tests/unit/health/test_duplication.py b/tests/unit/health/test_duplication.py
index 85ca40482..d57e0109f 100644
--- a/tests/unit/health/test_duplication.py
+++ b/tests/unit/health/test_duplication.py
@@ -403,6 +403,13 @@ def test_tokenize_file_drops_python_imports():
assert [t.kind for t in toks] == ["ID", "=", "LIT"]
+def test_tokenize_file_drops_the_java_package_declaration():
+ toks = tokenize_file("java", b"package a.b;\nimport x.Y;\nclass C {}\n", "C.java")
+ assert "package" not in [t.kind for t in toks]
+ assert toks[0].kind == "class"
+ assert toks[0].start_line == 3
+
+
def test_tokenize_file_drops_ts_imports_and_reexports_but_keeps_local_exports():
source = (
b'import { a, b } from "./x";\n'
@@ -454,6 +461,28 @@ def test_detect_clones_ignores_identical_import_blocks(tmp_path: Path):
assert report.pairs == []
+def test_a_java_clone_starts_at_the_shared_code_not_the_package_line(tmp_path: Path):
+ header = "/* License header */\npackage com.acme.core.util;\n\n" + "".join(
+ f"import com.acme.core.mod{i}.Thing{i};\n" for i in range(12)
+ ) + "\n"
+ method = (
+ " int total(int a, int b) {\n int sum = a + b;\n if (sum > 10) {\n"
+ " return sum * 2;\n }\n for (int i = 0; i < b; i++) {\n"
+ " sum += i;\n }\n return sum;\n }\n"
+ )
+ a = _write(tmp_path, "A.java", header + "public class Alpha {\n" + method + "}\n")
+ b = _write(tmp_path, "B.java", header + "public class Beta {\n" + method + "}\n")
+ report = detect_clones(
+ [_pf("A.java", str(a), language="java"), _pf("B.java", str(b), language="java")]
+ )
+ # The pair is still found, from the class line (17) on: the package line
+ # is line 2 and the shared method is lines 18-27.
+ assert len(report.pairs) == 1
+ pair = report.pairs[0]
+ assert pair.a_start_line >= 17 and pair.b_start_line >= 17
+ assert pair.a_end_line >= 27 and pair.b_end_line >= 27
+
+
def test_detect_clones_ignores_matching_data_literals(tmp_path: Path):
table = "TABLE = [\n" + "".join(f" ({i}, '{i}', {i}.5),\n" for i in range(20)) + "]\n"
a = _write(tmp_path, "a.py", table)