Skip to content

Export internal functions needed by dtsBundler.mjs #52941

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

Merged
merged 14 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ import {
NonNullExpression,
not,
noTruncationMaximumTruncationLength,
nullTransformationContext,
NumberLiteralType,
NumericLiteral,
objectAllocator,
Expand Down Expand Up @@ -2365,7 +2364,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function markAsSynthetic<T extends Node>(node: T): VisitResult<T> {
setTextRangePosEnd(node, -1, -1);
return visitEachChild(node, markAsSynthetic, nullTransformationContext);
return visitEachChild(node, markAsSynthetic, /*context*/ undefined);
}

function getEmitResolver(sourceFile: SourceFile, cancellationToken: CancellationToken) {
Expand Down Expand Up @@ -7032,7 +7031,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!nodeIsSynthesized(node) && getParseTreeNode(node) === node) {
return node;
}
return setTextRange(factory.cloneNode(visitEachChild(node, deepCloneOrReuseNode, nullTransformationContext, deepCloneOrReuseNodes)), node);
return setTextRange(factory.cloneNode(visitEachChild(node, deepCloneOrReuseNode, /*context*/ undefined, deepCloneOrReuseNodes)), node);
}

function deepCloneOrReuseNodes(
Expand Down Expand Up @@ -7826,7 +7825,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (context.tracker.canTrackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
trackComputedName(node.expression, context.enclosingDeclaration, context);
}
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, /*context*/ undefined, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
if (isBindingElement(visited)) {
visited = factory.updateBindingElement(
visited,
Expand Down Expand Up @@ -8663,7 +8662,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
setEmitFlags(node, EmitFlags.SingleLine);
}

return visitEachChild(node, visitExistingNodeTreeSymbols, nullTransformationContext);
return visitEachChild(node, visitExistingNodeTreeSymbols, /*context*/ undefined);

function getEffectiveDotDotDotForParameter(p: ParameterDeclaration) {
return p.dotDotDotToken || (p.type && isJSDocVariadicType(p.type) ? factory.createToken(SyntaxKind.DotDotDotToken) : undefined);
Expand Down
36 changes: 1 addition & 35 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
canHaveModifiers,
canProduceDiagnostics,
ClassDeclaration,
CommentRange,
compact,
concatenate,
ConditionalTypeNode,
Expand Down Expand Up @@ -63,8 +62,6 @@ import {
getExternalModuleImportEqualsDeclarationExpression,
getExternalModuleNameFromDeclaration,
getFirstConstructorWithBody,
getLeadingCommentRanges,
getLeadingCommentRangesOfNode,
getLineAndCharacterOfPosition,
getNameOfDeclaration,
getNormalizedAbsolutePath,
Expand All @@ -80,7 +77,6 @@ import {
GetSymbolAccessibilityDiagnostic,
getTextOfNode,
getThisParameter,
getTrailingCommentRanges,
hasDynamicName,
hasEffectiveModifier,
hasExtension,
Expand Down Expand Up @@ -126,6 +122,7 @@ import {
isImportEqualsDeclaration,
isIndexSignatureDeclaration,
isInterfaceDeclaration,
isInternalDeclaration,
isJsonSourceFile,
isLateVisibilityPaintedStatement,
isLiteralImportTypeNode,
Expand Down Expand Up @@ -158,7 +155,6 @@ import {
isVariableDeclaration,
isVarUsing,
JSDocFunctionType,
last,
LateBoundDeclaration,
LateVisibilityPaintedStatement,
length,
Expand Down Expand Up @@ -202,7 +198,6 @@ import {
setParent,
setTextRange,
SignatureDeclaration,
skipTrivia,
some,
SourceFile,
startsWith,
Expand Down Expand Up @@ -243,35 +238,6 @@ export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver
return result.diagnostics;
}

function hasInternalAnnotation(range: CommentRange, currentSourceFile: SourceFile) {
const comment = currentSourceFile.text.substring(range.pos, range.end);
return comment.includes("@internal");
}

/** @internal */
export function isInternalDeclaration(node: Node, currentSourceFile: SourceFile) {
const parseTreeNode = getParseTreeNode(node);
if (parseTreeNode && parseTreeNode.kind === SyntaxKind.Parameter) {
const paramIdx = (parseTreeNode.parent as SignatureDeclaration).parameters.indexOf(parseTreeNode as ParameterDeclaration);
const previousSibling = paramIdx > 0 ? (parseTreeNode.parent as SignatureDeclaration).parameters[paramIdx - 1] : undefined;
const text = currentSourceFile.text;
const commentRanges = previousSibling
? concatenate(
// to handle
// ... parameters, /** @internal */
// public param: string
getTrailingCommentRanges(text, skipTrivia(text, previousSibling.end + 1, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true)),
getLeadingCommentRanges(text, node.pos),
)
: getTrailingCommentRanges(text, skipTrivia(text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true));
return commentRanges && commentRanges.length && hasInternalAnnotation(last(commentRanges), currentSourceFile);
}
const leadingCommentRanges = parseTreeNode && getLeadingCommentRangesOfNode(parseTreeNode, currentSourceFile);
return !!forEach(leadingCommentRanges, range => {
return hasInternalAnnotation(range, currentSourceFile);
});
}

const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals |
NodeBuilderFlags.WriteClassExpressionAsTypeLiteral |
NodeBuilderFlags.UseTypeOfFunction |
Expand Down
9 changes: 4 additions & 5 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ import {
NodeCheckFlags,
NodeFlags,
nodeIsSynthesized,
nullTransformationContext,
NumericLiteral,
ObjectLiteralElementLike,
ObjectLiteralExpression,
Expand Down Expand Up @@ -1642,12 +1641,12 @@ export function transformES2015(context: TransformationContext): (x: SourceFile
case SyntaxKind.PropertyDeclaration: {
const named = node as AccessorDeclaration | MethodDeclaration | PropertyDeclaration;
if (isComputedPropertyName(named.name)) {
return factory.replacePropertyName(named, visitEachChild(named.name, elideUnusedThisCaptureWorker, nullTransformationContext));
return factory.replacePropertyName(named, visitEachChild(named.name, elideUnusedThisCaptureWorker, /*context*/ undefined));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uses of nullTransformationContext (and now undefined) in this file seem suspicious; all of these call sites have a context available and using it still passes tests. @rbuckton should I be "fixing" these?

}
return node;
}
}
return visitEachChild(node, elideUnusedThisCaptureWorker, nullTransformationContext);
return visitEachChild(node, elideUnusedThisCaptureWorker, /*context*/ undefined);
}

/**
Expand Down Expand Up @@ -1728,12 +1727,12 @@ export function transformES2015(context: TransformationContext): (x: SourceFile
case SyntaxKind.PropertyDeclaration: {
const named = node as AccessorDeclaration | MethodDeclaration | PropertyDeclaration;
if (isComputedPropertyName(named.name)) {
return factory.replacePropertyName(named, visitEachChild(named.name, injectSuperPresenceCheckWorker, nullTransformationContext));
return factory.replacePropertyName(named, visitEachChild(named.name, injectSuperPresenceCheckWorker, /*context*/ undefined));
}
return node;
}
}
return visitEachChild(node, injectSuperPresenceCheckWorker, nullTransformationContext);
return visitEachChild(node, injectSuperPresenceCheckWorker, /*context*/ undefined);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2432,7 +2432,6 @@ export const fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\
const fullTripleSlashAMDModuleRegEx = /^\/\/\/\s*<amd-module\s+.*?\/>/;
const defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)(('[^']*')|("[^"]*"))\s*\/>/;

/** @internal */
export function isPartOfTypeNode(node: Node): boolean {
if (SyntaxKind.FirstTypeNode <= node.kind && node.kind <= SyntaxKind.LastTypeNode) {
return true;
Expand Down
40 changes: 39 additions & 1 deletion src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import {
ClassLikeDeclaration,
ClassStaticBlockDeclaration,
combinePaths,
CommentRange,
compareDiagnostics,
CompilerOptions,
concatenate,
ConciseBody,
ConstructorDeclaration,
ConstructorTypeNode,
Expand Down Expand Up @@ -62,6 +64,7 @@ import {
filter,
find,
flatMap,
forEach,
ForInitializer,
ForInOrOfStatement,
FunctionBody,
Expand All @@ -81,6 +84,10 @@ import {
getJSDocCommentsAndTags,
getJSDocRoot,
getJSDocTypeParameterDeclarations,
getLeadingCommentRanges,
getLeadingCommentRangesOfNode,
getSourceFileOfNode,
getTrailingCommentRanges,
hasAccessorModifier,
HasDecorators,
hasDecorators,
Expand Down Expand Up @@ -204,6 +211,7 @@ import {
JsxTagNameExpression,
KeywordSyntaxKind,
LabeledStatement,
last,
lastOrUndefined,
LeftHandSideExpression,
length,
Expand Down Expand Up @@ -258,9 +266,11 @@ import {
setUILocale,
SignatureDeclaration,
skipOuterExpressions,
skipTrivia,
some,
sortAndDeduplicate,
SortedReadonlyArray,
SourceFile,
Statement,
StringLiteral,
StringLiteralLike,
Expand Down Expand Up @@ -2376,7 +2386,6 @@ export function isDeclaration(node: Node): node is NamedDeclaration {
return isDeclarationKind(node.kind);
}

/** @internal */
export function isDeclarationStatement(node: Node): node is DeclarationStatement {
return isDeclarationStatementKind(node.kind);
}
Expand Down Expand Up @@ -2606,3 +2615,32 @@ export function isRestParameter(node: ParameterDeclaration | JSDocParameterTag):
const type = isJSDocParameterTag(node) ? (node.typeExpression && node.typeExpression.type) : node.type;
return (node as ParameterDeclaration).dotDotDotToken !== undefined || !!type && type.kind === SyntaxKind.JSDocVariadicType;
}

function hasInternalAnnotation(range: CommentRange, sourceFile: SourceFile) {
const comment = sourceFile.text.substring(range.pos, range.end);
return comment.includes("@internal");
}

export function isInternalDeclaration(node: Node, sourceFile?: SourceFile) {
sourceFile ??= getSourceFileOfNode(node);
const parseTreeNode = getParseTreeNode(node);
if (parseTreeNode && parseTreeNode.kind === SyntaxKind.Parameter) {
const paramIdx = (parseTreeNode.parent as SignatureDeclaration).parameters.indexOf(parseTreeNode as ParameterDeclaration);
const previousSibling = paramIdx > 0 ? (parseTreeNode.parent as SignatureDeclaration).parameters[paramIdx - 1] : undefined;
const text = sourceFile.text;
const commentRanges = previousSibling
? concatenate(
// to handle
// ... parameters, /** @internal */
// public param: string
getTrailingCommentRanges(text, skipTrivia(text, previousSibling.end + 1, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true)),
getLeadingCommentRanges(text, node.pos),
)
: getTrailingCommentRanges(text, skipTrivia(text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true));
return some(commentRanges) && hasInternalAnnotation(last(commentRanges), sourceFile);
}
const leadingCommentRanges = parseTreeNode && getLeadingCommentRangesOfNode(parseTreeNode, sourceFile);
return !!forEach(leadingCommentRanges, range => {
return hasInternalAnnotation(range, sourceFile!);
});
}
11 changes: 6 additions & 5 deletions src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import {
NodeArray,
NodesVisitor,
NodeVisitor,
nullTransformationContext,
ParameterDeclaration,
ScriptTarget,
setEmitFlags,
Expand Down Expand Up @@ -580,20 +581,20 @@ export function visitCommaListElements(elements: NodeArray<Expression>, visitor:
* @param visitor The callback used to visit each child.
* @param context A lexical environment context for the visitor.
*/
export function visitEachChild<T extends Node>(node: T, visitor: Visitor, context: TransformationContext): T;
export function visitEachChild<T extends Node>(node: T, visitor: Visitor, context: TransformationContext | undefined): T;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, debating whether or not visitLexicalEnvironment, visitParameterList, visitFunctionBody, and visitIterationBody need this change. Those are pretty explicitly about working with the context and within a transform, though, so I'm leaning towards no.

https://github.com/search?q=%2Fts%5C.%28visitLexicalEnvironment%7CvisitParameterList%7CvisitFunctionBody%7CvisitIterationBody%29%2F+%28language%3ATypeScript+OR+language%3AJavaScript%29&type=code

/** @internal */
export function visitEachChild<T extends Node>(node: T, visitor: Visitor, context: TransformationContext, nodesVisitor?: NodesVisitor, tokenVisitor?: Visitor, nodeVisitor?: NodeVisitor): T; // eslint-disable-line @typescript-eslint/unified-signatures
export function visitEachChild<T extends Node>(node: T, visitor: Visitor, context: TransformationContext | undefined, nodesVisitor?: NodesVisitor, tokenVisitor?: Visitor, nodeVisitor?: NodeVisitor): T; // eslint-disable-line @typescript-eslint/unified-signatures
/**
* Visits each child of a Node using the supplied visitor, possibly returning a new Node of the same kind in its place.
*
* @param node The Node whose children will be visited.
* @param visitor The callback used to visit each child.
* @param context A lexical environment context for the visitor.
*/
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext, nodesVisitor?: typeof visitNodes, tokenVisitor?: Visitor): T | undefined;
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext | undefined, nodesVisitor?: typeof visitNodes, tokenVisitor?: Visitor): T | undefined;
/** @internal */
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext, nodesVisitor?: NodesVisitor, tokenVisitor?: Visitor, nodeVisitor?: NodeVisitor): T | undefined;
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext, nodesVisitor = visitNodes, tokenVisitor?: Visitor, nodeVisitor: NodeVisitor = visitNode): T | undefined {
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext | undefined, nodesVisitor?: NodesVisitor, tokenVisitor?: Visitor, nodeVisitor?: NodeVisitor): T | undefined;
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context = nullTransformationContext, nodesVisitor = visitNodes, tokenVisitor?: Visitor, nodeVisitor: NodeVisitor = visitNode): T | undefined {
if (node === undefined) {
return undefined;
}
Expand Down
3 changes: 1 addition & 2 deletions src/services/codefixes/annotateWithTypeFromJSDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
last,
map,
Node,
nullTransformationContext,
ParameterDeclaration,
PropertyDeclaration,
PropertySignature,
Expand Down Expand Up @@ -144,7 +143,7 @@ function transformJSDocType(node: Node): Node {
case SyntaxKind.JSDocTypeLiteral:
return transformJSDocTypeLiteral(node as JSDocTypeLiteral);
default:
const visited = visitEachChild(node, transformJSDocType, nullTransformationContext);
const visited = visitEachChild(node, transformJSDocType, /*context*/ undefined);
setEmitFlags(visited, EmitFlags.SingleLine);
return visited;
}
Expand Down
3 changes: 1 addition & 2 deletions src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import {
NodeArray,
NodeBuilderFlags,
NodeFlags,
nullTransformationContext,
ObjectFlags,
ObjectLiteralExpression,
ObjectType,
Expand Down Expand Up @@ -913,7 +912,7 @@ export function tryGetAutoImportableReferenceFromTypeNode(importTypeNode: TypeNo
const typeArguments = visitNodes(node.typeArguments, visit, isTypeNode);
return factory.createTypeReferenceNode(qualifier, typeArguments);
}
return visitEachChild(node, visit, nullTransformationContext);
return visitEachChild(node, visit, /*context*/ undefined);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ import {
Node,
NodeBuilderFlags,
NodeFlags,
nullTransformationContext,
ObjectLiteralElementLike,
ParameterDeclaration,
positionIsSynthesized,
Expand Down Expand Up @@ -1648,7 +1647,7 @@ function transformFunctionBody(body: Node, exposedVariableDeclarations: readonly
const oldIgnoreReturns = ignoreReturns;
ignoreReturns = ignoreReturns || isFunctionLikeDeclaration(node) || isClassLike(node);
const substitution = substitutions.get(getNodeId(node).toString());
const result = substitution ? getSynthesizedDeepClone(substitution) : visitEachChild(node, visitor, nullTransformationContext);
const result = substitution ? getSynthesizedDeepClone(substitution) : visitEachChild(node, visitor, /*context*/ undefined);
ignoreReturns = oldIgnoreReturns;
return result;
}
Expand All @@ -1662,7 +1661,7 @@ function transformConstantInitializer(initializer: Expression, substitutions: Re

function visitor(node: Node): VisitResult<Node> {
const substitution = substitutions.get(getNodeId(node).toString());
return substitution ? getSynthesizedDeepClone(substitution) : visitEachChild(node, visitor, nullTransformationContext);
return substitution ? getSynthesizedDeepClone(substitution) : visitEachChild(node, visitor, /*context*/ undefined);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ import {
normalizePath,
NoSubstitutionTemplateLiteral,
notImplemented,
nullTransformationContext,
NumericLiteral,
or,
OrganizeImports,
Expand Down Expand Up @@ -3175,7 +3174,7 @@ function getSynthesizedDeepCloneWorker<T extends Node>(node: T, replaceNode?: (n
const nodesClone: <T extends Node>(ns: NodeArray<T> | undefined) => NodeArray<T> | undefined = replaceNode
? ns => ns && getSynthesizedDeepClonesWithReplacements(ns, /*includeTrivia*/ true, replaceNode)
: ns => ns && getSynthesizedDeepClones(ns);
const visited = visitEachChild(node, nodeClone, nullTransformationContext, nodesClone, nodeClone);
const visited = visitEachChild(node, nodeClone, /*context*/ undefined, nodesClone, nodeClone);

if (visited === node) {
// This only happens for leaf nodes - internal nodes always see their children change.
Expand Down
Loading