Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 91 92 93 94 95 96 97 98 99 100 101 | 5x 5x 145x 145x 145x 145x 145x 145x 145x 145x 145x 145x | import {
CompilerOptions,
FileSystemHost,
InMemoryFileSystemHost,
RealFileSystemHost,
SyntaxKind,
ts
} from '@ts-morph/common';
import * as path from 'path';
import { Node, SourceFile, Project } from 'ts-morph';
type IsAny<T> = 0 extends 1 & T ? true : false;
const fileSystem = new RealFileSystemHost();
const versionLibFolder = new Map<string, string>();
function getCompilerLibFolder(version: string) {
if (!versionLibFolder.has(version)) versionLibFolder.set(version, getFolder());
return versionLibFolder.get(version)!;
function getFolder() {
const basePath = path.join(__dirname, `../../../../../common/node_modules/`);
const versionPath = path.join(basePath, `typescript-${version}/lib`);
if (fileSystem.directoryExistsSync(versionPath)) return versionPath;
else return path.join(basePath, `typescript/lib`);
}
}
export interface GetInfoFromTextOptions {
isDefinitionFile?: boolean;
filePath?: string;
host?: FileSystemHost;
compilerOptions?: CompilerOptions;
includeLibDts?: boolean;
isJsx?: boolean;
}
export interface GetInfoFromTextResult<TFirstChild extends Node> extends GetInfoFromTextInternalResult {
// typescript bug fix in ts 3.5.1 (todo: check if this works by removing the conditional type in a future version)
firstChild: IsAny<TFirstChild> extends true ? Node : TFirstChild;
}
export interface GetInfoFromTextWithDescendantResult<TDescendant extends Node>
extends GetInfoFromTextInternalResult {
// typescript bug fix in ts 3.5.1 (todo: check if this works by removing the conditional type in a future version)
descendant: IsAny<TDescendant> extends true ? Node : TDescendant;
}
export interface GetInfoFromTextInternalResult {
project: Project;
sourceFile: SourceFile;
}
// I know type parameters aren't supposed to be used this way, but it's way too convenient
export function getInfoFromText<TFirstChild extends Node = Node>(
text: string,
opts?: GetInfoFromTextOptions
): GetInfoFromTextResult<TFirstChild> {
const info = getInfoFromTextInternal(text, opts);
return {
...info,
firstChild: info.sourceFile.forEachChild((child) => child) as any
};
}
// todo: use the mapping between syntax kind and nodes for the descendant
export function getInfoFromTextWithDescendant<TDescendant extends Node>(
text: string,
descendantKind: SyntaxKind,
opts?: GetInfoFromTextOptions
): GetInfoFromTextWithDescendantResult<TDescendant> {
const info = getInfoFromTextInternal(text, opts);
return {
...info,
descendant: info.sourceFile.getFirstDescendantByKindOrThrow(descendantKind) as any
};
}
function getInfoFromTextInternal(text: string, opts?: GetInfoFromTextOptions) {
const {
isDefinitionFile = false,
isJsx = false,
filePath = undefined,
host = new InMemoryFileSystemHost(),
compilerOptions = undefined,
includeLibDts = false
} = opts || {};
const project = new Project({ compilerOptions, fileSystem: host, skipLoadingLibFiles: !includeLibDts });
const sourceFile = project.createSourceFile(getFilePath(), text);
return { project, sourceFile };
function getFilePath() {
Iif (filePath != null) return filePath;
Iif (isJsx) return 'testFile.tsx';
return isDefinitionFile ? 'testFile.d.ts' : 'testFile.ts';
}
}
|