Skip to content
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

Use native RegExp Match indices #1652

Merged
merged 7 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cucumber-expressions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

* Use native RegExp Match indices (currently relying on a polyfill)
([#1652](https://github.com/cucumber/common/pull/1652)
[aslakhellesoy])

## [12.1.1] - 2021-04-06

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"typescript": "4.3.5"
},
"dependencies": {
"becke-ch--regex--s0-0-v1--base--pl--lib": "1.4.0"
"regexp-match-indices": "1.0.2"
},
"directories": {
"test": "test"
Expand Down
4 changes: 2 additions & 2 deletions cucumber-expressions/javascript/src/Group.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default class Group {
constructor(
public readonly value: string | undefined,
public readonly start: number,
public readonly end: number,
public readonly start: number | undefined,
public readonly end: number | undefined,
public readonly children: readonly Group[]
) {}

Expand Down
15 changes: 7 additions & 8 deletions cucumber-expressions/javascript/src/GroupBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Group from './Group'
import RegexExecArray from './RegexExecArray'
import { RegExpExecArray } from 'regexp-match-indices'

export default class GroupBuilder {
public source: string
Expand All @@ -10,15 +10,14 @@ export default class GroupBuilder {
this.groupBuilders.push(groupBuilder)
}

public build(match: RegexExecArray, nextGroupIndex: () => number): Group {
public build(match: RegExpExecArray, nextGroupIndex: () => number): Group {
const groupIndex = nextGroupIndex()
const children = this.groupBuilders.map((gb) => gb.build(match, nextGroupIndex))
return new Group(
match[groupIndex] || undefined,
match.index[groupIndex],
match.index[groupIndex] + (match[groupIndex] || '').length,
children
)
const value = match[groupIndex] || undefined
const index = match.indices[groupIndex]
const start = index ? index[0] : undefined
const end = index ? index[1] : undefined
return new Group(value, start, end, children)
}

public setNonCapturing() {
Expand Down
4 changes: 0 additions & 4 deletions cucumber-expressions/javascript/src/RegexExecArray.ts

This file was deleted.

26 changes: 13 additions & 13 deletions cucumber-expressions/javascript/src/TreeRegexp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import GroupBuilder from './GroupBuilder'
// @ts-ignore
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
import RegexExecArray from './RegexExecArray'
import Group from './Group'
import execWithIndices from 'regexp-match-indices'

export default class TreeRegexp {
public regexp: RegExp
private regex: any
public groupBuilder: GroupBuilder
public readonly regexp: RegExp
public readonly groupBuilder: GroupBuilder

constructor(regexp: RegExp | string) {
this.regexp = 'string' === typeof regexp ? new RegExp(regexp) : regexp
this.regex = new Regex(this.regexp.source, this.regexp.flags)
this.groupBuilder = TreeRegexp.createGroupBuilder(this.regex)
if (regexp instanceof RegExp) {
this.regexp = regexp
} else {
this.regexp = new RegExp(regexp)
}
this.groupBuilder = TreeRegexp.createGroupBuilder(this.regexp)
}

private static createGroupBuilder(regexp: RegExp) {
Expand Down Expand Up @@ -53,22 +53,22 @@ export default class TreeRegexp {

private static isNonCapturing(source: string, i: number): boolean {
// Regex is valid. Bounds check not required.
if (source[i + 1] != '?') {
if (source[i + 1] !== '?') {
// (X)
return false
}
if (source[i + 2] != '<') {
if (source[i + 2] !== '<') {
// (?:X)
// (?=X)
// (?!X)
return true
}
// (?<=X) or (?<!X) else (?<name>X)
return source[i + 3] == '=' || source[i + 3] == '!'
return source[i + 3] === '=' || source[i + 3] === '!'
}

public match(s: string): Group | null {
const match: RegexExecArray = this.regex.exec(s)
const match = execWithIndices(this.regexp, s)
if (!match) {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/javascript/test/TreeRegexpTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('TreeRegexp', () => {
assert.strictEqual(group.children.length, 1)
})

it('works with flags', () => {
it('works with case insensitive flag', () => {
const tr = new TreeRegexp(/HELLO/i)
const group = tr.match('hello')
assert.strictEqual(group.value, 'hello')
Expand Down
33 changes: 31 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.