From 88c7e7d0f04b222d8fb3e231be04fb3f284cca68 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Fri, 28 Oct 2022 10:50:13 +0800 Subject: [PATCH 1/3] Generate correct `scipSymbol` for namespace imports --- snapshots/input/syntax/src/import.ts | 4 +++- snapshots/output/syntax/src/import.ts | 11 +++++++++-- src/FileIndexer.ts | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/snapshots/input/syntax/src/import.ts b/snapshots/input/syntax/src/import.ts index 89b74f72..72af1dfe 100644 --- a/snapshots/input/syntax/src/import.ts +++ b/snapshots/input/syntax/src/import.ts @@ -1,3 +1,4 @@ +import * as ts from 'typescript' import { Class } from './class' import { Enum } from './enum' import { newFunction } from './function' @@ -8,7 +9,8 @@ export function useEverything(): string { new Class('a').classProperty + renamedInterface().methodSignature('a') + Enum[Enum.A] + - newFunction() + newFunction() + + ts.SyntaxKind.ArrayType ) } diff --git a/snapshots/output/syntax/src/import.ts b/snapshots/output/syntax/src/import.ts index 833abcc5..6d9bde59 100644 --- a/snapshots/output/syntax/src/import.ts +++ b/snapshots/output/syntax/src/import.ts @@ -1,6 +1,9 @@ - import { Class } from './class' + import * as ts from 'typescript' // definition syntax 1.0.0 src/`import.ts`/ //documentation ```ts\nmodule "import.ts"\n``` +// ^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/ +// ^^^^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ + import { Class } from './class' // ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class# // ^^^^^^^^^ reference syntax 1.0.0 src/`class.ts`/ import { Enum } from './enum' @@ -28,8 +31,12 @@ // ^^^^ reference syntax 1.0.0 src/`enum.ts`/Enum# // ^^^^ reference syntax 1.0.0 src/`enum.ts`/Enum# // ^ reference syntax 1.0.0 src/`enum.ts`/Enum#A. - newFunction() + newFunction() + // ^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/newFunction(). + ts.SyntaxKind.ArrayType +// ^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/ +// ^^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/SyntaxKind# +// ^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/SyntaxKind#ArrayType. ) } diff --git a/src/FileIndexer.ts b/src/FileIndexer.ts index 27f6a52b..6013c518 100644 --- a/src/FileIndexer.ts +++ b/src/FileIndexer.ts @@ -389,7 +389,11 @@ export class FileIndexer { return this.cached(node, this.scipSymbol(node.parent)) } - if (ts.isImportSpecifier(node) || ts.isImportClause(node)) { + if ( + ts.isImportSpecifier(node) || + ts.isImportClause(node) || + ts.isNamespaceImport(node) + ) { const tpe = this.checker.getTypeAtLocation(node) for (const declaration of tpe.symbol?.declarations || []) { return this.scipSymbol(declaration) From af4bb95001872116df3d7e20b8d165002a90d2b6 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Fri, 28 Oct 2022 12:44:12 +0800 Subject: [PATCH 2/3] Add `namespace` to the `import` test --- snapshots/input/syntax/src/import.ts | 4 ++-- .../src/{namespace.d.ts => namespace.ts} | 3 ++- snapshots/output/syntax/src/import.ts | 18 +++++++------- snapshots/output/syntax/src/namespace.d.ts | 21 ---------------- snapshots/output/syntax/src/namespace.ts | 24 +++++++++++++++++++ src/FileIndexer.ts | 6 +---- 6 files changed, 38 insertions(+), 38 deletions(-) rename snapshots/input/syntax/src/{namespace.d.ts => namespace.ts} (63%) delete mode 100644 snapshots/output/syntax/src/namespace.d.ts create mode 100644 snapshots/output/syntax/src/namespace.ts diff --git a/snapshots/input/syntax/src/import.ts b/snapshots/input/syntax/src/import.ts index 72af1dfe..5c60f68c 100644 --- a/snapshots/input/syntax/src/import.ts +++ b/snapshots/input/syntax/src/import.ts @@ -1,4 +1,4 @@ -import * as ts from 'typescript' +import * as namespace from './namespace' import { Class } from './class' import { Enum } from './enum' import { newFunction } from './function' @@ -10,7 +10,7 @@ export function useEverything(): string { renamedInterface().methodSignature('a') + Enum[Enum.A] + newFunction() + - ts.SyntaxKind.ArrayType + namespace.a.value ) } diff --git a/snapshots/input/syntax/src/namespace.d.ts b/snapshots/input/syntax/src/namespace.ts similarity index 63% rename from snapshots/input/syntax/src/namespace.d.ts rename to snapshots/input/syntax/src/namespace.ts index 1c20b655..ec76f09d 100644 --- a/snapshots/input/syntax/src/namespace.d.ts +++ b/snapshots/input/syntax/src/namespace.ts @@ -1,7 +1,8 @@ -declare namespace a { +export declare namespace a { function hello(): string interface Interface { hello: string } var i: Interface + export const value = 1 } diff --git a/snapshots/output/syntax/src/import.ts b/snapshots/output/syntax/src/import.ts index 6d9bde59..46a59340 100644 --- a/snapshots/output/syntax/src/import.ts +++ b/snapshots/output/syntax/src/import.ts @@ -1,8 +1,8 @@ - import * as ts from 'typescript' + import * as namespace from './namespace' // definition syntax 1.0.0 src/`import.ts`/ //documentation ```ts\nmodule "import.ts"\n``` -// ^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/ -// ^^^^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ +// ^^^^^^^^^ reference syntax 1.0.0 src/`namespace.ts`/ +// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`namespace.ts`/ import { Class } from './class' // ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class# // ^^^^^^^^^ reference syntax 1.0.0 src/`class.ts`/ @@ -33,10 +33,10 @@ // ^ reference syntax 1.0.0 src/`enum.ts`/Enum#A. newFunction() + // ^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/newFunction(). - ts.SyntaxKind.ArrayType -// ^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/ -// ^^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/SyntaxKind# -// ^^^^^^^^^ reference typescript 4.8.4 lib/`typescript.d.ts`/ts/SyntaxKind#ArrayType. + namespace.a.value +// ^^^^^^^^^ reference local 0 +// ^ reference syntax 1.0.0 src/`namespace.ts`/a/ +// ^^^^^ reference syntax 1.0.0 src/`namespace.ts`/a/value. ) } @@ -51,9 +51,9 @@ return import('./function').then(c => c.newFunction()) // ^^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/ // ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then(). -// ^ definition local 3 +// ^ definition local 4 // documentation ```ts\n(parameter) c: typeof import("/src/function")\n``` -// ^ reference local 3 +// ^ reference local 4 // ^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/newFunction(). } diff --git a/snapshots/output/syntax/src/namespace.d.ts b/snapshots/output/syntax/src/namespace.d.ts deleted file mode 100644 index 7652ac14..00000000 --- a/snapshots/output/syntax/src/namespace.d.ts +++ /dev/null @@ -1,21 +0,0 @@ - declare namespace a { -// definition syntax 1.0.0 src/`namespace.d.ts`/ -//documentation ```ts\nmodule "namespace.d.ts"\n``` -// ^ definition syntax 1.0.0 src/`namespace.d.ts`/a/ -// documentation ```ts\na: typeof a\n``` - function hello(): string -// ^^^^^ definition syntax 1.0.0 src/`namespace.d.ts`/a/hello(). -// documentation ```ts\nfunction hello(): string\n``` - interface Interface { -// ^^^^^^^^^ definition syntax 1.0.0 src/`namespace.d.ts`/a/Interface# -// documentation ```ts\ninterface Interface\n``` - hello: string -// ^^^^^ definition syntax 1.0.0 src/`namespace.d.ts`/a/Interface#hello. -// documentation ```ts\n(property) hello: string\n``` - } - var i: Interface -// ^ definition syntax 1.0.0 src/`namespace.d.ts`/a/i. -// documentation ```ts\nvar i: Interface\n``` -// ^^^^^^^^^ reference syntax 1.0.0 src/`namespace.d.ts`/a/Interface# - } - diff --git a/snapshots/output/syntax/src/namespace.ts b/snapshots/output/syntax/src/namespace.ts new file mode 100644 index 00000000..43cc5e9d --- /dev/null +++ b/snapshots/output/syntax/src/namespace.ts @@ -0,0 +1,24 @@ + export declare namespace a { +// definition syntax 1.0.0 src/`namespace.ts`/ +//documentation ```ts\nmodule "namespace.ts"\n``` +// ^ definition syntax 1.0.0 src/`namespace.ts`/a/ +// documentation ```ts\na: typeof a\n``` + function hello(): string +// ^^^^^ definition syntax 1.0.0 src/`namespace.ts`/a/hello(). +// documentation ```ts\nfunction hello(): string\n``` + interface Interface { +// ^^^^^^^^^ definition syntax 1.0.0 src/`namespace.ts`/a/Interface# +// documentation ```ts\ninterface Interface\n``` + hello: string +// ^^^^^ definition syntax 1.0.0 src/`namespace.ts`/a/Interface#hello. +// documentation ```ts\n(property) hello: string\n``` + } + var i: Interface +// ^ definition syntax 1.0.0 src/`namespace.ts`/a/i. +// documentation ```ts\nvar i: Interface\n``` +// ^^^^^^^^^ reference syntax 1.0.0 src/`namespace.ts`/a/Interface# + export const value = 1 +// ^^^^^ definition syntax 1.0.0 src/`namespace.ts`/a/value. +// documentation ```ts\nvar value: 1\n``` + } + diff --git a/src/FileIndexer.ts b/src/FileIndexer.ts index 6013c518..27f6a52b 100644 --- a/src/FileIndexer.ts +++ b/src/FileIndexer.ts @@ -389,11 +389,7 @@ export class FileIndexer { return this.cached(node, this.scipSymbol(node.parent)) } - if ( - ts.isImportSpecifier(node) || - ts.isImportClause(node) || - ts.isNamespaceImport(node) - ) { + if (ts.isImportSpecifier(node) || ts.isImportClause(node)) { const tpe = this.checker.getTypeAtLocation(node) for (const declaration of tpe.symbol?.declarations || []) { return this.scipSymbol(declaration) From e62facf6dfc36ac141c40162a206fb416ca2483e Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Fri, 28 Oct 2022 12:44:31 +0800 Subject: [PATCH 3/3] Fix the `import` test --- snapshots/output/syntax/src/import.ts | 6 +++--- src/FileIndexer.ts | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/snapshots/output/syntax/src/import.ts b/snapshots/output/syntax/src/import.ts index 46a59340..5a796855 100644 --- a/snapshots/output/syntax/src/import.ts +++ b/snapshots/output/syntax/src/import.ts @@ -34,7 +34,7 @@ newFunction() + // ^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/newFunction(). namespace.a.value -// ^^^^^^^^^ reference local 0 +// ^^^^^^^^^ reference syntax 1.0.0 src/`namespace.ts`/ // ^ reference syntax 1.0.0 src/`namespace.ts`/a/ // ^^^^^ reference syntax 1.0.0 src/`namespace.ts`/a/value. ) @@ -51,9 +51,9 @@ return import('./function').then(c => c.newFunction()) // ^^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/ // ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then(). -// ^ definition local 4 +// ^ definition local 3 // documentation ```ts\n(parameter) c: typeof import("/src/function")\n``` -// ^ reference local 4 +// ^ reference local 3 // ^^^^^^^^^^^ reference syntax 1.0.0 src/`function.ts`/newFunction(). } diff --git a/src/FileIndexer.ts b/src/FileIndexer.ts index 27f6a52b..6013c518 100644 --- a/src/FileIndexer.ts +++ b/src/FileIndexer.ts @@ -389,7 +389,11 @@ export class FileIndexer { return this.cached(node, this.scipSymbol(node.parent)) } - if (ts.isImportSpecifier(node) || ts.isImportClause(node)) { + if ( + ts.isImportSpecifier(node) || + ts.isImportClause(node) || + ts.isNamespaceImport(node) + ) { const tpe = this.checker.getTypeAtLocation(node) for (const declaration of tpe.symbol?.declarations || []) { return this.scipSymbol(declaration)