Skip to content

Commit 94bf5f8

Browse files
committed
Expose hasOnlyExpressionInitializer as a public type guard
Exposes `hasOnlyExpressionInitializer` as a public function so users of TypeScript compiler APIs do not have to roll their own `HasExpressionInitializer` type guards.
1 parent f41472b commit 94bf5f8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/compiler/utilities.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7054,9 +7054,19 @@ namespace ts {
70547054
}
70557055

70567056
/** True if has initializer node attached to it. */
7057-
/* @internal */
70587057
export function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer {
7059-
return hasInitializer(node) && !isForStatement(node) && !isForInStatement(node) && !isForOfStatement(node) && !isJsxAttribute(node);
7058+
switch (node.kind) {
7059+
case SyntaxKind.VariableDeclaration:
7060+
case SyntaxKind.Parameter:
7061+
case SyntaxKind.BindingElement:
7062+
case SyntaxKind.PropertySignature:
7063+
case SyntaxKind.PropertyDeclaration:
7064+
case SyntaxKind.PropertyAssignment:
7065+
case SyntaxKind.EnumMember:
7066+
return true;
7067+
default:
7068+
return false;
7069+
}
70607070
}
70617071

70627072
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
@@ -3612,6 +3612,8 @@ declare namespace ts {
36123612
function isJSDocCommentContainingNode(node: Node): boolean;
36133613
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
36143614
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
3615+
/** True if has initializer node attached to it. */
3616+
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
36153617
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
36163618
function isStringLiteralLike(node: Node): node is StringLiteralLike;
36173619
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,8 @@ declare namespace ts {
36123612
function isJSDocCommentContainingNode(node: Node): boolean;
36133613
function isSetAccessor(node: Node): node is SetAccessorDeclaration;
36143614
function isGetAccessor(node: Node): node is GetAccessorDeclaration;
3615+
/** True if has initializer node attached to it. */
3616+
function hasOnlyExpressionInitializer(node: Node): node is HasExpressionInitializer;
36153617
function isObjectLiteralElement(node: Node): node is ObjectLiteralElement;
36163618
function isStringLiteralLike(node: Node): node is StringLiteralLike;
36173619
}

0 commit comments

Comments
 (0)