Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/parser-native/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,25 @@ function transformCallExpression(node: TreeSitterNode): Expression {
: { type: "variable" as const, name: "undefined" };
const method = propNode ? (propNode as NodeBase).text : "";

let isOptional = false;
if (objNode) {
const objEnd = (objNode as NodeBase).endIndex;
const propStart = propNode ? (propNode as NodeBase).startIndex : fn.endIndex;
const operatorText = fn.source.substring(objEnd, propStart);
if (operatorText.indexOf("?.") !== -1) {
isOptional = true;
}
}

return {
type: "method_call",
object: object,
method: method,
args: args,
typeParameter: typeParameter,
pos: 0,
loc: undefined,
optional: isOptional || undefined,
};
} else if (fn.type === "identifier") {
let callTypeArgs: string[] | undefined;
Expand All @@ -1002,6 +1014,8 @@ function transformCallExpression(node: TreeSitterNode): Expression {
args,
typeParameter: undefined,
pos: 0,
loc: undefined,
optional: undefined,
};
}
}
Expand Down Expand Up @@ -2107,6 +2121,8 @@ function transformForInStatement(node: TreeSitterNode): ForOfStatement {
args: [iterable],
typeParameter: undefined,
pos: 0,
loc: undefined,
optional: undefined,
};
}

Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/classes/optional-method-call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Helper {
process(s: string): string {
return s + "_processed";
}
}

class Container {
helper: Helper | null = null;

setHelper(h: Helper): void {
this.helper = h;
}

doWork(s: string): string {
const result = this.helper?.process(s);
if (result) return result;
return "no_helper";
}
}

const c = new Container();
if (c.doWork("test") !== "no_helper") {
console.log("FAIL: expected no_helper");
process.exit(1);
}
c.setHelper(new Helper());
if (c.doWork("test") !== "test_processed") {
console.log("FAIL: expected test_processed");
process.exit(1);
}
console.log("TEST_PASSED");
Loading