Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more Node tests for use in public API #52284

Merged
merged 11 commits into from
Jan 20, 2023
7 changes: 0 additions & 7 deletions src/compiler/factory/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,27 +292,22 @@ export function isAsteriskToken(node: Node): node is AsteriskToken {
return node.kind === SyntaxKind.AsteriskToken;
}

/** @internal */
rbuckton marked this conversation as resolved.
Show resolved Hide resolved
export function isExclamationToken(node: Node): node is ExclamationToken {
return node.kind === SyntaxKind.ExclamationToken;
}

/** @internal */
export function isQuestionToken(node: Node): node is QuestionToken {
return node.kind === SyntaxKind.QuestionToken;
}

/** @internal */
export function isColonToken(node: Node): node is ColonToken {
return node.kind === SyntaxKind.ColonToken;
}

/** @internal */
export function isQuestionDotToken(node: Node): node is QuestionDotToken {
return node.kind === SyntaxKind.QuestionDotToken;
}

/** @internal */
export function isEqualsGreaterThanToken(node: Node): node is EqualsGreaterThanToken {
return node.kind === SyntaxKind.EqualsGreaterThanToken;
}
Expand Down Expand Up @@ -344,12 +339,10 @@ export function isAsyncModifier(node: Node): node is AsyncKeyword {
return node.kind === SyntaxKind.AsyncKeyword;
}

/** @internal */
export function isAssertsKeyword(node: Node): node is AssertsKeyword {
return node.kind === SyntaxKind.AssertsKeyword;
}

/** @internal */
export function isAwaitKeyword(node: Node): node is AwaitKeyword {
return node.kind === SyntaxKind.AwaitKeyword;
}
Expand Down
38 changes: 20 additions & 18 deletions src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ import {
isSpreadElement,
isStringLiteral,
isThisTypeNode,
isTypeNode,
isTypeParameterDeclaration,
isVariableDeclarationList,
JSDocNamespaceBody,
JSDocTypeAssertion,
Expand Down Expand Up @@ -143,7 +141,6 @@ import {
NumericLiteral,
ObjectLiteralElementLike,
ObjectLiteralExpression,
or,
OuterExpression,
OuterExpressionKinds,
outFile,
Expand Down Expand Up @@ -178,7 +175,6 @@ import {
ThisTypeNode,
Token,
TypeNode,
TypeParameterDeclaration,
} from "../_namespaces/ts";

// Compound nodes
Expand Down Expand Up @@ -629,7 +625,7 @@ export function isJSDocTypeAssertion(node: Node): node is JSDocTypeAssertion {
}

/** @internal */
export function getJSDocTypeAssertionType(node: JSDocTypeAssertion) {
export function getJSDocTypeAssertionType(node: JSDocTypeAssertion): TypeNode {
const type = getJSDocType(node);
Debug.assertIsDefined(type);
return type;
Expand Down Expand Up @@ -1160,18 +1156,25 @@ export function canHaveIllegalModifiers(node: Node): node is HasIllegalModifiers
|| kind === SyntaxKind.NamespaceExportDeclaration;
}

/** @internal */
export const isTypeNodeOrTypeParameterDeclaration = or(isTypeNode, isTypeParameterDeclaration) as (node: Node) => node is TypeNode | TypeParameterDeclaration;
/** @internal */
export const isQuestionOrExclamationToken = or(isQuestionToken, isExclamationToken) as (node: Node) => node is QuestionToken | ExclamationToken;
/** @internal */
export const isIdentifierOrThisTypeNode = or(isIdentifier, isThisTypeNode) as (node: Node) => node is Identifier | ThisTypeNode;
/** @internal */
export const isReadonlyKeywordOrPlusOrMinusToken = or(isReadonlyKeyword, isPlusToken, isMinusToken) as (node: Node) => node is ReadonlyKeyword | PlusToken | MinusToken;
/** @internal */
export const isQuestionOrPlusOrMinusToken = or(isQuestionToken, isPlusToken, isMinusToken) as (node: Node) => node is QuestionToken | PlusToken | MinusToken;
/** @internal */
export const isModuleName = or(isIdentifier, isStringLiteral) as (node: Node) => node is ModuleName;
export function isQuestionOrExclamationToken(node: Node): node is QuestionToken | ExclamationToken {
return isQuestionToken(node) || isExclamationToken(node);
}

export function isIdentifierOrThisTypeNode(node: Node): node is Identifier | ThisTypeNode {
return isIdentifier(node) || isThisTypeNode(node);
}

export function isReadonlyKeywordOrPlusOrMinusToken(node: Node): node is ReadonlyKeyword | PlusToken | MinusToken {
return isReadonlyKeyword(node) || isPlusToken(node) || isMinusToken(node);
}

export function isQuestionOrPlusOrMinusToken(node: Node): node is QuestionToken | PlusToken | MinusToken {
return isQuestionToken(node) || isPlusToken(node) || isMinusToken(node);
}

export function isModuleName(node: Node): node is ModuleName {
return isIdentifier(node) || isStringLiteral(node);
}

/** @internal */
export function isLiteralTypeLikeExpression(node: Node): node is NullLiteral | BooleanLiteral | LiteralExpression | PrefixUnaryExpression {
Expand Down Expand Up @@ -1278,7 +1281,6 @@ function isBinaryOperator(kind: SyntaxKind): kind is BinaryOperator {
|| kind === SyntaxKind.CommaToken;
}

/** @internal */
export function isBinaryOperatorToken(node: Node): node is BinaryOperatorToken {
return isBinaryOperator(node.kind);
}
Expand Down
23 changes: 2 additions & 21 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,6 @@ export function isAssignmentPattern(node: Node): node is AssignmentPattern {
}


/** @internal */
export function isArrayBindingElement(node: Node): node is ArrayBindingElement {
const kind = node.kind;
return kind === SyntaxKind.BindingElement
Expand Down Expand Up @@ -1911,7 +1910,6 @@ export function isTemplateLiteral(node: Node): node is TemplateLiteral {
|| kind === SyntaxKind.NoSubstitutionTemplateLiteral;
}

/** @internal */
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
return isLeftHandSideExpressionKind(skipPartiallyEmittedExpressions(node).kind);
}
Expand Down Expand Up @@ -1988,13 +1986,8 @@ export function isUnaryExpressionWithWrite(expr: Node): expr is PrefixUnaryExpre
}
}

/**
* See isExpression; not for use in transforms.
* @internal
*/
export function isLiteralTypeLiteral(node: Node): node is NullLiteral | BooleanLiteral | LiteralExpression | PrefixUnaryExpression {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
node = skipPartiallyEmittedExpressions(node);
switch (skipPartiallyEmittedExpressions(node).kind) {
switch (node.kind) {
case SyntaxKind.NullKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
Expand All @@ -2007,9 +2000,6 @@ export function isLiteralTypeLiteral(node: Node): node is NullLiteral | BooleanL

/**
* Determines whether a node is an expression based only on its kind.
* Use `isExpressionNode` if not in transforms.
*
* @internal
*/
export function isExpression(node: Node): node is Expression {
return isExpressionKind(skipPartiallyEmittedExpressions(node).kind);
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -2092,7 +2082,6 @@ export function isForInOrOfStatement(node: Node): node is ForInOrOfStatement {

// Element

/** @internal */
export function isConciseBody(node: Node): node is ConciseBody {
return isBlock(node)
|| isExpression(node);
Expand All @@ -2103,13 +2092,11 @@ export function isFunctionBody(node: Node): node is FunctionBody {
return isBlock(node);
}

/** @internal */
export function isForInitializer(node: Node): node is ForInitializer {
return isVariableDeclarationList(node)
|| isExpression(node);
}

/** @internal */
export function isModuleBody(node: Node): node is ModuleBody {
const kind = node.kind;
return kind === SyntaxKind.ModuleBlock
Expand All @@ -2131,7 +2118,6 @@ export function isJSDocNamespaceBody(node: Node): node is JSDocNamespaceBody {
|| kind === SyntaxKind.ModuleDeclaration;
}

/** @internal */
export function isNamedImportBindings(node: Node): node is NamedImportBindings {
const kind = node.kind;
return kind === SyntaxKind.NamedImports
Expand Down Expand Up @@ -2357,7 +2343,6 @@ export function isStatementButNotDeclaration(node: Node): node is Statement {
return isStatementKindButNotDeclarationKind(node.kind);
}

/** @internal */
export function isStatement(node: Node): node is Statement {
const kind = node.kind;
return isStatementKindButNotDeclarationKind(kind)
Expand All @@ -2375,6 +2360,7 @@ function isBlockStatement(node: Node): node is Block {
return !isFunctionBlock(node);
}

// TODO(jakebailey): should we be exporting this function and not isStatement?
/**
* NOTE: This is similar to `isStatement` but does not access parent pointers.
*
Expand All @@ -2389,7 +2375,6 @@ export function isStatementOrBlock(node: Node): node is Statement | Block {

// Module references

/** @internal */
export function isModuleReference(node: Node): node is ModuleReference {
const kind = node.kind;
return kind === SyntaxKind.ExternalModuleReference
Expand All @@ -2399,15 +2384,13 @@ export function isModuleReference(node: Node): node is ModuleReference {

// JSX

/** @internal */
export function isJsxTagNameExpression(node: Node): node is JsxTagNameExpression {
const kind = node.kind;
return kind === SyntaxKind.ThisKeyword
|| kind === SyntaxKind.Identifier
|| kind === SyntaxKind.PropertyAccessExpression;
}

/** @internal */
export function isJsxChild(node: Node): node is JsxChild {
const kind = node.kind;
return kind === SyntaxKind.JsxElement
Expand All @@ -2417,14 +2400,12 @@ export function isJsxChild(node: Node): node is JsxChild {
|| kind === SyntaxKind.JsxFragment;
}

/** @internal */
export function isJsxAttributeLike(node: Node): node is JsxAttributeLike {
const kind = node.kind;
return kind === SyntaxKind.JsxAttribute
|| kind === SyntaxKind.JsxSpreadAttribute;
}

/** @internal */
export function isStringLiteralOrJsxExpression(node: Node): node is StringLiteral | JsxExpression {
const kind = node.kind;
return kind === SyntaxKind.StringLiteral
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8862,13 +8862,30 @@ declare namespace ts {
*/
function isTypeNode(node: Node): node is TypeNode;
function isFunctionOrConstructorTypeNode(node: Node): node is FunctionTypeNode | ConstructorTypeNode;
function isArrayBindingElement(node: Node): node is ArrayBindingElement;
function isPropertyAccessOrQualifiedName(node: Node): node is PropertyAccessExpression | QualifiedName;
function isCallLikeExpression(node: Node): node is CallLikeExpression;
function isCallOrNewExpression(node: Node): node is CallExpression | NewExpression;
function isTemplateLiteral(node: Node): node is TemplateLiteral;
function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression;
function isLiteralTypeLiteral(node: Node): node is NullLiteral | BooleanLiteral | LiteralExpression | PrefixUnaryExpression;
/**
* Determines whether a node is an expression based only on its kind.
*/
function isExpression(node: Node): node is Expression;
function isAssertionExpression(node: Node): node is AssertionExpression;
function isIterationStatement(node: Node, lookInLabeledStatements: false): node is IterationStatement;
function isIterationStatement(node: Node, lookInLabeledStatements: boolean): node is IterationStatement | LabeledStatement;
function isConciseBody(node: Node): node is ConciseBody;
function isForInitializer(node: Node): node is ForInitializer;
function isModuleBody(node: Node): node is ModuleBody;
function isNamedImportBindings(node: Node): node is NamedImportBindings;
function isStatement(node: Node): node is Statement;
function isModuleReference(node: Node): node is ModuleReference;
function isJsxTagNameExpression(node: Node): node is JsxTagNameExpression;
function isJsxChild(node: Node): node is JsxChild;
function isJsxAttributeLike(node: Node): node is JsxAttributeLike;
function isStringLiteralOrJsxExpression(node: Node): node is StringLiteral | JsxExpression;
function isJsxOpeningLikeElement(node: Node): node is JsxOpeningLikeElement;
function isCaseOrDefaultClause(node: Node): node is CaseOrDefaultClause;
/** True if node is of a kind that may contain comment text. */
Expand Down Expand Up @@ -8981,8 +8998,15 @@ declare namespace ts {
function isPlusToken(node: Node): node is PlusToken;
function isMinusToken(node: Node): node is MinusToken;
function isAsteriskToken(node: Node): node is AsteriskToken;
function isExclamationToken(node: Node): node is ExclamationToken;
function isQuestionToken(node: Node): node is QuestionToken;
function isColonToken(node: Node): node is ColonToken;
function isQuestionDotToken(node: Node): node is QuestionDotToken;
function isEqualsGreaterThanToken(node: Node): node is EqualsGreaterThanToken;
function isIdentifier(node: Node): node is Identifier;
function isPrivateIdentifier(node: Node): node is PrivateIdentifier;
function isAssertsKeyword(node: Node): node is AssertsKeyword;
function isAwaitKeyword(node: Node): node is AwaitKeyword;
function isQualifiedName(node: Node): node is QualifiedName;
function isComputedPropertyName(node: Node): node is ComputedPropertyName;
function isTypeParameterDeclaration(node: Node): node is TypeParameterDeclaration;
Expand Down Expand Up @@ -9171,6 +9195,12 @@ declare namespace ts {
function isJSDocImplementsTag(node: Node): node is JSDocImplementsTag;
function isJSDocSatisfiesTag(node: Node): node is JSDocSatisfiesTag;
function isJSDocThrowsTag(node: Node): node is JSDocThrowsTag;
function isQuestionOrExclamationToken(node: Node): node is QuestionToken | ExclamationToken;
function isIdentifierOrThisTypeNode(node: Node): node is Identifier | ThisTypeNode;
function isReadonlyKeywordOrPlusOrMinusToken(node: Node): node is ReadonlyKeyword | PlusToken | MinusToken;
function isQuestionOrPlusOrMinusToken(node: Node): node is QuestionToken | PlusToken | MinusToken;
function isModuleName(node: Node): node is ModuleName;
function isBinaryOperatorToken(node: Node): node is BinaryOperatorToken;
function setTextRange<T extends TextRange>(range: T, location: TextRange | undefined): T;
function canHaveModifiers(node: Node): node is HasModifiers;
function canHaveDecorators(node: Node): node is HasDecorators;
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4869,13 +4869,30 @@ declare namespace ts {
*/
function isTypeNode(node: Node): node is TypeNode;
function isFunctionOrConstructorTypeNode(node: Node): node is FunctionTypeNode | ConstructorTypeNode;
function isArrayBindingElement(node: Node): node is ArrayBindingElement;
function isPropertyAccessOrQualifiedName(node: Node): node is PropertyAccessExpression | QualifiedName;
function isCallLikeExpression(node: Node): node is CallLikeExpression;
function isCallOrNewExpression(node: Node): node is CallExpression | NewExpression;
function isTemplateLiteral(node: Node): node is TemplateLiteral;
function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression;
function isLiteralTypeLiteral(node: Node): node is NullLiteral | BooleanLiteral | LiteralExpression | PrefixUnaryExpression;
/**
* Determines whether a node is an expression based only on its kind.
*/
function isExpression(node: Node): node is Expression;
function isAssertionExpression(node: Node): node is AssertionExpression;
function isIterationStatement(node: Node, lookInLabeledStatements: false): node is IterationStatement;
function isIterationStatement(node: Node, lookInLabeledStatements: boolean): node is IterationStatement | LabeledStatement;
function isConciseBody(node: Node): node is ConciseBody;
function isForInitializer(node: Node): node is ForInitializer;
function isModuleBody(node: Node): node is ModuleBody;
function isNamedImportBindings(node: Node): node is NamedImportBindings;
function isStatement(node: Node): node is Statement;
function isModuleReference(node: Node): node is ModuleReference;
function isJsxTagNameExpression(node: Node): node is JsxTagNameExpression;
function isJsxChild(node: Node): node is JsxChild;
function isJsxAttributeLike(node: Node): node is JsxAttributeLike;
function isStringLiteralOrJsxExpression(node: Node): node is StringLiteral | JsxExpression;
function isJsxOpeningLikeElement(node: Node): node is JsxOpeningLikeElement;
function isCaseOrDefaultClause(node: Node): node is CaseOrDefaultClause;
/** True if node is of a kind that may contain comment text. */
Expand Down Expand Up @@ -4988,8 +5005,15 @@ declare namespace ts {
function isPlusToken(node: Node): node is PlusToken;
function isMinusToken(node: Node): node is MinusToken;
function isAsteriskToken(node: Node): node is AsteriskToken;
function isExclamationToken(node: Node): node is ExclamationToken;
function isQuestionToken(node: Node): node is QuestionToken;
function isColonToken(node: Node): node is ColonToken;
function isQuestionDotToken(node: Node): node is QuestionDotToken;
function isEqualsGreaterThanToken(node: Node): node is EqualsGreaterThanToken;
function isIdentifier(node: Node): node is Identifier;
function isPrivateIdentifier(node: Node): node is PrivateIdentifier;
function isAssertsKeyword(node: Node): node is AssertsKeyword;
function isAwaitKeyword(node: Node): node is AwaitKeyword;
function isQualifiedName(node: Node): node is QualifiedName;
function isComputedPropertyName(node: Node): node is ComputedPropertyName;
function isTypeParameterDeclaration(node: Node): node is TypeParameterDeclaration;
Expand Down Expand Up @@ -5178,6 +5202,12 @@ declare namespace ts {
function isJSDocImplementsTag(node: Node): node is JSDocImplementsTag;
function isJSDocSatisfiesTag(node: Node): node is JSDocSatisfiesTag;
function isJSDocThrowsTag(node: Node): node is JSDocThrowsTag;
function isQuestionOrExclamationToken(node: Node): node is QuestionToken | ExclamationToken;
function isIdentifierOrThisTypeNode(node: Node): node is Identifier | ThisTypeNode;
function isReadonlyKeywordOrPlusOrMinusToken(node: Node): node is ReadonlyKeyword | PlusToken | MinusToken;
function isQuestionOrPlusOrMinusToken(node: Node): node is QuestionToken | PlusToken | MinusToken;
function isModuleName(node: Node): node is ModuleName;
function isBinaryOperatorToken(node: Node): node is BinaryOperatorToken;
function setTextRange<T extends TextRange>(range: T, location: TextRange | undefined): T;
function canHaveModifiers(node: Node): node is HasModifiers;
function canHaveDecorators(node: Node): node is HasDecorators;
Expand Down