diff --git a/packages/typescript/src/ast/ast.ts b/packages/typescript/src/ast/ast.ts index 18867bd9e13c9..d502577d7b256 100644 --- a/packages/typescript/src/ast/ast.ts +++ b/packages/typescript/src/ast/ast.ts @@ -10,13 +10,18 @@ import { SyntaxKind } from "../../enums/syntaxKind.js"; import type { AssertionExpression, BindingElement, + BindingPattern, + CallExpression, CaseClause, ComputedPropertyName, Decorator, DoStatement, + ElementAccessExpression, EndOfFile, EntityName, EnumMember, + ExportDeclaration, + ExportSpecifier, Expression, ExpressionStatement, ForInStatement, @@ -24,12 +29,20 @@ import type { ForStatement, Identifier, IfStatement, + ImportClause, + ImportEqualsDeclaration, + ImportSpecifier, JSDoc, JsxAttribute, JsxExpression, JsxSpreadAttribute, KeywordSyntaxKind, ModifierSyntaxKind, + NamedExports, + NamedImports, + NamespaceExport, + NamespaceImport, + NonNullExpression, ParameterDeclaration, PropertyAccessExpression, PropertyAssignment, @@ -58,6 +71,59 @@ export { TokenFlags } from "../../enums/tokenFlags.js"; export * from "./ast.generated.ts"; export * as factory from "./factory.generated.ts"; +declare const autoAccessorBrand: unique symbol; +export interface AutoAccessorPropertyDeclaration extends PropertyDeclaration { + readonly [autoAccessorBrand]: void; +} + +declare const optionalChainBrand: unique symbol; +export interface CallChain extends CallExpression { + readonly [optionalChainBrand]: void; +} + +export interface ElementAccessChain extends ElementAccessExpression { + readonly [optionalChainBrand]: void; +} + +declare const emptyBindingPatternBrand: unique symbol; +export type EmptyBindingPattern = BindingPattern & { readonly [emptyBindingPatternBrand]: void; }; + +export interface NonNullChain extends NonNullExpression { + readonly [optionalChainBrand]: void; +} + +export interface PropertyAccessChain extends PropertyAccessExpression { + readonly [optionalChainBrand]: void; +} + +export type OptionalChain = PropertyAccessChain | ElementAccessChain | CallChain | NonNullChain; + +declare const parameterPropertyBrand: unique symbol; +export interface ParameterPropertyDeclaration extends ParameterDeclaration { + readonly [parameterPropertyBrand]: void; +} + +export type TypeOnlyImportDeclaration = + | ImportClause & { readonly phaseModifier: SyntaxKind.TypeKeyword; } + | ImportEqualsDeclaration & { readonly isTypeOnly: true; } + | NamespaceImport & { readonly parent: ImportClause & { readonly phaseModifier: SyntaxKind.TypeKeyword; }; } + | ImportSpecifier + & ( + | { readonly isTypeOnly: true; } + | { readonly parent: NamedImports & { readonly parent: ImportClause & { readonly phaseModifier: SyntaxKind.TypeKeyword; }; }; } + ); + +export type TypeOnlyExportDeclaration = + | ExportSpecifier + & ( + | { readonly isTypeOnly: true; } + | { readonly parent: NamedExports & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true; }; }; } + ) + | ExportDeclaration & { readonly isTypeOnly: true; readonly moduleSpecifier: Expression; readonly exportClause?: undefined; } + | NamespaceExport & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true; }; }; + +export type TypeOnlyAliasDeclaration = TypeOnlyImportDeclaration | TypeOnlyExportDeclaration; + // ── Core types ── declare const rootedPathBrand: unique symbol; diff --git a/packages/typescript/src/ast/is.ts b/packages/typescript/src/ast/is.ts index c9f8fe7b76e9a..c298cb04f8e0a 100644 --- a/packages/typescript/src/ast/is.ts +++ b/packages/typescript/src/ast/is.ts @@ -7,60 +7,138 @@ import { OuterExpressionKinds } from "../../enums/outerExpressionKinds.js"; import { ScriptKind } from "../../enums/scriptKind.js"; import { SyntaxKind } from "../../enums/syntaxKind.js"; import type { + ArrayBindingElement, AsExpression, + AutoAccessorPropertyDeclaration, + BindingElement, + BindingName, BindingPattern, BlockOrExpression, BooleanLiteral, + CallChain, + CallExpression, + ClassElement, ComputedPropertyName, ConciseBody, + ConstructorTypeNode, + Declaration, + DoStatement, + ElementAccessChain, + EmptyBindingPattern, ExclamationToken, + ExportDeclaration, + ExportSpecifier, Expression, ExpressionWithTypeArguments, ForInitializer, ForInOrOfStatement, + ForInStatement, + ForOfStatement, ForStatement, + FunctionTypeNode, Identifier, + ImportClause, + ImportEqualsDeclaration, + ImportSpecifier, JSDocLink, JSDocLinkCode, JSDocLinkPlain, + JSDocParameterTag, + JSDocSignature, + JSDocTemplateTag, JSDocTypeExpression, JSDocTypeLiteral, + JsxAttributeLike, + JsxExpression, + JsxOpeningElement, + JsxOpeningFragment, + JsxSelfClosingElement, JsxTagNameExpression, + LabeledStatement, LeftHandSideExpression, LiteralExpression, MetaProperty, MinusToken, ModuleDeclaration, + NewExpression, Node, + NonNullChain, NonNullExpression, NullLiteral, + ObjectLiteralElementLike, + OptionalChain, + ParameterDeclaration, + ParameterPropertyDeclaration, ParenthesizedExpression, PartiallyEmittedExpression, PlusToken, PrefixUnaryExpression, + PropertyAccessChain, + PropertyAccessExpression, + PropertyDeclaration, + QualifiedName, QuestionToken, ReadonlyKeyword, SatisfiesExpression, + SignatureDeclaration, Statement, + StringLiteral, + TaggedTemplateExpression, + TemplateLiteralToken, TemplateMiddle, TemplateTail, ThisTypeNode, TypeAssertion, + TypeElement, TypeNode, + TypeOnlyAliasDeclaration, + TypeOnlyExportDeclaration, + TypeOnlyImportDeclaration, + TypeParameterDeclaration, UnaryExpressionBase, + WhileStatement, } from "./ast.ts"; import { + isAccessExpression, isBinaryExpression, + isCallExpression, isClassLikeDeclaration, isComputedPropertyName, + isConstructorDeclaration, + isConstructorTypeNode, + isElementAccessExpression, + isExportDeclaration, + isExportSpecifier, + isFunctionTypeNode, isHeritageClause, isIdentifier, + isImportClause, + isImportSpecifier, + isImportTypeNode, + isJSDocAugmentsTag, + isJSDocImplementsTag, isJSDocLink, isJSDocLinkCode, isJSDocLinkPlain, isJSDocNameReference, + isJSDocSignature, + isJsxAttributeLike, + isJsxExpression, + isJsxOpeningElement, + isJsxOpeningFragment, + isJsxSelfClosingElement, isLiteralExpression, + isNonNullExpression, + isObjectLiteralElementLike, + isOmittedExpression, + isParameterDeclaration, + isPropertyAccessExpression, + isQualifiedName, isShorthandPropertyAssignment, + isSignatureDeclaration, + isStringLiteral, + isTemplateLiteralToken, + isTypeReferenceNode, } from "./is.generated.ts"; import { hasExpression, @@ -70,6 +148,11 @@ import { export * from "./is.generated.ts"; +export function isAutoAccessorPropertyDeclaration(node: Node): node is AutoAccessorPropertyDeclaration { + return node.kind === SyntaxKind.PropertyDeclaration && + !!((node as PropertyDeclaration).modifierFlags & ModifierFlags.Accessor); +} + type JSDocNamespaceDeclaration = ModuleDeclaration; type WrappedExpression = @@ -577,3 +660,282 @@ export function isJsxTagNameExpression(node: Node): node is JsxTagNameExpression || kind === SyntaxKind.PropertyAccessExpression || kind === SyntaxKind.JsxNamespacedName; } + +export function isCallChain(node: Node): node is CallChain { + return isCallExpression(node) && !!(node.flags & NodeFlags.OptionalChain); +} + +export function isClassElement(node: Node): node is ClassElement { + switch (node.kind) { + case SyntaxKind.Constructor: + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.IndexSignature: + case SyntaxKind.ClassStaticBlockDeclaration: + case SyntaxKind.SemicolonClassElement: + return true; + default: + return false; + } +} + +export function isConstTypeReference(node: Node): boolean { + return isTypeReferenceNode(node) && isIdentifier(node.typeName) && + node.typeName.text === "const" && !node.typeArguments; +} + +export function isDeclarationStatement(node: Node): node is Statement & Declaration { + switch (node.kind) { + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.MissingDeclaration: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.TypeAliasDeclaration: + case SyntaxKind.JSTypeAliasDeclaration: + case SyntaxKind.EnumDeclaration: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.ImportDeclaration: + case SyntaxKind.JSImportDeclaration: + case SyntaxKind.ImportEqualsDeclaration: + case SyntaxKind.ExportDeclaration: + case SyntaxKind.ExportAssignment: + case SyntaxKind.NamespaceExportDeclaration: + return true; + default: + return false; + } +} + +export function isElementAccessChain(node: Node): node is ElementAccessChain { + return isElementAccessExpression(node) && !!(node.flags & NodeFlags.OptionalChain); +} + +export function isEmptyBindingElement(node: BindingElement | ArrayBindingElement): boolean { + return isOmittedExpression(node) || !node.name || + isBindingPattern(node.name) && node.name.elements.every(isEmptyBindingElement); +} + +export function isEmptyBindingPattern(node: BindingName): node is EmptyBindingPattern { + return isBindingPattern(node) && node.elements.every(isEmptyBindingElement); +} + +export function isFunctionLike(node: Node | undefined): node is SignatureDeclaration | JSDocSignature { + return !!node && (isSignatureDeclaration(node) || isJSDocSignature(node)); +} + +export function isFunctionOrConstructorTypeNode(node: Node): node is FunctionTypeNode | ConstructorTypeNode { + return isFunctionTypeNode(node) || isConstructorTypeNode(node); +} + +export function isImportOrExportSpecifier(node: Node): node is ImportSpecifier | ExportSpecifier { + return isImportSpecifier(node) || isExportSpecifier(node); +} + +export function isIterationStatement(node: Node, lookInLabeledStatements: false): node is DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement; +export function isIterationStatement(node: Node, lookInLabeledStatements: boolean): node is DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement | LabeledStatement; +export function isIterationStatement(node: Node, lookInLabeledStatements: boolean): boolean { + switch (node.kind) { + case SyntaxKind.ForStatement: + case SyntaxKind.ForInStatement: + case SyntaxKind.ForOfStatement: + case SyntaxKind.DoStatement: + case SyntaxKind.WhileStatement: + return true; + case SyntaxKind.LabeledStatement: + return lookInLabeledStatements && isIterationStatement((node as LabeledStatement).statement, true); + default: + return false; + } +} + +export function isJsxCallLike(node: Node): node is JsxOpeningElement | JsxSelfClosingElement | JsxOpeningFragment { + return isJsxOpeningElement(node) || isJsxSelfClosingElement(node) || isJsxOpeningFragment(node); +} + +export function isNonNullChain(node: Node): node is NonNullChain { + return isNonNullExpression(node) && !!(node.flags & NodeFlags.OptionalChain); +} + +export function isNullishCoalesce(node: Node): boolean { + return isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.QuestionQuestionToken; +} + +export function isObjectLiteralElement(node: Node): node is ObjectLiteralElementLike | JsxAttributeLike { + return isObjectLiteralElementLike(node) || isJsxAttributeLike(node); +} + +export function isOptionalChain(node: Node): node is OptionalChain { + return !!(node.flags & NodeFlags.OptionalChain) && + (isAccessExpression(node) || isCallExpression(node) || isNonNullExpression(node)); +} + +export function isParameterPropertyDeclaration(node: Node, parent: Node): node is ParameterPropertyDeclaration { + return isParameterDeclaration(node) && !!(node.modifierFlags & ModifierFlags.ParameterPropertyModifier) && + isConstructorDeclaration(parent); +} + +export function isPropertyAccessChain(node: Node): node is PropertyAccessChain { + return isPropertyAccessExpression(node) && !!(node.flags & NodeFlags.OptionalChain); +} + +export function isPropertyAccessOrQualifiedName(node: Node): node is PropertyAccessExpression | QualifiedName { + return isPropertyAccessExpression(node) || isQualifiedName(node); +} + +export function isRestParameter(node: ParameterDeclaration | JSDocParameterTag): boolean { + if (isParameterDeclaration(node)) { + return node.dotDotDotToken !== undefined || node.type?.kind === SyntaxKind.JSDocVariadicType; + } + const type = node.typeExpression; + return type?.kind === SyntaxKind.JSDocVariadicType || + type?.kind === SyntaxKind.JSDocTypeExpression && (type as JSDocTypeExpression).type.kind === SyntaxKind.JSDocVariadicType; +} + +export function isStringLiteralOrJsxExpression(node: Node): node is StringLiteral | JsxExpression { + return isStringLiteral(node) || isJsxExpression(node); +} + +export function isStringTextContainingNode(node: Node): node is StringLiteral | TemplateLiteralToken { + return isStringLiteral(node) || isTemplateLiteralToken(node); +} + +export function isTypeElement(node: Node): node is TypeElement { + switch (node.kind) { + case SyntaxKind.ConstructSignature: + case SyntaxKind.CallSignature: + case SyntaxKind.PropertySignature: + case SyntaxKind.MethodSignature: + case SyntaxKind.IndexSignature: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.NotEmittedTypeElement: + return true; + default: + return false; + } +} + +export function isClassOrTypeElement(node: Node): node is ClassElement | TypeElement { + return isClassElement(node) || isTypeElement(node); +} + +export function isTypeOnlyImportDeclaration(node: Node): node is TypeOnlyImportDeclaration { + switch (node.kind) { + case SyntaxKind.ImportSpecifier: { + const specifier = node as ImportSpecifier; + const clause = specifier.parent?.parent; + return specifier.isTypeOnly || !!clause && isImportClause(clause) && clause.phaseModifier === SyntaxKind.TypeKeyword; + } + case SyntaxKind.NamespaceImport: + return !!node.parent && isImportClause(node.parent) && node.parent.phaseModifier === SyntaxKind.TypeKeyword; + case SyntaxKind.ImportClause: + return (node as ImportClause).phaseModifier === SyntaxKind.TypeKeyword; + case SyntaxKind.ImportEqualsDeclaration: + return (node as ImportEqualsDeclaration).isTypeOnly; + default: + return false; + } +} + +export function isTypeOnlyExportDeclaration(node: Node): node is TypeOnlyExportDeclaration { + switch (node.kind) { + case SyntaxKind.ExportSpecifier: { + const specifier = node as ExportSpecifier; + const declaration = specifier.parent?.parent; + return specifier.isTypeOnly || !!declaration && isExportDeclaration(declaration) && declaration.isTypeOnly; + } + case SyntaxKind.ExportDeclaration: { + const declaration = node as ExportDeclaration; + return declaration.isTypeOnly && !!declaration.moduleSpecifier && !declaration.exportClause; + } + case SyntaxKind.NamespaceExport: + return !!node.parent && isExportDeclaration(node.parent) && node.parent.isTypeOnly; + default: + return false; + } +} + +export function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyAliasDeclaration { + return isTypeOnlyImportDeclaration(node) || isTypeOnlyExportDeclaration(node); +} + +export function isPartOfTypeOnlyImportOrExportDeclaration(node: Node): boolean { + return findAncestor(node, isTypeOnlyImportOrExportDeclaration) !== undefined; +} + +export function isPartOfTypeNode(node: Node): boolean { + function isTypeExpression(node: ExpressionWithTypeArguments): boolean { + return !!node.parent && (isJSDocImplementsTag(node.parent) || isJSDocAugmentsTag(node.parent) || + isPartOfTypeExpressionWithTypeArguments(node)); + } + + if (node.kind >= SyntaxKind.FirstTypeNode && node.kind <= SyntaxKind.LastTypeNode) return true; + switch (node.kind) { + case SyntaxKind.AnyKeyword: + case SyntaxKind.UnknownKeyword: + case SyntaxKind.NumberKeyword: + case SyntaxKind.BigIntKeyword: + case SyntaxKind.StringKeyword: + case SyntaxKind.BooleanKeyword: + case SyntaxKind.SymbolKeyword: + case SyntaxKind.ObjectKeyword: + case SyntaxKind.UndefinedKeyword: + case SyntaxKind.NullKeyword: + case SyntaxKind.NeverKeyword: + return true; + case SyntaxKind.VoidKeyword: + return node.parent?.kind !== SyntaxKind.VoidExpression; + case SyntaxKind.ExpressionWithTypeArguments: + return isTypeExpression(node as ExpressionWithTypeArguments); + case SyntaxKind.TypeParameter: + return node.parent?.kind === SyntaxKind.MappedType || node.parent?.kind === SyntaxKind.InferType; + case SyntaxKind.Identifier: + if ( + node.parent && (isQualifiedName(node.parent) && node.parent.right === node || + isPropertyAccessExpression(node.parent) && node.parent.name === node) + ) { + node = node.parent; + } + // falls through + case SyntaxKind.QualifiedName: + case SyntaxKind.PropertyAccessExpression: + case SyntaxKind.ThisKeyword: { + const parent = node.parent; + if (!parent || parent.kind === SyntaxKind.TypeQuery) return false; + if (isImportTypeNode(parent)) return !parent.isTypeOf; + if (parent.kind >= SyntaxKind.FirstTypeNode && parent.kind <= SyntaxKind.LastTypeNode) return true; + switch (parent.kind) { + case SyntaxKind.ExpressionWithTypeArguments: + return isTypeExpression(parent as ExpressionWithTypeArguments); + case SyntaxKind.TypeParameter: + return (parent as TypeParameterDeclaration).constraint === node; + case SyntaxKind.JSDocTemplateTag: + return (parent as JSDocTemplateTag).constraint === node; + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.PropertySignature: + case SyntaxKind.Parameter: + case SyntaxKind.VariableDeclaration: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + case SyntaxKind.Constructor: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.MethodSignature: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.CallSignature: + case SyntaxKind.ConstructSignature: + case SyntaxKind.IndexSignature: + case SyntaxKind.TypeAssertionExpression: + return (parent as never as { type: unknown; }).type === node; + case SyntaxKind.CallExpression: + case SyntaxKind.NewExpression: + case SyntaxKind.TaggedTemplateExpression: + return !!(parent as CallExpression | NewExpression | TaggedTemplateExpression).typeArguments?.includes(node as TypeNode); + } + } + } + return false; +} diff --git a/packages/typescript/src/ast/utils.ts b/packages/typescript/src/ast/utils.ts index 1b0085e51478b..dfaecc7ae1b5f 100644 --- a/packages/typescript/src/ast/utils.ts +++ b/packages/typescript/src/ast/utils.ts @@ -4,6 +4,7 @@ import { ModifierFlags } from "../../enums/modifierFlags.js"; import { NodeFlags } from "../../enums/nodeFlags.js"; import { OuterExpressionKinds } from "../../enums/outerExpressionKinds.js"; import { SyntaxKind } from "../../enums/syntaxKind.js"; +import { isRootedDiskPath } from "../api/path.ts"; import type { __String, AccessExpression, @@ -116,6 +117,10 @@ export function isExternalModule(file: SourceFile): boolean { return file.externalModuleIndicator !== undefined; } +export function isExternalModuleNameRelative(moduleName: string): boolean { + return /^\.\.?(?:$|[\\/])/.test(moduleName) || isRootedDiskPath(moduleName); +} + function getRootDeclaration(node: Node): Node { while (node.kind === SyntaxKind.BindingElement) { node = node.parent.parent; diff --git a/packages/typescript/test/astPredicates.test.ts b/packages/typescript/test/astPredicates.test.ts new file mode 100644 index 0000000000000..0f97fae203128 --- /dev/null +++ b/packages/typescript/test/astPredicates.test.ts @@ -0,0 +1,469 @@ +import * as ast from "@typescript/typescript/ast"; +import * as factory from "@typescript/typescript/ast/factory"; +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { fileURLToPath } from "node:url"; +import { API } from "./sync/api.testUtils.ts"; +import { createVirtualFileSystem } from "./testUtils.ts"; + +function createAPI(): API { + return new API({ + cwd: fileURLToPath(new URL("../../../", import.meta.url)), + tsserverPath: fileURLToPath(new URL(`../../../built/local/tsc${process.platform === "win32" ? ".exe" : ""}`, import.meta.url)), + fs: createVirtualFileSystem({}), + }); +} + +test("isAutoAccessorPropertyDeclaration distinguishes accessor modifiers from accessors", () => { + const name = factory.createIdentifier("value"); + const property: ast.Node = factory.createPropertyDeclaration([factory.createToken(ast.SyntaxKind.AccessorKeyword)], name); + assert.ok(ast.isAutoAccessorPropertyDeclaration(property)); + const narrowed: ast.AutoAccessorPropertyDeclaration = property; + assert.equal(narrowed.name, name); + assert.equal(ast.isAutoAccessorPropertyDeclaration(factory.createPropertyDeclaration(undefined, name)), false); + assert.equal(ast.isAutoAccessorPropertyDeclaration(factory.createGetAccessorDeclaration(undefined, name, undefined, [])), false); + const ordinary = factory.createPropertyDeclaration(undefined, name); + if (!ast.isAutoAccessorPropertyDeclaration(ordinary)) { + assert.equal(ordinary.name, name); + } +}); + +test("isCallChain recognizes continuations without a question-dot token", () => { + const name = factory.createIdentifier("f"); + const call: ast.Node = factory.createCallExpression(name, undefined, undefined, [], ast.NodeFlags.OptionalChain); + assert.ok(ast.isCallChain(call)); + const narrowed: ast.CallChain = call; + assert.equal(narrowed.questionDotToken, undefined); + const ordinary = factory.createCallExpression(name, undefined, undefined, [], ast.NodeFlags.None); + if (!ast.isCallChain(ordinary)) { + assert.equal(ordinary.arguments.length, 0); + } + assert.equal(ast.isCallChain(ordinary), false); + assert.equal(ast.isCallChain(factory.createNonNullExpression(name, ast.NodeFlags.OptionalChain)), false); +}); + +test("isClassElement includes static blocks and index signatures but not interface properties", () => { + const signature: ast.Node = factory.createIndexSignatureDeclaration(undefined, []); + assert.ok(ast.isClassElement(signature)); + const narrowed: ast.ClassElement = signature; + assert.equal(narrowed, signature); + assert.ok(ast.isClassElement(factory.createClassStaticBlockDeclaration(undefined, factory.createBlock([])))); + assert.equal(ast.isClassElement(factory.createPropertySignatureDeclaration(undefined, factory.createIdentifier("x"))), false); +}); + +test("isConstTypeReference rejects qualified names and even empty type argument lists", () => { + const name = factory.createIdentifier("const"); + const reference = factory.createTypeReferenceNode(name); + assert.ok(ast.isConstTypeReference(reference)); + assert.equal(reference.typeName, name); + assert.equal(ast.isConstTypeReference(factory.createTypeReferenceNode(name, [])), false); + assert.equal(ast.isConstTypeReference(factory.createTypeReferenceNode(factory.createQualifiedName(factory.createIdentifier("ns"), name))), false); + assert.equal(ast.isConstTypeReference(factory.createIdentifier("const")), false); +}); + +test("isDeclarationStatement does not classify variable statements as declaration statements", () => { + const declaration: ast.Node = factory.createExportDeclaration(); + assert.ok(ast.isDeclarationStatement(declaration)); + const narrowed: ast.Statement & ast.Declaration = declaration; + assert.equal(narrowed, declaration); + assert.ok(ast.isDeclarationStatement(factory.createMissingDeclaration())); + assert.equal(ast.isDeclarationStatement(factory.createVariableStatement(undefined, factory.createVariableDeclarationList([], ast.NodeFlags.Const))), false); + assert.equal(ast.isDeclarationStatement(factory.createBlock([])), false); +}); + +test("isDeclarationStatement includes declarations produced by JavaScript JSDoc", () => { + using api = createAPI(); + const source = api.createSourceFile( + "/declarations.js", + `/** @typedef {string} Name */ +/** @callback Callback + * @returns {void} + */ +/** @import { Value } from "./types" */ +`, + ).sourceFile; + const declarations = source.statements.filter(node => node.kind === ast.SyntaxKind.JSTypeAliasDeclaration || node.kind === ast.SyntaxKind.JSImportDeclaration); + assert.deepEqual(declarations.map(node => node.kind), [ + ast.SyntaxKind.JSTypeAliasDeclaration, + ast.SyntaxKind.JSTypeAliasDeclaration, + ast.SyntaxKind.JSImportDeclaration, + ]); + assert.ok(declarations.every(ast.isDeclarationStatement)); +}); + +test("isElementAccessChain uses the chain flag rather than the question-dot token", () => { + const name = factory.createIdentifier("obj"); + const index = factory.createStringLiteral("key", ast.TokenFlags.None); + const access: ast.Node = factory.createElementAccessExpression(name, undefined, index, ast.NodeFlags.OptionalChain); + assert.ok(ast.isElementAccessChain(access)); + const narrowed: ast.ElementAccessChain = access; + assert.equal(narrowed.argumentExpression, index); + assert.equal(ast.isElementAccessChain(factory.createElementAccessExpression(name, undefined, index, ast.NodeFlags.None)), false); + assert.equal(ast.isElementAccessChain(factory.createPropertyAccessExpression(name, undefined, name, ast.NodeFlags.OptionalChain)), false); +}); + +test("isEmptyBindingElement recursively distinguishes bindings from empty nested patterns", () => { + const empty = factory.createBindingElement(undefined, undefined, factory.createObjectBindingPattern([])); + assert.ok(ast.isEmptyBindingElement(empty)); + assert.ok(ast.isEmptyBindingElement(factory.createOmittedExpression())); + assert.ok(ast.isEmptyBindingElement(factory.createBindingElement())); + const binding = factory.createBindingElement(undefined, undefined, factory.createIdentifier("value")); + assert.equal(ast.isEmptyBindingElement(binding), false); + assert.equal(ast.isEmptyBindingElement(factory.createBindingElement(undefined, undefined, factory.createArrayBindingPattern([binding]))), false); +}); + +test("isEmptyBindingElement recognizes parsed array-binding holes", () => { + using api = createAPI(); + const source = api.createSourceFile("/holes.ts", "const [, , value] = values;").sourceFile; + const statement = source.statements[0]; + assert.ok(ast.isVariableStatement(statement)); + const pattern = statement.declarationList.declarations[0].name; + assert.ok(ast.isArrayBindingPattern(pattern)); + assert.deepEqual(pattern.elements.map(element => element.kind), [ + ast.SyntaxKind.BindingElement, + ast.SyntaxKind.BindingElement, + ast.SyntaxKind.BindingElement, + ]); + assert.deepEqual(pattern.elements.map(ast.isEmptyBindingElement), [true, true, false]); +}); + +test("isEmptyBindingPattern accepts nested empty patterns but not identifiers", () => { + const nested: ast.BindingName = factory.createArrayBindingPattern([ + factory.createBindingElement(undefined, undefined, factory.createObjectBindingPattern([])), + ]); + assert.ok(ast.isEmptyBindingPattern(nested)); + const narrowed: ast.EmptyBindingPattern = nested; + assert.equal(narrowed.elements.length, 1); + assert.equal(ast.isEmptyBindingPattern(factory.createIdentifier("value")), false); + assert.equal( + ast.isEmptyBindingPattern(factory.createObjectBindingPattern([ + factory.createBindingElement(undefined, undefined, factory.createIdentifier("value")), + ])), + false, + ); + const nonempty = factory.createObjectBindingPattern([ + factory.createBindingElement(undefined, undefined, factory.createIdentifier("value")), + ]); + if (!ast.isEmptyBindingPattern(nonempty)) { + assert.equal(nonempty.elements.length, 1); + } +}); + +test("isEmptyBindingPattern accepts parsed hole-only and nested empty patterns", () => { + using api = createAPI(); + const source = api.createSourceFile( + "/patterns.ts", + "const [,] = values; const [, [,], {}] = values; const [, value] = values;", + ).sourceFile; + const patterns = source.statements.filter(ast.isVariableStatement).map(statement => statement.declarationList.declarations[0].name); + assert.deepEqual(patterns.map(ast.isEmptyBindingPattern), [true, true, false]); +}); + +test("isFunctionLike includes signatures and JSDoc callbacks, not only functions with bodies", () => { + const signature: ast.Node | undefined = factory.createCallSignatureDeclaration(undefined, []); + assert.ok(ast.isFunctionLike(signature)); + const narrowed: ast.SignatureDeclaration | ast.JSDocSignature = signature; + assert.equal(narrowed.parameters.length, 0); + assert.ok(ast.isFunctionLike(factory.createJSDocSignature(undefined, []))); + assert.ok(ast.isFunctionLike(factory.createFunctionTypeNode(undefined, [], factory.createKeywordTypeNode(ast.SyntaxKind.VoidKeyword)))); + assert.equal(ast.isFunctionLike(undefined), false); + assert.equal(ast.isFunctionLike(factory.createClassStaticBlockDeclaration(undefined, factory.createBlock([]))), false); +}); + +test("isFunctionOrConstructorTypeNode excludes callable declaration signatures", () => { + const result = factory.createKeywordTypeNode(ast.SyntaxKind.VoidKeyword); + const type: ast.Node = factory.createFunctionTypeNode(undefined, [], result); + assert.ok(ast.isFunctionOrConstructorTypeNode(type)); + const narrowed: ast.FunctionTypeNode | ast.ConstructorTypeNode = type; + assert.equal(narrowed.type, result); + assert.ok(ast.isFunctionOrConstructorTypeNode(factory.createConstructorTypeNode(undefined, undefined, [], result))); + assert.equal(ast.isFunctionOrConstructorTypeNode(factory.createCallSignatureDeclaration(undefined, [])), false); +}); + +test("isImportOrExportSpecifier distinguishes named specifiers from namespace imports", () => { + const name = factory.createIdentifier("value"); + const specifier: ast.Node = factory.createImportSpecifier(true, undefined, name); + assert.ok(ast.isImportOrExportSpecifier(specifier)); + const narrowed: ast.ImportSpecifier | ast.ExportSpecifier = specifier; + assert.equal(narrowed.isTypeOnly, true); + assert.ok(ast.isImportOrExportSpecifier(factory.createExportSpecifier(false, undefined, name))); + assert.equal(ast.isImportOrExportSpecifier(factory.createNamespaceImport(name)), false); +}); + +test("isIterationStatement follows labels only when requested", () => { + const empty = factory.createEmptyStatement(); + const loop: ast.Node = factory.createWhileStatement(factory.createIdentifier("condition"), empty); + assert.ok(ast.isIterationStatement(loop, false)); + const narrowed: ast.DoStatement | ast.WhileStatement | ast.ForStatement | ast.ForInStatement | ast.ForOfStatement = loop; + assert.equal(narrowed.statement, empty); + const labeled = factory.createLabeledStatement(factory.createIdentifier("outer"), factory.createLabeledStatement(factory.createIdentifier("inner"), loop)); + assert.equal(ast.isIterationStatement(labeled, false), false); + assert.equal(ast.isIterationStatement(labeled, true), true); + assert.equal(ast.isIterationStatement(factory.createLabeledStatement(factory.createIdentifier("label"), empty), true), false); +}); + +test("isJsxCallLike includes opening fragments but excludes complete fragments", () => { + const opening: ast.Node = factory.createJsxOpeningFragment(); + assert.ok(ast.isJsxCallLike(opening)); + const narrowed: ast.JsxOpeningElement | ast.JsxSelfClosingElement | ast.JsxOpeningFragment = opening; + assert.equal(narrowed, opening); + assert.ok(ast.isJsxCallLike(factory.createJsxSelfClosingElement(factory.createIdentifier("Component"), undefined, factory.createJsxAttributes([])))); + assert.equal(ast.isJsxCallLike(factory.createJsxFragment(factory.createJsxOpeningFragment(), [], factory.createJsxClosingFragment())), false); +}); + +test("isNonNullChain distinguishes optional-chain assertions from ordinary assertions", () => { + const name = factory.createIdentifier("value"); + const assertion: ast.Node = factory.createNonNullExpression(name, ast.NodeFlags.OptionalChain); + assert.ok(ast.isNonNullChain(assertion)); + const narrowed: ast.NonNullChain = assertion; + assert.equal(narrowed.expression, name); + assert.equal(ast.isNonNullChain(factory.createNonNullExpression(name, ast.NodeFlags.None)), false); + assert.equal(ast.isNonNullChain(factory.createCallExpression(name, undefined, undefined, [], ast.NodeFlags.OptionalChain)), false); +}); + +test("isNullishCoalesce distinguishes nullish coalescing from logical OR", () => { + const name = factory.createIdentifier("value"); + const expression = factory.createBinaryExpression(undefined, name, undefined, factory.createToken(ast.SyntaxKind.QuestionQuestionToken), name); + assert.ok(ast.isNullishCoalesce(expression)); + assert.equal(expression.left, name); + assert.equal(ast.isNullishCoalesce(factory.createBinaryExpression(undefined, name, undefined, factory.createToken(ast.SyntaxKind.BarBarToken), name)), false); + assert.equal(ast.isNullishCoalesce(name), false); +}); + +test("isObjectLiteralElement includes JSX attributes as well as object spreads", () => { + const name = factory.createIdentifier("value"); + const attribute: ast.Node = factory.createJsxAttribute(name); + assert.ok(ast.isObjectLiteralElement(attribute)); + const narrowed: ast.ObjectLiteralElementLike | ast.JsxAttributeLike = attribute; + assert.equal(narrowed, attribute); + assert.ok(ast.isObjectLiteralElement(factory.createJsxSpreadAttribute(name))); + assert.ok(ast.isObjectLiteralElement(factory.createSpreadAssignment(name))); + assert.equal(ast.isObjectLiteralElement(factory.createPropertyDeclaration(undefined, name)), false); +}); + +test("isOptionalChain includes non-null continuations but rejects flagged unrelated nodes", () => { + const name = factory.createIdentifier("value"); + const chain: ast.Node = factory.createNonNullExpression(name, ast.NodeFlags.OptionalChain); + assert.ok(ast.isOptionalChain(chain)); + const narrowed: ast.OptionalChain = chain; + assert.equal(narrowed.expression, name); + assert.ok(ast.isOptionalChain(factory.createCallExpression(name, undefined, undefined, [], ast.NodeFlags.OptionalChain))); + assert.equal(ast.isOptionalChain(factory.createNonNullExpression(name, ast.NodeFlags.None)), false); + const unrelated = factory.createParenthesizedExpression(name); + Object.defineProperty(unrelated, "flags", { value: ast.NodeFlags.OptionalChain }); + assert.equal(ast.isOptionalChain(unrelated), false); +}); + +test("isParameterPropertyDeclaration requires both a property modifier and a constructor", () => { + const name = factory.createIdentifier("value"); + const constructor = factory.createConstructorDeclaration(undefined, undefined, []); + const parameter: ast.Node = factory.createParameterDeclaration([factory.createToken(ast.SyntaxKind.ReadonlyKeyword)], undefined, name); + assert.ok(ast.isParameterPropertyDeclaration(parameter, constructor)); + const narrowed: ast.ParameterPropertyDeclaration = parameter; + assert.equal(narrowed.name, name); + assert.equal(ast.isParameterPropertyDeclaration(parameter, factory.createFunctionDeclaration(undefined, undefined, undefined, undefined, [])), false); + assert.equal(ast.isParameterPropertyDeclaration(factory.createParameterDeclaration(undefined, undefined, name), constructor), false); + assert.equal(ast.isParameterPropertyDeclaration(factory.createPropertyDeclaration(undefined, name), constructor), false); +}); + +test("isPropertyAccessChain includes chain continuations without their own question-dot token", () => { + const name = factory.createIdentifier("value"); + const access: ast.Node = factory.createPropertyAccessExpression(name, undefined, name, ast.NodeFlags.OptionalChain); + assert.ok(ast.isPropertyAccessChain(access)); + const narrowed: ast.PropertyAccessChain = access; + assert.equal(narrowed.name, name); + assert.equal(ast.isPropertyAccessChain(factory.createPropertyAccessExpression(name, undefined, name, ast.NodeFlags.None)), false); + assert.equal(ast.isPropertyAccessChain(factory.createElementAccessExpression(name, undefined, name, ast.NodeFlags.OptionalChain)), false); +}); + +test("isPropertyAccessOrQualifiedName accepts entity names and expressions, not element access", () => { + const name = factory.createIdentifier("value"); + const qualified: ast.Node = factory.createQualifiedName(name, name); + assert.ok(ast.isPropertyAccessOrQualifiedName(qualified)); + const narrowed: ast.PropertyAccessExpression | ast.QualifiedName = qualified; + assert.equal(narrowed, qualified); + assert.ok(ast.isPropertyAccessOrQualifiedName(factory.createPropertyAccessExpression(name, undefined, name, ast.NodeFlags.None))); + assert.equal(ast.isPropertyAccessOrQualifiedName(factory.createElementAccessExpression(name, undefined, name, ast.NodeFlags.None)), false); +}); + +test("isRestParameter supports syntactic rest parameters and JSDoc variadic types", () => { + const name = factory.createIdentifier("values"); + const type = factory.createJSDocVariadicType(factory.createKeywordTypeNode(ast.SyntaxKind.StringKeyword)); + assert.ok(ast.isRestParameter(factory.createParameterDeclaration(undefined, factory.createToken(ast.SyntaxKind.DotDotDotToken), name))); + assert.ok(ast.isRestParameter(factory.createParameterDeclaration(undefined, undefined, name, undefined, type))); + assert.ok(ast.isRestParameter(factory.createJSDocParameterTag(factory.createIdentifier("param"), name, false, type, false, undefined))); + assert.ok(ast.isRestParameter(factory.createJSDocParameterTag(factory.createIdentifier("param"), name, false, factory.createJSDocTypeExpression(type), false, undefined))); + assert.equal(ast.isRestParameter(factory.createParameterDeclaration(undefined, undefined, name)), false); + assert.equal(ast.isRestParameter(factory.createJSDocParameterTag(factory.createIdentifier("param"), name, false, undefined, false, undefined)), false); +}); + +test("isStringLiteralOrJsxExpression excludes template literals and full JSX elements", () => { + const expression: ast.Node = factory.createJsxExpression(); + assert.ok(ast.isStringLiteralOrJsxExpression(expression)); + const narrowed: ast.StringLiteral | ast.JsxExpression = expression; + assert.equal(narrowed, expression); + assert.ok(ast.isStringLiteralOrJsxExpression(factory.createStringLiteral("text", ast.TokenFlags.None))); + assert.equal(ast.isStringLiteralOrJsxExpression(factory.createNoSubstitutionTemplateLiteral("text", ast.TokenFlags.None)), false); + assert.equal(ast.isStringLiteralOrJsxExpression(factory.createJsxOpeningFragment()), false); +}); + +test("isStringTextContainingNode includes template fragments but not template expressions", () => { + const head: ast.Node = factory.createTemplateHead("text", "text", ast.TokenFlags.None); + assert.ok(ast.isStringTextContainingNode(head)); + const narrowed: ast.StringLiteral | ast.TemplateLiteralToken = head; + assert.equal(narrowed.text, "text"); + assert.ok(ast.isStringTextContainingNode(factory.createTemplateMiddle("middle", "middle", ast.TokenFlags.None))); + assert.ok(ast.isStringTextContainingNode(factory.createTemplateTail("tail", "tail", ast.TokenFlags.None))); + assert.ok(ast.isStringTextContainingNode(factory.createNoSubstitutionTemplateLiteral("text", ast.TokenFlags.None))); + assert.ok(ast.isStringTextContainingNode(factory.createStringLiteral("text", ast.TokenFlags.None))); + assert.equal(ast.isStringTextContainingNode(factory.createTemplateExpression(factory.createTemplateHead("", "", ast.TokenFlags.None), [])), false); +}); + +test("isTypeElement includes accessors and synthetic elisions but excludes method declarations", () => { + const name = factory.createIdentifier("value"); + const accessor: ast.Node = factory.createGetAccessorDeclaration(undefined, name, undefined, []); + assert.ok(ast.isTypeElement(accessor)); + const narrowed: ast.TypeElement = accessor; + assert.equal(narrowed, accessor); + assert.ok(ast.isTypeElement(factory.createNotEmittedTypeElement())); + assert.ok(ast.isTypeElement(factory.createCallSignatureDeclaration(undefined, []))); + assert.equal(ast.isTypeElement(factory.createMethodDeclaration(undefined, undefined, name, undefined, undefined, [])), false); +}); + +test("isClassOrTypeElement unifies class-only and type-only members", () => { + const member: ast.Node = factory.createClassStaticBlockDeclaration(undefined, factory.createBlock([])); + assert.ok(ast.isClassOrTypeElement(member)); + const narrowed: ast.ClassElement | ast.TypeElement = member; + assert.equal(narrowed, member); + assert.ok(ast.isClassOrTypeElement(factory.createCallSignatureDeclaration(undefined, []))); + assert.equal(ast.isClassOrTypeElement(factory.createPropertyAssignment(undefined, factory.createIdentifier("value"), undefined, undefined, factory.createNumericLiteral("1", ast.TokenFlags.None))), false); +}); + +test("isTypeOnlyImportDeclaration inherits type-only status but not defer status", () => { + const name = factory.createIdentifier("value"); + const specifier = factory.createImportSpecifier(false, undefined, name); + const bindings = factory.createNamedImports([specifier]); + const clause = factory.createImportClause(ast.SyntaxKind.TypeKeyword, undefined, bindings); + Object.defineProperty(specifier, "parent", { value: bindings }); + Object.defineProperty(bindings, "parent", { value: clause }); + const node: ast.Node = specifier; + assert.ok(ast.isTypeOnlyImportDeclaration(node)); + const narrowed: ast.TypeOnlyImportDeclaration = node; + assert.equal(narrowed, specifier); + assert.ok(ast.isTypeOnlyImportDeclaration(clause)); + assert.ok(ast.isTypeOnlyImportDeclaration(factory.createImportSpecifier(true, undefined, name))); + assert.equal(ast.isTypeOnlyImportDeclaration(factory.createImportClause(ast.SyntaxKind.DeferKeyword, name)), false); + assert.equal(ast.isTypeOnlyImportDeclaration(factory.createIdentifier("value")), false); + const valueSpecifier = factory.createImportSpecifier(false, undefined, name); + assert.equal(ast.isTypeOnlyImportDeclaration(valueSpecifier), false); + if (!ast.isTypeOnlyImportDeclaration(valueSpecifier)) { + assert.equal(valueSpecifier.name, name); + } + const namespace = factory.createNamespaceImport(name); + Object.defineProperty(namespace, "parent", { value: factory.createImportClause(ast.SyntaxKind.TypeKeyword, undefined, namespace) }); + assert.ok(ast.isTypeOnlyImportDeclaration(namespace)); + assert.ok(ast.isTypeOnlyImportDeclaration(factory.createImportEqualsDeclaration(undefined, true, name, name))); + assert.equal(ast.isTypeOnlyImportDeclaration(factory.createImportEqualsDeclaration(undefined, false, name, name)), false); +}); + +test("isTypeOnlyExportDeclaration distinguishes export-star from named export containers", () => { + const name = factory.createIdentifier("value"); + const specifier = factory.createExportSpecifier(false, undefined, name); + const bindings = factory.createNamedExports([specifier]); + const declaration = factory.createExportDeclaration(undefined, true, bindings); + Object.defineProperty(specifier, "parent", { value: bindings }); + Object.defineProperty(bindings, "parent", { value: declaration }); + const node: ast.Node = specifier; + assert.ok(ast.isTypeOnlyExportDeclaration(node)); + const narrowed: ast.TypeOnlyExportDeclaration = node; + assert.equal(narrowed, specifier); + assert.equal(ast.isTypeOnlyExportDeclaration(declaration), false); + assert.ok(ast.isTypeOnlyExportDeclaration(factory.createExportDeclaration(undefined, true, undefined, factory.createStringLiteral("module", ast.TokenFlags.None)))); + assert.equal(ast.isTypeOnlyExportDeclaration(factory.createExportDeclaration(undefined, false, undefined, factory.createStringLiteral("module", ast.TokenFlags.None))), false); + assert.ok(ast.isTypeOnlyExportDeclaration(factory.createExportSpecifier(true, undefined, name))); + const valueSpecifier = factory.createExportSpecifier(false, undefined, name); + assert.equal(ast.isTypeOnlyExportDeclaration(valueSpecifier), false); + if (!ast.isTypeOnlyExportDeclaration(valueSpecifier)) { + assert.equal(valueSpecifier.name, name); + } + const namespace = factory.createNamespaceExport(name); + Object.defineProperty(namespace, "parent", { value: factory.createExportDeclaration(undefined, true, namespace) }); + assert.ok(ast.isTypeOnlyExportDeclaration(namespace)); +}); + +test("isTypeOnlyImportOrExportDeclaration includes both kinds of explicit type-only aliases", () => { + const name = factory.createIdentifier("value"); + const node: ast.Node = factory.createImportSpecifier(true, undefined, name); + assert.ok(ast.isTypeOnlyImportOrExportDeclaration(node)); + const narrowed: ast.TypeOnlyAliasDeclaration = node; + assert.equal(narrowed, node); + assert.ok(ast.isTypeOnlyImportOrExportDeclaration(factory.createExportSpecifier(true, undefined, name))); + assert.equal(ast.isTypeOnlyImportOrExportDeclaration(factory.createImportClause(undefined, name)), false); +}); + +test("isPartOfTypeOnlyImportOrExportDeclaration includes the alias itself and its descendants", () => { + const name = factory.createIdentifier("value"); + const specifier = factory.createImportSpecifier(true, undefined, name); + Object.defineProperty(name, "parent", { value: specifier }); + assert.ok(ast.isPartOfTypeOnlyImportOrExportDeclaration(name)); + assert.ok(ast.isPartOfTypeOnlyImportOrExportDeclaration(specifier)); + assert.equal(ast.isPartOfTypeOnlyImportOrExportDeclaration(factory.createIdentifier("value")), false); +}); + +test("isPartOfTypeNode classifies contextual names without recursively classifying every qualifier", async () => { + const { API } = await import("./sync/api.testUtils.ts"); + const { createVirtualFileSystem } = await import("./testUtils.ts"); + const { fileURLToPath } = await import("node:url"); + using api = new API({ + cwd: fileURLToPath(new URL("../../../", import.meta.url)), + tsserverPath: fileURLToPath(new URL(`../../../built/local/tsc${process.platform === "win32" ? ".exe" : ""}`, import.meta.url)), + fs: createVirtualFileSystem({}), + }); + const source = api.createSourceFile( + "/test.ts", + ` +let value: A.B.C; +let query: typeof A.B.C; +class Derived extends Base implements Interface {} +interface I extends Base {} +void value; +type Result = void; +type Imported = import("module").ImportedName; +type ImportQuery = typeof import("module").QueriedName; +type Mapped = { [K in keyof T]: T[K] }; +type Conditional = T extends infer U ? U : never; +f(); +`, + ).sourceFile; + const nodes: ast.Node[] = []; + function collect(node: ast.Node): void { + nodes.push(node); + node.forEachChild(child => { + collect(child); + }); + } + collect(source); + const names = nodes.filter(ast.isIdentifier).filter(node => ["A", "B", "C"].includes(node.text)); + assert.deepEqual(names.map(ast.isPartOfTypeNode), [false, false, true, false, false, false]); + const heritage = nodes.filter(ast.isHeritageClause).flatMap(clause => [...clause.types]); + assert.deepEqual(heritage.map(ast.isPartOfTypeNode), [false, true, true]); + const voids = nodes.filter(node => node.kind === ast.SyntaxKind.VoidKeyword); + assert.deepEqual(voids.map(ast.isPartOfTypeNode), [true]); + const valueUses = nodes.filter(ast.isIdentifier).filter(node => node.text === "value"); + assert.deepEqual(valueUses.map(ast.isPartOfTypeNode), [false, false]); + const importNames = nodes.filter(ast.isIdentifier).filter(node => node.text === "ImportedName" || node.text === "QueriedName"); + assert.deepEqual(importNames.map(ast.isPartOfTypeNode), [true, false]); + const parameters = nodes.filter(ast.isTypeParameterDeclaration); + assert.deepEqual(parameters.map(ast.isPartOfTypeNode), [false, true, false, true]); + const argument = nodes.find(node => ast.isIdentifier(node) && node.text === "TypeArgument"); + assert.ok(argument && ast.isPartOfTypeNode(argument)); +}); + +test("isExternalModuleNameRelative includes rooted disk paths but excludes URLs and bare names", () => { + for (const name of [".", "..", "./module", "../module", ".\\module", "..\\module", "/module", "C:/module", "C:\\module", "\\\\server\\share\\module"]) { + assert.ok(ast.isExternalModuleNameRelative(name), name); + } + for (const name of ["", "module", "@scope/module", ".module", ".../module", "https://example.com/module", "file:///module", "C:module"]) { + assert.equal(ast.isExternalModuleNameRelative(name), false, name); + } +});