UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

140 lines (139 loc) 6.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var codeBlockWriter_1 = require("./codeBlockWriter"); var compiler_1 = require("./compiler"); var errors = require("./errors"); var factories_1 = require("./factories"); var fileSystem_1 = require("./fileSystem"); var options_1 = require("./options"); var typescript_1 = require("./typescript"); var utils_1 = require("./utils"); var createWrappedNode_1 = require("./utils/compiler/createWrappedNode"); /** * Context for a project instance. * @internal */ var ProjectContext = /** @class */ (function () { function ProjectContext(fileSystemWrapper, compilerOptions, opts) { var _this = this; this._compilerOptions = new options_1.CompilerOptionsContainer(); this.logger = new utils_1.ConsoleLogger(); this.manipulationSettings = new options_1.ManipulationSettingsContainer(); this.fileSystemWrapper = fileSystemWrapper; this._compilerOptions.set(compilerOptions); this.compilerFactory = new factories_1.CompilerFactory(this); this.inProjectCoordinator = new factories_1.InProjectCoordinator(this.compilerFactory); this.structurePrinterFactory = new factories_1.StructurePrinterFactory(function () { return _this.manipulationSettings.getFormatCodeSettings(); }); this.lazyReferenceCoordinator = new utils_1.LazyReferenceCoordinator(this.compilerFactory); this.directoryCoordinator = new fileSystem_1.DirectoryCoordinator(this.compilerFactory, fileSystemWrapper); this._languageService = opts.createLanguageService ? new compiler_1.LanguageService(this) : undefined; if (opts.typeChecker != null) { errors.throwIfTrue(opts.createLanguageService, "Cannot specify a type checker and create a language service."); this._customTypeChecker = new compiler_1.TypeChecker(this); this._customTypeChecker._reset(function () { return opts.typeChecker; }); } } Object.defineProperty(ProjectContext.prototype, "compilerOptions", { /** Gets the compiler options. */ get: function () { return this._compilerOptions; }, enumerable: true, configurable: true }); Object.defineProperty(ProjectContext.prototype, "languageService", { /** Gets the language service. Throws an exception if it doesn't exist. */ get: function () { if (this._languageService == null) throw this.getToolRequiredError("language service"); return this._languageService; }, enumerable: true, configurable: true }); Object.defineProperty(ProjectContext.prototype, "program", { /** * Gets the program. */ get: function () { if (this._languageService == null) throw this.getToolRequiredError("program"); return this.languageService.getProgram(); }, enumerable: true, configurable: true }); Object.defineProperty(ProjectContext.prototype, "typeChecker", { /** * Gets the type checker. */ get: function () { if (this._customTypeChecker != null) return this._customTypeChecker; if (this._languageService == null) throw this.getToolRequiredError("type checker"); return this.program.getTypeChecker(); }, enumerable: true, configurable: true }); /** * Gets if this object has a language service. */ ProjectContext.prototype.hasLanguageService = function () { return this._languageService != null; }; /** * Gets the encoding. */ ProjectContext.prototype.getEncoding = function () { return this.compilerOptions.get().charset || "utf-8"; }; /** * Helper for getting the format code settings. */ ProjectContext.prototype.getFormatCodeSettings = function () { return this.manipulationSettings.getFormatCodeSettings(); }; /** * Helper for getting the user preferences. */ ProjectContext.prototype.getUserPreferences = function () { return this.manipulationSettings.getUserPreferences(); }; /** * Resets the program. */ ProjectContext.prototype.resetProgram = function () { this.languageService._resetProgram(); }; /** * Creates a code block writer. */ ProjectContext.prototype.createWriter = function () { var indentationText = this.manipulationSettings.getIndentationText(); return new codeBlockWriter_1.CodeBlockWriter({ newLine: this.manipulationSettings.getNewLineKindAsString(), indentNumberOfSpaces: indentationText === options_1.IndentationText.Tab ? undefined : indentationText.length, useTabs: indentationText === options_1.IndentationText.Tab, useSingleQuote: this.manipulationSettings.getQuoteKind() === compiler_1.QuoteKind.Single }); }; /** * Gets the pre-emit diagnostics. * @param sourceFile - Optional source file to filter the results by. */ ProjectContext.prototype.getPreEmitDiagnostics = function (sourceFile) { var _this = this; var compilerDiagnostics = typescript_1.ts.getPreEmitDiagnostics(this.program.compilerObject, sourceFile == null ? undefined : sourceFile.compilerNode); return compilerDiagnostics.map(function (d) { return _this.compilerFactory.getDiagnostic(d); }); }; ProjectContext.prototype.getToolRequiredError = function (name) { return new errors.InvalidOperationError("A " + name + " is required for this operation. " + "This might occur when manipulating or getting type information from a node that was not added " + ("to a Project object and created via " + "createWrappedNode" + ". ") + ("Please submit a bug report if you don't believe a " + name + " should be required for this operation.")); }; return ProjectContext; }()); exports.ProjectContext = ProjectContext;