Skip to content

Commit

Permalink
feat: support x === undefined & x !== undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jun 4, 2019
1 parent 457236f commit f5d71ae
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/features/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
EmitHint,
isCallExpression,
isPropertyAccessExpression,
isIdentifier
isIdentifier,
isBinaryExpression,
SyntaxKind,
createCall,
createIdentifier
} from 'typescript';

import method from '../utilities/method';
Expand Down Expand Up @@ -59,6 +63,23 @@ export default {
}
}

if (
isBinaryExpression(node)
&& node.operatorToken.kind === SyntaxKind.EqualsEqualsEqualsToken
&& node.right.originalKeywordKind === SyntaxKind.UndefinedKeyword
) {
helpers.writePunctuation('!');
return helpers.emitExpression(createCall(createIdentifier('isset'), [], [node.left]));
}

if (
isBinaryExpression(node)
&& node.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken
&& node.right.originalKeywordKind === SyntaxKind.UndefinedKeyword
) {
return helpers.emitExpression(createCall(createIdentifier('isset'), [], [node.left]));
}

return false;
}
};
2 changes: 1 addition & 1 deletion src/utilities/nodeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ export function isVisibilityModifier(node: ts.Modifier) {

export function isSupportedPropertyModifier(node: ts.Modifier) {
return isVisibilityModifier(node) || node.kind === SyntaxKind.StaticKeyword;
}
}
9 changes: 9 additions & 0 deletions test/features/isset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace test\case_isset;
$a = 1;
if (!isset($a)) {
echo "a";
}
if (isset($a)) {
echo "a";
}
9 changes: 9 additions & 0 deletions test/features/isset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const a = 1;

if (a === undefined) {
console.log('a');
}

if (a !== undefined) {
console.log('a');
}
1 change: 1 addition & 0 deletions typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ declare namespace ts {
type StringLiteralLike = StringLiteral | NoSubstitutionTemplateLiteral;
interface Expression extends Node {
_expressionBrand: any;
originalKeywordKind?: SyntaxKind
}
interface OmittedExpression extends Expression {
kind: SyntaxKind.OmittedExpression;
Expand Down

0 comments on commit f5d71ae

Please sign in to comment.