Skip to content

Commit dae1bd3

Browse files
authored
feat(pluginutils): add exactRegex and prefixRegex (#1865)
1 parent a433ac9 commit dae1bd3

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

packages/pluginutils/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ export default function myPlugin(options = {}) {
223223
}
224224
```
225225

226+
### exactRegex
227+
228+
Constructs a RegExp that matches the exact string specified. This is useful for plugin hook filters.
229+
230+
Parameters: `(str: String, flags?: String)`<br>
231+
Returns: `RegExp`
232+
233+
#### Usage
234+
235+
```js
236+
import { exactRegex } from '@rollup/pluginutils';
237+
238+
exactRegex('foobar'); // /^foobar$/
239+
exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
240+
```
241+
226242
### makeLegalIdentifier
227243

228244
Constructs a bundle-safe identifier from a `String`.
@@ -255,6 +271,22 @@ normalizePath('foo\\bar'); // 'foo/bar'
255271
normalizePath('foo/bar'); // 'foo/bar'
256272
```
257273

274+
### prefixRegex
275+
276+
Constructs a RegExp that matches a value that has the specified prefix. This is useful for plugin hook filters.
277+
278+
Parameters: `(str: String, flags?: String)`<br>
279+
Returns: `RegExp`
280+
281+
#### Usage
282+
283+
```js
284+
import { prefixRegex } from '@rollup/pluginutils';
285+
286+
prefixRegex('foobar'); // /^foobar/
287+
prefixRegex('foo(bar)', 'i'); // /^foo\(bar\)/i
288+
```
289+
258290
## Meta
259291

260292
[CONTRIBUTING](/.github/CONTRIBUTING.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function exactRegex(str: string, flags?: string): RegExp {
2+
return new RegExp(`^${escapeRegex(str)}$`, flags);
3+
}
4+
5+
export function prefixRegex(str: string, flags?: string): RegExp {
6+
return new RegExp(`^${escapeRegex(str)}`, flags);
7+
}
8+
9+
const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
10+
function escapeRegex(str: string): string {
11+
return str.replace(escapeRegexRE, '\\$&');
12+
}

packages/pluginutils/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import dataToEsm from './dataToEsm';
55
import extractAssignedNames from './extractAssignedNames';
66
import makeLegalIdentifier from './makeLegalIdentifier';
77
import normalizePath from './normalizePath';
8+
import { exactRegex, prefixRegex } from './filterUtils';
89

910
export {
1011
addExtension,
1112
attachScopes,
1213
createFilter,
1314
dataToEsm,
15+
exactRegex,
1416
extractAssignedNames,
1517
makeLegalIdentifier,
16-
normalizePath
18+
normalizePath,
19+
prefixRegex
1720
};
1821

1922
// TODO: remove this in next major
@@ -22,7 +25,9 @@ export default {
2225
attachScopes,
2326
createFilter,
2427
dataToEsm,
28+
exactRegex,
2529
extractAssignedNames,
2630
makeLegalIdentifier,
27-
normalizePath
31+
normalizePath,
32+
prefixRegex
2833
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava';
2+
3+
import { exactRegex, prefixRegex } from '../';
4+
5+
test('exactRegex supports without flag parameter', (t) => {
6+
t.is(exactRegex('foo').toString(), '/^foo$/');
7+
});
8+
9+
test('exactRegex supports with flag parameter', (t) => {
10+
t.is(exactRegex('foo', 'i').toString(), '/^foo$/i');
11+
});
12+
13+
test('exactRegex escapes special characters for Regex', (t) => {
14+
t.is(exactRegex('foo(bar)').toString(), '/^foo\\(bar\\)$/');
15+
});
16+
17+
test('prefixRegex supports without flag parameter', (t) => {
18+
t.is(prefixRegex('foo').toString(), '/^foo/');
19+
});
20+
21+
test('prefixRegex supports with flag parameter', (t) => {
22+
t.is(prefixRegex('foo', 'i').toString(), '/^foo/i');
23+
});
24+
25+
test('prefixRegex escapes special characters for Regex', (t) => {
26+
t.is(prefixRegex('foo(bar)').toString(), '/^foo\\(bar\\)/');
27+
});

packages/pluginutils/types/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ export function createFilter(
6262
*/
6363
export function dataToEsm(data: unknown, options?: DataToEsmOptions): string;
6464

65+
/**
66+
* Constructs a RegExp that matches the exact string specified.
67+
* @param str the string to match.
68+
* @param flags flags for the RegExp.
69+
*/
70+
export function exactRegex(str: string, flags?: string): RegExp;
71+
6572
/**
6673
* Extracts the names of all assignment targets based upon specified patterns.
6774
* @param param An `acorn` AST Node.
@@ -78,21 +85,32 @@ export function makeLegalIdentifier(str: string): string;
7885
*/
7986
export function normalizePath(filename: string): string;
8087

88+
/**
89+
* Constructs a RegExp that matches a value that has the specified prefix.
90+
* @param str the string to match.
91+
* @param flags flags for the RegExp.
92+
*/
93+
export function prefixRegex(str: string, flags?: string): RegExp;
94+
8195
export type AddExtension = typeof addExtension;
8296
export type AttachScopes = typeof attachScopes;
8397
export type CreateFilter = typeof createFilter;
98+
export type ExactRegex = typeof exactRegex;
8499
export type ExtractAssignedNames = typeof extractAssignedNames;
85100
export type MakeLegalIdentifier = typeof makeLegalIdentifier;
86101
export type NormalizePath = typeof normalizePath;
87102
export type DataToEsm = typeof dataToEsm;
103+
export type PrefixRegex = typeof prefixRegex;
88104

89105
declare const defaultExport: {
90106
addExtension: AddExtension;
91107
attachScopes: AttachScopes;
92108
createFilter: CreateFilter;
93109
dataToEsm: DataToEsm;
110+
exactRegex: ExactRegex;
94111
extractAssignedNames: ExtractAssignedNames;
95112
makeLegalIdentifier: MakeLegalIdentifier;
96113
normalizePath: NormalizePath;
114+
prefixRegex: PrefixRegex;
97115
};
98116
export default defaultExport;

0 commit comments

Comments
 (0)