diff --git a/package.json b/package.json index de8f47d..7da7169 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-strict-plugin", - "version": "2.2.1", + "version": "2.2.2", "description": "Typescript tools that help with migration to the strict mode", "author": "Allegro", "contributors": [ diff --git a/src/plugin/index.ts b/src/plugin/index.ts index aa1dee0..26f57db 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -1,22 +1,37 @@ import { PluginStrictFileChecker } from './PluginStrictFileChecker'; -import { log, PluginInfo, setupProxy, turnOffStrictMode, turnOnStrictMode } from './utils'; +import { + log, + PluginInfo, + setupLanguageServiceProxy, + setupStrictLanguageServiceHostProxy, +} from './utils'; import * as ts from 'typescript/lib/tsserverlibrary'; -const init: ts.server.PluginModuleFactory = () => { +const init: ts.server.PluginModuleFactory = ({ typescript }) => { function create(info: PluginInfo) { - const proxy = setupProxy(info); + const proxy = setupLanguageServiceProxy(info); + + const strictLanguageServiceHost = setupStrictLanguageServiceHostProxy(info); + const strictLanguageService = typescript.createLanguageService(strictLanguageServiceHost); + log(info, 'Plugin initialized'); proxy.getSemanticDiagnostics = function (filePath) { const strictFile = new PluginStrictFileChecker(info).isFileStrict(filePath); if (strictFile) { - turnOnStrictMode(info); + return strictLanguageService.getSemanticDiagnostics(filePath); } else { - turnOffStrictMode(info); + return info.languageService.getSemanticDiagnostics(filePath); } - - return info.languageService.getSemanticDiagnostics(filePath); + }; + proxy.cleanupSemanticCache = function () { + strictLanguageService.cleanupSemanticCache(); + info.languageService.cleanupSemanticCache(); + }; + proxy.dispose = function () { + strictLanguageService.dispose(); + info.languageService.dispose(); }; return proxy; diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index eef87d1..f5dc89a 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -3,15 +3,7 @@ import { PLUGIN_NAME } from '../common/constants'; export type PluginInfo = ts.server.PluginCreateInfo; -export function turnOnStrictMode(info: PluginInfo): void { - info.project['compilerOptions'].strict = true; -} - -export function turnOffStrictMode(info: PluginInfo): void { - info.project['compilerOptions'].strict = false; -} - -export function setupProxy(info: PluginInfo) { +export function setupLanguageServiceProxy(info: PluginInfo) { const proxy: ts.LanguageService = Object.create(null); for (const k of Object.keys(info.languageService) as Array) { const serviceFunction = info.languageService[k]; @@ -22,6 +14,23 @@ export function setupProxy(info: PluginInfo) { return proxy; } +export function setupStrictLanguageServiceHostProxy(info: PluginInfo): ts.LanguageServiceHost { + const host = info.languageServiceHost; + const strictGetCompilationSettings = () => { + const settings = info.languageServiceHost.getCompilationSettings(); + return { ...settings, strict: true }; + }; + const proxy = new Proxy(host, { + get(target, prop, receiver) { + if (prop === 'getCompilationSettings') { + return strictGetCompilationSettings; + } + return Reflect.get(target, prop, receiver); + }, + }); + return proxy; +} + export function log(info: PluginInfo, message: string) { info.project.projectService.logger.info(`[${PLUGIN_NAME}]: ` + message); }