diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 38efabd6..e6c2b27e 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -1013,7 +1013,7 @@ class LookaheadIterable { } reset(language, stateId) { - if (C._ts_lookahead_iterator_reset(this[0], language, stateId)) { + if (C._ts_lookahead_iterator_reset(this[0], language[0], stateId)) { this.language = language; return true; } @@ -1028,7 +1028,7 @@ class LookaheadIterable { if (C._ts_lookahead_iterator_advance(self[0])) { return { done: false, value: self.currentType }; } - + return { done: true, value: "" }; } }; diff --git a/lib/binding_web/test/language-test.js b/lib/binding_web/test/language-test.js index 385b77ed..dab212d6 100644 --- a/lib/binding_web/test/language-test.js +++ b/lib/binding_web/test/language-test.js @@ -42,3 +42,41 @@ describe("Language", () => { }); }); }); + +describe("Lookahead iterator", () => { + let lookahead; + let state; + before(async () => { + let Parser; + ({ JavaScript, Parser } = await require("./helper")); + const parser = new Parser().setLanguage(JavaScript); + const tree = parser.parse("function fn() {}"); + const cursor = tree.walk(); + cursor.gotoFirstChild(); + cursor.gotoFirstChild(); + state = cursor.currentNode().nextParseState; + lookahead = JavaScript.lookaheadIterator(state); + assert.exists(lookahead); + }); + + const expected = ["identifier", "comment", "(", "*", "formal_parameters"]; + it("should iterate over valid symbols in the state", () => { + const symbols = Array.from(lookahead); + assert.includeMembers(symbols, expected); + assert.lengthOf(symbols, expected.length); + }); + + it("should reset to the initial state", () => { + assert(lookahead.resetState(state)); + const symbols = Array.from(lookahead); + assert.includeMembers(symbols, expected); + assert.lengthOf(symbols, expected.length); + }); + + it("should reset", () => { + assert(lookahead.reset(JavaScript, state)); + const symbols = Array.from(lookahead); + assert.includeMembers(symbols, expected); + assert.lengthOf(symbols, expected.length); + }); +});