Skip to content

Commit

Permalink
Fix types to allow literal nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 18, 2022
1 parent 40941a9 commit 1da3f61
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* @typedef {import('unist').Position} Position
* @typedef {import('unist').Node} Node
* @typedef {Record<string, unknown> & {type: string, position?: Position|undefined}} NodeLike
*/

import {any} from './lib/any.js'
import {parse} from './lib/parse.js'

/**
* @param {string} selector
* @param {Node} [node]
* @param {NodeLike|Node} [node]
* @returns {boolean}
*/
export function matches(selector, node) {
Expand All @@ -16,7 +18,7 @@ export function matches(selector, node) {

/**
* @param {string} selector
* @param {Node} [node]
* @param {NodeLike|Node} [node]
* @returns {Node|null}
*/
export function select(selector, node) {
Expand All @@ -25,8 +27,8 @@ export function select(selector, node) {

/**
* @param {string} selector
* @param {Node} [node]
* @returns {Array.<Node>}
* @param {NodeLike|Node} [node]
* @returns {Array<Node>}
*/
export function selectAll(selector, node) {
return any(parse(selector), node, {any})
Expand Down
3 changes: 3 additions & 0 deletions test/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {u} from 'unist-builder'
import {matches} from '../index.js'

test('select.matches()', (t) => {
t.equal(matches('*', u('root', [])), true, 'should work (1)')
t.equal(matches('*', {type: 'a', children: []}), true, 'should work (2)')

t.test('invalid selector', (st) => {
st.throws(
() => {
Expand Down

0 comments on commit 1da3f61

Please sign in to comment.