Skip to content

feat(pluginutils): add exactRegex and prefixRegex #1865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/pluginutils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ export default function myPlugin(options = {}) {
}
```

### exactRegex

Constructs a RegExp that matches the exact string specified. This is useful for plugin hook filters.

Parameters: `(str: String, flags?: String)`<br>
Returns: `RegExp`

#### Usage

```js
import { exactRegex } from '@rollup/pluginutils';

exactRegex('foobar'); // /^foobar$/
exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
```

### makeLegalIdentifier

Constructs a bundle-safe identifier from a `String`.
Expand Down Expand Up @@ -255,6 +271,22 @@ normalizePath('foo\\bar'); // 'foo/bar'
normalizePath('foo/bar'); // 'foo/bar'
```

### prefixRegex

Constructs a RegExp that matches a value that has the specified prefix. This is useful for plugin hook filters.

Parameters: `(str: String, flags?: String)`<br>
Returns: `RegExp`

#### Usage

```js
import { prefixRegex } from '@rollup/pluginutils';

prefixRegex('foobar'); // /^foobar/
prefixRegex('foo(bar)', 'i'); // /^foo\(bar\)/i
```

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)
Expand Down
12 changes: 12 additions & 0 deletions packages/pluginutils/src/filterUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function exactRegex(str: string, flags?: string): RegExp {
return new RegExp(`^${escapeRegex(str)}$`, flags);
}

export function prefixRegex(str: string, flags?: string): RegExp {
return new RegExp(`^${escapeRegex(str)}`, flags);
}

const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
function escapeRegex(str: string): string {
return str.replace(escapeRegexRE, '\\$&');
}
9 changes: 7 additions & 2 deletions packages/pluginutils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import dataToEsm from './dataToEsm';
import extractAssignedNames from './extractAssignedNames';
import makeLegalIdentifier from './makeLegalIdentifier';
import normalizePath from './normalizePath';
import { exactRegex, prefixRegex } from './filterUtils';

export {
addExtension,
attachScopes,
createFilter,
dataToEsm,
exactRegex,
extractAssignedNames,
makeLegalIdentifier,
normalizePath
normalizePath,
prefixRegex
};

// TODO: remove this in next major
Expand All @@ -22,7 +25,9 @@ export default {
attachScopes,
createFilter,
dataToEsm,
exactRegex,
extractAssignedNames,
makeLegalIdentifier,
normalizePath
normalizePath,
prefixRegex
};
27 changes: 27 additions & 0 deletions packages/pluginutils/test/filterUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'ava';

import { exactRegex, prefixRegex } from '../';

test('exactRegex supports without flag parameter', (t) => {
t.is(exactRegex('foo').toString(), '/^foo$/');
});

test('exactRegex supports with flag parameter', (t) => {
t.is(exactRegex('foo', 'i').toString(), '/^foo$/i');
});

test('exactRegex escapes special characters for Regex', (t) => {
t.is(exactRegex('foo(bar)').toString(), '/^foo\\(bar\\)$/');
});

test('prefixRegex supports without flag parameter', (t) => {
t.is(prefixRegex('foo').toString(), '/^foo/');
});

test('prefixRegex supports with flag parameter', (t) => {
t.is(prefixRegex('foo', 'i').toString(), '/^foo/i');
});

test('prefixRegex escapes special characters for Regex', (t) => {
t.is(prefixRegex('foo(bar)').toString(), '/^foo\\(bar\\)/');
});
18 changes: 18 additions & 0 deletions packages/pluginutils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export function createFilter(
*/
export function dataToEsm(data: unknown, options?: DataToEsmOptions): string;

/**
* Constructs a RegExp that matches the exact string specified.
* @param str the string to match.
* @param flags flags for the RegExp.
*/
export function exactRegex(str: string, flags?: string): RegExp;

/**
* Extracts the names of all assignment targets based upon specified patterns.
* @param param An `acorn` AST Node.
Expand All @@ -78,21 +85,32 @@ export function makeLegalIdentifier(str: string): string;
*/
export function normalizePath(filename: string): string;

/**
* Constructs a RegExp that matches a value that has the specified prefix.
* @param str the string to match.
* @param flags flags for the RegExp.
*/
export function prefixRegex(str: string, flags?: string): RegExp;

export type AddExtension = typeof addExtension;
export type AttachScopes = typeof attachScopes;
export type CreateFilter = typeof createFilter;
export type ExactRegex = typeof exactRegex;
export type ExtractAssignedNames = typeof extractAssignedNames;
export type MakeLegalIdentifier = typeof makeLegalIdentifier;
export type NormalizePath = typeof normalizePath;
export type DataToEsm = typeof dataToEsm;
export type PrefixRegex = typeof prefixRegex;

declare const defaultExport: {
addExtension: AddExtension;
attachScopes: AttachScopes;
createFilter: CreateFilter;
dataToEsm: DataToEsm;
exactRegex: ExactRegex;
extractAssignedNames: ExtractAssignedNames;
makeLegalIdentifier: MakeLegalIdentifier;
normalizePath: NormalizePath;
prefixRegex: PrefixRegex;
};
export default defaultExport;
Loading