Skip to content

Commit

Permalink
Check typeof(x) === function in safecalls
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Sep 24, 2017
1 parent c39b0d1 commit f2c3328
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 11 deletions.
14 changes: 10 additions & 4 deletions src/safe.lsc
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ replaceOptionals(path, replacementPath, loose = true): void ->

replacementPath.replaceWith(
t.conditionalExpression(
t.binaryExpression("==", check, t.nullLiteral()),
nil,
replacementPath.node,
),
if isCall:
t.binaryExpression("!==",
t.unaryExpression("typeof", check),
t.stringLiteral("function")
)
else:
t.binaryExpression("==", check, t.nullLiteral())
nil
replacementPath.node
)
)

now replacementPath = replacementPath.get("alternate")
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/bang-calls/safe-tilde/expected.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
typeof b === "function" ? b(a, c) : void 0;
typeof b !== "function" ? void 0 : b(a, c);
2 changes: 1 addition & 1 deletion test/fixtures/bang-calls/safe/expected.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
typeof a === "function" ? a(b, c) : void 0;
typeof a !== "function" ? void 0 : a(b, c);
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
// we do
'use @oigroup/lightscript with safeCall';

typeof what === 'function' ? what() : void 0;
typeof what !== 'function' ? void 0 : what();
2 changes: 1 addition & 1 deletion test/fixtures/safe-call-expression/basic/expected.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
typeof a === "function" ? a(b) : void 0;
typeof a !== "function" ? void 0 : a(b);
4 changes: 2 additions & 2 deletions test/fixtures/safe-call-expression/this/expected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
typeof obj.method === "function" ? obj.method() : void 0;
typeof obj.method !== "function" ? void 0 : obj.method();

typeof this.method === "function" ? this.method() : void 0;
typeof this.method !== "function" ? void 0 : this.method();
2 changes: 1 addition & 1 deletion test/fixtures/safe-call-expression/tilde/expected.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
typeof b === "function" ? b(a, c) : void 0;
typeof b !== "function" ? void 0 : b(a, c);

0 comments on commit f2c3328

Please sign in to comment.