Skip to content

Commit

Permalink
Rename classes to match terminology from the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Dec 10, 2024
1 parent 8010c0e commit 28d65cf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
18 changes: 8 additions & 10 deletions src/path/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FilterExpressionLiteral,
FunctionExtension,
InfixExpression,
JSONPathQuery,
FilterQuery,
} from "./expression";
import { Count as CountFilterFunction } from "./functions/count";
import { FilterFunction, FunctionExpressionType } from "./functions/function";
Expand All @@ -15,7 +15,7 @@ import { Value as ValueFilterFunction } from "./functions/value";
import { tokenize } from "./lex";
import { JSONPathNode, JSONPathNodeList } from "./node";
import { Parser } from "./parse";
import { JSONPath } from "./path";
import { JSONPathQuery } from "./path";
import { Token, TokenStream } from "./token";
import { JSONValue } from "../types";
import { CurrentKey } from "./extra/expression";
Expand Down Expand Up @@ -140,10 +140,10 @@ export class JSONPathEnvironment {

/**
* @param path - A JSONPath query to parse.
* @returns A new {@link JSONPath} object, bound to this environment.
* @returns A new {@link JSONPathQuery} object, bound to this environment.
*/
public compile(path: string): JSONPath {
return new JSONPath(
public compile(path: string): JSONPathQuery {
return new JSONPathQuery(
this,
this.parser.parse(new TokenStream(tokenize(this, path))),
);
Expand Down Expand Up @@ -252,7 +252,7 @@ export class JSONPathEnvironment {
!(
arg instanceof FilterExpressionLiteral ||
arg instanceof CurrentKey ||
(arg instanceof JSONPathQuery && arg.path.singularQuery()) ||
(arg instanceof FilterQuery && arg.path.singularQuery()) ||
(arg instanceof FunctionExtension &&
this.functionRegister.get(arg.name)?.returnType ===
FunctionExpressionType.ValueType)
Expand All @@ -265,9 +265,7 @@ export class JSONPathEnvironment {
}
break;
case FunctionExpressionType.LogicalType:
if (
!(arg instanceof JSONPathQuery || arg instanceof InfixExpression)
) {
if (!(arg instanceof FilterQuery || arg instanceof InfixExpression)) {
throw new JSONPathTypeError(
`${token.value}() argument ${idx} must be of LogicalType`,
arg.token,
Expand All @@ -277,7 +275,7 @@ export class JSONPathEnvironment {
case FunctionExpressionType.NodesType:
if (
!(
arg instanceof JSONPathQuery ||
arg instanceof FilterQuery ||
(arg instanceof FunctionExtension &&
this.functionRegister.get(arg.name)?.returnType ===
FunctionExpressionType.NodesType)
Expand Down
10 changes: 5 additions & 5 deletions src/path/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { deepEquals } from "../deep_equals";
import { JSONPathTypeError, UndefinedFilterFunctionError } from "./errors";
import { FunctionExpressionType } from "./functions/function";
import { JSONPathNodeList } from "./node";
import { JSONPath } from "./path";
import { JSONPathQuery } from "./path";
import { Token } from "./token";
import { FilterContext, Nothing } from "./types";
import { isNumber, isString } from "../types";
Expand Down Expand Up @@ -190,16 +190,16 @@ export class LogicalExpression extends FilterExpression {
/**
* Base class for relative and absolute JSONPath query expressions.
*/
export abstract class JSONPathQuery extends FilterExpression {
export abstract class FilterQuery extends FilterExpression {
constructor(
readonly token: Token,
readonly path: JSONPath,
readonly path: JSONPathQuery,
) {
super(token);
}
}

export class RelativeQuery extends JSONPathQuery {
export class RelativeQuery extends FilterQuery {
public evaluate(context: FilterContext): JSONPathNodeList {
return context.lazy
? new JSONPathNodeList(
Expand All @@ -213,7 +213,7 @@ export class RelativeQuery extends JSONPathQuery {
}
}

export class RootQuery extends JSONPathQuery {
export class RootQuery extends FilterQuery {
public evaluate(context: FilterContext): JSONPathNodeList {
return context.lazy
? new JSONPathNodeList(Array.from(this.path.lazyQuery(context.rootValue)))
Expand Down
6 changes: 3 additions & 3 deletions src/path/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JSONValue } from "../types";
import { JSONPathEnvironment } from "./environment";
import { JSONPathNode, JSONPathNodeList } from "./node";
import { JSONPath } from "./path";
import { JSONPathQuery } from "./path";

export { JSONPathEnvironment } from "./environment";
export type { JSONPathEnvironmentOptions } from "./environment";

export { JSONPath } from "./path";
export { JSONPathQuery } from "./path";
export { JSONPathNodeList, JSONPathNode } from "./node";
export { Token, TokenKind } from "./token";

Expand Down Expand Up @@ -85,7 +85,7 @@ export function lazyQuery(
* If filter function arguments are invalid, or filter expression are
* used in an invalid way.
*/
export function compile(path: string): JSONPath {
export function compile(path: string): JSONPathQuery {
return DEFAULT_ENVIRONMENT.compile(path);
}

Expand Down
14 changes: 5 additions & 9 deletions src/path/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
StringLiteral,
} from "./expression";
import { FunctionExpressionType } from "./functions/function";
import { JSONPath } from "./path";
import { JSONPathQuery } from "./path";
import {
FilterSelector,
IndexSelector,
Expand All @@ -24,11 +24,7 @@ import {
SliceSelector,
WildcardSelector,
} from "./selectors";
import {
RecursiveDescentSegment,
ChildSegment,
JSONPathSegment,
} from "./segments";
import { DescendantSegment, ChildSegment, JSONPathSegment } from "./segments";
import { Token, TokenKind, TokenStream } from "./token";
import { CurrentKey } from "./extra/expression";
import {
Expand Down Expand Up @@ -115,7 +111,7 @@ export class Parser {
const token = stream.next();
const selectors = this.parseSelectors(stream);
segments.push(
new RecursiveDescentSegment(this.environment, token, selectors),
new DescendantSegment(this.environment, token, selectors),
);
break;
}
Expand Down Expand Up @@ -453,15 +449,15 @@ export class Parser {
const tok = stream.next();
return new RootQuery(
tok,
new JSONPath(this.environment, this.parseQuery(stream, true)),
new JSONPathQuery(this.environment, this.parseQuery(stream, true)),
);
}

protected parseRelativeQuery(stream: TokenStream): RelativeQuery {
const tok = stream.next();
return new RelativeQuery(
tok,
new JSONPath(this.environment, this.parseQuery(stream, true)),
new JSONPathQuery(this.environment, this.parseQuery(stream, true)),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/path/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { JSONPathEnvironment } from "./environment";
import { JSONPathNode, JSONPathNodeList } from "./node";
import { IndexSelector, NameSelector } from "./selectors";
import { JSONValue } from "../types";
import { JSONPathSegment, RecursiveDescentSegment } from "./segments";
import { JSONPathSegment, DescendantSegment } from "./segments";

/**
*
*/
export class JSONPath {
export class JSONPathQuery {
/**
*
* @param environment -
Expand Down Expand Up @@ -70,7 +70,7 @@ export class JSONPath {

public singularQuery(): boolean {
for (const segment of this.segments) {
if (segment instanceof RecursiveDescentSegment) return false;
if (segment instanceof DescendantSegment) return false;

if (
segment.selectors.length === 1 &&
Expand Down
2 changes: 1 addition & 1 deletion src/path/segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ChildSegment extends JSONPathSegment {
}

/** The recursive descent segment. */
export class RecursiveDescentSegment extends JSONPathSegment {
export class DescendantSegment extends JSONPathSegment {
public resolve(nodes: JSONPathNode[]): JSONPathNode[] {
const rv: JSONPathNode[] = [];

Expand Down

0 comments on commit 28d65cf

Please sign in to comment.