diff --git a/src/compiler/_namespaces/ts.ts b/src/compiler/_namespaces/ts.ts index 60de9f45379df..14eb95213c884 100644 --- a/src/compiler/_namespaces/ts.ts +++ b/src/compiler/_namespaces/ts.ts @@ -60,6 +60,7 @@ export * from "../transformers/declarations/diagnostics"; export * from "../transformers/declarations/emitBinder"; export * from "../transformers/declarations/emitResolver"; export * from "../transformers/declarations/transpileDeclaration"; +export * from "../transformers/declarations/localInferenceResolver"; export * from "../transformers/declarations/types"; export * from "../transformers/declarations"; export * from "../transformer"; diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index dabb706b5ec3d..acab390d74e7a 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -28,6 +28,7 @@ import { createEmptyExports, createGetSymbolAccessibilityDiagnosticForNode, createGetSymbolAccessibilityDiagnosticForNodeName, + createLocalInferenceResolver, createSymbolTable, createUnparsedSourceFile, Debug, @@ -165,6 +166,7 @@ import { LateBoundDeclaration, LateVisibilityPaintedStatement, length, + LocalInferenceResolver, map, mapDefined, MethodDeclaration, @@ -238,10 +240,6 @@ import { VisitResult, } from "../_namespaces/ts"; import * as moduleSpecifiers from "../_namespaces/ts.moduleSpecifiers"; -import { - createLocalInferenceResolver, - LocalInferenceResolver, -} from "./declarations/localInferenceResolver"; /** @internal */ export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] | undefined { diff --git a/src/compiler/transformers/declarations/emitBinder.ts b/src/compiler/transformers/declarations/emitBinder.ts index 7cf3bb0119189..a0457b1070396 100644 --- a/src/compiler/transformers/declarations/emitBinder.ts +++ b/src/compiler/transformers/declarations/emitBinder.ts @@ -1,10 +1,29 @@ import { + __String, + ArrayBindingElement, + BinaryExpression, + BindingPattern, + ClassDeclaration, + ClassElement, + ComputedPropertyName, + Debug, + Declaration, + EnumDeclaration, + EnumMember, + Expression, factory, + findAncestor, forEachChild, + FunctionDeclaration, getModuleInstanceState, getNodeId, + hasAmbientModifier, + hasSyntacticModifier, + InterfaceDeclaration, isBinaryExpression, + isBindingPattern, isBlock, + isBlockScopedContainerTopLevel, isClassDeclaration, isComputedPropertyName, isConditionalTypeNode, @@ -12,12 +31,16 @@ import { isConstructSignatureDeclaration, isDoStatement, isElementAccessExpression, + isEnumConst, isEnumDeclaration, isExportAssignment, isExportDeclaration, + isExpression, isExpressionStatement, + isForInOrOfStatement, isForStatement, isFunctionDeclaration, + isFunctionExpressionOrArrowFunction, isIdentifier, isIfStatement, isImportDeclaration, @@ -34,68 +57,35 @@ import { isPrefixUnaryExpression, isPrivateIdentifier, isPropertyAccessExpression, + isPropertyName, isSourceFile, + isStringLiteralLike, isTypeAliasDeclaration, + isVarConst, isVariableDeclaration, isVariableDeclarationList, isVariableStatement, isWhileStatement, - ModuleInstanceState, - Symbol, -} from "../../_namespaces/ts"; -import { - Debug, -} from "../../debug"; -import { - __String, - ArrayBindingElement, - BinaryExpression, - BindingPattern, - ClassDeclaration, - ClassElement, - ComputedPropertyName, - Declaration, - EnumDeclaration, - EnumMember, - Expression, - FunctionDeclaration, - InterfaceDeclaration, + MemberKey, ModifierFlags, ModuleDeclaration, + ModuleInstanceState, Node, NodeArray, NoSubstitutionTemplateLiteral, ObjectLiteralElement, ParameterDeclaration, PropertyName, + setValueDeclaration, SourceFile, + Symbol, SymbolFlags, SyntaxKind, TypeAliasDeclaration, TypeElement, TypeParameterDeclaration, VariableDeclaration, -} from "../../types"; -import { - hasAmbientModifier, - hasSyntacticModifier, - isBlockScopedContainerTopLevel, - isEnumConst, - isFunctionExpressionOrArrowFunction, - isVarConst, - setValueDeclaration, -} from "../../utilities"; -import { - findAncestor, - isBindingPattern, - isExpression, - isForInOrOfStatement, - isPropertyName, - isStringLiteralLike, -} from "../../utilitiesPublic"; -import { - MemberKey, -} from "./types"; +} from "../../_namespaces/ts"; /** @internal */ export interface EmitDeclarationNodeLinks { @@ -121,46 +111,56 @@ export interface EmitDeclarationSymbol { exports?: EmitDeclarationSymbolTable; } -type SymbolRegistrationFlags = readonly [flags: SymbolFlags, forbiddenFlags: SymbolFlags]; +interface SymbolRegistrationFlags { + includes: SymbolFlags; + excludes: SymbolFlags; +} const syntaxKindToSymbolMap = { - [SyntaxKind.TypeParameter]: [SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes], - [SyntaxKind.Parameter]: [SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes], - [SyntaxKind.VariableDeclaration]: [SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes], - [SyntaxKind.BindingElement]: [SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes], - [SyntaxKind.PropertyDeclaration]: [SymbolFlags.Property, SymbolFlags.PropertyExcludes], - [SyntaxKind.PropertySignature]: [SymbolFlags.Property, SymbolFlags.PropertyExcludes], - [SyntaxKind.PropertyAssignment]: [SymbolFlags.Property, SymbolFlags.PropertyExcludes], - [SyntaxKind.ShorthandPropertyAssignment]: [SymbolFlags.Property, SymbolFlags.PropertyExcludes], - [SyntaxKind.EnumMember]: [SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes], - [SyntaxKind.CallSignature]: [SymbolFlags.Signature, SymbolFlags.None], - [SyntaxKind.ConstructSignature]: [SymbolFlags.Signature, SymbolFlags.None], - [SyntaxKind.IndexSignature]: [SymbolFlags.Signature, SymbolFlags.None], - [SyntaxKind.MethodDeclaration]: [SymbolFlags.Method, SymbolFlags.MethodExcludes], - [SyntaxKind.MethodSignature]: [SymbolFlags.Method, SymbolFlags.MethodExcludes], - [SyntaxKind.FunctionDeclaration]: [SymbolFlags.Function, SymbolFlags.FunctionExcludes], - [SyntaxKind.Constructor]: [SymbolFlags.Constructor, SymbolFlags.None], - [SyntaxKind.GetAccessor]: [SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes], - [SyntaxKind.SetAccessor]: [SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes], - [SyntaxKind.ClassExpression]: [SymbolFlags.Class, SymbolFlags.ClassExcludes], - [SyntaxKind.ClassDeclaration]: [SymbolFlags.Class, SymbolFlags.ClassExcludes], - [SyntaxKind.InterfaceDeclaration]: [SymbolFlags.Interface, SymbolFlags.InterfaceExcludes], - [SyntaxKind.TypeAliasDeclaration]: [SymbolFlags.TypeAlias, SymbolFlags.TypeAliasExcludes], + [SyntaxKind.TypeParameter]: { includes: SymbolFlags.TypeParameter, excludes: SymbolFlags.TypeParameterExcludes }, + [SyntaxKind.Parameter]: { includes: SymbolFlags.FunctionScopedVariable, excludes: SymbolFlags.ParameterExcludes }, + [SyntaxKind.VariableDeclaration]: { includes: SymbolFlags.BlockScopedVariable, excludes: SymbolFlags.BlockScopedVariableExcludes }, + [SyntaxKind.BindingElement]: { includes: SymbolFlags.BlockScopedVariable, excludes: SymbolFlags.BlockScopedVariableExcludes }, + [SyntaxKind.PropertyDeclaration]: { includes: SymbolFlags.Property, excludes: SymbolFlags.PropertyExcludes }, + [SyntaxKind.PropertySignature]: { includes: SymbolFlags.Property, excludes: SymbolFlags.PropertyExcludes }, + [SyntaxKind.PropertyAssignment]: { includes: SymbolFlags.Property, excludes: SymbolFlags.PropertyExcludes }, + [SyntaxKind.ShorthandPropertyAssignment]: { includes: SymbolFlags.Property, excludes: SymbolFlags.PropertyExcludes }, + [SyntaxKind.EnumMember]: { includes: SymbolFlags.EnumMember, excludes: SymbolFlags.EnumMemberExcludes }, + [SyntaxKind.CallSignature]: { includes: SymbolFlags.Signature, excludes: SymbolFlags.None }, + [SyntaxKind.ConstructSignature]: { includes: SymbolFlags.Signature, excludes: SymbolFlags.None }, + [SyntaxKind.IndexSignature]: { includes: SymbolFlags.Signature, excludes: SymbolFlags.None }, + [SyntaxKind.MethodDeclaration]: { includes: SymbolFlags.Method, excludes: SymbolFlags.MethodExcludes }, + [SyntaxKind.MethodSignature]: { includes: SymbolFlags.Method, excludes: SymbolFlags.MethodExcludes }, + [SyntaxKind.FunctionDeclaration]: { includes: SymbolFlags.Function, excludes: SymbolFlags.FunctionExcludes }, + [SyntaxKind.Constructor]: { includes: SymbolFlags.Constructor, excludes: SymbolFlags.None }, + [SyntaxKind.GetAccessor]: { includes: SymbolFlags.GetAccessor, excludes: SymbolFlags.GetAccessorExcludes }, + [SyntaxKind.SetAccessor]: { includes: SymbolFlags.SetAccessor, excludes: SymbolFlags.SetAccessorExcludes }, + [SyntaxKind.ClassExpression]: { includes: SymbolFlags.Class, excludes: SymbolFlags.ClassExcludes }, + [SyntaxKind.ClassDeclaration]: { includes: SymbolFlags.Class, excludes: SymbolFlags.ClassExcludes }, + [SyntaxKind.InterfaceDeclaration]: { includes: SymbolFlags.Interface, excludes: SymbolFlags.InterfaceExcludes }, + [SyntaxKind.TypeAliasDeclaration]: { includes: SymbolFlags.TypeAlias, excludes: SymbolFlags.TypeAliasExcludes }, [SyntaxKind.EnumDeclaration]: { - const: [SymbolFlags.ConstEnum, SymbolFlags.ConstEnumExcludes], - regular: [SymbolFlags.RegularEnum, SymbolFlags.RegularEnumExcludes], + const: { includes: SymbolFlags.ConstEnum, excludes: SymbolFlags.ConstEnumExcludes }, + regular: { includes: SymbolFlags.RegularEnum, excludes: SymbolFlags.RegularEnumExcludes }, }, [SyntaxKind.ModuleDeclaration]: { - value: [SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes], - namespace: [SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes], + value: { includes: SymbolFlags.ValueModule, excludes: SymbolFlags.ValueModuleExcludes }, + namespace: { includes: SymbolFlags.NamespaceModule, excludes: SymbolFlags.NamespaceModuleExcludes }, }, - [SyntaxKind.ImportEqualsDeclaration]: [SymbolFlags.Alias, SymbolFlags.AliasExcludes], - [SyntaxKind.NamespaceImport]: [SymbolFlags.Alias, SymbolFlags.AliasExcludes], - [SyntaxKind.ImportSpecifier]: [SymbolFlags.Alias, SymbolFlags.AliasExcludes], - [SyntaxKind.ExportSpecifier]: [SymbolFlags.Alias | SymbolFlags.ExportValue, SymbolFlags.AliasExcludes], - [SyntaxKind.NamespaceExportDeclaration]: [SymbolFlags.Alias, SymbolFlags.AliasExcludes], - [SyntaxKind.ImportClause]: [SymbolFlags.Alias, SymbolFlags.AliasExcludes], + [SyntaxKind.ImportEqualsDeclaration]: { includes: SymbolFlags.Alias, excludes: SymbolFlags.AliasExcludes }, + [SyntaxKind.NamespaceImport]: { includes: SymbolFlags.Alias, excludes: SymbolFlags.AliasExcludes }, + [SyntaxKind.ImportSpecifier]: { includes: SymbolFlags.Alias, excludes: SymbolFlags.AliasExcludes }, + [SyntaxKind.ExportSpecifier]: { includes: SymbolFlags.Alias | SymbolFlags.ExportValue, excludes: SymbolFlags.AliasExcludes }, + [SyntaxKind.NamespaceExportDeclaration]: { includes: SymbolFlags.Alias, excludes: SymbolFlags.AliasExcludes }, + [SyntaxKind.ImportClause]: { includes: SymbolFlags.Alias, excludes: SymbolFlags.AliasExcludes }, } as const satisfies Partial>>; +/** + * Assigning values to a property of a function will usually cause those members to be implicitly declared on the function + * even if they were were not declared (expando functions) + * DTE needs to detect these members and error on them since this behavior is not supported in isolated declarations + * There are however members that can be assigned on a function that are not expando members, namely members that come from Function + * In DTE we do not load the full d.ts so we keep a list of known members of function that can be assigned without considering them expando members. + */ const knownFunctionMembers = new Set([ "I:apply", "I:call", @@ -262,18 +262,20 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) { } function bind() { - let currentScope: Node = undefined!; - let currentSymbol: EmitDeclarationSymbol = undefined!; - let currentLocalSymbolTable: EmitDeclarationSymbolTable = undefined!; - let currentExportsSymbolTable: EmitDeclarationSymbolTable | undefined; - const postBindingAction: (() => void)[] = []; + /* eslint-disable no-var */ + var currentScope: Node = undefined!; + var currentSymbol: EmitDeclarationSymbol = undefined!; + var currentLocalSymbolTable: EmitDeclarationSymbolTable = undefined!; + var currentExportsSymbolTable: EmitDeclarationSymbolTable | undefined; + var postBindingAction: (() => void)[] = []; - const fileLinks = getNodeLinks(file).symbol = newSymbol(); + var fileLinks = getNodeLinks(file).symbol = createEmitSymbol(); + /* eslint-enable no-var */ fileLinks.exports = new Map(); withScope(file, fileLinks.exports, () => bindEachFunctionsFirst(file.statements)); postBindingAction.forEach(fn => fn()); - function newSymbol(): EmitDeclarationSymbol { + function createEmitSymbol(): EmitDeclarationSymbol { return { declarations: [], flags: 0, @@ -282,25 +284,25 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) { function getSymbol(table: EmitDeclarationSymbolTable, name: MemberKey) { let symbol = table.get(name); if (!symbol) { - symbol = newSymbol(); + symbol = createEmitSymbol(); symbol.name = name; table.set(name, symbol); } return symbol; } - function addLocalAndExportDeclaration(name: LocalAndExportName | MemberKey | undefined, node: Declaration, [flags, forbiddenFlags]: SymbolRegistrationFlags, isExport: boolean) { + function addLocalAndExportDeclaration(name: LocalAndExportName | MemberKey | undefined, node: Declaration, flags: SymbolRegistrationFlags, isExport: boolean) { const { exportName, localName } = typeof name === "object" ? name : { exportName: name, localName: name }; if (isExport) { - const exportKind = flags & SymbolFlags.Value ? SymbolFlags.ExportValue : 0; - const localSymbol = addLocalOnlyDeclaration(localName, node, [exportKind, forbiddenFlags]); - const exportSymbol = addExportOnlyDeclaration(exportName, node, [flags, forbiddenFlags]); + const exportKind = flags.includes & SymbolFlags.Value ? SymbolFlags.ExportValue : 0; + const localSymbol = addLocalOnlyDeclaration(localName, node, { includes: exportKind, excludes: flags.excludes }); + const exportSymbol = addExportOnlyDeclaration(exportName, node, flags); localSymbol.exportSymbol = exportSymbol; // Export symbol can be undefined if the export modifier was placed in an unexpected position. // We just assume the local symbol should be used. There are already bigger issues in the file anyway. return exportSymbol ?? localSymbol; } else { - return addLocalOnlyDeclaration(localName, node, [flags, forbiddenFlags]); + return addLocalOnlyDeclaration(localName, node, flags); } } function addExportOnlyDeclaration(name: MemberKey | undefined, node: Declaration, flagsAndForbiddenFlags: SymbolRegistrationFlags) { @@ -313,13 +315,13 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) { return addDeclaration(currentLocalSymbolTable, name, node, flagsAndForbiddenFlags); } - function addDeclaration(table: EmitDeclarationSymbolTable, name: MemberKey | undefined, node: Declaration, [includes, excludes]: SymbolRegistrationFlags) { - let symbol = name !== undefined ? getSymbol(table, name) : newSymbol(); + function addDeclaration(table: EmitDeclarationSymbolTable, name: MemberKey | undefined, node: Declaration, { includes, excludes }: SymbolRegistrationFlags) { + let symbol = name !== undefined ? getSymbol(table, name) : createEmitSymbol(); // Symbols don't merge, create new one if (excludes & symbol.flags) { // Variables and expando members from assignments are always allowed to merge if (!(includes & SymbolFlags.Variable && symbol.flags & SymbolFlags.Assignment)) { - symbol = newSymbol(); + symbol = createEmitSymbol(); } } symbol.declarations.push(node); @@ -486,8 +488,11 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) { target.exportSymbol.exports = target.exports; } withScope(fn, target.exports, () => { - const [include, excludes] = syntaxKindToSymbolMap[SyntaxKind.PropertyDeclaration]; - addExportOnlyDeclaration(key, assignmentTarget, [include | SymbolFlags.Assignment, excludes & ~SymbolFlags.Assignment]); + const { includes, excludes } = syntaxKindToSymbolMap[SyntaxKind.PropertyDeclaration]; + addExportOnlyDeclaration(key, assignmentTarget, { + includes: includes | SymbolFlags.Assignment, + excludes: excludes & ~SymbolFlags.Assignment, + }); }); } } @@ -590,8 +595,11 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) { // TODO is currentExportsSymbolTable ok here? withScope(node, /*exports*/ undefined, () => { elements.forEach(e => { - const [flags, forbiddenFlags] = getSymbolFlagsForNode(e); - addLocalOnlyDeclaration(getMemberKey(e.propertyName ?? e.name), e, [flags | SymbolFlags.ExportValue, forbiddenFlags]); + const { includes, excludes } = getSymbolFlagsForNode(e); + addLocalOnlyDeclaration(getMemberKey(e.propertyName ?? e.name), e, { + includes: includes | SymbolFlags.ExportValue, + excludes, + }); }); }); } diff --git a/src/compiler/transformers/declarations/emitResolver.ts b/src/compiler/transformers/declarations/emitResolver.ts index a81d7e0dbede2..dd67d370b47a6 100644 --- a/src/compiler/transformers/declarations/emitResolver.ts +++ b/src/compiler/transformers/declarations/emitResolver.ts @@ -1,5 +1,6 @@ import { appendIfUnique, + bindSourceFileForDeclarationEmit, ComputedPropertyName, createEvaluator, Debug, @@ -7,6 +8,8 @@ import { DeclarationName, determineIfDeclarationIsVisible, ElementAccessExpression, + EmitDeclarationNodeLinks, + EmitDeclarationSymbol, emptyArray, EntityNameOrEntityNameExpression, EnumDeclaration, @@ -21,6 +24,7 @@ import { FunctionLikeDeclaration, getAnyImportSyntax, getFirstIdentifier, + getMemberKey, getNameOfDeclaration, getParseTreeNode, getTextOfNode, @@ -46,6 +50,7 @@ import { isInJSFile, isLateVisibilityPaintedStatement, isNumericLiteral, + IsolatedEmitResolver, isPartOfTypeNode, isPrefixUnaryExpression, isPropertyAccessExpression, @@ -78,15 +83,8 @@ import { SyntaxKind, VariableDeclaration, } from "../../_namespaces/ts"; -import { - bindSourceFileForDeclarationEmit, - EmitDeclarationNodeLinks, - EmitDeclarationSymbol, - getMemberKey, -} from "./emitBinder"; -import { - IsolatedEmitResolver, -} from "./types"; + + /** @internal */ export function createEmitDeclarationResolver(file: SourceFile): IsolatedEmitResolver { diff --git a/src/compiler/transformers/declarations/localInferenceResolver.ts b/src/compiler/transformers/declarations/localInferenceResolver.ts index 7839c95fcb62b..24564df6bca77 100644 --- a/src/compiler/transformers/declarations/localInferenceResolver.ts +++ b/src/compiler/transformers/declarations/localInferenceResolver.ts @@ -1,14 +1,26 @@ import { + ArrayLiteralExpression, + ArrowFunction, + AsExpression, + ClassExpression, + createDiagnosticForNode, + createPropertyNameNodeForIdentifierOrLiteral, + DiagnosticMessage, + Diagnostics, + EntityNameOrEntityNameExpression, + ExportAssignment, + FunctionExpression, + GetAccessorDeclaration, getCommentRange, + getEmitScriptTarget, getMemberKeyFromElement, - setCommentRange, -} from "../../_namespaces/ts"; -import { - Diagnostics, -} from "../../diagnosticInformationMap.generated"; -import { + HasInferredType, + hasSyntacticModifier, + Identifier, isClassExpression, isComputedPropertyName, + isConstTypeReference, + isEntityNameExpression, isExportAssignment, isGetAccessorDeclaration, isIdentifier, @@ -18,40 +30,26 @@ import { isNoSubstitutionTemplateLiteral, isNumericLiteral, isOmittedExpression, + isOptionalDeclaration, isParameter, isPrefixUnaryExpression, isPrivateIdentifier, isPropertyAssignment, isPropertyDeclaration, + isPropertyName, isSetAccessorDeclaration, isShorthandPropertyAssignment, isSpreadAssignment, isSpreadElement, + isStringDoubleQuoted, isStringLiteral, + isStringLiteralLike, isTypeLiteralNode, + isTypeNode, isTypeParameterDeclaration, isTypeReferenceNode, isUnionTypeNode, isVariableDeclaration, -} from "../../factory/nodeTests"; -import { - setTextRange, -} from "../../factory/utilitiesPublic"; -import { - nullTransformationContext, -} from "../../transformer"; -import { - ArrayLiteralExpression, - ArrowFunction, - AsExpression, - ClassExpression, - DiagnosticMessage, - EntityNameOrEntityNameExpression, - ExportAssignment, - FunctionExpression, - GetAccessorDeclaration, - HasInferredType, - Identifier, KeywordTypeSyntaxKind, LiteralExpression, MethodDeclaration, @@ -59,42 +57,28 @@ import { Node, NodeArray, NodeFlags, + nullTransformationContext, ObjectLiteralExpression, ParameterDeclaration, ParenthesizedExpression, PrefixUnaryExpression, PropertyName, SetAccessorDeclaration, + setCommentRange, + setTextRange, SourceFile, SyntaxKind, TransformationContext, TypeAssertion, TypeElement, TypeNode, - Visitor, - VisitResult, -} from "../../types"; -import { - createDiagnosticForNode, - createPropertyNameNodeForIdentifierOrLiteral, - getEmitScriptTarget, - hasSyntacticModifier, - isEntityNameExpression, - isOptionalDeclaration, - isStringDoubleQuoted, -} from "../../utilities"; -import { - isConstTypeReference, - isPropertyName, - isStringLiteralLike, - isTypeNode, unescapeLeadingUnderscores, -} from "../../utilitiesPublic"; -import { visitEachChild, visitNode, visitNodes, -} from "../../visitorPublic"; + Visitor, + VisitResult, +} from "../../_namespaces/ts"; enum NarrowBehavior { None = 0, @@ -115,6 +99,9 @@ export interface LocalInferenceResolver { makeInvalidType(): Node; fromInitializer(node: HasInferredType | ExportAssignment, type: TypeNode | undefined, sourceFile: SourceFile): TypeNode; } +/** + * @internal + */ export function createLocalInferenceResolver({ setEnclosingDeclarations, visitDeclarationSubtree, @@ -696,15 +683,15 @@ export function createLocalInferenceResolver({ * function x(o = "", v: string) */ if (node.initializer && !isOptional) { - localType.typeNode = addUndefinedInUnion(localType.typeNode); - } + localType.typeNode = addUndefinedInUnion(localType.typeNode); + } /** * Constructor properties that are optional must have | undefined included to work well with exactOptionalPropertyTypes * constructor(public x?: number) -> x?: number | undefined */ if (isOptional && !node.initializer && hasSyntacticModifier(node, ModifierFlags.ParameterPropertyModifier)) { localType.typeNode = addUndefinedInUnion(localType.typeNode); - } + } } } else if (type) { @@ -732,7 +719,7 @@ export function createLocalInferenceResolver({ localType = localInference(node.initializer); if (isOptionalDeclaration(node)) { localType.typeNode = addUndefinedInUnion(localType.typeNode); - } + } } else if (isInterfaceDeclaration(node.parent) || isTypeLiteralNode(node.parent)) { return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword); diff --git a/src/compiler/transformers/declarations/transpileDeclaration.ts b/src/compiler/transformers/declarations/transpileDeclaration.ts index 4d28982e4d9b8..f1a8961ecb34d 100644 --- a/src/compiler/transformers/declarations/transpileDeclaration.ts +++ b/src/compiler/transformers/declarations/transpileDeclaration.ts @@ -40,7 +40,10 @@ function createEmitDeclarationHost(options: TranspileDeclarationsOptions): EmitH useCaseSensitiveFileNames: () => !!options.useCaseSensitiveFileNames, getCompilerOptions: () => options.compilerOptions, getCommonSourceDirectory: () => ensureTrailingDirectorySeparator(options.commonSourceDirectory ?? "."), - redirectTargetsMap: undefined!, // new Map(), + get redirectTargetsMap(): never { + Debug.fail("redirectTargetsMap should not be used in isolated declarations"); + return undefined!; // Need return despite fail call GH#52214 + }, directoryExists: throws, fileExists: throws, readFile: throws, @@ -86,7 +89,7 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T addDiagnostic(diag: any) { diagnostics.push(diag); }, - } as Partial as TransformationContext); + } as TransformationContext); const result = transformer(sourceFile); const printer = createPrinter({ diff --git a/src/harness/_namespaces/fixer.ts b/src/harness/_namespaces/fixer.ts new file mode 100644 index 0000000000000..c5b7f26dc68f3 --- /dev/null +++ b/src/harness/_namespaces/fixer.ts @@ -0,0 +1,3 @@ +/* Generated file to emulate the fixer namespace. */ + +export * from "../isolatedDeclarationFixer"; diff --git a/src/harness/isolatedDeclarationFixer.ts b/src/harness/isolatedDeclarationFixer.ts index 936086ada33d3..0bc55da2e4d22 100644 --- a/src/harness/isolatedDeclarationFixer.ts +++ b/src/harness/isolatedDeclarationFixer.ts @@ -1,6 +1,6 @@ +import * as fake from "./_namespaces/fakes"; import * as ts from "./_namespaces/ts"; import * as vfs from "./_namespaces/vfs"; -import * as fake from "./fakesHosts"; export const isolatedDeclarationsErrors = new Set([ ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.code, diff --git a/src/testRunner/_namespaces/fixer.ts b/src/testRunner/_namespaces/fixer.ts new file mode 100644 index 0000000000000..eecd8b75acc09 --- /dev/null +++ b/src/testRunner/_namespaces/fixer.ts @@ -0,0 +1,3 @@ +/* Generated file to emulate the fixer namespace. */ + +export * from "../../harness/_namespaces/fixer"; diff --git a/src/testRunner/compilerRunner.ts b/src/testRunner/compilerRunner.ts index 13ded5da538dc..f94cb962a8105 100644 --- a/src/testRunner/compilerRunner.ts +++ b/src/testRunner/compilerRunner.ts @@ -1,7 +1,7 @@ +import * as compiler from "./_namespaces/compiler"; import { fixTestFiles, -} from "../harness/isolatedDeclarationFixer"; -import * as compiler from "./_namespaces/compiler"; +} from "./_namespaces/fixer"; import { Baseline, Compiler,