Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in-memory strict language server #67

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading