Skip to content

Commit ef0fe25

Browse files
authored
Add in-memory strict language server (#67)
* Add in-memory strict language server * Proxy mutative-seeming methods * Bump version to 2.2.2
1 parent 28fe6e1 commit ef0fe25

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-strict-plugin",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "Typescript tools that help with migration to the strict mode",
55
"author": "Allegro",
66
"contributors": [

src/plugin/index.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
import { PluginStrictFileChecker } from './PluginStrictFileChecker';
2-
import { log, PluginInfo, setupProxy, turnOffStrictMode, turnOnStrictMode } from './utils';
2+
import {
3+
log,
4+
PluginInfo,
5+
setupLanguageServiceProxy,
6+
setupStrictLanguageServiceHostProxy,
7+
} from './utils';
38
import * as ts from 'typescript/lib/tsserverlibrary';
49

5-
const init: ts.server.PluginModuleFactory = () => {
10+
const init: ts.server.PluginModuleFactory = ({ typescript }) => {
611
function create(info: PluginInfo) {
7-
const proxy = setupProxy(info);
12+
const proxy = setupLanguageServiceProxy(info);
13+
14+
const strictLanguageServiceHost = setupStrictLanguageServiceHostProxy(info);
15+
const strictLanguageService = typescript.createLanguageService(strictLanguageServiceHost);
16+
817
log(info, 'Plugin initialized');
918

1019
proxy.getSemanticDiagnostics = function (filePath) {
1120
const strictFile = new PluginStrictFileChecker(info).isFileStrict(filePath);
1221

1322
if (strictFile) {
14-
turnOnStrictMode(info);
23+
return strictLanguageService.getSemanticDiagnostics(filePath);
1524
} else {
16-
turnOffStrictMode(info);
25+
return info.languageService.getSemanticDiagnostics(filePath);
1726
}
18-
19-
return info.languageService.getSemanticDiagnostics(filePath);
27+
};
28+
proxy.cleanupSemanticCache = function () {
29+
strictLanguageService.cleanupSemanticCache();
30+
info.languageService.cleanupSemanticCache();
31+
};
32+
proxy.dispose = function () {
33+
strictLanguageService.dispose();
34+
info.languageService.dispose();
2035
};
2136

2237
return proxy;

src/plugin/utils.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import { PLUGIN_NAME } from '../common/constants';
33

44
export type PluginInfo = ts.server.PluginCreateInfo;
55

6-
export function turnOnStrictMode(info: PluginInfo): void {
7-
info.project['compilerOptions'].strict = true;
8-
}
9-
10-
export function turnOffStrictMode(info: PluginInfo): void {
11-
info.project['compilerOptions'].strict = false;
12-
}
13-
14-
export function setupProxy(info: PluginInfo) {
6+
export function setupLanguageServiceProxy(info: PluginInfo) {
157
const proxy: ts.LanguageService = Object.create(null);
168
for (const k of Object.keys(info.languageService) as Array<keyof ts.LanguageService>) {
179
const serviceFunction = info.languageService[k];
@@ -22,6 +14,23 @@ export function setupProxy(info: PluginInfo) {
2214
return proxy;
2315
}
2416

17+
export function setupStrictLanguageServiceHostProxy(info: PluginInfo): ts.LanguageServiceHost {
18+
const host = info.languageServiceHost;
19+
const strictGetCompilationSettings = () => {
20+
const settings = info.languageServiceHost.getCompilationSettings();
21+
return { ...settings, strict: true };
22+
};
23+
const proxy = new Proxy(host, {
24+
get(target, prop, receiver) {
25+
if (prop === 'getCompilationSettings') {
26+
return strictGetCompilationSettings;
27+
}
28+
return Reflect.get(target, prop, receiver);
29+
},
30+
});
31+
return proxy;
32+
}
33+
2534
export function log(info: PluginInfo, message: string) {
2635
info.project.projectService.logger.info(`[${PLUGIN_NAME}]: ` + message);
2736
}

0 commit comments

Comments
 (0)