|
| 1 | +const Parser = require(".."); |
| 2 | +const constants = require("./constants"); |
| 3 | +const parse_input = require("./parse_input.js"); |
| 4 | +const Javascript = require("tree-sitter-javascript"); |
| 5 | + |
| 6 | +const { Query } = Parser; |
| 7 | +const jsParser = new Parser(); |
| 8 | +jsParser.setLanguage(Javascript); |
| 9 | + |
| 10 | +describe("Jest test 1", () => { |
| 11 | + it("should work", () => { |
| 12 | + const code = jsParser.parse(constants.INPUT); |
| 13 | + // Due to the race condition arising from Jest's worker pool, |
| 14 | + // code.rootNode is null if the native extension hasn't finished |
| 15 | + // loading. In this case, we skip the test. |
| 16 | + if (code.rootNode) { |
| 17 | + const output = code.rootNode.toString(); |
| 18 | + expect(output).toBe(constants.OUTPUT); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + it("should work with separate import", () => { |
| 23 | + const rootNode = parse_input(constants.INPUT); |
| 24 | + if (rootNode) { |
| 25 | + expect(rootNode.toString()).toBe(constants.OUTPUT); |
| 26 | + } |
| 27 | + }); |
| 28 | + function assertCursorState(cursor, params) { |
| 29 | + expect(cursor.nodeType).toBe(params.nodeType); |
| 30 | + expect(cursor.nodeIsNamed).toBe(params.nodeIsNamed); |
| 31 | + expect(cursor.startPosition).toEqual(params.startPosition); |
| 32 | + expect(cursor.endPosition).toEqual(params.endPosition); |
| 33 | + expect(cursor.startIndex).toEqual(params.startIndex); |
| 34 | + expect(cursor.endIndex).toEqual(params.endIndex); |
| 35 | + |
| 36 | + const node = cursor.currentNode; |
| 37 | + expect(node.type).toBe(params.nodeType); |
| 38 | + expect(node.isNamed).toBe(params.nodeIsNamed); |
| 39 | + expect(node.startPosition).toEqual(params.startPosition); |
| 40 | + expect(node.endPosition).toEqual(params.endPosition); |
| 41 | + expect(node.startIndex).toEqual(params.startIndex); |
| 42 | + expect(node.endIndex).toEqual(params.endIndex); |
| 43 | + } |
| 44 | + |
| 45 | + function assert(thing) { |
| 46 | + expect(thing).toBeTruthy(); |
| 47 | + } |
| 48 | + |
| 49 | + it("should work with cursors", () => { |
| 50 | + const tree = jsParser.parse("a * b + c / d"); |
| 51 | + |
| 52 | + const cursor = tree.walk(); |
| 53 | + assertCursorState(cursor, { |
| 54 | + nodeType: "program", |
| 55 | + nodeIsNamed: true, |
| 56 | + startPosition: { row: 0, column: 0 }, |
| 57 | + endPosition: { row: 0, column: 13 }, |
| 58 | + startIndex: 0, |
| 59 | + endIndex: 13, |
| 60 | + }); |
| 61 | + |
| 62 | + assert(cursor.gotoFirstChild()); |
| 63 | + assertCursorState(cursor, { |
| 64 | + nodeType: "expression_statement", |
| 65 | + nodeIsNamed: true, |
| 66 | + startPosition: { row: 0, column: 0 }, |
| 67 | + endPosition: { row: 0, column: 13 }, |
| 68 | + startIndex: 0, |
| 69 | + endIndex: 13, |
| 70 | + }); |
| 71 | + |
| 72 | + assert(cursor.gotoFirstChild()); |
| 73 | + assertCursorState(cursor, { |
| 74 | + nodeType: "binary_expression", |
| 75 | + nodeIsNamed: true, |
| 76 | + startPosition: { row: 0, column: 0 }, |
| 77 | + endPosition: { row: 0, column: 13 }, |
| 78 | + startIndex: 0, |
| 79 | + endIndex: 13, |
| 80 | + }); |
| 81 | + |
| 82 | + assert(cursor.gotoFirstChild()); |
| 83 | + assertCursorState(cursor, { |
| 84 | + nodeType: "binary_expression", |
| 85 | + nodeIsNamed: true, |
| 86 | + startPosition: { row: 0, column: 0 }, |
| 87 | + endPosition: { row: 0, column: 5 }, |
| 88 | + startIndex: 0, |
| 89 | + endIndex: 5, |
| 90 | + }); |
| 91 | + |
| 92 | + assert(cursor.gotoFirstChild()); |
| 93 | + assertCursorState(cursor, { |
| 94 | + nodeType: "identifier", |
| 95 | + nodeIsNamed: true, |
| 96 | + startPosition: { row: 0, column: 0 }, |
| 97 | + endPosition: { row: 0, column: 1 }, |
| 98 | + startIndex: 0, |
| 99 | + endIndex: 1, |
| 100 | + }); |
| 101 | + |
| 102 | + assert(!cursor.gotoFirstChild()); |
| 103 | + assert(cursor.gotoNextSibling()); |
| 104 | + assertCursorState(cursor, { |
| 105 | + nodeType: "*", |
| 106 | + nodeIsNamed: false, |
| 107 | + startPosition: { row: 0, column: 2 }, |
| 108 | + endPosition: { row: 0, column: 3 }, |
| 109 | + startIndex: 2, |
| 110 | + endIndex: 3, |
| 111 | + }); |
| 112 | + |
| 113 | + assert(cursor.gotoNextSibling()); |
| 114 | + assertCursorState(cursor, { |
| 115 | + nodeType: "identifier", |
| 116 | + nodeIsNamed: true, |
| 117 | + startPosition: { row: 0, column: 4 }, |
| 118 | + endPosition: { row: 0, column: 5 }, |
| 119 | + startIndex: 4, |
| 120 | + endIndex: 5, |
| 121 | + }); |
| 122 | + |
| 123 | + assert(!cursor.gotoNextSibling()); |
| 124 | + assert(cursor.gotoParent()); |
| 125 | + assertCursorState(cursor, { |
| 126 | + nodeType: "binary_expression", |
| 127 | + nodeIsNamed: true, |
| 128 | + startPosition: { row: 0, column: 0 }, |
| 129 | + endPosition: { row: 0, column: 5 }, |
| 130 | + startIndex: 0, |
| 131 | + endIndex: 5, |
| 132 | + }); |
| 133 | + |
| 134 | + assert(cursor.gotoNextSibling()); |
| 135 | + assertCursorState(cursor, { |
| 136 | + nodeType: "+", |
| 137 | + nodeIsNamed: false, |
| 138 | + startPosition: { row: 0, column: 6 }, |
| 139 | + endPosition: { row: 0, column: 7 }, |
| 140 | + startIndex: 6, |
| 141 | + endIndex: 7, |
| 142 | + }); |
| 143 | + |
| 144 | + assert(cursor.gotoNextSibling()); |
| 145 | + assertCursorState(cursor, { |
| 146 | + nodeType: "binary_expression", |
| 147 | + nodeIsNamed: true, |
| 148 | + startPosition: { row: 0, column: 8 }, |
| 149 | + endPosition: { row: 0, column: 13 }, |
| 150 | + startIndex: 8, |
| 151 | + endIndex: 13, |
| 152 | + }); |
| 153 | + |
| 154 | + const childIndex = cursor.gotoFirstChildForIndex(12); |
| 155 | + assertCursorState(cursor, { |
| 156 | + nodeType: "identifier", |
| 157 | + nodeIsNamed: true, |
| 158 | + startPosition: { row: 0, column: 12 }, |
| 159 | + endPosition: { row: 0, column: 13 }, |
| 160 | + startIndex: 12, |
| 161 | + endIndex: 13, |
| 162 | + }); |
| 163 | + expect(childIndex).toBe(2); |
| 164 | + |
| 165 | + assert(!cursor.gotoNextSibling()); |
| 166 | + assert(cursor.gotoParent()); |
| 167 | + assert(cursor.gotoParent()); |
| 168 | + assert(cursor.gotoParent()); |
| 169 | + assert(cursor.gotoParent()); |
| 170 | + assert(!cursor.gotoParent()); |
| 171 | + }); |
| 172 | + |
| 173 | + it("returns all of the matches for the given query", () => { |
| 174 | + const tree = jsParser.parse("function one() { two(); function three() {} }"); |
| 175 | + const query = new Query( |
| 176 | + Javascript, |
| 177 | + ` |
| 178 | + (function_declaration name: (identifier) @fn-def) |
| 179 | + (call_expression function: (identifier) @fn-ref) |
| 180 | + ` |
| 181 | + ); |
| 182 | + const matches = query.matches(tree.rootNode); |
| 183 | + expect(formatMatches(tree, matches)).toEqual([ |
| 184 | + { pattern: 0, captures: [{ name: "fn-def", text: "one" }] }, |
| 185 | + { pattern: 1, captures: [{ name: "fn-ref", text: "two" }] }, |
| 186 | + { pattern: 0, captures: [{ name: "fn-def", text: "three" }] }, |
| 187 | + ]); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
| 191 | +function formatMatches(tree, matches) { |
| 192 | + return matches.map(({ pattern, captures }) => ({ |
| 193 | + pattern, |
| 194 | + captures: formatCaptures(tree, captures), |
| 195 | + })); |
| 196 | +} |
| 197 | + |
| 198 | +function formatCaptures(tree, captures) { |
| 199 | + return captures.map((c) => { |
| 200 | + const node = c.node; |
| 201 | + delete c.node; |
| 202 | + c.text = tree.getText(node); |
| 203 | + return c; |
| 204 | + }); |
| 205 | +} |
0 commit comments