feat: migrate to ESM
This commit is contained in:
parent
67f50b85f5
commit
39a67eec61
59 changed files with 132 additions and 80 deletions
|
|
@ -297,7 +297,7 @@ pub fn generate_grammar_files(
|
|||
)
|
||||
},
|
||||
|path| {
|
||||
let contents = fs::read_to_string(path)?
|
||||
let mut contents = fs::read_to_string(path)?
|
||||
.replace(
|
||||
r#""node-addon-api": "^8.3.1"#,
|
||||
r#""node-addon-api": "^8.5.0""#,
|
||||
|
|
@ -311,6 +311,16 @@ pub fn generate_grammar_files(
|
|||
"tree-sitter": "^0.22.4",
|
||||
"tree-sitter-cli":"#},
|
||||
);
|
||||
if !contents.contains("module") {
|
||||
eprintln!("Updating package.json");
|
||||
contents = contents.replace(
|
||||
indoc! {r#"
|
||||
"repository": {"#},
|
||||
indoc! {r#"
|
||||
"type": "module",
|
||||
"repository": {"#},
|
||||
);
|
||||
}
|
||||
write_file(path, contents)?;
|
||||
Ok(())
|
||||
},
|
||||
|
|
@ -318,9 +328,20 @@ pub fn generate_grammar_files(
|
|||
|
||||
// Do not create a grammar.js file in a repo with multiple language configs
|
||||
if !tree_sitter_config.has_multiple_language_configs() {
|
||||
missing_path(repo_path.join("grammar.js"), |path| {
|
||||
generate_file(path, GRAMMAR_JS_TEMPLATE, language_name, &generate_opts)
|
||||
})?;
|
||||
missing_path_else(
|
||||
repo_path.join("grammar.js"),
|
||||
allow_update,
|
||||
|path| generate_file(path, GRAMMAR_JS_TEMPLATE, language_name, &generate_opts),
|
||||
|path| {
|
||||
let mut contents = fs::read_to_string(path)?;
|
||||
if contents.contains("module.exports") {
|
||||
contents = contents.replace("module.exports =", "export default");
|
||||
write_file(path, contents)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
// Write .gitignore file
|
||||
|
|
@ -410,7 +431,7 @@ pub fn generate_grammar_files(
|
|||
|path| generate_file(path, INDEX_JS_TEMPLATE, language_name, &generate_opts),
|
||||
|path| {
|
||||
let contents = fs::read_to_string(path)?;
|
||||
if !contents.contains("bun") {
|
||||
if !contents.contains("new URL") {
|
||||
eprintln!("Replacing index.js");
|
||||
generate_file(path, INDEX_JS_TEMPLATE, language_name, &generate_opts)?;
|
||||
}
|
||||
|
|
@ -422,14 +443,31 @@ pub fn generate_grammar_files(
|
|||
generate_file(path, INDEX_D_TS_TEMPLATE, language_name, &generate_opts)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("binding_test.js"), |path| {
|
||||
generate_file(
|
||||
path,
|
||||
BINDING_TEST_JS_TEMPLATE,
|
||||
language_name,
|
||||
&generate_opts,
|
||||
)
|
||||
})?;
|
||||
missing_path_else(
|
||||
path.join("binding_test.js"),
|
||||
allow_update,
|
||||
|path| {
|
||||
generate_file(
|
||||
path,
|
||||
BINDING_TEST_JS_TEMPLATE,
|
||||
language_name,
|
||||
&generate_opts,
|
||||
)
|
||||
},
|
||||
|path| {
|
||||
let contents = fs::read_to_string(path)?;
|
||||
if !contents.contains("import") {
|
||||
eprintln!("Replacing binding_test.js");
|
||||
generate_file(
|
||||
path,
|
||||
BINDING_TEST_JS_TEMPLATE,
|
||||
language_name,
|
||||
&generate_opts,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
|
||||
missing_path(path.join("binding.cc"), |path| {
|
||||
generate_file(path, JS_BINDING_CC_TEMPLATE, language_name, &generate_opts)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
const assert = require("node:assert");
|
||||
const { test } = require("node:test");
|
||||
|
||||
const Parser = require("tree-sitter");
|
||||
import assert from "node:assert";
|
||||
import { test } from "node:test";
|
||||
import Parser from "tree-sitter";
|
||||
import language from "./index.js";
|
||||
|
||||
test("can load grammar", () => {
|
||||
const parser = new Parser();
|
||||
assert.doesNotThrow(() => parser.setLanguage(require(".")));
|
||||
assert.doesNotThrow(() => parser.setLanguage(language));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
/// <reference types="tree-sitter-cli/dsl" />
|
||||
// @ts-check
|
||||
|
||||
module.exports = grammar({
|
||||
export default grammar({
|
||||
name: "LOWER_PARSER_NAME",
|
||||
|
||||
rules: {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
const root = require("path").join(__dirname, "..", "..");
|
||||
const root = new URL("../..", import.meta.url).pathname;
|
||||
|
||||
module.exports =
|
||||
typeof process.versions.bun === "string"
|
||||
// Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
|
||||
? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-KEBAB_PARSER_NAME.node`)
|
||||
: require("node-gyp-build")(root);
|
||||
const binding = typeof process.versions.bun === "string"
|
||||
// Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
|
||||
? await import(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-KEBAB_PARSER_NAME.node`)
|
||||
: await import("node-gyp-build");
|
||||
|
||||
const result = binding.default ? binding.default(root) : binding(root);
|
||||
|
||||
try {
|
||||
module.exports.nodeTypeInfo = require("../../src/node-types.json");
|
||||
const nodeTypeInfo = await import("../../src/node-types.json", {assert: {type: "json"}});
|
||||
result.nodeTypeInfo = nodeTypeInfo.default;
|
||||
} catch (_) {}
|
||||
|
||||
export default result;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"name": "tree-sitter-PARSER_NAME",
|
||||
"version": "PARSER_VERSION",
|
||||
"description": "PARSER_DESCRIPTION",
|
||||
"type": "module",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+PARSER_URL.git"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue