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 1 commit
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
Prev Previous commit
Next Next commit
jsdoc import type completions
  • Loading branch information
a-tarasyuk committed Jan 30, 2024
commit 0b0029dc934f5b5e5d75ed9290abdf4a47967ee0
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47625,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) && isJSDocImportTypeTag(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
39 changes: 23 additions & 16 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3172,6 +3172,7 @@ function getCompletionData(
log("getCompletionData: Is inside comment: " + (timestamp() - start));

let insideJsDocTagTypeExpression = false;
let insideJsDocImportTypeTag = false;
let isInSnippetScope = false;
if (insideComment) {
if (hasDocComment(sourceFile, position)) {
Expand Down Expand Up @@ -3212,25 +3213,30 @@ function getCompletionData(
if (tag.tagName.pos <= position && position <= tag.tagName.end) {
return { kind: CompletionDataKind.JsDocTagName };
}
const typeExpression = tryGetTypeExpressionFromTag(tag);
if (typeExpression) {
currentToken = getTokenAtPosition(sourceFile, position);
if (
!currentToken ||
(!isDeclarationName(currentToken) &&
(currentToken.parent.kind !== SyntaxKind.JSDocPropertyTag ||
(currentToken.parent as JSDocPropertyTag).name !== currentToken))
) {
// Use as type location if inside tag's type expression
insideJsDocTagTypeExpression = isCurrentlyEditingNode(typeExpression);
}
if (isJSDocImportTypeTag(tag)) {
insideJsDocImportTypeTag = true;
}
if (!insideJsDocTagTypeExpression && isJSDocParameterTag(tag) && (nodeIsMissing(tag.name) || tag.name.pos <= position && position <= tag.name.end)) {
return { kind: CompletionDataKind.JsDocParameterName, tag };
else {
const typeExpression = tryGetTypeExpressionFromTag(tag);
if (typeExpression) {
currentToken = getTokenAtPosition(sourceFile, position);
if (
!currentToken ||
(!isDeclarationName(currentToken) &&
(currentToken.parent.kind !== SyntaxKind.JSDocPropertyTag ||
(currentToken.parent as JSDocPropertyTag).name !== currentToken))
) {
// Use as type location if inside tag's type expression
insideJsDocTagTypeExpression = isCurrentlyEditingNode(typeExpression);
}
}
if (!insideJsDocTagTypeExpression && isJSDocParameterTag(tag) && (nodeIsMissing(tag.name) || tag.name.pos <= position && position <= tag.name.end)) {
return { kind: CompletionDataKind.JsDocParameterName, tag };
}
}
}

if (!insideJsDocTagTypeExpression) {
if (!insideJsDocTagTypeExpression && !insideJsDocImportTypeTag) {
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
// Proceed if the current position is in jsDoc tag expression; otherwise it is a normal
// comment or the plain text part of a jsDoc comment, so no completion should be available
log("Returning an empty list because completion was inside a regular comment or plain text part of a JsDoc comment.");
Expand All @@ -3241,7 +3247,7 @@ function getCompletionData(
start = timestamp();
// The decision to provide completion depends on the contextToken, which is determined through the previousToken.
// Note: 'previousToken' (and thus 'contextToken') can be undefined if we are the beginning of the file
const isJsOnlyLocation = !insideJsDocTagTypeExpression && isSourceFileJS(sourceFile);
const isJsOnlyLocation = !insideJsDocTagTypeExpression && !insideJsDocImportTypeTag && isSourceFileJS(sourceFile);
const tokens = getRelevantTokens(position, sourceFile);
const previousToken = tokens.previousToken!;
let contextToken = tokens.contextToken!;
Expand Down Expand Up @@ -3922,6 +3928,7 @@ function getCompletionData(

function isTypeOnlyCompletion(): boolean {
return insideJsDocTagTypeExpression
|| insideJsDocImportTypeTag
|| !!importStatementCompletion && isTypeOnlyImportOrExportDeclaration(location.parent)
|| !isContextTokenValueLocation(contextToken) &&
(isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker)
Expand Down
1 change: 1 addition & 0 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringL
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExternalModuleReference:
case SyntaxKind.JSDocImportTypeTag:
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
// Get all known external module names or complete a path to a module
// i.e. import * as ns from "/*completion position*/";
// var y = import("/*completion position*/");
Expand Down
48 changes: 48 additions & 0 deletions tests/baselines/reference/jsdocImportTypeTagCompletion2.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// === Completions ===
=== /b.js ===
// /**
// * @importType { } from "./a"
// ^
// | ----------------------------------------------------------------------
// | interface A
// | ----------------------------------------------------------------------
// */

[
{
"marker": {
"fileName": "/b.js",
"position": 21,
"name": ""
},
"item": {
"flags": 0,
"isGlobalCompletion": false,
"isMemberCompletion": true,
"isNewIdentifierLocation": false,
"entries": [
{
"name": "A",
"kind": "interface",
"kindModifiers": "export",
"sortText": "11",
"displayParts": [
{
"text": "interface",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "A",
"kind": "interfaceName"
}
],
"documentation": []
}
]
}
}
]
51 changes: 51 additions & 0 deletions tests/baselines/reference/jsdocImportTypeTagCompletion3.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// === Completions ===
=== /tests/cases/fourslash/./c.js ===
// /**
// * @importType * as types from "./"
// ^
// | ----------------------------------------------------------------------
// | a
// | b
// | ----------------------------------------------------------------------
// */

[
{
"marker": {
"fileName": "/tests/cases/fourslash/./c.js",
"position": 38,
"name": ""
},
"item": {
"isGlobalCompletion": false,
"isMemberCompletion": false,
"isNewIdentifierLocation": true,
"entries": [
{
"name": "a",
"kind": "script",
"kindModifiers": ".ts",
"sortText": "11",
"displayParts": [
{
"text": "a",
"kind": "text"
}
]
},
{
"name": "b",
"kind": "script",
"kindModifiers": ".ts",
"sortText": "11",
"displayParts": [
{
"text": "b",
"kind": "text"
}
]
}
]
}
}
]
14 changes: 14 additions & 0 deletions tests/cases/fourslash/jsdocImportTypeTagCompletion2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///<reference path="fourslash.ts" />

// @allowJS: true
// @checkJs: true

// @filename: /a.ts
////export interface A {}

// @filename: /b.js
/////**
//// * @importType { /**/ } from "./a"
//// */

verify.baselineCompletions();
18 changes: 18 additions & 0 deletions tests/cases/fourslash/jsdocImportTypeTagCompletion3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
///<reference path="fourslash.ts" />

// @allowJS: true
// @checkJs: true
// @module: esnext

// @filename: ./a.ts
////export interface A {}

// @filename: ./b.ts
////export interface B {}

// @filename: ./c.js
/////**
//// * @importType * as types from ".//**/"
//// */

verify.baselineCompletions();
Loading