From 28d65cf909d43fab4852968c3903b788995fdd03 Mon Sep 17 00:00:00 2001 From: James Prior Date: Tue, 10 Dec 2024 13:09:27 +0000 Subject: [PATCH] Rename classes to match terminology from the spec --- src/path/environment.ts | 18 ++++++++---------- src/path/expression.ts | 10 +++++----- src/path/index.ts | 6 +++--- src/path/parse.ts | 14 +++++--------- src/path/path.ts | 6 +++--- src/path/segments.ts | 2 +- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/path/environment.ts b/src/path/environment.ts index 747baa6..97efe26 100644 --- a/src/path/environment.ts +++ b/src/path/environment.ts @@ -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"; @@ -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"; @@ -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))), ); @@ -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) @@ -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, @@ -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) diff --git a/src/path/expression.ts b/src/path/expression.ts index 31cdbf0..371af51 100644 --- a/src/path/expression.ts +++ b/src/path/expression.ts @@ -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"; @@ -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( @@ -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))) diff --git a/src/path/index.ts b/src/path/index.ts index 968d14a..9134830 100644 --- a/src/path/index.ts +++ b/src/path/index.ts @@ -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"; @@ -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); } diff --git a/src/path/parse.ts b/src/path/parse.ts index b1e248c..9bf35b7 100644 --- a/src/path/parse.ts +++ b/src/path/parse.ts @@ -15,7 +15,7 @@ import { StringLiteral, } from "./expression"; import { FunctionExpressionType } from "./functions/function"; -import { JSONPath } from "./path"; +import { JSONPathQuery } from "./path"; import { FilterSelector, IndexSelector, @@ -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 { @@ -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; } @@ -453,7 +449,7 @@ 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)), ); } @@ -461,7 +457,7 @@ export class Parser { const tok = stream.next(); return new RelativeQuery( tok, - new JSONPath(this.environment, this.parseQuery(stream, true)), + new JSONPathQuery(this.environment, this.parseQuery(stream, true)), ); } diff --git a/src/path/path.ts b/src/path/path.ts index 28b350b..e75a3ed 100644 --- a/src/path/path.ts +++ b/src/path/path.ts @@ -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 - @@ -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 && diff --git a/src/path/segments.ts b/src/path/segments.ts index 94960c4..17b3100 100644 --- a/src/path/segments.ts +++ b/src/path/segments.ts @@ -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[] = [];