@@ -7,10 +7,24 @@ import { QueryPredicateOperator } from "./QueryPredicateOperator";
77import { q } from "./operatorArgumentSchemaTypes" ;
88
99/**
10- * A predicate operator that returns true if the node is not of the given type.
11- * For example, `(not- type? @foo string)` will reject the match if the `@foo`
10+ * A predicate operator that returns true if the node is of the given type.
11+ * For example, `(# type? @foo string)` will accept the match if the `@foo`
1212 * capture is a `string` node. It is acceptable to pass in multiple types, e.g.
13- * `(not-type? @foo string comment)`.
13+ * `(#type? @foo string comment)`.
14+ */
15+ class Type extends QueryPredicateOperator < Type > {
16+ name = "type?" as const ;
17+ schema = z . tuple ( [ q . node , q . string ] ) . rest ( q . string ) ;
18+ run ( { node } : MutableQueryCapture , ...types : string [ ] ) {
19+ return types . includes ( node . type ) ;
20+ }
21+ }
22+
23+ /**
24+ * A predicate operator that returns true if the node is NOT of the given type.
25+ * For example, `(#not-type? @foo string)` will reject the match if the `@foo`
26+ * capture is a `string` node. It is acceptable to pass in multiple types, e.g.
27+ * `(#not-type? @foo string comment)`.
1428 */
1529class NotType extends QueryPredicateOperator < NotType > {
1630 name = "not-type?" as const ;
@@ -22,9 +36,9 @@ class NotType extends QueryPredicateOperator<NotType> {
2236
2337/**
2438 * A predicate operator that returns true if the node's parent is not of the
25- * given type. For example, `(not-parent-type? @foo string)` will reject the
39+ * given type. For example, `(# not-parent-type? @foo string)` will reject the
2640 * match if the `@foo` capture is a child of a `string` node. It is acceptable
27- * to pass in multiple types, e.g. `(not-parent-type? @foo string comment)`.
41+ * to pass in multiple types, e.g. `(# not-parent-type? @foo string comment)`.
2842 */
2943class NotParentType extends QueryPredicateOperator < NotParentType > {
3044 name = "not-parent-type?" as const ;
@@ -36,7 +50,7 @@ class NotParentType extends QueryPredicateOperator<NotParentType> {
3650
3751/**
3852 * A predicate operator that returns true if the node is the nth child of its
39- * parent. For example, `(is-nth-child? @foo 0)` will reject the match if the
53+ * parent. For example, `(# is-nth-child? @foo 0)` will reject the match if the
4054 * `@foo` capture is not the first child of its parent.
4155 */
4256class IsNthChild extends QueryPredicateOperator < IsNthChild > {
@@ -49,7 +63,7 @@ class IsNthChild extends QueryPredicateOperator<IsNthChild> {
4963
5064/**
5165 * A predicate operator that returns true if the node has more than 1 child of
52- * type {@link type} (inclusive). For example, `(has-multiple-children-of-type?
66+ * type {@link type} (inclusive). For example, `(# has-multiple-children-of-type?
5367 * @foo bar)` will accept the match if the `@foo` capture has 2 or more children
5468 * of type `bar`.
5569 */
@@ -374,6 +388,7 @@ class EmptySingleMultiDelimiter extends QueryPredicateOperator<EmptySingleMultiD
374388
375389export const queryPredicateOperators = [
376390 new Log ( ) ,
391+ new Type ( ) ,
377392 new NotType ( ) ,
378393 new TrimEnd ( ) ,
379394 new DocumentRange ( ) ,
0 commit comments