This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtypescript.ts
More file actions
43 lines (38 loc) · 1.65 KB
/
typescript.ts
File metadata and controls
43 lines (38 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as path from 'path'
import { cStyleComment } from './comments'
import { FilterContext, LanguageSpec, Result, LSIFSupport } from './spec'
import { extractFromLines, filterResultsByImports, removeExtension } from './util'
/**
* Filter a list of candidate definitions to select those likely to be valid
* cross-references for a definition in this file. Accept candidates whose
* path matches a relative import.
*
* If no candidates match, fall back to the raw (unfiltered) results so that
* the user doesn't get an empty response unless there really is nothing.
*/
function filterDefinitions<T extends Result>(results: T[], { filePath, fileContent }: FilterContext): T[] {
const importPaths = extractFromLines(fileContent, /\bfrom ["'](.*)["'];?$/, /\brequire\(["'](.*)["']\)/)
return filterResultsByImports(
results,
importPaths,
// Match results with a basename suffix of an import candidate
({ file }, importPath) => candidates(filePath, importPath).includes(removeExtension(file))
)
}
/**
* Construct a list of candidate paths that serve the given import path
* relative to the given source path.
*/
function candidates(sourcePath: string, importPath: string): string[] {
return [path.join(path.dirname(sourcePath), importPath), path.join(path.dirname(sourcePath), importPath, 'index')]
}
export const typescriptSpec: LanguageSpec = {
languageID: 'typescript',
additionalLanguages: ['javascript'],
stylized: 'TypeScript',
fileExts: ['ts', 'tsx', 'js', 'jsx'],
commentStyles: [cStyleComment],
filterDefinitions,
lsifSupport: LSIFSupport.Robust,
textDocumentImplemenationSupport: true,
}