Skip to content

Commit 2a3ed4d

Browse files
authored
Fix/472 (#518)
* remove unnecessary escapes in ranges * fix lintstagedrc * size-linit upd
1 parent 10d0b8d commit 2a3ed4d

File tree

6 files changed

+2616
-2767
lines changed

6 files changed

+2616
-2767
lines changed

.lintstagedrc

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"src/**/*.{ts,tsx}": [
3-
"trigen-scripts lint:ts",
4-
"git add"
5-
]
2+
"src/**/*.{ts,tsx}": "trigen-scripts lint:ts"
63
}

.size-limit

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
{
88
"path": "lib/cli.js",
9-
"limit": "1.6 KB",
9+
"limit": "1.7 KB",
1010
"webpack": false
1111
}
1212
]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"useragent": "^2.3.0"
4646
},
4747
"devDependencies": {
48-
"@size-limit/preset-small-lib": "^3.0.0",
48+
"@size-limit/preset-small-lib": "^4.6.0",
4949
"@trigen/scripts": "^6.0.0",
5050
"@trigen/scripts-plugin-babel": "^6.1.1",
5151
"@trigen/scripts-plugin-eslint": "^6.0.0",

src/regexp/optimize.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,14 @@ describe('UserAgentRegExp', () => {
2626
'Family(foo)?(?=NotFamily)'
2727
);
2828
});
29+
30+
it('should remove unnecessary escapes in ranges', () => {
31+
32+
expect(
33+
optimize('[\\.\\[]')
34+
).toBe(
35+
'[.[]'
36+
);
37+
});
2938
});
3039
});

src/regexp/optimize.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import {
66
} from './util';
77

88
export const OPTIMIZABLE_GROUP = /^\([\s\w\d_\-/!]+\)$/;
9+
export const CHARCLASS_UNESCAPES = /[/.$*+?[{}|()]/;
910

1011
/**
11-
* Optimize RegExp string: remove useless braces.
12+
* Optimize RegExp string:
13+
* - remove unnecessary braces;
14+
* - remove unnecessary escapes in ranges.
1215
* @param regExpStr - RegExp string to optimize.
1316
* @return Optimized RegExp string.
1417
*/
@@ -19,6 +22,7 @@ export function optimize(regExpStr: string) {
1922
let skip = false;
2023
let char = '';
2124
let prevChar = '';
25+
let nextChar = '';
2226
let postfix = '';
2327
let groupAccum = '';
2428
let optimizedRegExpStr = '';
@@ -27,6 +31,7 @@ export function optimize(regExpStr: string) {
2731

2832
char = regExpStr[i];
2933
prevChar = regExpStr[i - 1];
34+
nextChar = regExpStr[i + 1];
3035
skip = skipSquareBraces(skip, prevChar, char);
3136

3237
if (!skip
@@ -42,6 +47,14 @@ export function optimize(regExpStr: string) {
4247
groupAccum = '';
4348
}
4449

50+
if (skip
51+
&& char === ESCAPE_SYMBOL
52+
&& CHARCLASS_UNESCAPES.test(nextChar)
53+
) {
54+
i++;
55+
char = nextChar;
56+
}
57+
4558
if (inGroup) {
4659
groupAccum += char;
4760
} else {

0 commit comments

Comments
 (0)