Skip to content

Commit e927421

Browse files
Merge pull request DmitrySoshnikov#104 from Golmote/remove-empty-group-transform
Optimizer: remove non-capturing empty groups
2 parents 40539f8 + 70c3bbd commit e927421

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017-present Dmitry Soshnikov <[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
const {transform} = require('../../../transform');
9+
const removeEmptyGroup = require('../remove-empty-group-transform');
10+
11+
describe('remove empty groups', () => {
12+
13+
it('removes empty groups', () => {
14+
const re = transform(/a(?:)b/, [
15+
removeEmptyGroup
16+
]);
17+
expect(re.toString()).toBe('/ab/');
18+
19+
const re2 = transform(/((?:))/, [
20+
removeEmptyGroup
21+
]);
22+
expect(re2.toString()).toBe('/()/');
23+
});
24+
25+
it('does not remove empty regexp', () => {
26+
const re = transform(/(?:)/, [
27+
removeEmptyGroup
28+
]);
29+
expect(re.toString()).toBe('/(?:)/');
30+
});
31+
32+
it('removes empty group quantifier', () => {
33+
const re = transform(/(?:)+/, [
34+
removeEmptyGroup
35+
]);
36+
expect(re.toString()).toBe('/(?:)/');
37+
});
38+
39+
});

src/optimizer/transforms/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ module.exports = [
3030
// (a|b|c) -> [abc]
3131
require('./group-single-chars-to-char-class'),
3232

33+
// (?:)a -> a
34+
require('./remove-empty-group-transform'),
35+
3336
// (?:a) -> a
3437
require('./ungroup-transform')
3538
];
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017-present Dmitry Soshnikov <[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
/**
9+
* A regexp-tree plugin to remove non-capturing empty groups.
10+
*
11+
* /(?:)a/ -> /a/
12+
* /a|(?:)/ -> /a|/
13+
*/
14+
15+
module.exports = {
16+
Group(path) {
17+
const {node, parent} = path;
18+
const childPath = path.getChild();
19+
20+
if (node.capturing || childPath) {
21+
return;
22+
}
23+
24+
if (parent.type === 'Repetition') {
25+
26+
path.getParent().replace(node);
27+
28+
} else if (parent.type !== 'RegExp') {
29+
30+
path.remove();
31+
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)