Skip to content

Commit

Permalink
jsdoc import type find-all-refs/rename
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Jan 30, 2024
1 parent 0b0029d commit 0a20ad3
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5586,7 +5586,7 @@ export interface ValidImportTypeNode extends ImportTypeNode {

/** @internal */
export type AnyValidImportOrReExport =
| (ImportDeclaration | ExportDeclaration) & { moduleSpecifier: StringLiteral; }
| (ImportDeclaration | ExportDeclaration | JSDocImportTypeTag) & { moduleSpecifier: StringLiteral; }
| ImportEqualsDeclaration & { moduleReference: ExternalModuleReference & { expression: StringLiteral; }; }
| RequireOrImportCall
| ValidImportTypeNode;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4022,6 +4022,7 @@ export function tryGetImportFromModuleSpecifier(node: StringLiteralLike): AnyVal
switch (node.parent.kind) {
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
case SyntaxKind.JSDocImportTypeTag:
return node.parent as AnyValidImportOrReExport;
case SyntaxKind.ExternalModuleReference:
return (node.parent as ExternalModuleReference).parent as AnyValidImportOrReExport;
Expand Down Expand Up @@ -4070,7 +4071,7 @@ export function getNamespaceDeclarationNode(node: ImportDeclaration | ImportEqua
}

/** @internal */
export function isDefaultImport(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): boolean {
export function isDefaultImport(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration | JSDocImportTypeTag): boolean {
return node.kind === SyntaxKind.ImportDeclaration && !!node.importClause && !!node.importClause.name;
}

Expand Down
6 changes: 4 additions & 2 deletions src/services/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
isVariableDeclaration,
isVariableDeclarationInitializedToBareOrAccessedRequire,
isVariableStatement,
JSDocImportTypeTag,
ModifierFlags,
ModuleBlock,
ModuleDeclaration,
Expand Down Expand Up @@ -138,7 +139,7 @@ interface AmbientModuleDeclaration extends ModuleDeclaration {
}
type SourceFileLike = SourceFile | AmbientModuleDeclaration;
// Identifier for the case of `const x = require("y")`.
type Importer = AnyImportOrReExport | ValidImportTypeNode | Identifier;
type Importer = AnyImportOrReExport | ValidImportTypeNode | Identifier | JSDocImportTypeTag;
type ImporterOrCallExpression = Importer | CallExpression;

/** Returns import statements that directly reference the exporting module, and a list of files that may access the module through a namespace. */
Expand Down Expand Up @@ -215,6 +216,7 @@ function getImportersForExport(
break;

case SyntaxKind.ImportDeclaration:
case SyntaxKind.JSDocImportTypeTag:
directImports.push(direct);
const namedBindings = direct.importClause && direct.importClause.namedBindings;
if (namedBindings && namedBindings.kind === SyntaxKind.NamespaceImport) {
Expand Down Expand Up @@ -267,7 +269,7 @@ function getImportersForExport(
});
}

function handleNamespaceImport(importDeclaration: ImportEqualsDeclaration | ImportDeclaration, name: Identifier, isReExport: boolean, alreadyAddedDirect: boolean): void {
function handleNamespaceImport(importDeclaration: ImportEqualsDeclaration | ImportDeclaration | JSDocImportTypeTag, name: Identifier, isReExport: boolean, alreadyAddedDirect: boolean): void {
if (exportKind === ExportKind.ExportEquals) {
// This is a direct import, not import-as-namespace.
if (!alreadyAddedDirect) directImports.push(importDeclaration);
Expand Down
105 changes: 105 additions & 0 deletions tests/baselines/reference/findAllRefsJsDocImportTypeTag.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// === findAllReferences ===
// === /a.js ===
// /**
// * <|@importType { [|{| defId: 0, isWriteAccess: true |}A|] } from "./b";
// |>*/
//
// /**
// * @param { [|{| defId: 0 |}A|]/*FIND ALL REFS*/ } a
// */
// function f(a) {}

// === /b.ts ===
// <|export interface [|{| defId: 1, isWriteAccess: true |}A|] { }|>

// === Definitions ===
// === /a.js ===
// /**
// * <|@importType { [|{| defId: 0 |}A|] } from "./b";
// |>*/
//
// /**
// * @param { A/*FIND ALL REFS*/ } a
// */
// function f(a) {}

// === /b.ts ===
// <|export interface [|{| defId: 1 |}A|] { }|>

// === Details ===
[
{
"defId": 0,
"containerKind": "",
"containerName": "",
"kind": "alias",
"name": "(alias) interface A\nimport A",
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "alias",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "interface",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "A",
"kind": "aliasName"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": "import",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "A",
"kind": "aliasName"
}
]
},
{
"defId": 1,
"containerKind": "",
"containerName": "",
"kind": "interface",
"name": "interface A",
"displayParts": [
{
"text": "interface",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "A",
"kind": "interfaceName"
}
]
}
]
10 changes: 10 additions & 0 deletions tests/baselines/reference/renameJsDocImportTypeTag.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// === findRenameLocations ===
// === /a.js ===
// /**
// * <|@importType { /*START PREFIX*/A as [|ARENAME|] } from "./b";
// |>*/
//
// /**
// * @param { [|ARENAME|]/*RENAME*/ } a
// */
// function f(a) {}
19 changes: 19 additions & 0 deletions tests/cases/fourslash/findAllRefsJsDocImportTypeTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />

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

// @Filename: /b.ts
////export interface A { }

// @Filename: /a.js
/////**
//// * @importType { A } from "./b";
//// */
////
/////**
//// * @param { [|A/**/|] } a
//// */
////function f(a) {}

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

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

// @Filename: /b.ts
////export interface A { }

// @Filename: /a.js
/////**
//// * @importType { A } from "./b";
//// */
////
/////**
//// * @param { [|A/**/|] } a
//// */
////function f(a) {}

verify.baselineRename("");

0 comments on commit 0a20ad3

Please sign in to comment.