-
-
Notifications
You must be signed in to change notification settings - Fork 279
feat: Add support of inner expressions enclosed by parentheses #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harttle
merged 26 commits into
harttle:master
from
skynetigor:833_Support-Value-Expressions-as-Operands-in-Conditional-and-Loop-Tags
Aug 1, 2026
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e7e3aeb
Add support of inner expressions enclosed by parentheses
skynetigor 75314d0
Add support of inner expressions enclosed by parentheses
skynetigor 489d136
Merge branch '833_Support-Value-Expressions-as-Operands-in-Conditiona…
skynetigor a84df57
simplify implementation
skynetigor 4b5296a
fix lint
skynetigor ba50a32
Merge branch 'master' into 833_Support-Value-Expressions-as-Operands-…
skynetigor 0bedce9
fix test
skynetigor d6ec7cd
Merge branch '833_Support-Value-Expressions-as-Operands-in-Conditiona…
skynetigor f45c4f1
Enhance tests for parenthesized filter chains in Liquid tags. Added s…
skynetigor bd65ca6
Merge branch 'master' of https://github.com/harttle/liquidjs into HEAD
rosomri 2cc74bf
test: remove duplicate readGroupedExpression test block
rosomri e1afb68
refactor: extract extractGroupedExpressionTokenVariables helper
rosomri 60a6d0a
refactor(types): explicit type for collection in for tag
rosomri c4b4b6a
refactor: evaluate grouped expressions at render time with resolvedFi…
rosomri 7f2b3ff
refactor: reuse FilteredValueToken and fix architectural layering
rosomri 74e73b7
revert redundant'
rosomri 4b620a5
refactor: make getFilter private and improve code organization
rosomri 2d5b811
test: fix test name in case.spec.ts for when disabled block
rosomri e202158
refactor: no need for Deprecated flag
rosomri 1b9fb80
test: fix test name and logic to properly test if tag with nested exp…
rosomri 4ff6534
feat: support real parenthesis grouping in grouped expressions
skynetigor db271da
feat: enhance expression tokenization with new generator methods
skynetigor 5e60327
add tests
skynetigor 2c5f36d
Merge branch 'master' into 833_Support-Value-Expressions-as-Operands-…
skynetigor 75beaca
address comments
skynetigor ff2841b
Merge branch 'master' into 833_Support-Value-Expressions-as-Operands-…
skynetigor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { FilteredValueToken, TagToken, HTMLToken, HashToken, QuotedToken, LiquidTagToken, OutputToken, ValueToken, Token, RangeToken, FilterToken, TopLevelToken, PropertyAccessToken, OperatorToken, LiteralToken, IdentifierToken, NumberToken } from '../tokens' | ||
| import { FilteredValueToken, TagToken, HTMLToken, HashToken, QuotedToken, LiquidTagToken, OutputToken, ValueToken, Token, RangeToken, FilterToken, TopLevelToken, PropertyAccessToken, OperatorToken, LiteralToken, IdentifierToken, NumberToken, GroupedExpressionToken } from '../tokens' | ||
| import { OperatorHandler } from '../render/operator' | ||
| import { TrieNode, LiteralValue, Trie, createTrie, ellipsis, literalValues, TokenizationError, TYPES, QUOTE, BLANK, NUMBER, SIGN, isWord, isString } from '../util' | ||
| import { Operators, Expression } from '../render' | ||
|
|
@@ -9,6 +9,7 @@ import { whiteSpaceCtrl } from './whitespace-ctrl' | |
| export class Tokenizer { | ||
| p: number | ||
| N: number | ||
| public groupedExpressions: boolean | ||
| private rawBeginAt = -1 | ||
| private opTrie: Trie<OperatorHandler> | ||
| private literalTrie: Trie<LiteralValue> | ||
|
|
@@ -17,12 +18,14 @@ export class Tokenizer { | |
| public input: string, | ||
| operators: Operators = defaultOptions.operators, | ||
| public file?: string, | ||
| range?: [number, number] | ||
| range?: [number, number], | ||
| groupedExpressions = false | ||
| ) { | ||
| this.p = range ? range[0] : 0 | ||
| this.N = range ? range[1] : input.length | ||
| this.opTrie = createTrie(operators) | ||
| this.literalTrie = createTrie(literalValues) | ||
| this.groupedExpressions = groupedExpressions | ||
| } | ||
|
|
||
| readExpression () { | ||
|
|
@@ -310,7 +313,15 @@ export class Tokenizer { | |
| readValue (): ValueToken | undefined { | ||
| this.skipBlank() | ||
| const begin = this.p | ||
| const variable = this.readLiteral() || this.readQuoted() || this.readRange() || this.readNumber() | ||
| let variable: ValueToken | undefined = this.readLiteral() || this.readQuoted() | ||
| if (!variable && this.peek() === '(') { | ||
| if (this.groupedExpressions && !this.looksLikeRange()) { | ||
| variable = this.readGroupedExpression() | ||
| } else { | ||
| variable = this.readRange() | ||
| } | ||
| } | ||
| variable = variable || this.readNumber() | ||
|
harttle marked this conversation as resolved.
Outdated
|
||
| const props = this.readProperties(!variable) | ||
| if (!props.length) return variable | ||
| return new PropertyAccessToken(variable, props, this.input, begin, this.p) | ||
|
|
@@ -420,6 +431,70 @@ export class Tokenizer { | |
| return new QuotedToken(this.input, begin, this.p, this.file) | ||
| } | ||
|
|
||
| readGroupedExpression (): GroupedExpressionToken | undefined { | ||
| this.skipBlank() | ||
| if (this.peek() !== '(') return | ||
| const begin = this.p | ||
| ++this.p | ||
| const closeParen = this.findMatchingParen() | ||
| if (closeParen === -1) { | ||
| this.p = begin | ||
| return | ||
| } | ||
| const savedN = this.N | ||
|
skynetigor marked this conversation as resolved.
Outdated
|
||
| this.N = closeParen | ||
| const fvt = this.readFilteredValue() | ||
| this.N = savedN | ||
| this.p = closeParen + 1 | ||
| return new GroupedExpressionToken(fvt.initial, fvt.filters, this.input, begin, this.p, this.file) | ||
| } | ||
|
|
||
| private findMatchingParen (): number { | ||
| let depth = 1 | ||
| let i = this.p | ||
| while (i < this.N && depth > 0) { | ||
| const ch = this.input[i] | ||
| if (ch === '(') { | ||
| depth++ | ||
| } else if (ch === ')') { | ||
| depth-- | ||
| if (depth === 0) return i | ||
| } else if (ch === '"' || ch === "'") { | ||
| const quote = ch | ||
| i++ | ||
| while (i < this.N && this.input[i] !== quote) { | ||
| if (this.input[i] === '\\') i++ | ||
| i++ | ||
| } | ||
| } | ||
| i++ | ||
| } | ||
| return -1 | ||
| } | ||
|
|
||
| private looksLikeRange (): boolean { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parsing is no longer near O(N) complexity with |
||
| let i = this.p + 1 | ||
| let depth = 1 | ||
| while (i < this.N && depth > 0) { | ||
| const ch = this.input[i] | ||
| if (ch === '(') { | ||
| depth++ | ||
| } else if (ch === ')') { | ||
| depth-- | ||
| } else if (ch === '"' || ch === "'") { | ||
| i++ | ||
| while (i < this.N && this.input[i] !== ch) { | ||
| if (this.input[i] === '\\') i++ | ||
| i++ | ||
| } | ||
| } else if (depth === 1 && ch === '.' && this.input[i + 1] === '.') { | ||
| return true | ||
| } | ||
| i++ | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| * readFileNameTemplate (options: NormalizedFullOptions): IterableIterator<TopLevelToken> { | ||
| const { outputDelimiterLeft } = options | ||
| const htmlStopStrings = [',', ' ', '\r', '\n', '\t', outputDelimiterLeft] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { Argument, Template, Value } from '.' | |
| import { isKeyValuePair } from '../parser/filter-arg' | ||
| import { PropertyAccessToken, ValueToken } from '../tokens' | ||
| import { | ||
| isGroupedExpressionToken, | ||
| isNumberToken, | ||
| isPropertyAccessToken, | ||
| isQuotedToken, | ||
|
|
@@ -371,6 +372,16 @@ function * extractValueTokenVariables (token: ValueToken): Generator<Variable> { | |
| if (isRangeToken(token)) { | ||
| yield * extractValueTokenVariables(token.lhs) | ||
| yield * extractValueTokenVariables(token.rhs) | ||
| } else if (isGroupedExpressionToken(token)) { | ||
| for (const t of token.initial.postfix) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a |
||
| if (isValueToken(t)) yield * extractValueTokenVariables(t) | ||
| } | ||
| for (const filter of token.filters) { | ||
| for (const arg of filter.args) { | ||
| if (isKeyValuePair(arg) && arg[1]) yield * extractValueTokenVariables(arg[1]) | ||
| else if (isValueToken(arg)) yield * extractValueTokenVariables(arg) | ||
| } | ||
| } | ||
| } else if (isPropertyAccessToken(token)) { | ||
| yield extractPropertyAccessVariable(token) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Token } from './token' | ||
| import { FilterToken } from './filter-token' | ||
| import { TokenKind } from '../parser' | ||
| import { Expression } from '../render' | ||
|
|
||
| export class GroupedExpressionToken extends Token { | ||
| public resolvedValue?: { value (ctx: any, lenient?: boolean): Generator<unknown, unknown, unknown> } | ||
|
harttle marked this conversation as resolved.
Outdated
|
||
| constructor ( | ||
| public initial: Expression, | ||
| public filters: FilterToken[], | ||
| public input: string, | ||
| public begin: number, | ||
| public end: number, | ||
| public file?: string | ||
| ) { | ||
| super(TokenKind.GroupedExpression, input, begin, end, file) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.