Skip to content

Commit 3a8e52c

Browse files
authored
Typechecking with JavaScript (#972)
Using the world-famous `jsconfig.json`
1 parent bf5e999 commit 3a8e52c

10 files changed

Lines changed: 63 additions & 14 deletions

File tree

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
path = blueprint-compiler
99
url = https://gitlab.gnome.org/jwestman/blueprint-compiler.git
1010
[submodule "src/langs/typescript/template/gi-types"]
11-
path = src/langs/typescript/gi-types
11+
path = gi-types
1212
url = https://gitlab.gnome.org/BrainBlasted/gi-typescript-definitions.git
1313
branch = nightly
Submodule gi-types updated from 0000000 to 396fe14

src/PanelCode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import GObject from "gi://GObject";
44
import { makeDropdownFlat, settings as global_settings } from "./util.js";
55
import { setupRustProject } from "./langs/rust/rust.js";
66
import { setupTypeScriptProject } from "./langs/typescript/typescript.js";
7+
import { setupJavascriptProject } from "./langs/javascript/javascript.js";
78

89
export default function PanelCode({
910
builder,
@@ -55,6 +56,12 @@ export default function PanelCode({
5556
stack_code.visible_child_name = panel.language;
5657
previewer.useInternal().catch(console.error);
5758

59+
if (panel.language.toLowerCase() === "javascript") {
60+
setupJavascriptProject(file, langs.javascript.document).catch(
61+
console.error,
62+
);
63+
}
64+
5865
if (panel.language.toLowerCase() === "rust") {
5966
setupRustProject(file).catch(console.error);
6067
}

src/common.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import GLib from "gi://GLib";
2-
31
import LSPClient from "./lsp/LSPClient.js";
42

53
const formatting_options = {
@@ -47,14 +45,7 @@ export const languages = [
4745
document: null,
4846
default_file: "main.js",
4947
index: 0,
50-
language_server: [
51-
"biome",
52-
"lsp-proxy",
53-
// src/meson.build installs biome.json there
54-
GLib.getenv("FLATPAK_ID")
55-
? `--config-path=${pkg.pkgdatadir}`
56-
: `--config-path=src/langs/javascript`,
57-
],
48+
language_server: ["typescript-language-server", "--stdio"],
5849
formatting_options: {
5950
...formatting_options,
6051
tabSize: 2,

src/langs/javascript/javascript.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Gio from "gi://Gio";
2+
13
import { createLSPClient } from "../../common.js";
2-
import { getLanguage } from "../../util.js";
4+
import { getLanguage, copy } from "../../util.js";
35

46
export function setup({ document }) {
57
const { file, buffer, code_view } = document;
@@ -30,3 +32,22 @@ export function setup({ document }) {
3032

3133
return lspc;
3234
}
35+
36+
const javascript_template_dir = Gio.File.new_for_path(
37+
pkg.pkgdatadir,
38+
).resolve_relative_path("langs/javascript/template");
39+
40+
export async function setupJavascriptProject(destination, document) {
41+
const destination_file = await copy(
42+
"jsconfig.json",
43+
javascript_template_dir,
44+
destination,
45+
Gio.FileCopyFlags.NONE,
46+
);
47+
48+
// Notify the language server that the jsconfig file was created
49+
// to initialize diagnostics and type checkings
50+
await document.lspc.notify("workspace/didCreateFile", {
51+
files: [{ uri: destination_file.get_uri() }],
52+
});
53+
}

src/langs/javascript/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
configure_file(
2+
input: 'template/jsconfig.json',
3+
output: 'jsconfig.json',
4+
install_dir: join_paths(pkgdatadir, 'langs/javascript/template/'),
5+
configuration: bin_conf,
6+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"checkJs": true,
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler",
6+
// TODO: should probably be fixed to ES2023, or whatever standard is
7+
// currently supported by the latest GJS
8+
"target": "ESNext",
9+
"outDir": "compiled_javascript",
10+
"baseUrl": ".",
11+
"paths": {
12+
"*": ["*", "@pkgdatadir@/langs/typescript/gi-types/*"]
13+
},
14+
"skipLibCheck": true
15+
},
16+
"include": [
17+
"main.js",
18+
"@pkgdatadir@/langs/typescript/types/ambient.d.ts",
19+
"@pkgdatadir@/langs/typescript/gi-types/gi.d.ts"
20+
]
21+
}

src/langs/typescript/meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ install_data(
1111
preserve_path: true,
1212
)
1313

14-
install_subdir('gi-types', install_dir: join_paths(pkgdatadir, 'langs/typescript'))
14+
install_subdir(
15+
meson.project_source_root() / 'gi-types',
16+
install_dir: join_paths(pkgdatadir, 'langs/typescript'),
17+
)

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ blueprint_compiler = find_program(
1515

1616
meson.add_install_script('../build-aux/library.js', pkgdatadir)
1717

18+
subdir('langs/javascript')
1819
subdir('langs/rust/template')
1920
subdir('langs/typescript')
2021

src/widgets/CodeView.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class CodeView extends Gtk.Widget {
7373
}
7474

7575
#onUpdate = () => {
76-
this.clearDiagnostics();
7776
this.emit("changed");
7877
};
7978

0 commit comments

Comments
 (0)