Skip to content

Commit 10b9260

Browse files
committed
use import instead of require in Installer
1 parent 3376fde commit 10b9260

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

src/FileTree.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as vscode from "vscode";
22

33
import { Block, getQueryBlocks } from "./BlockTree";
4-
import Parser, { Query, SyntaxNode, Tree } from "tree-sitter";
4+
import Parser, { type Language, Query, SyntaxNode, Tree } from "tree-sitter";
55
import { Result, err, ok } from "./result";
66

7-
import { Language } from "./Installer";
87
import { Selection } from "./Selection";
98
import { getLanguageConfig } from "./configuration";
109
import { getLogger } from "./outputChannel";

src/Installer.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import * as vscode from "vscode";
55
import { ExecException, ExecOptions, exec } from "child_process";
66
import { Result, err, ok } from "./result";
77
import { existsSync, rmSync } from "fs";
8-
import Parser from "tree-sitter";
8+
import type { Language } from "tree-sitter";
99
import { getLogger } from "./outputChannel";
1010
import { mkdir } from "fs/promises";
1111
import which from "which";
1212

1313
const NPM_INSTALL_URL = "https://nodejs.org/en/download";
1414

15-
export type Language = Parser.Language;
16-
1715
export function getAbsoluteParserDir(parsersDir: string, parserName: string): string {
1816
return path.resolve(path.join(parsersDir, parserName));
1917
}
@@ -22,11 +20,11 @@ export function getAbsoluteBindingsDir(parsersDir: string, parserName: string):
2220
return path.resolve(path.join(parsersDir, parserName, "bindings", "node", "index.js"));
2321
}
2422

25-
export function loadParser(
23+
export async function loadParser(
2624
parsersDir: string,
2725
parserName: string,
2826
subdirectory?: string
29-
): Result<Language, string> {
27+
): Promise<Result<Language, string>> {
3028
const logger = getLogger();
3129

3230
const bindingsDir = getAbsoluteBindingsDir(parsersDir, parserName);
@@ -39,13 +37,14 @@ export function loadParser(
3937
try {
4038
logger.log(`Loading parser from ${bindingsDir}`);
4139

42-
// using dynamic import causes issues on windows
43-
// make sure to test well on windows before changing this
44-
// TODO(02/11/24): change to dynamic import
45-
// let { default: language } = (await import(bindingsDir)) as { default: Language };
46-
47-
// eslint-disable-next-line @typescript-eslint/no-var-requires
48-
let language = require(bindingsDir) as Language;
40+
let language: Language;
41+
try {
42+
language = ((await import(bindingsDir)) as { default: Language }).default;
43+
} catch (error) {
44+
// TODO(1/11/25): Always use import and remove this backup
45+
// eslint-disable-next-line @typescript-eslint/no-var-requires
46+
language = require(bindingsDir) as Language;
47+
}
4948

5049
logger.log(`Got language: ${JSON.stringify(Object.keys(language))}`);
5150

@@ -118,7 +117,7 @@ export async function downloadAndBuildParser(
118117
}
119118

120119
// try to load parser optimistically
121-
const loadResult = loadParser(parsersDir, parserName);
120+
const loadResult = await loadParser(parsersDir, parserName);
122121
if (loadResult.status === "ok") {
123122
return ok(undefined);
124123
}
@@ -137,7 +136,7 @@ export async function downloadAndBuildParser(
137136
}
138137

139138
// if it fails, try to build it
140-
const buildResult = await runCmd(`${treeSitterCli} generate`, { cwd: parserDir }, (d) =>
139+
const buildResult = await runCmd(`${treeSitterCli} build`, { cwd: parserDir }, (d) =>
141140
onData?.(d.toString())
142141
);
143142
if (buildResult.status === "err") {
@@ -273,7 +272,7 @@ export async function getLanguage(
273272
}
274273
}
275274

276-
const loadResult = loadParser(parsersDir, parserName, subdirectory);
275+
const loadResult = await loadParser(parsersDir, parserName, subdirectory);
277276
if (loadResult.status === "err") {
278277
const msg = `Failed to load parser for language ${languageId} > ${loadResult.result}`;
279278

src/test/suite/Installer.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import * as vscode from "vscode";
44
import Parser from "tree-sitter";
55
import { TreeViewer } from "../../TreeViewer";
66
import { openDocument } from "./testUtils";
7+
import path from "path";
78

89
export async function testParser(language: string, content?: string): Promise<void> {
910
// fail the test if the parser could not be installed
10-
const result = await Installer.getLanguage("test-parsers", language, true);
11+
const testParsersDir = path.resolve(__dirname, "..", "..", "..", "test-parsers");
12+
const result = await Installer.getLanguage(testParsersDir, language, true);
1113
if (result.status === "err") {
1214
throw new Error(`Failed to install language: ${JSON.stringify(result.result)}`);
1315
}
@@ -49,15 +51,15 @@ suite("Installer integration tests", function () {
4951
["Ruby", "ruby", "def foo\nend\ndef bar\nend"],
5052
// ["SQL", "sql"],
5153
["HTML", "html", "<html></html>"],
52-
// ["CSS", "css", "body { color: red; }"],
54+
["CSS", "css", "body { color: red; }"],
5355
["YAML", "yaml", "key: value"],
5456
["JSON", "json", '{ "key": "value" }'],
5557
["XML", "xml"],
5658
["Markdown", "markdown", "# Title"],
5759
// ["LaTeX", "latex"],
5860
["Bash", "shellscript", "echo 'Hello, World!'"],
5961
["TOML", "toml"],
60-
// ["Swift", "swift"],
62+
["Swift", "swift"],
6163
["Kotlin", "kotlin", "fun main() { }"],
6264
["Zig", "zig", 'const std = @import("std");\n\npub fn main() void { }'],
6365
];

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "ES2022",
3+
"module": "nodenext",
4+
"moduleResolution": "nodenext",
5+
"target": "ES2020",
56
"outDir": "out",
67
"lib": [
78
"ES2022",

0 commit comments

Comments
 (0)