All files / lib/utils getInfoFromText.js

65.38% Statements 34/52
60% Branches 27/45
53.84% Functions 7/13
65.9% Lines 29/44

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  4x 64x 64x         4x 4x       4x 4x 4x 64x 4x 4x   4x 4x 4x 4x 4x 4x 4x                               15x 15x   4x           4x   15x 15x 15x 15x   15x   15x   15x      
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    Iif (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInfoFromTextWithDescendant = exports.getInfoFromText = void 0;
const common_1 = require("@ts-morph/common");
const path = __importStar(require("path"));
const ts_morph_1 = require("ts-morph");
const fileSystem = new common_1.RealFileSystemHost();
const versionLibFolder = new Map();
function getCompilerLibFolder(version) {
    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`);
    }
}
// I know type parameters aren't supposed to be used this way, but it's way too convenient
function getInfoFromText(text, opts) {
    const info = getInfoFromTextInternal(text, opts);
    return Object.assign(Object.assign({}, info), { firstChild: info.sourceFile.forEachChild((child) => child) });
}
exports.getInfoFromText = getInfoFromText;
// todo: use the mapping between syntax kind and nodes for the descendant
function getInfoFromTextWithDescendant(text, descendantKind, opts) {
    const info = getInfoFromTextInternal(text, opts);
    return Object.assign(Object.assign({}, info), { descendant: info.sourceFile.getFirstDescendantByKindOrThrow(descendantKind) });
}
exports.getInfoFromTextWithDescendant = getInfoFromTextWithDescendant;
function getInfoFromTextInternal(text, opts) {
    const { isDefinitionFile = false, isJsx = false, filePath = undefined, host = new common_1.InMemoryFileSystemHost(), compilerOptions = undefined, includeLibDts = false } = opts || {};
    const project = new ts_morph_1.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';
    }
}