Read JavaScript as TypeScript without the types #1

Merged
bj merged 1 commits from javascript into master 2026-11-15 18:28:52 +00:00
8 changed files with 93 additions and 9 deletions
Showing only changes of commit f48bd0c60f - Show all commits
+2 -1
View File
@@ -1,6 +1,6 @@
# dryer # dryer
dryer finds candidate duplicate code in Clojure, Java, Go, TypeScript, Rust, and Python. One run detects the language of each source file, normalizes that language's forms, and reports pairs whose structure is close enough to review. dryer finds candidate duplicate code in Clojure, Java, Go, TypeScript, JavaScript, Rust, and Python. One run detects the language of each source file, normalizes that language's forms, and reports pairs whose structure is close enough to review.
Clojure follows [dry4clj](https://github.com/unclebob/dry4clj). The other languages use the same idea on the functions and methods [crapper](https://github.com/unclebob/crapper) and [mutator](https://github.com/unclebob/mutator) already treat as one unit. A form is only compared with forms written in the same language. Clojure follows [dry4clj](https://github.com/unclebob/dry4clj). The other languages use the same idea on the functions and methods [crapper](https://github.com/unclebob/crapper) and [mutator](https://github.com/unclebob/mutator) already treat as one unit. A form is only compared with forms written in the same language.
@@ -99,6 +99,7 @@ DUPLICATE score=0.89
| Java | methods with a body | constructors, abstract methods, methods inside anonymous or local classes | | Java | methods with a body | constructors, abstract methods, methods inside anonymous or local classes |
| Go | functions and methods with a body | function literals stay inside the enclosing function | | Go | functions and methods with a body | function literals stay inside the enclosing function |
| TypeScript | functions, methods, and top-level arrow functions | callbacks nested inside another function | | TypeScript | functions, methods, and top-level arrow functions | callbacks nested inside another function |
| JavaScript | functions, methods, and top-level arrow functions | callbacks nested inside another function |
| Rust | functions and methods with a body | functions inside `mod tests`, and closures | | Rust | functions and methods with a body | functions inside `mod tests`, and closures |
| Python | functions and methods | functions nested inside another function | | Python | functions and methods | functions nested inside another function |
+1 -1
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "dryer" name = "dryer"
version = "0.1.0" version = "0.1.0"
description = "Structural duplicate finder for Clojure, Java, Go, TypeScript, Rust, and Python." description = "Structural duplicate finder for Clojure, Java, Go, TypeScript, JavaScript, Rust, and Python."
readme = "README.md" readme = "README.md"
license = { file = "LICENSE" } license = { file = "LICENSE" }
classifiers = ["License :: OSI Approved :: Zero-Clause BSD (0BSD)"] classifiers = ["License :: OSI Approved :: Zero-Clause BSD (0BSD)"]
+1 -1
View File
@@ -1,3 +1,3 @@
"""Find structural duplicates in Clojure, Java, Go, TypeScript, Rust, and Python.""" """Find structural duplicates in Clojure, Java, Go, TypeScript, JavaScript, Rust, and Python."""
__version__ = "0.1.0" __version__ = "0.1.0"
+4 -3
View File
@@ -21,8 +21,8 @@ def _skipped_directory_text() -> str:
HELP = f"""\ HELP = f"""\
Usage: dryer [options] [path-or-filter ...] Usage: dryer [options] [path-or-filter ...]
Find candidate duplicate code in Clojure, Java, Go, TypeScript, Rust, and Find candidate duplicate code in Clojure, Java, Go, TypeScript, JavaScript,
Python. Each form is normalized — call and method names, operators, and Rust, and Python. Each form is normalized — call and method names, operators, and
tree shape stay; local names, field names, and literals do not — and compared tree shape stay; local names, field names, and literals do not — and compared
with the other forms of the same language. The score is Jaccard similarity of with the other forms of the same language. The score is Jaccard similarity of
those structural fingerprints, the same score as dry4clj. those structural fingerprints, the same score as dry4clj.
@@ -30,7 +30,8 @@ those structural fingerprints, the same score as dry4clj.
Prints a report and writes .metrics/dry.edn. Prints a report and writes .metrics/dry.edn.
Languages: Clojure (.clj .cljc .cljs .cljd .bb), Java (.java), Go (.go), Languages: Clojure (.clj .cljc .cljs .cljd .bb), Java (.java), Go (.go),
TypeScript (.ts .tsx .mts .cts), Rust (.rs), Python (.py). TypeScript (.ts .tsx .mts .cts), JavaScript (.js .jsx .mjs .cjs),
Rust (.rs), Python (.py).
Options: Options:
-h, --help Print this help and exit. -h, --help Print this help and exit.
+12
View File
@@ -19,6 +19,10 @@ EXTENSIONS = {
".tsx": "typescript", ".tsx": "typescript",
".mts": "typescript", ".mts": "typescript",
".cts": "typescript", ".cts": "typescript",
".js": "javascript",
".jsx": "javascript",
".mjs": "javascript",
".cjs": "javascript",
".rs": "rust", ".rs": "rust",
".py": "python", ".py": "python",
} }
@@ -71,6 +75,14 @@ def is_test_file(path: str | Path) -> bool:
".spec.mts", ".spec.mts",
".test.cts", ".test.cts",
".spec.cts", ".spec.cts",
".test.js",
".spec.js",
".test.jsx",
".spec.jsx",
".test.mjs",
".spec.mjs",
".test.cjs",
".spec.cjs",
) )
): ):
return True return True
+1
View File
@@ -148,6 +148,7 @@ _PICKERS = {
"go": ("go", _go_nodes), "go": ("go", _go_nodes),
"python": ("python", _python_nodes), "python": ("python", _python_nodes),
"typescript": (_grammar, _typescript_nodes), "typescript": (_grammar, _typescript_nodes),
"javascript": ("javascript", _typescript_nodes),
"rust": ("rust", _rust_nodes), "rust": ("rust", _rust_nodes),
} }
+11
View File
@@ -28,6 +28,8 @@ def test_language_detection():
assert language_of("Widget.java") == "java" assert language_of("Widget.java") == "java"
assert language_of("board.go") == "go" assert language_of("board.go") == "go"
assert language_of("ui/view.tsx") == "typescript" assert language_of("ui/view.tsx") == "typescript"
for name in ("web/app.js", "web/view.jsx", "web/mod.mjs", "web/lib.cjs"):
assert language_of(name) == "javascript", name
assert language_of("src/lib.rs") == "rust" assert language_of("src/lib.rs") == "rust"
assert language_of("src/dryer/cli.py") == "python" assert language_of("src/dryer/cli.py") == "python"
assert language_of("types.d.ts") is None assert language_of("types.d.ts") is None
@@ -167,6 +169,14 @@ def test_names_that_are_tests():
"ui/a.spec.mts", "ui/a.spec.mts",
"ui/a.test.cts", "ui/a.test.cts",
"ui/a.spec.cts", "ui/a.spec.cts",
"web/a.test.js",
"web/a.spec.js",
"web/a.test.jsx",
"web/a.spec.jsx",
"web/a.test.mjs",
"web/a.spec.mjs",
"web/a.test.cjs",
"web/a.spec.cjs",
"tests/conftest.py", "tests/conftest.py",
"pkg/foo_test.py", "pkg/foo_test.py",
"pkg/test_foo.py", "pkg/test_foo.py",
@@ -175,6 +185,7 @@ def test_names_that_are_tests():
for name in names: for name in names:
assert is_test_file(name), name assert is_test_file(name), name
assert not is_test_file("src/app.py") assert not is_test_file("src/app.py")
assert not is_test_file("web/app.js")
def _init_repo(path: Path) -> None: def _init_repo(path: Path) -> None:
+61 -3
View File
@@ -97,6 +97,20 @@ export const beta = (items: number[]): number[] => {
}; };
""" """
JS_LEFT = """\
export function alpha(xs) {
const ys = filter(xs, odd);
return map(ys, inc);
}
"""
JS_RIGHT = """\
export function beta(items) {
const kept = filter(items, even);
return map(kept, dec);
}
"""
RUST_LEFT = """\ RUST_LEFT = """\
pub fn alpha(xs: Vec<i32>) -> Vec<i32> { pub fn alpha(xs: Vec<i32>) -> Vec<i32> {
let ys = filter(xs, odd); let ys = filter(xs, odd);
@@ -124,6 +138,8 @@ def test_each_language_matches_renamed_locals(tmp_path):
"right.ts": TS_RIGHT, "right.ts": TS_RIGHT,
"left.tsx": TSX_LEFT, "left.tsx": TSX_LEFT,
"right.tsx": TSX_RIGHT, "right.tsx": TSX_RIGHT,
"left.js": JS_LEFT,
"right.js": JS_RIGHT,
"left.rs": RUST_LEFT, "left.rs": RUST_LEFT,
"right.rs": RUST_RIGHT, "right.rs": RUST_RIGHT,
} }
@@ -133,7 +149,10 @@ def test_each_language_matches_renamed_locals(tmp_path):
by_language = {} by_language = {}
for item in found: for item in found:
by_language.setdefault(item.language, []).append(item) by_language.setdefault(item.language, []).append(item)
assert set(by_language) == {"java", "go", "python", "typescript", "rust"} assert set(by_language) == {"java", "go", "python", "typescript", "javascript", "rust"}
assert [(item.left.file, item.right.file) for item in by_language["javascript"]] == [
("left.js", "right.js"),
]
assert [(item.left.file, item.right.file) for item in by_language["typescript"]] == [ assert [(item.left.file, item.right.file) for item in by_language["typescript"]] == [
("left.ts", "right.ts"), ("left.ts", "right.ts"),
("left.tsx", "right.tsx"), ("left.tsx", "right.tsx"),
@@ -237,7 +256,14 @@ class Right {
def _suffix(language: str) -> str: def _suffix(language: str) -> str:
return {"python": "py", "go": "go", "rust": "rs", "java": "java", "typescript": "ts"}[language] return {
"python": "py",
"go": "go",
"rust": "rs",
"java": "java",
"typescript": "ts",
"javascript": "js",
}[language]
def test_callee_names_stay_and_member_names_do_not(tmp_path): def test_callee_names_stay_and_member_names_do_not(tmp_path):
@@ -307,6 +333,16 @@ export function alpha(xs: number[]): number {
export function beta(items: number[]): number { export function beta(items: number[]): number {
return foo<number>(items.baz); return foo<number>(items.baz);
} }
""",
"left.js": """\
export function alpha(xs) {
return foo(xs.bar);
}
""",
"right.js": """\
export function beta(items) {
return foo(items.baz);
}
""", """,
} }
for name, source in samples.items(): for name, source in samples.items():
@@ -315,7 +351,7 @@ export function beta(items: number[]): number {
by_language = {} by_language = {}
for item in found: for item in found:
by_language.setdefault(item.language, []).append(item) by_language.setdefault(item.language, []).append(item)
assert set(by_language) == {"python", "go", "rust", "java", "typescript"} assert set(by_language) == {"python", "go", "rust", "java", "typescript", "javascript"}
for language, group in by_language.items(): for language, group in by_language.items():
assert [(item.left.file, item.right.file, item.score) for item in group] == [ assert [(item.left.file, item.right.file, item.score) for item in group] == [
(f"left.{_suffix(language)}", f"right.{_suffix(language)}", 1.0) (f"left.{_suffix(language)}", f"right.{_suffix(language)}", 1.0)
@@ -522,6 +558,28 @@ xs.map((item: number) => item + 1);
) == [(2, 4), (6, 12)] ) == [(2, 4), (6, 12)]
def test_javascript_keeps_methods_and_skips_nested_functions():
assert spans(
"javascript",
"""\
export class Box {
run(xs) {
return xs;
}
}
export function outer(xs) {
function inner(ys) {
return ys;
}
const cb = (zs) => zs;
return inner(xs);
}
xs.map((item) => item + 1);
""",
"a.js",
) == [(2, 4), (6, 12)]
def test_a_private_field_name_is_not_part_of_the_structure(): def test_a_private_field_name_is_not_part_of_the_structure():
entries = forms( entries = forms(
"typescript", "typescript",