Skip to content

Commit 258beff

Browse files
committed
Fix to return results in tree order
Closes GH-14.
1 parent 367ffb7 commit 258beff

File tree

9 files changed

+353
-587
lines changed

9 files changed

+353
-587
lines changed

index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @typedef {Record<string, unknown> & {type: string, position?: Position | undefined}} NodeLike
66
*/
77

8-
import {any} from './lib/any.js'
8+
import {queryToSelectors, walk} from './lib/walk.js'
99
import {parse} from './lib/parse.js'
1010
import {parent} from './lib/util.js'
1111

@@ -25,10 +25,10 @@ import {parent} from './lib/util.js'
2525
* Whether `node` matches `selector`.
2626
*/
2727
export function matches(selector, node) {
28-
const state = createState(node)
28+
const state = createState(selector, node)
2929
state.one = true
3030
state.shallow = true
31-
any(parse(selector), node || undefined, state)
31+
walk(state, node || undefined)
3232
return state.results.length > 0
3333
}
3434

@@ -48,9 +48,9 @@ export function matches(selector, node) {
4848
* This could be `tree` itself.
4949
*/
5050
export function select(selector, tree) {
51-
const state = createState(tree)
51+
const state = createState(selector, tree)
5252
state.one = true
53-
any(parse(selector), tree || undefined, state)
53+
walk(state, tree || undefined)
5454
// To do next major: return `undefined`.
5555
return state.results[0] || null
5656
}
@@ -70,20 +70,23 @@ export function select(selector, tree) {
7070
* This could include `tree` itself.
7171
*/
7272
export function selectAll(selector, tree) {
73-
const state = createState(tree)
74-
any(parse(selector), tree || undefined, state)
73+
const state = createState(selector, tree)
74+
walk(state, tree || undefined)
7575
return state.results
7676
}
7777

7878
/**
79+
* @param {string} selector
80+
* Selector to parse.
7981
* @param {Node | null | undefined} tree
82+
* Tree to search.
8083
* @returns {SelectState}
8184
*/
82-
function createState(tree) {
85+
function createState(selector, tree) {
8386
return {
87+
// State of the query.
88+
rootQuery: queryToSelectors(parse(selector)),
8489
results: [],
85-
any,
86-
iterator: undefined,
8790
scopeNodes: tree
8891
? parent(tree) &&
8992
// Root in nlcst.
@@ -93,8 +96,8 @@ function createState(tree) {
9396
: [],
9497
one: false,
9598
shallow: false,
96-
index: false,
9799
found: false,
100+
// State in the tree.
98101
typeIndex: undefined,
99102
nodeIndex: undefined,
100103
typeCount: undefined,

lib/any.js

Lines changed: 0 additions & 176 deletions
This file was deleted.

lib/name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/**
7-
* Check whether an element has a type.
7+
* Check whether a node has a type.
88
*
99
* @param {Rule} query
1010
* @param {Node} node

0 commit comments

Comments
 (0)