Skip to content

Commit 9d1bb52

Browse files
committed
get childNodeRange of empty parameters
1 parent aba3baf commit 9d1bb52

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/adapter/typescript.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ class TypescriptAdapter implements Adapter<Node> {
7272
childNodeRange(node: Node, childName: string): { start: number, end: number } {
7373
if (["arguments", "parameters"].includes(childName)) {
7474
const elements = (node as any)[childName];
75-
return { start: this.getStart(elements[0] as Node) - 1, end: this.getEnd(elements[elements.length - 1] as Node) + 1 };
75+
if (Array.isArray(elements) && elements.hasOwnProperty('0')) {
76+
return { start: this.getStart(elements[0] as Node) - 1, end: this.getEnd(elements[elements.length - 1] as Node) + 1 };
77+
} else {
78+
return { start: elements['pos'] - 1, end: elements['pos'] + 1 };
79+
}
7680
} else if (node.kind === SyntaxKind.PropertyAssignment && childName === "semicolon") {
7781
return { start: this.getEnd((node as PropertyAssignment).name), end: this.getEnd((node as PropertyAssignment).name) + 1 };
7882
} else if (node.kind === SyntaxKind.PropertyAccessExpression && childName === "dot") {

test/adapter/typescript.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ describe("TypescriptAdapter", () => {
100100
});
101101

102102
describe("#childNodeRange", () => {
103+
test("ArrowFunction parameters", () => {
104+
const node = (parseCode("const foobar = () => {}") as any)['declarationList']['declarations'][0]['initializer'];
105+
expect(adapter.childNodeRange(node, "parameters")).toEqual({ start: 15, end: 17 });
106+
});
107+
103108
test("FunctionDeclaration parameters", () => {
104109
const node = parseCode("function foobar(foo, bar) {}");
105110
expect(adapter.childNodeRange(node, "parameters")).toEqual({ start: 15, end: 25 });

0 commit comments

Comments
 (0)