Skip to content

Commit

Permalink
Expose hasOnlyExpressionInitializer as a public type guard (#33229)
Browse files Browse the repository at this point in the history
Exposes `hasOnlyExpressionInitializer` as a public function so users of
TypeScript compiler APIs do not have to roll their own
`HasExpressionInitializer` type guards.

Co-authored-by: Nathan Shively-Sanders <[email protected]>
  • Loading branch information
ayazhafiz and sandersn authored Feb 5, 2020
1 parent 044355b commit 0944110
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2507,9 +2507,19 @@ namespace ts {
}

/** True if has initializer node attached to it. */
/* @internal */
export function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer {
return hasInitializer(node) && !isForStatement(node) && !isForInStatement(node) && !isForOfStatement(node) && !isJsxAttribute(node);
switch (node.kind) {
case SyntaxKind.VariableDeclaration:
case SyntaxKind.Parameter:
case SyntaxKind.BindingElement:
case SyntaxKind.PropertySignature:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.EnumMember:
return true;
default:
return false;
}
}

export function isObjectLiteralElement(node: Node): node is ObjectLiteralElement {
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,8 @@ declare namespace ts {
function isJSDocCommentContainingNode(node: Node): boolean;
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
/** True if has initializer node attached to it. */
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
function isStringLiteralLike(node: Node): node is StringLiteralLike;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,8 @@ declare namespace ts {
function isJSDocCommentContainingNode(node: Node): boolean;
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
/** True if has initializer node attached to it. */
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
function isStringLiteralLike(node: Node): node is StringLiteralLike;
}
Expand Down

0 comments on commit 0944110

Please sign in to comment.