Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(41825): JSDoc equivalent of import * #57207

Merged
merged 32 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
953e54b
feat(41825): add importType jsdoc tag
a-tarasyuk Jan 28, 2024
b67cba6
add jsdocImportTypeTag parsing tests
a-tarasyuk Jan 29, 2024
fc4ac15
update baseline
a-tarasyuk Jan 29, 2024
df23ce3
add jsdoc import type tag go to definition tests
a-tarasyuk Jan 29, 2024
0b0029d
jsdoc import type completions
a-tarasyuk Jan 30, 2024
0a20ad3
jsdoc import type find-all-refs/rename
a-tarasyuk Jan 30, 2024
386120f
rename importType to import
a-tarasyuk Jan 31, 2024
08806b6
add tests
a-tarasyuk Feb 1, 2024
d629693
auto import fix to existing jsdoc imports
a-tarasyuk Feb 1, 2024
6ec3b27
cleanup
a-tarasyuk Feb 2, 2024
ad24624
handle multiline import clause
a-tarasyuk Feb 3, 2024
26f633f
add tests
a-tarasyuk Feb 3, 2024
82f79be
cleanup
a-tarasyuk Feb 7, 2024
a76450e
add additional import tag parsing test
a-tarasyuk Feb 8, 2024
9003fbf
add additional import tag parsing tests
a-tarasyuk Feb 8, 2024
5a6cb6b
handle jsdoc import tags in convert import refactoring
a-tarasyuk Feb 8, 2024
67caac1
add additional tests
a-tarasyuk Feb 8, 2024
f7da8be
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Feb 12, 2024
dd2a481
add import attrs to import tag. add additional tests
a-tarasyuk Feb 13, 2024
af9bc71
emit import declarations
a-tarasyuk Mar 1, 2024
5bbe7e7
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 1, 2024
7caacbd
emit type only imports
a-tarasyuk Mar 2, 2024
340ea99
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 7, 2024
e28ff89
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 16, 2024
2e4b90d
fix types
a-tarasyuk Mar 16, 2024
d37f70c
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 18, 2024
6bd9d73
always display errors for JSDoc imports in import attributes
a-tarasyuk Mar 18, 2024
aa9600d
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 18, 2024
32bbf4c
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 20, 2024
0da4e23
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 21, 2024
51f902e
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk Mar 26, 2024
653955e
add tests
a-tarasyuk Mar 26, 2024
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
AmbientModuleDeclaration,
and,
AnonymousType,
AnyImportOrJsDocImport,
AnyImportOrReExport,
AnyImportSyntax,
append,
appendIfUnique,
ArrayBindingPattern,
Expand Down Expand Up @@ -583,6 +583,7 @@ import {
isJSDocCallbackTag,
isJSDocConstructSignature,
isJSDocFunctionType,
isJSDocImportTag,
isJSDocIndexSignature,
isJSDocLinkLike,
isJSDocMemberName,
Expand Down Expand Up @@ -770,6 +771,7 @@ import {
JSDocEnumTag,
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -3942,7 +3944,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|| (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || (getFunctionFlags(n) & FunctionFlags.AsyncGenerator)) ? "quit" : false));
}

function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined {
function getAnyImportSyntax(node: Node): AnyImportOrJsDocImport | undefined {
switch (node.kind) {
case SyntaxKind.ImportEqualsDeclaration:
return node as ImportEqualsDeclaration;
Expand Down Expand Up @@ -4280,8 +4282,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, specifier: ImportOrExportSpecifier | BindingElement | PropertyAccessExpression, dontResolveAlias = false): Symbol | undefined {
const moduleSpecifier = getExternalModuleRequireArgument(node) || (node as ImportDeclaration | ExportDeclaration).moduleSpecifier!;
function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration | JSDocImportTag, specifier: ImportOrExportSpecifier | BindingElement | PropertyAccessExpression, dontResolveAlias = false): Symbol | undefined {
const moduleSpecifier = getExternalModuleRequireArgument(node) || (node as ImportDeclaration | ExportDeclaration | JSDocImportTag).moduleSpecifier!;
const moduleSymbol = resolveExternalModuleName(node, moduleSpecifier)!; // TODO: GH#18217
const name = !isPropertyAccessExpression(specifier) && specifier.propertyName || specifier.name;
if (!isIdentifier(name)) {
Expand Down Expand Up @@ -5006,6 +5008,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
? location
: (isModuleDeclaration(location) ? location : location.parent && isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)?.name ||
(isLiteralImportTypeNode(location) ? location : undefined)?.argument.literal ||
(isInJSFile(location) && isJSDocImportTag(location) ? location.moduleSpecifier : undefined) ||
(isVariableDeclaration(location) && location.initializer && isRequireCall(location.initializer, /*requireStringLiteralLikeArgument*/ true) ? location.initializer.arguments[0] : undefined) ||
findAncestor(location, isImportCall)?.arguments[0] ||
findAncestor(location, isImportDeclaration)?.moduleSpecifier ||
Expand Down Expand Up @@ -9720,12 +9723,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.ImportClause: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportClause).parent.moduleSpecifier;
const attributes = isImportDeclaration(node.parent) ? node.parent.attributes : undefined;
addResult(
factory.createImportDeclaration(
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, factory.createIdentifier(localName), /*namedBindings*/ undefined),
specifier,
(node as ImportClause).parent.attributes,
attributes,
),
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
ModifierFlags.None,
);
Expand All @@ -9734,12 +9738,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NamespaceImport: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as NamespaceImport).parent.parent.moduleSpecifier;
const attributes = isImportDeclaration(node.parent) ? node.parent.attributes : undefined;
addResult(
factory.createImportDeclaration(
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
specifier,
(node as ImportClause).parent.attributes,
attributes,
),
ModifierFlags.None,
);
Expand All @@ -9759,6 +9764,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.ImportSpecifier: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportSpecifier).parent.parent.parent.moduleSpecifier;
const attributes = isImportDeclaration(node.parent.parent.parent) ? node.parent.parent.parent.attributes : undefined;
addResult(
factory.createImportDeclaration(
/*modifiers*/ undefined,
Expand All @@ -9774,7 +9780,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
]),
),
specifier,
(node as ImportSpecifier).parent.parent.parent.attributes,
attributes,
),
ModifierFlags.None,
);
Expand Down Expand Up @@ -47619,6 +47625,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (
(isExternalModuleImportEqualsDeclaration(node.parent.parent) && getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) ||
((node.parent.kind === SyntaxKind.ImportDeclaration || node.parent.kind === SyntaxKind.ExportDeclaration) && (node.parent as ImportDeclaration).moduleSpecifier === node) ||
(isInJSFile(node) && isJSDocImportTag(node.parent) && node.parent.moduleSpecifier === node) ||
((isInJSFile(node) && isRequireCall(node.parent, /*requireStringLiteralLikeArgument*/ false)) || isImportCall(node.parent)) ||
(isLiteralTypeNode(node.parent) && isLiteralImportTypeNode(node.parent.parent) && node.parent.parent.argument === node.parent)
) {
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ import {
JSDocEnumTag,
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocNameReference,
JSDocNonNullableType,
JSDocNullableType,
Expand Down Expand Up @@ -2204,6 +2205,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
return emitJSDocTypedefTag(node as JSDocTypedefTag);
case SyntaxKind.JSDocSeeTag:
return emitJSDocSeeTag(node as JSDocSeeTag);
case SyntaxKind.JSDocImportTag:
return emitJSDocImportTag(node as JSDocImportTag);
// SyntaxKind.JSDocPropertyTag (see JSDocParameterTag, above)

// Transformation nodes
Expand Down Expand Up @@ -4452,6 +4455,21 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
emitJSDocComment(tag.comment);
}

function emitJSDocImportTag(tag: JSDocImportTag) {
emitJSDocTagName(tag.tagName);

writeSpace();
emit(tag.importClause);

writeSpace();
emitTokenWithComment(SyntaxKind.FromKeyword, tag.importClause.end, writeKeyword, tag);

writeSpace();
emitExpression(tag.moduleSpecifier);

emitJSDocComment(tag.comment);
}

function emitJSDocNameReference(node: JSDocNameReference) {
writeSpace();
writePunctuation("{");
Expand Down
23 changes: 23 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ import {
JSDocEnumTag,
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -882,6 +883,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
updateJSDocImplementsTag,
createJSDocSeeTag,
updateJSDocSeeTag,
createJSDocImportTag,
updateJSDocImportTag,
createJSDocNameReference,
updateJSDocNameReference,
createJSDocMemberName,
Expand Down Expand Up @@ -5554,6 +5557,24 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
: node;
}

// @api
function createJSDocImportTag(tagName: Identifier | undefined, importClause: ImportClause, moduleSpecifier: Expression, comment?: string | NodeArray<JSDocComment>): JSDocImportTag {
const node = createBaseJSDocTag<JSDocImportTag>(SyntaxKind.JSDocImportTag, tagName ?? createIdentifier("import"), comment);
node.importClause = importClause;
node.moduleSpecifier = moduleSpecifier;
node.comment = comment;
return node;
}

function updateJSDocImportTag(node: JSDocImportTag, tagName: Identifier | undefined, importClause: ImportClause, moduleSpecifier: Expression, comment: string | NodeArray<JSDocComment> | undefined): JSDocImportTag {
return node.tagName !== tagName
|| node.comment !== comment
|| node.importClause !== importClause
|| node.moduleSpecifier !== moduleSpecifier
? update(createJSDocImportTag(tagName, importClause, moduleSpecifier, comment), node)
: node;
}

// @api
function createJSDocText(text: string): JSDocText {
const node = createBaseNode<JSDocText>(SyntaxKind.JSDocText);
Expand Down Expand Up @@ -7265,6 +7286,8 @@ function getDefaultTagNameForKind(kind: JSDocTag["kind"]): string {
return "augments";
case SyntaxKind.JSDocImplementsTag:
return "implements";
case SyntaxKind.JSDocImportTag:
return "import";
default:
return Debug.fail(`Unsupported kind: ${Debug.formatSyntaxKind(kind)}`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/factory/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
JSDocEnumTag,
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -1202,6 +1203,10 @@ export function isJSDocThrowsTag(node: Node): node is JSDocThrowsTag {
return node.kind === SyntaxKind.JSDocThrowsTag;
}

export function isJSDocImportTag(node: Node): node is JSDocImportTag {
return node.kind === SyntaxKind.JSDocImportTag;
}

// Synthesized list

/** @internal */
Expand Down
65 changes: 60 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import {
JSDocEnumTag,
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -1125,6 +1126,7 @@ const forEachChildTable: ForEachChildTable = {
[SyntaxKind.JSDocReadonlyTag]: forEachChildInJSDocTag,
[SyntaxKind.JSDocDeprecatedTag]: forEachChildInJSDocTag,
[SyntaxKind.JSDocOverrideTag]: forEachChildInJSDocTag,
[SyntaxKind.JSDocImportTag]: forEachChildInJSDocImportTag,
[SyntaxKind.PartiallyEmittedExpression]: forEachChildInPartiallyEmittedExpression,
};

Expand Down Expand Up @@ -1214,6 +1216,13 @@ function forEachChildInJSDocTag<T>(node: JSDocUnknownTag | JSDocClassTag | JSDoc
|| (typeof node.comment === "string" ? undefined : visitNodes(cbNode, cbNodes, node.comment));
}

function forEachChildInJSDocImportTag<T>(node: JSDocImportTag, cbNode: (node: Node) => T | undefined, cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
return visitNode(cbNode, node.tagName)
|| visitNode(cbNode, node.importClause)
|| visitNode(cbNode, node.moduleSpecifier)
|| (typeof node.comment === "string" ? undefined : visitNodes(cbNode, cbNodes, node.comment));
}

function forEachChildInPartiallyEmittedExpression<T>(node: PartiallyEmittedExpression, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
return visitNode(cbNode, node.expression);
}
Expand Down Expand Up @@ -3875,7 +3884,7 @@ namespace Parser {
}

function parseJSDocType(): TypeNode {
scanner.setInJSDocType(true);
scanner.setSkipJsDocLeadingAsterisks(true);
const pos = getNodePos();
if (parseOptional(SyntaxKind.ModuleKeyword)) {
// TODO(rbuckton): We never set the type for a JSDocNamepathType. What should we put here?
Expand All @@ -3893,13 +3902,13 @@ namespace Parser {
}
}

scanner.setInJSDocType(false);
scanner.setSkipJsDocLeadingAsterisks(false);
return finishNode(moduleTag, pos);
}

const hasDotDotDot = parseOptional(SyntaxKind.DotDotDotToken);
let type = parseTypeOrTypePredicate();
scanner.setInJSDocType(false);
scanner.setSkipJsDocLeadingAsterisks(false);
if (hasDotDotDot) {
type = finishNode(factory.createJSDocVariadicType(type), pos);
}
Expand Down Expand Up @@ -9073,6 +9082,9 @@ namespace Parser {
case "throws":
tag = parseThrowsTag(start, tagName, margin, indentText);
break;
case "import":
tag = parseImportTag(start, tagName, margin, indentText);
break;
default:
tag = parseUnknownTag(start, tagName, margin, indentText);
break;
Expand Down Expand Up @@ -9445,13 +9457,56 @@ namespace Parser {
return finishNode(factory.createJSDocSatisfiesTag(tagName, typeExpression, comments), start);
}

function parseImportTag(start: number, tagName: Identifier, margin: number, indentText: string): JSDocImportTag {
const afterImportTypeTagPos = scanner.getTokenFullStart();
sandersn marked this conversation as resolved.
Show resolved Hide resolved

let identifier: Identifier | undefined;
if (isIdentifier()) {
identifier = parseIdentifier();
}

let importClause: ImportClause | undefined;
if (
identifier // @import id
|| token() === SyntaxKind.AsteriskToken // @import *
|| token() === SyntaxKind.OpenBraceToken // @import {
) {
importClause = parseJsDocImportClause(identifier, afterImportTypeTagPos);
parseExpected(SyntaxKind.FromKeyword);
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
}

Debug.assert(importClause);

const moduleSpecifier = parseModuleSpecifier();
sandersn marked this conversation as resolved.
Show resolved Hide resolved
const comments = margin !== undefined && indentText !== undefined ? parseTrailingTagComments(start, getNodePos(), margin, indentText) : undefined;
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
return finishNode(factory.createJSDocImportTag(tagName, importClause, moduleSpecifier, comments), start);
}

function parseJsDocImportClause(identifier: Identifier | undefined, pos: number) {
let namedBindings: NamespaceImport | NamedImports | undefined;
if (
!identifier ||
parseOptional(SyntaxKind.CommaToken)
) {
if (token() === SyntaxKind.AsteriskToken) {
namedBindings = parseNamespaceImport();
}
else {
scanner.setSkipJsDocLeadingAsterisks(true);
namedBindings = parseNamedImportsOrExports(SyntaxKind.NamedImports);
scanner.setSkipJsDocLeadingAsterisks(false);
}
}
return finishNode(factory.createImportClause(/*isTypeOnly*/ true, identifier, namedBindings), pos);
}

function parseExpressionWithTypeArgumentsForAugments(): ExpressionWithTypeArguments & { expression: Identifier | PropertyAccessEntityNameExpression; } {
const usedBrace = parseOptional(SyntaxKind.OpenBraceToken);
const pos = getNodePos();
const expression = parsePropertyAccessEntityNameExpression();
scanner.setInJSDocType(true);
scanner.setSkipJsDocLeadingAsterisks(true);
const typeArguments = tryParseTypeArguments();
scanner.setInJSDocType(false);
scanner.setSkipJsDocLeadingAsterisks(false);
const node = factory.createExpressionWithTypeArguments(expression, typeArguments) as ExpressionWithTypeArguments & { expression: Identifier | PropertyAccessEntityNameExpression; };
const res = finishNode(node, pos);
if (usedBrace) {
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ import {
isImportTypeNode,
isIncrementalCompilation,
isInJSFile,
isJSDocImportTag,
isLiteralImportTypeNode,
isModifier,
isModuleDeclaration,
Expand Down Expand Up @@ -3370,6 +3371,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
collectDynamicImportOrRequireCalls(file);
}

if (isJavaScriptFile) {
collectJsDocImportTypeReferences(file);
}

file.imports = imports || emptyArray;
file.moduleAugmentations = moduleAugmentations || emptyArray;
file.ambientModuleNames = ambientModules || emptyArray;
Expand Down Expand Up @@ -3444,6 +3449,20 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
}

function collectJsDocImportTypeReferences(file: SourceFile) {
sandersn marked this conversation as resolved.
Show resolved Hide resolved
const r = /@import/g;
while (r.exec(file.text) !== null) { // eslint-disable-line no-null/no-null
const node = getNodeAtPosition(file, r.lastIndex);
if (isJSDocImportTag(node)) {
const moduleNameExpr = getExternalModuleName(node);
if (moduleNameExpr && isStringLiteral(moduleNameExpr) && moduleNameExpr.text) {
setParentRecursive(node, /*incremental*/ false);
imports = append(imports, moduleNameExpr);
}
}
}
}

/** Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only in JS files */
function getNodeAtPosition(sourceFile: SourceFile, position: number): Node {
let current: Node = sourceFile;
Expand Down
Loading
Loading