Skip to content

Commit

Permalink
fix: in operator close #26
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Apr 29, 2019
1 parent 246a840 commit c031867
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/features/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ import {
EmitHint,
isPropertyAccessExpression,
isIdentifier,
isCallExpression
isCallExpression,
isBinaryExpression,
SyntaxKind,
isStringLiteral,
TypeFlags,
createCall,
createIdentifier
} from 'typescript';

import method from '../utilities/method';
import {
isClassLike,
isClassInstance
} from '../utilities/nodeTest';

const staticMap = {
assign: method('array_merge', false),
Expand All @@ -23,7 +33,7 @@ const staticMap = {

export default {

emit(hint, node, {helpers}) {
emit(hint, node, {helpers, typeChecker}) {

const expNode = node.expression;
let func;
Expand All @@ -39,6 +49,36 @@ export default {
return func(node, helpers);
}

if (
hint === EmitHint.Expression
&& isBinaryExpression(node)
&& node.operatorToken.kind === SyntaxKind.InKeyword
) {
if (isClassInstance(node.right, typeChecker)) {
return helpers.emitExpression(
createCall(
createIdentifier('property_exists'),
[],
[
node.right,
node.left
]
)
);
}
return helpers.emitExpression(
createCall(
createIdentifier('array_key_exists'),
[],
[
node.left,
node.right
]
)
);
}


return false;
}
};
1 change: 1 addition & 0 deletions test/features/ObjectLiteralExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
"b" => $b,
"a-b" => $b
);
array_key_exists("b", $a);
2 changes: 2 additions & 0 deletions test/features/ObjectLiteralExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ const a = {
b,
'a-b': b
};

const c = 'b' in a;

0 comments on commit c031867

Please sign in to comment.