Skip to content

Commit

Permalink
Add in-memory strict language server (#67)
Browse files Browse the repository at this point in the history
* Add in-memory strict language server

* Proxy mutative-seeming methods

* Bump version to 2.2.2
  • Loading branch information
heyimalex authored Jan 29, 2024
1 parent 28fe6e1 commit ef0fe25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
29 changes: 22 additions & 7 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
27 changes: 18 additions & 9 deletions src/plugin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof ts.LanguageService>) {
const serviceFunction = info.languageService[k];
Expand All @@ -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);
}

0 comments on commit ef0fe25

Please sign in to comment.