UNPKG

alm

Version:

The best IDE for TypeScript

112 lines (111 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../../../../common/utils"); var typescriptDir = require("./typeScriptDir"); var utils = require("../../../../common/utils"); var languageServiceHostNode_1 = require("../../../../languageServiceHost/languageServiceHostNode"); var master; function setMaster(m) { master = m; } exports.setMaster = setMaster; /** * Wraps up `langaugeService` `languageServiceHost` and `projectFile` in a single package */ var Project = /** @class */ (function () { function Project(projectData) { var _this = this; this.configFile = projectData.configFile; this.languageServiceHost = new languageServiceHostNode_1.LanguageServiceHost(projectData.configFile.projectFilePath, projectData.configFile.project.compilerOptions) .addAlmDemo(); // Add all the files projectData.filePathWithContents.forEach(function (_a) { var filePath = _a.filePath, contents = _a.contents; _this.languageServiceHost.addScript(filePath, contents); }); this.languageServiceHost.incrementallyAddedFile.on(function (data) { // console.log(data); // DEBUG master.receiveIncrementallyAddedFile(data); }); this.languageService = ts.createLanguageService(this.languageServiceHost, ts.createDocumentRegistry()); } Project.prototype.getAllSourceFiles = function () { return this.languageService.getProgram().getSourceFiles().concat(); }; /** * all files * - except lib.d.ts * - And files in `node_modules * Note: this function is exceedingly slow on cold boot (13s on vscode codebase) as it calls getProgram.getSourceFiles */ Project.prototype.getProjectSourceFiles = function () { var libFileLookup = utils_1.createMap(typescriptDir.getDefaultLibFilePaths(this.configFile.project.compilerOptions)); var files = this.getAllSourceFiles().filter(function (x) { return !libFileLookup[x.fileName]; }); return files; }; Project.prototype.getSourceFile = function (filePath) { return this.getAllSourceFiles().find(function (f) { return f.fileName === filePath; }); }; Project.prototype.includesSourceFile = function (filePath) { return (this.getAllSourceFiles().filter(function (f) { return f.fileName === filePath; }).length === 1); }; /** * Gets all the files in the project that are not `.json` files */ Project.prototype.getFilePaths = function () { return (this.configFile.project.files).filter(function (f) { return !f.endsWith('.json'); }); }; Project.prototype.getDiagnosticsForFile = function (filePath) { var diagnostics = this.languageService.getSyntacticDiagnostics(filePath); if (diagnostics.length === 0) { if (this.configFile.project.compilerOptions.skipLibCheck && filePath.endsWith('.d.ts')) { // Nothing to do } else { diagnostics = this.languageService.getSemanticDiagnostics(filePath); } } return diagnostics; }; Project.prototype.getDiagnostics = function (cancellationToken) { var _this = this; var program = this.languageService.getProgram(); return new Promise(function (resolve, reject) { var allDiagnostics = []; allDiagnostics = program.getGlobalDiagnostics().concat(); var sourceFiles = program.getSourceFiles().concat(); utils .cancellableForEach({ cancellationToken: cancellationToken, items: sourceFiles, cb: function (sourceFile) { ts.addRange(allDiagnostics, program.getSyntacticDiagnostics(sourceFile)); }, }) .then(function () { return utils.cancellableForEach({ cancellationToken: cancellationToken, items: sourceFiles, cb: function (sourceFile) { if (_this.configFile.project.compilerOptions.skipLibCheck && sourceFile.isDeclarationFile) { // Nothing to do } else { ts.addRange(allDiagnostics, program.getSemanticDiagnostics(sourceFile)); } }, }); }) .then(function () { allDiagnostics = ts.sortAndDeduplicateDiagnostics(allDiagnostics); resolve(allDiagnostics); }) .catch(function (res) { reject(res); }); }); }; return Project; }()); exports.Project = Project;