Skip to content

Commit

Permalink
Fix/472 (#518)
Browse files Browse the repository at this point in the history
* remove unnecessary escapes in ranges

* fix lintstagedrc

* size-linit upd
  • Loading branch information
dangreen committed Oct 17, 2020
1 parent 10d0b8d commit 2a3ed4d
Show file tree
Hide file tree
Showing 6 changed files with 2,616 additions and 2,767 deletions.
5 changes: 1 addition & 4 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"src/**/*.{ts,tsx}": [
"trigen-scripts lint:ts",
"git add"
]
"src/**/*.{ts,tsx}": "trigen-scripts lint:ts"
}
2 changes: 1 addition & 1 deletion .size-limit
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"path": "lib/cli.js",
"limit": "1.6 KB",
"limit": "1.7 KB",
"webpack": false
}
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"useragent": "^2.3.0"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^3.0.0",
"@size-limit/preset-small-lib": "^4.6.0",
"@trigen/scripts": "^6.0.0",
"@trigen/scripts-plugin-babel": "^6.1.1",
"@trigen/scripts-plugin-eslint": "^6.0.0",
Expand Down
9 changes: 9 additions & 0 deletions src/regexp/optimize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ describe('UserAgentRegExp', () => {
'Family(foo)?(?=NotFamily)'
);
});

it('should remove unnecessary escapes in ranges', () => {

expect(
optimize('[\\.\\[]')
).toBe(
'[.[]'
);
});
});
});
15 changes: 14 additions & 1 deletion src/regexp/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {
} from './util';

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

/**
* Optimize RegExp string: remove useless braces.
* Optimize RegExp string:
* - remove unnecessary braces;
* - remove unnecessary escapes in ranges.
* @param regExpStr - RegExp string to optimize.
* @return Optimized RegExp string.
*/
Expand All @@ -19,6 +22,7 @@ export function optimize(regExpStr: string) {
let skip = false;
let char = '';
let prevChar = '';
let nextChar = '';
let postfix = '';
let groupAccum = '';
let optimizedRegExpStr = '';
Expand All @@ -27,6 +31,7 @@ export function optimize(regExpStr: string) {

char = regExpStr[i];
prevChar = regExpStr[i - 1];
nextChar = regExpStr[i + 1];
skip = skipSquareBraces(skip, prevChar, char);

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

if (skip
&& char === ESCAPE_SYMBOL
&& CHARCLASS_UNESCAPES.test(nextChar)
) {
i++;
char = nextChar;
}

if (inGroup) {
groupAccum += char;
} else {
Expand Down
Loading

0 comments on commit 2a3ed4d

Please sign in to comment.