Skip to content

Commit 677d50d

Browse files
Escape special characters in generated pattern
Install escape-string-regexp to escape any characters with special meaning in RegEx before adding them to the pattern.
1 parent ada9261 commit 677d50d

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@babel/core": "^7.5.5",
4141
"@babel/preset-env": "^7.5.5",
4242
"@babel/preset-typescript": "^7.5.5",
43+
"@types/escape-string-regexp": "^1.0.0",
4344
"@types/jest": "^24.0.18",
4445
"@types/jest-when": "^2.7.0",
4546
"@typescript-eslint/eslint-plugin": "^2.2.0",
@@ -61,5 +62,8 @@
6162
"typescript": "^3.6.3",
6263
"webpack": "^4.17.2",
6364
"webpack-command": "^0.4.1"
65+
},
66+
"dependencies": {
67+
"escape-string-regexp": "^1.0.0"
6468
}
6569
}

src/utils/pattern.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ describe('build', () => {
4747
expect(build(trie)).toBe('foo bar');
4848
});
4949

50+
it('escapes characters with special meaning in RegEx', () => {
51+
const specialChars = String.raw`foo.?+|(){}^$\[]bar`;
52+
const trie = CharTrie.create({ [specialChars]: {} });
53+
54+
const pattern = String.raw`foo\.\?\+\|\(\)\{\}\^\$\\\[\]bar`;
55+
expect(build(trie)).toBe(pattern);
56+
});
57+
5058
it('processes complex trie', () => {
5159
const trie = CharTrie.create({
5260
M: {

src/utils/pattern.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import escapeStringRegexp from 'escape-string-regexp';
12
import { CharTrie } from '../types/charTrie';
23

34
/**
@@ -10,7 +11,7 @@ import { CharTrie } from '../types/charTrie';
1011
export function build(charTrie: CharTrie): string {
1112
const patternSegments = Array.from(
1213
[...charTrie],
13-
([head, suffixTrie]) => `${head}${build(suffixTrie)}`
14+
([head, suffixTrie]) => `${escapeStringRegexp(head)}${build(suffixTrie)}`
1415
);
1516

1617
let pattern = patternSegments.join('|');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"compilerOptions": {
33
"allowJs": true,
4-
"allowSyntheticDefaultImports": true,
54
"baseUrl": "./",
65
"downlevelIteration": true,
6+
"esModuleInterop": true,
77
"rootDir": "./",
88
"lib": [
99
"es5",

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,11 @@
880880
dependencies:
881881
"@babel/types" "^7.3.0"
882882

883+
"@types/escape-string-regexp@^1.0.0":
884+
version "1.0.0"
885+
resolved "https://registry.yarnpkg.com/@types/escape-string-regexp/-/escape-string-regexp-1.0.0.tgz#052d16d87db583b72daceae4eaabddb66954424c"
886+
integrity sha512-KAruqgk9/340M4MYYasdBET+lyYN8KMXUuRKWO72f4SbmIMMFp9nnJiXUkJS0HC2SFe4x0R/fLozXIzqoUfSjA==
887+
883888
"@types/eslint-visitor-keys@^1.0.0":
884889
version "1.0.0"
885890
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@@ -2520,7 +2525,7 @@ es6-symbol@^3.1.1, es6-symbol@~3.1.1:
25202525
d "^1.0.1"
25212526
es5-ext "^0.10.51"
25222527

2523-
escape-string-regexp@^1.0.5:
2528+
escape-string-regexp@^1.0.0, escape-string-regexp@^1.0.5:
25242529
version "1.0.5"
25252530
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
25262531
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=

0 commit comments

Comments
 (0)