Skip to content

Commit 14910a2

Browse files
committed
Handle named import clauses
Previously, lsif-typescript emitted an incorrect symbol for named import clauses like `import React from 'react'`. Now, we handle this case differently from anonymous import clauses such as `import { React } from 'react'`
1 parent 7b0bd88 commit 14910a2

File tree

7 files changed

+93
-4
lines changed

7 files changed

+93
-4
lines changed

snapshots/input/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock.json
2+
yarn.lock
3+
dump.svg

snapshots/input/react/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "Example TS/JS project",
5+
"main": "src/main.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"workspaces": [
11+
"packages/*"
12+
],
13+
"license": "ISC",
14+
"private": true,
15+
"dependencies": {
16+
"react": "16.4.0"
17+
},
18+
"devDependencies": {
19+
"@types/react": "17.0.0"
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
3+
/** Takes loading prop, input component as child */
4+
interface Props {
5+
loading: boolean
6+
children: React.ReactNode
7+
className?: string
8+
}
9+
10+
export const LoaderInput: React.FunctionComponent<Props> = ({ loading, children, className }) => (
11+
<div className="hello">
12+
{children}
13+
{loading && <p className="spinner">spinner</p>}
14+
</div>
15+
)

snapshots/input/react/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"esModuleInterop": true,
5+
"outDir": "dist"
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react'
2+
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
3+
4+
/** Takes loading prop, input component as child */
5+
interface Props {
6+
// ^^^^^ definition example 1.0.0 src/`LoaderInput.tsx`/Props#
7+
loading: boolean
8+
// ^^^^^^^ definition example 1.0.0 src/`LoaderInput.tsx`/Props#loading.
9+
children: React.ReactNode
10+
// ^^^^^^^^ definition example 1.0.0 src/`LoaderInput.tsx`/Props#children.
11+
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
12+
// ^^^^^^^^^ reference local 0
13+
className?: string
14+
// ^^^^^^^^^ definition example 1.0.0 src/`LoaderInput.tsx`/Props#className.
15+
}
16+
17+
export const LoaderInput: React.FunctionComponent<Props> = ({ loading, children, className }) => (
18+
// ^^^^^^^^^^^ definition example 1.0.0 src/`LoaderInput.tsx`/LoaderInput.
19+
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
20+
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
21+
// ^^^^^ reference example 1.0.0 src/`LoaderInput.tsx`/Props#
22+
// ^^^^^^^ reference local 4
23+
// ^^^^^^^^ reference local 5
24+
// ^^^^^^^^^ reference local 6
25+
<div className="hello">
26+
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
27+
// ^^^^^^^^^ reference local 11
28+
{children}
29+
// ^^^^^^^^ reference local 5
30+
{loading && <p className="spinner">spinner</p>}
31+
// ^^^^^^^ reference local 4
32+
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
33+
// ^^^^^^^^^ reference local 17
34+
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
35+
</div>
36+
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
37+
)
38+

src/FileIndexer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export class FileIndexer {
189189
if (isAnonymousContainerOfSymbols(node)) {
190190
return this.cached(node, this.lsifSymbol(node.parent))
191191
}
192-
if (ts.isImportSpecifier(node)) {
192+
193+
if (ts.isImportSpecifier(node) || ts.isImportClause(node)) {
193194
const tpe = this.checker.getTypeAtLocation(node)
194195
for (const declaration of tpe.symbol?.declarations || []) {
195196
return this.lsifSymbol(declaration)
@@ -265,7 +266,7 @@ function isAnonymousContainerOfSymbols(node: ts.Node): boolean {
265266
return (
266267
ts.isModuleBlock(node) ||
267268
ts.isImportDeclaration(node) ||
268-
ts.isImportClause(node) ||
269+
(ts.isImportClause(node) && !node.name) ||
269270
ts.isNamedImports(node) ||
270271
ts.isVariableStatement(node) ||
271272
ts.isVariableDeclarationList(node)

src/main.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ if (isUpdate && fs.existsSync(outputDirectory)) {
2626
fs.rmSync(outputDirectory, { recursive: true })
2727
}
2828
for (const snapshotDirectory of snapshotDirectories) {
29+
const inputRoot = join(inputDirectory, snapshotDirectory)
30+
const outputRoot = join(outputDirectory, snapshotDirectory)
31+
if (!fs.statSync(inputRoot).isDirectory()) {
32+
continue
33+
}
2934
test(snapshotDirectory, () => {
3035
const index = new lsif.lib.codeintel.lsiftyped.Index()
31-
const inputRoot = join(inputDirectory, snapshotDirectory)
32-
const outputRoot = join(outputDirectory, snapshotDirectory)
3336
lsifIndex({
3437
workspaceRoot: inputRoot,
3538
projectRoot: inputRoot,
@@ -42,6 +45,7 @@ for (const snapshotDirectory of snapshotDirectories) {
4245
}
4346
},
4447
})
48+
fs.mkdirSync(outputRoot, { recursive: true })
4549
fs.writeFileSync(
4650
path.join(outputRoot, 'dump.lsif-typed'),
4751
index.serializeBinary()

0 commit comments

Comments
 (0)