UNPKG

ts-lit-plugin

Version:

Typescript plugin that adds type checking and code completion to lit-html

117 lines (116 loc) 5.57 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; /* eslint-disable @typescript-eslint/no-explicit-any */ var lit_analyzer_1 = require("lit-analyzer"); var web_component_analyzer_1 = require("web-component-analyzer"); var decorate_language_service_js_1 = require("./decorate-language-service.js"); var logger_js_1 = require("./logger.js"); var lit_plugin_context_js_1 = require("./ts-lit-plugin/lit-plugin-context.js"); var ts_lit_plugin_js_1 = require("./ts-lit-plugin/ts-lit-plugin.js"); var ts_module_js_1 = require("./ts-module.js"); var tsHtmlPluginSymbol = Symbol.for("__tsHtmlPlugin__"); var context = undefined; /** * Export a function for the ts-service to initialize our plugin. * @param typescript */ function init(_a) { var typescript = _a.typescript; // Cache the typescript module (0, ts_module_js_1.setTypescriptModule)(typescript); /** * This function is used to print debug info once * Yes, it's a self destructing function! */ var printDebugOnce = function () { if (logger_js_1.logger.level >= lit_analyzer_1.LitAnalyzerLoggerLevel.DEBUG) { logger_js_1.logger.debug("Lit Analyzer: ".concat(lit_analyzer_1.VERSION)); logger_js_1.logger.debug("Web Component Analyzer: ".concat(web_component_analyzer_1.VERSION)); logger_js_1.logger.debug("Running Typescript: ".concat(typescript.version)); logger_js_1.logger.debug("DIRNAME: ".concat(__dirname)); printDebugOnce = undefined; } }; return { create: function (info) { // Check if the language service is already decorated if (info.languageService[tsHtmlPluginSymbol] != null) { return info.languageService; } logger_js_1.logger.setTsServerLogging(info.project.projectService.logger); // Save the current working directory info.config.cwd = info.config.cwd || info.project.getCurrentDirectory(); // Extend existing language service with the plugin functions try { context = new lit_plugin_context_js_1.LitPluginContext({ ts: typescript, getProgram: function () { return info.languageService.getProgram(); }, getProject: function () { return info.project; } }); context.updateConfig((0, lit_analyzer_1.makeConfig)(info.config)); logger_js_1.logger.verbose("Starting ts-lit-plugin..."); if (printDebugOnce != null) printDebugOnce(); var plugin = new ts_lit_plugin_js_1.TsLitPlugin(info.languageService, context); var decoratedService = (0, decorate_language_service_js_1.decorateLanguageService)(info.languageService, plugin); // Save that we've extended this service to prevent extending it again decoratedService[tsHtmlPluginSymbol] = plugin; return decoratedService; } catch (e) { logger_js_1.logger.error("ts-lit-plugin crashed while decorating the language service...", e); return info.languageService; } }, /** * Unfortunately this function isn't called with configuration from tsconfig.json * @param externalConfig */ onConfigurationChanged: function (externalConfig) { var _a; if (context == null || externalConfig == null) return; // Manually merge in configuration from "tsconfig.json" var compilerOptions = (_a = context.project) === null || _a === void 0 ? void 0 : _a.getCompilerOptions(); var tsLitPluginOptions = compilerOptions != null ? readLitAnalyzerConfigFromCompilerOptions(compilerOptions) : undefined; // Make seed where options from "external" takes precedence over options from "tsconfig.json" var configSeed = __assign(__assign(__assign({}, (tsLitPluginOptions || {})), externalConfig), { // Also merge rules deep rules: __assign(__assign({}, ((tsLitPluginOptions === null || tsLitPluginOptions === void 0 ? void 0 : tsLitPluginOptions.rules) || {})), (externalConfig.rules || {})) }); context.updateConfig((0, lit_analyzer_1.makeConfig)(configSeed)); if (printDebugOnce != null) printDebugOnce(); } }; } exports.init = init; /** * Resolves the nearest tsconfig.json and returns the configuration seed within the plugins section for "ts-lit-plugin" */ function readLitAnalyzerConfigFromCompilerOptions(compilerOptions) { // Finds the plugin section if ("plugins" in compilerOptions) { var plugins = compilerOptions.plugins; var tsLitPluginOptions = plugins.find(function (plugin) { return plugin.name === "ts-lit-plugin"; }); if (tsLitPluginOptions != null) { return tsLitPluginOptions; } } return undefined; }