Read JavaScript as TypeScript without the types
Why: gtd-inbox-pull's Plan 6 runs the dryer over the swipe page's JavaScript, and the dryer skipped .js files. This is for BJ's mirror only. What changed: .js, .jsx, .mjs and .cjs are discovered as "javascript"; their .test and .spec forms are test files. extract.py gains a "javascript" picker that parses with the JavaScript grammar and reuses _typescript_nodes, since the tree is TypeScript's without type nodes. No other code is keyed on the language name. README, pyproject, the CLI help and the package docstring name JavaScript. Proof: four tests written first and seen to fail on their own assertions (language detection, test-file names, the language-set tests with a JS pair, and the method and nested-function rule), then the full suite passed (81 tests). Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015GF1LYr5V3zY3815Nt6KwR
This commit is contained in:
@@ -28,6 +28,8 @@ def test_language_detection():
|
||||
assert language_of("Widget.java") == "java"
|
||||
assert language_of("board.go") == "go"
|
||||
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/dryer/cli.py") == "python"
|
||||
assert language_of("types.d.ts") is None
|
||||
@@ -167,6 +169,14 @@ def test_names_that_are_tests():
|
||||
"ui/a.spec.mts",
|
||||
"ui/a.test.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",
|
||||
"pkg/foo_test.py",
|
||||
"pkg/test_foo.py",
|
||||
@@ -175,6 +185,7 @@ def test_names_that_are_tests():
|
||||
for name in names:
|
||||
assert is_test_file(name), name
|
||||
assert not is_test_file("src/app.py")
|
||||
assert not is_test_file("web/app.js")
|
||||
|
||||
|
||||
def _init_repo(path: Path) -> None:
|
||||
|
||||
+61
-3
@@ -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 = """\
|
||||
pub fn alpha(xs: Vec<i32>) -> Vec<i32> {
|
||||
let ys = filter(xs, odd);
|
||||
@@ -124,6 +138,8 @@ def test_each_language_matches_renamed_locals(tmp_path):
|
||||
"right.ts": TS_RIGHT,
|
||||
"left.tsx": TSX_LEFT,
|
||||
"right.tsx": TSX_RIGHT,
|
||||
"left.js": JS_LEFT,
|
||||
"right.js": JS_RIGHT,
|
||||
"left.rs": RUST_LEFT,
|
||||
"right.rs": RUST_RIGHT,
|
||||
}
|
||||
@@ -133,7 +149,10 @@ def test_each_language_matches_renamed_locals(tmp_path):
|
||||
by_language = {}
|
||||
for item in found:
|
||||
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"]] == [
|
||||
("left.ts", "right.ts"),
|
||||
("left.tsx", "right.tsx"),
|
||||
@@ -237,7 +256,14 @@ class Right {
|
||||
|
||||
|
||||
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):
|
||||
@@ -307,6 +333,16 @@ export function alpha(xs: number[]): number {
|
||||
export function beta(items: number[]): number {
|
||||
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():
|
||||
@@ -315,7 +351,7 @@ export function beta(items: number[]): number {
|
||||
by_language = {}
|
||||
for item in found:
|
||||
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():
|
||||
assert [(item.left.file, item.right.file, item.score) for item in group] == [
|
||||
(f"left.{_suffix(language)}", f"right.{_suffix(language)}", 1.0)
|
||||
@@ -522,6 +558,28 @@ xs.map((item: number) => item + 1);
|
||||
) == [(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():
|
||||
entries = forms(
|
||||
"typescript",
|
||||
|
||||
Reference in New Issue
Block a user