Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions javascript/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import test from "node:test";
import assert from "node:assert";
import { loadPrism } from "./src/index.js";
import * as nodes from "./src/nodes.js";
import { Visitor } from "./src/visitor.js";

const parse = await loadPrism();

function statement(result) {
return result.value.statements.body[0];
}

function eachNode(node, callback) {
callback(node);

for (const child of node.childNodes()) {
if (child) eachNode(child, callback);
}
}

test("node", () => {
const result = parse("foo");
assert(result.value instanceof nodes.ProgramNode);
Expand Down Expand Up @@ -208,3 +217,45 @@ test("scopes", () => {
result = parse("foo(*)", { scopes: [{ forwarding: ["*"] }] });
assert(result.errors.length === 0);
});

test("compactChildNodes() matches childNodes() without the nulls", () => {
const sources = [
"case a\nwhen b\n c\nend",
"case a\nin [b]\n c\nend",
"a, b = 1, 2",
"def foo(a, b = 1, *c, d:, &e); end",
"begin\n a\nrescue B, C\n d\nend",
"case a\nin { b: Integer => c }\n d\nend"
];

for (const source of sources) {
eachNode(parse(source).value, (node) => {
assert.deepStrictEqual(
node.compactChildNodes(),
node.childNodes().filter((child) => child !== null),
`${node.constructor.name} in ${JSON.stringify(source)}`
);
});
}
});

test("visitor visits nodes inside node list fields", () => {
class CallCollector extends Visitor {
names = [];

visitCallNode(node) {
this.names.push(node.name);
super.visitCallNode(node);
}
}

function collect(source) {
const visitor = new CallCollector();
visitor.visit(parse(source).value);
return visitor.names;
}

assert.deepStrictEqual(collect("case a\nwhen b\n c\nend"), ["a", "b", "c"]);
assert.deepStrictEqual(collect("begin\n a\nrescue B\n c\nend"), ["a", "c"]);
assert.deepStrictEqual(collect("def foo(a = b); end"), ["b"]);
});
2 changes: 1 addition & 1 deletion templates/javascript/src/nodes.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class <%= node.name -%> {
compact.push(this.<%= prop(field) %>);
}
<%- when Prism::Template::NodeListField -%>
compact.concat(this.<%= prop(field) %>);
compact.push(...this.<%= prop(field) %>);
<%- end -%>
<%- end -%>

Expand Down
Loading