Skip to content

Commit 463c120

Browse files
committed
[LiquidDoc] Parsing support for optional parameters and default values
---- - Added a parameter `required` to the LiquidDocParamNode - By default, parameters are required unless they have `[]` around the name - Parameters with incomplete delimiters `e.g. ([missingTail)` will map to a `TextNode` - Default values are supported for optional parameters - `[param=default]` - Default values will match anything between `=` and `]`. Leading and Trailing spaces are trimmed. I originally tried accessing everything from the top level in the param node. This is still the goal, but I think this should be done at stage 2 so that we can leverage the recursive nature of how to-AST works. My original approach attempted to Write everything to the top level in stage 1, but we should be doing this in stage 2.
1 parent 8bceaa3 commit 463c120

File tree

6 files changed

+465
-71
lines changed

6 files changed

+465
-71
lines changed

.changeset/chatty-scissors-relax.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@shopify/liquid-html-parser': minor
3+
'theme-check-vscode': minor
4+
---
5+
6+
Add support for parsing optional parameters in LiquidDoc. Optional parameters are designated by wrapping the name in square brackets, and can accept a default value (e.g. `@param [parameter_name=default]`).

packages/liquid-html-parser/grammar/liquid-html.ohm

+17-3
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,28 @@ LiquidDoc <: Helpers {
398398
strictSpace = " " | "\t"
399399
// We use this as an escape hatch to stop matching TextNode and try again when one of these characters is encountered
400400
openControl:= "@" | end
401+
// We use this to map text values into a textNodes via mappings in stage-1-cst.ts
402+
textValue = identifierCharacter+
401403

402404
fallbackNode = "@" anyExceptStar<endOfParam>
403-
paramNode = "@param" strictSpace* paramType? strictSpace* paramName (strictSpace* "-")? strictSpace* paramDescription
405+
paramNode = "@param" strictSpace* paramType? strictSpace* (paramName | optionalParamName) (strictSpace* "-")? strictSpace* paramDescription
406+
404407
paramType = "{" strictSpace* paramTypeContent strictSpace* "}"
405408
paramTypeContent = anyExceptStar<("}"| strictSpace)>
406-
paramName = identifierCharacter+
407-
paramDescription = anyExceptStar<endOfParam>
409+
410+
paramDescription = (~"]" anyExceptStar<endOfParam>)
408411
endOfParam = strictSpace* (newline | end)
412+
413+
// Required parameter name - e.g. "paramName"
414+
paramName = textValue
415+
// Optional parameter name - e.g. "[paramName]"
416+
optionalParamName = "[" strictSpace* optionalParamNameContent strictSpace* "]"
417+
// Content of an optional parameter name - e.g. "paramName=defaultValue"
418+
optionalParamNameContent = strictSpace* textValue strictSpace* optionalParamNameDefaultValue?
419+
// Default value of an optional parameter name - e.g. "=defaultValue"
420+
optionalParamNameDefaultValue = "=" strictSpace* optionalParamNameDefaultContent
421+
optionalParamNameDefaultContent = anyExceptPlus<endOfOptionalParamNameDefaultValue>
422+
endOfOptionalParamNameDefaultValue = strictSpace* "]"
409423
}
410424

411425
LiquidHTML <: Liquid {

0 commit comments

Comments
 (0)