Skip to content

Commit 0944110

Browse files
ayazhafizsandersn
andauthored
Expose hasOnlyExpressionInitializer as a public type guard (#33229)
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]>
1 parent 044355b commit 0944110

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/compiler/utilitiesPublic.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,9 +2507,19 @@ namespace ts {
25072507
}
25082508

25092509
/** True if has initializer node attached to it. */
2510-
/* @internal */
25112510
export function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer {
2512-
return hasInitializer(node) && !isForStatement(node) && !isForInStatement(node) && !isForOfStatement(node) && !isJsxAttribute(node);
2511+
switch (node.kind) {
2512+
case SyntaxKind.VariableDeclaration:
2513+
case SyntaxKind.Parameter:
2514+
case SyntaxKind.BindingElement:
2515+
case SyntaxKind.PropertySignature:
2516+
case SyntaxKind.PropertyDeclaration:
2517+
case SyntaxKind.PropertyAssignment:
2518+
case SyntaxKind.EnumMember:
2519+
return true;
2520+
default:
2521+
return false;
2522+
}
25132523
}
25142524

25152525
export function isObjectLiteralElement(node: Node): node is ObjectLiteralElement {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,8 @@ declare namespace ts {
37873787
function isJSDocCommentContainingNode(node: Node): boolean;
37883788
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
37893789
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
3790+
/** True if has initializer node attached to it. */
3791+
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
37903792
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
37913793
function isStringLiteralLike(node: Node): node is StringLiteralLike;
37923794
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,8 @@ declare namespace ts {
37873787
function isJSDocCommentContainingNode(node: Node): boolean;
37883788
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
37893789
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
3790+
/** True if has initializer node attached to it. */
3791+
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
37903792
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
37913793
function isStringLiteralLike(node: Node): node is StringLiteralLike;
37923794
}

0 commit comments

Comments
 (0)