This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
/
DeclarationIndexHelpers.ts
90 lines (82 loc) · 3.46 KB
/
DeclarationIndexHelpers.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { join, normalize, parse, relative } from 'path';
import { DeclarationInfo, ExternalModuleImport, Import, NamedImport, NamespaceImport } from 'typescript-parser';
/**
* Calculates a list of declarationInfos filtered by the already imported ones in the given document.
* The result is a list of declarations that are not already imported by the document.
*
* @export
* @param {ResolveIndex} resolveIndex
* @param {string} documentPath
* @param {TsImport[]} imports
* @param {string} [rootPath]
* @returns {DeclarationInfo[]}
*/
export function getDeclarationsFilteredByImports(
declarationInfos: DeclarationInfo[],
documentPath: string,
imports: Import[],
rootPath?: string,
): DeclarationInfo[] {
let declarations = declarationInfos;
for (const tsImport of imports) {
const importedLib = getAbsolutLibraryName(tsImport.libraryName, documentPath, rootPath);
if (tsImport instanceof NamedImport) {
declarations = declarations
.filter(o => o.from !== importedLib || !(tsImport as NamedImport).specifiers
.some(s => s.specifier === o.declaration.name));
// if (tsImport.defaultAlias) {
// else if (tsImport instanceof DefaultImport) {
// declarations = declarations
// .filter(o => (!(o.declaration instanceof DefaultDeclaration) || importedLib !== o.from));
// }
// }
} else if (tsImport instanceof NamespaceImport || tsImport instanceof ExternalModuleImport) {
declarations = declarations.filter(o => o.from !== tsImport.libraryName);
}
}
return declarations;
}
/**
* Returns the absolut workspace specific library path.
* If the library is a node module or a typings module, the name
* is returned. If the "lib" is in the local workspace, then the
* absolut path from the workspace root is returned.
*
* @param {string} library Name of the library
* @param {string} actualFilePath Filepath of the actually open file
* @param {string} [rootPath] Root path of the workspace
* @returns {string} Absolut path from the workspace root to the desired library
*/
export function getAbsolutLibraryName(library: string, actualFilePath: string, rootPath?: string): string {
if (!library.startsWith('.') || !rootPath) {
return library;
}
return '/' + relative(
rootPath,
normalize(join(parse(actualFilePath).dir, library)),
).replace(/[/]$/g, '');
}
/**
* Returns the relative path to a specific library.
* If the library is a node module or a typings module, the name
* is returned. If the "lib" is in the local workspace, then the
* relative path from the actual file is returned.
*
* @param {string} library Name of the library
* @param {string} actualFilePath Filepath of the actually open file
* @param {string} [rootPath] Root path of the workspace
* @returns {string} Relative path from the actual file to the library
*/
export function getRelativeLibraryName(library: string, actualFilePath: string, rootPath?: string): string {
if (!library.startsWith('/') || !rootPath) {
return library;
}
const actualDir = parse('/' + relative(rootPath, actualFilePath)).dir;
let relativePath = relative(actualDir, library);
if (!relativePath.startsWith('.')) {
relativePath = './' + relativePath;
} else if (relativePath === '..') {
relativePath += '/';
}
return relativePath.replace(/\\/g, '/');
}