Skip to content
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

Re-introduced RegExp escaping stage 2 proposal #1294

Merged
merged 3 commits into from
Oct 1, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog
##### Unreleased
- Re-introduced [`RegExp` escaping stage 2 proposal](https://github.com/tc39/proposal-regex-escaping), September 2023 TC39 meeting:
- Added `RegExp.escape` method with the new set of symbols for escaping
- Some years ago, it was presented in `core-js`, but it was removed after rejecting the old version of this proposal
- Added [`ArrayBuffer.prototype.{ transfer, transferToFixedLength }`](https://github.com/tc39/proposal-arraybuffer-transfer) and support transferring of `ArrayBuffer`s via [`structuredClone`](https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone) to engines with `MessageChannel`
- Optimized [`Math.f16round`](https://github.com/tc39/proposal-float16array) polyfill
- Fixed [some conversion cases](https://github.com/petamoriken/float16/issues/1046) of [`Math.f16round` and `DataView.prototype.{ getFloat16, setFloat16 }`](https://github.com/tc39/proposal-float16array)
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
- [`Map.prototype.emplace`](#mapprototypeemplace)
- [`Array.isTemplateObject`](#arrayistemplateobject)
- [`String.dedent`](#stringdedent)
- [`RegExp` escaping](#regexp-escaping)
- [`Symbol` predicates](#symbol-predicates)
- [Stage 1 proposals](#stage-1-proposals)
- [`Observable`](#observable)
Expand Down Expand Up @@ -2666,6 +2667,24 @@ String.dedent(console.log)`
print('${ message }')
`; // => ["print('", "')", raw: Array(2)], 42
```
##### [`RegExp` escaping](https://github.com/tc39/proposal-regex-escaping)[⬆](#index)
Module [`esnext.regexp.escape`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.regexp.escape.js)
```js
class RegExp {
static escape(value: string): string
}
```
[*CommonJS entry points:*](#commonjs-api)
```js
core-js/proposals/regexp-escaping
core-js(-pure)/full/regexp/escape
```
[*Example*](https://tinyurl.com/yqvehz5c):
```js
console.log(RegExp.escape('10$')); // => '\\x310\\$'
console.log(RegExp.escape('abcdefg_123456')); // => 'abcdefg_123456'
console.log(RegExp.escape('(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`')); // => '\\(\\)\\{\\}\\[\\]\\|\\,\\.\\?\\*\\+\\-\\^\\$\\=\\<\\>\\\\\\/\\#\\&\\!\\%\\:\\;\\@\\~\\\'\\"\\`'
```
##### [`Symbol` predicates](https://github.com/tc39/proposal-symbol-predicates)[⬆](#index)
Modules [`esnext.symbol.is-registered-symbol`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.symbol.is-registered-symbol.js), [`esnext.symbol.is-well-known-symbol`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.symbol.is-well-known-symbol.js).
```js
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,8 @@ export const data = {
// TODO: Remove from `core-js@4`
'esnext.reflect.metadata': {
},
'esnext.regexp.escape': {
},
'esnext.set.add-all': {
},
'esnext.set.delete-all': {
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,7 @@ export default {
'esnext.data-view.set-uint8-clamped',
'esnext.math.f16round',
],
3.33: [
'esnext.regexp.escape',
],
};
5 changes: 5 additions & 0 deletions packages/core-js/full/regexp/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
require('../../modules/esnext.regexp.escape');
var path = require('../../internals/path');

module.exports = path.RegExp.escape;
1 change: 1 addition & 0 deletions packages/core-js/full/regexp/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var parent = require('../../actual/regexp');
require('../../modules/esnext.regexp.escape');

module.exports = parent;
20 changes: 20 additions & 0 deletions packages/core-js/modules/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var toString = require('../internals/to-string');
var WHITESPACES = require('../internals/whitespaces');

var charCodeAt = uncurryThis(''.charCodeAt);
var replace = uncurryThis(''.replace);
var NEED_ESCAPING = RegExp('[!"#$%&\'()*+,\\-./:;<=>?@[\\\\\\]^`{|}~' + WHITESPACES + ']', 'g');

// `RegExp.escape` method
// https://github.com/tc39/proposal-regex-escaping
$({ target: 'RegExp', stat: true, forced: true }, {
escape: function escape(S) {
var str = toString(S);
var firstCode = charCodeAt(str, 0);
// escape first DecimalDigit
return (firstCode > 47 && firstCode < 58 ? '\\x3' : '') + replace(str, NEED_ESCAPING, '\\$&');
}
});
3 changes: 3 additions & 0 deletions packages/core-js/proposals/regexp-escaping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
// https://github.com/tc39/proposal-regex-escaping
require('../modules/esnext.regexp.escape');
1 change: 1 addition & 0 deletions packages/core-js/stage/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('../proposals/array-is-template-object');
require('../proposals/async-iterator-helpers');
require('../proposals/iterator-range');
require('../proposals/map-upsert-stage-2');
require('../proposals/regexp-escaping');
require('../proposals/string-dedent');
require('../proposals/symbol-predicates-v2');
// TODO: Obsolete versions, remove from `core-js@4`
Expand Down
3 changes: 3 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,9 @@ GLOBAL.tests = {
'esnext.promise.with-resolvers': [PROMISES_SUPPORT, function () {
return Promise.withResolvers;
}],
'esnext.regexp.escape': function () {
return RegExp.escape;
},
'esnext.set.add-all': function () {
return Set.prototype.addAll;
},
Expand Down
2 changes: 2 additions & 0 deletions tests/entries/unit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(typeof load(NS, 'reflect/has-own-metadata') == 'function');
ok(typeof load(NS, 'reflect/metadata') == 'function');
ok(load(NS, 'promise/try')(() => 42) instanceof load(NS, 'promise'));
ok(load(NS, 'regexp/escape')('10$') === '\\x310\\$');
ok(load(NS, 'set/add-all')(new Set([1, 2, 3]), 4, 5).size === 5);
ok(load(NS, 'set/delete-all')(new Set([1, 2, 3]), 4, 5) === false);
ok(load(NS, 'set/every')(new Set([1, 2, 3]), it => typeof it == 'number'));
Expand Down Expand Up @@ -953,6 +954,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
load('proposals/promise-with-resolvers');
load('proposals/reflect-metadata');
load('proposals/regexp-dotall-flag');
load('proposals/regexp-escaping');
load('proposals/regexp-named-groups');
load('proposals/relative-indexing-method');
load('proposals/seeded-random');
Expand Down
27 changes: 27 additions & 0 deletions tests/unit-global/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createConversionChecker } from '../helpers/helpers.js';

QUnit.test('RegExp.escape', assert => {
const { escape } = RegExp;
assert.isFunction(escape);
assert.arity(escape, 1);
assert.name(escape, 'escape');
assert.looksNative(escape);
assert.nonEnumerable(RegExp, 'escape');

assert.same(escape('10$'), '\\x310\\$', '10$');
assert.same(escape('abcdefg_123456'), 'abcdefg_123456', 'abcdefg_123456');
assert.same(
escape('(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`'),
'\\(\\)\\{\\}\\[\\]\\|\\,\\.\\?\\*\\+\\-\\^\\$\\=\\<\\>\\\\\\/\\#\\&\\!\\%\\:\\;\\@\\~\\\'\\"\\`',
'(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`',
);

const checker = createConversionChecker('10$');
assert.same(escape(checker), '\\x310\\$', 'checker result');
assert.same(checker.$valueOf, 0, 'checker valueOf calls');
assert.same(checker.$toString, 1, 'checker toString calls');

if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => escape(Symbol('thrower')), TypeError, 'throws on symbol');
}
});
26 changes: 26 additions & 0 deletions tests/unit-pure/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import escape from 'core-js-pure/full/regexp/escape';
import Symbol from 'core-js-pure/es/symbol';
import { createConversionChecker } from '../helpers/helpers.js';

QUnit.test('RegExp.escape', assert => {
assert.isFunction(escape);
assert.arity(escape, 1);
assert.name(escape, 'escape');

assert.same(escape('10$'), '\\x310\\$', '10$');
assert.same(escape('abcdefg_123456'), 'abcdefg_123456', 'abcdefg_123456');
assert.same(
escape('(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`'),
'\\(\\)\\{\\}\\[\\]\\|\\,\\.\\?\\*\\+\\-\\^\\$\\=\\<\\>\\\\\\/\\#\\&\\!\\%\\:\\;\\@\\~\\\'\\"\\`',
'(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`',
);

const checker = createConversionChecker('10$');
assert.same(escape(checker), '\\x310\\$', 'checker result');
assert.same(checker.$valueOf, 0, 'checker valueOf calls');
assert.same(checker.$toString, 1, 'checker toString calls');

if (!Symbol.sham) {
assert.throws(() => escape(Symbol('thrower')), TypeError, 'throws on symbol');
}
});