Skip to content

Commit

Permalink
Report an error on ts plugin version mismatch (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone authored Jul 8, 2024
1 parent f717e07 commit 390c7cc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/tsPlugin/initTsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { version as gratsTsVersion } from "typescript";
import type * as TS from "typescript/lib/tsserverlibrary";
import { extract } from "../Extractor";
import { FAKE_ERROR_CODE } from "../utils/DiagnosticError";
Expand Down Expand Up @@ -26,6 +27,24 @@ export function initTsPlugin(modules: { typescript: typeof TS }) {
proxy[k] = (...args: Array<any>) => x.apply(info.languageService, args);
}

if (ts.version !== gratsTsVersion) {
proxy.getCompilerOptionsDiagnostics = (): TS.Diagnostic[] => {
const prev = info.languageService.getCompilerOptionsDiagnostics();
return [
...prev,
{
category: ts.DiagnosticCategory.Error,
code: 0,
messageText: typeScriptVersionMismatch(ts.version),
file: undefined,
start: undefined,
length: undefined,
},
];
};
return proxy;
}

proxy.getSyntacticDiagnostics = (filename): TS.DiagnosticWithLocation[] => {
const prior = info.languageService.getSyntacticDiagnostics(filename);
const doc = info.languageService.getProgram()?.getSourceFile(filename);
Expand Down Expand Up @@ -103,3 +122,10 @@ export function initTsPlugin(modules: { typescript: typeof TS }) {

return { create };
}

function typeScriptVersionMismatch(extensionVersion: string) {
return `grats-plugin-ts error: The version of TypeScript picked up by Grats does not match the version used by VSCode.
Grats is using ${gratsTsVersion} but VSCode is using ${extensionVersion}.
This may be caused by a yarn.lock or package-lock.json which is pinning a different version of TypeScript for Grats than the version used by the rest of your project.
See https://github.com/captbaritone/grats/issues/142> for more information.`;
}

0 comments on commit 390c7cc

Please sign in to comment.