Skip to content

Commit 0c9b826

Browse files
Expose the AST in the JavaScript implementation (#109)
1 parent 3802248 commit 0c9b826

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- [JavaScript] Add `CucumberExpression.ast` and expose the AST types.
13+
1014
## [15.0.2] - 2022-03-15
1115
### Fixed
1216
- Add missing `name` field in CommonJS package file ([#87](https://github.com/cucumber/cucumber-expressions/pull/87))

javascript/src/Ast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface Located {
4040
readonly end: number
4141
}
4242

43-
export class Node {
43+
export class Node implements Located {
4444
constructor(
4545
public readonly type: NodeType,
4646
public readonly nodes: readonly Node[] | undefined,
@@ -70,7 +70,7 @@ export enum NodeType {
7070
expression = 'EXPRESSION_NODE',
7171
}
7272

73-
export class Token {
73+
export class Token implements Located {
7474
readonly type: TokenType
7575
readonly text: string
7676
readonly start: number

javascript/src/CucumberExpression.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const ESCAPE_PATTERN = () => /([\\^[({$.|?*+})\]])/g
2020
export default class CucumberExpression implements Expression {
2121
private readonly parameterTypes: Array<ParameterType<unknown>> = []
2222
private readonly treeRegexp: TreeRegexp
23+
private readonly ast: Node
2324

2425
/**
2526
* @param expression
@@ -30,8 +31,8 @@ export default class CucumberExpression implements Expression {
3031
private readonly parameterTypeRegistry: ParameterTypeRegistry
3132
) {
3233
const parser = new CucumberExpressionParser()
33-
const ast = parser.parse(expression)
34-
const pattern = this.rewriteToRegex(ast)
34+
this.ast = parser.parse(expression)
35+
const pattern = this.rewriteToRegex(this.ast)
3536
this.treeRegexp = new TreeRegexp(pattern)
3637
}
3738

@@ -73,14 +74,14 @@ export default class CucumberExpression implements Expression {
7374

7475
private rewriteAlternation(node: Node) {
7576
// Make sure the alternative parts aren't empty and don't contain parameter types
76-
;(node.nodes || []).forEach((alternative) => {
77+
for (const alternative of node.nodes || []) {
7778
if (!alternative.nodes || alternative.nodes.length == 0) {
7879
throw createAlternativeMayNotBeEmpty(alternative, this.expression)
7980
}
8081
this.assertNotEmpty(alternative, (astNode) =>
8182
createAlternativeMayNotExclusivelyContainOptionals(astNode, this.expression)
8283
)
83-
})
84+
}
8485
const regex = (node.nodes || []).map((node) => this.rewriteToRegex(node)).join('|')
8586
return `(?:${regex})`
8687
}

javascript/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Argument from './Argument.js'
2+
import { Located, Node, NodeType, Token, TokenType } from './Ast.js'
23
import CucumberExpression from './CucumberExpression.js'
34
import CucumberExpressionGenerator from './CucumberExpressionGenerator.js'
45
import ExpressionFactory from './ExpressionFactory.js'
@@ -17,7 +18,12 @@ export {
1718
ExpressionFactory,
1819
GeneratedExpression,
1920
Group,
21+
Located,
22+
Node,
23+
NodeType,
2024
ParameterType,
2125
ParameterTypeRegistry,
2226
RegularExpression,
27+
Token,
28+
TokenType,
2329
}

0 commit comments

Comments
 (0)