Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit 6514ca3

Browse files
committed
Add tests
1 parent 24ed255 commit 6514ca3

File tree

4 files changed

+82
-40
lines changed

4 files changed

+82
-40
lines changed

bin/build

-38
This file was deleted.

build.mjs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { execSync } from "child_process";
2+
import esbuild from "esbuild";
3+
import path from "path";
4+
import { fileURLToPath } from "url";
5+
import fs from "fs/promises";
6+
7+
const dirname = path.dirname(fileURLToPath(import.meta.url));
8+
9+
const options = {
10+
bundle: true,
11+
entryPoints: [path.join(dirname, "src/index.ts")],
12+
minify: false,
13+
outdir: path.join(dirname, "dist"),
14+
platform: "node",
15+
plugins: [{
16+
name: "wasm",
17+
setup(build) {
18+
build.onLoad({ filter: /\.wasm$/ }, async (args) => ({
19+
contents: await fs.readFile(args.path),
20+
loader: "binary"
21+
}));
22+
}
23+
}],
24+
sourcemap: false,
25+
target: "es6"
26+
};
27+
28+
Promise.all([
29+
esbuild.build({
30+
...options,
31+
format: "esm",
32+
outExtension: { ".js": ".mjs" },
33+
banner: {
34+
js: `import { createRequire } from "module"; const require = createRequire(import.meta.url);`,
35+
}
36+
}),
37+
esbuild.build({
38+
...options,
39+
format: "cjs",
40+
outExtension: { ".js": ".cjs" }
41+
})
42+
]).then(([esm, cjs]) => {
43+
// When we're done building the output, we'll want to additionally build the
44+
// typescript declarations file.
45+
if (esm.errors.length + cjs.errors.length === 0) {
46+
execSync("./node_modules/.bin/tsc");
47+
}
48+
});

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
"name": "node-syntax-tree",
33
"version": "0.1.0",
44
"description": "A node package for accessing Syntax Tree through WASM",
5-
"main": "dist/index.js",
6-
"scripts": {},
5+
"main": "dist/index.cjs",
6+
"module": "dist/index.mjs",
7+
"typings": "dist/index.d.ts",
8+
"scripts": {
9+
"test": "node test.mjs"
10+
},
711
"repository": {
812
"type": "git",
913
"url": "git+https://github.com/ruby-syntax-tree/node-syntax-tree.git"

test.mjs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import assert from "node:assert";
2+
import test from "node:test";
3+
4+
import createSyntaxTree from "./dist/index.mjs";
5+
6+
const syntaxTree = await createSyntaxTree();
7+
8+
test("handlers.haml.format", () => {
9+
assert.equal(syntaxTree.handlers.haml.format("= foo"), "= foo\n");
10+
});
11+
12+
test("handlers.haml.parse", () => {
13+
assert.equal(
14+
syntaxTree.handlers.haml.parse("= foo"),
15+
`(root children=[(script text=" foo")])\n`
16+
);
17+
});
18+
19+
test("handlers.ruby.format", () => {
20+
assert.equal(syntaxTree.handlers.ruby.format("1+1"), "1 + 1\n");
21+
});
22+
23+
test("handlers.ruby.parse", () => {
24+
assert.equal(
25+
syntaxTree.handlers.ruby.parse("foo"),
26+
`(program (statements ((vcall (ident "foo")))))\n`
27+
);
28+
});

0 commit comments

Comments
 (0)