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

Implement support for && and || in v sets #52

Merged
merged 4 commits into from
Dec 28, 2021

Conversation

nicolo-ribaudo
Copy link
Collaborator

@nicolo-ribaudo nicolo-ribaudo commented Sep 7, 2021

This adds support for && and ||. It doesn't support string alternatives in sets ([\q{ab|cd}]) or the updates to \p: I'll open other PRs for them.

I'm marking this as a wip because:

@@ -312,17 +402,20 @@ const regjsparserFeatures = {
'unicodePropertyEscape': true,
'namedGroups': true,
'lookbehind': true,
'unicodeSet': true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be enabled by default

@@ -363,21 +462,34 @@ const rewritePattern = (pattern, flags, options) => {
validateOptions(options);

config.flags.unicode = hasFlag(flags, 'u');
config.flags.unicodeSet = hasFlag(flags, 'v');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s continue matching the RegExp.prototype getter name, which in this case is unicodeSets.

// unicodeFlag: 'transform' implies unicodePropertyEscapes: 'transform'
config.transform.unicodePropertyEscapes = config.flags.unicode && (
transform(options, 'unicodeFlag') || transform(options, 'unicodePropertyEscapes')
);
config.transform.namedGroups = transform(options, 'namedGroups');

const regjsparserFeatures = {
'unicodeSet': Boolean(options && options.unicodeSetFlag),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps unicodeSets here too, for consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regjsparser option, so we could change it when it has a breaking release. However, it also uses unicodePropertyEscape so it's consistent with itself.

tests/tests.js Outdated
},
];

describe('unicode set (v) flag', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('unicode set (v) flag', () => {
describe('unicodeSets (v) flag', () => {

tests/tests.js Outdated
@@ -1001,3 +1001,134 @@ describe('character classes', () => {
}
});

const TRANSFORM_U = { unicodeFlag: 'transform', unicodeSetFlag: 'transform' };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m confused by this combination of settings, so just to clarify:

  1. The grammar for the u flag does not change with the v flag proposal.
  2. Only the v flag enables the new functionality.
  3. The two flags cannot be combined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the readme, hopefully it makes it clear. By default v is transformed to u, but you can set unicodeFlag: 'transform' to transform it even more. This is a sensible default, because now many browsers support u natively.

We also do something similar in Babel, where class fields are transformed to ES6 classes and then you can choose to transform them even more to ES5.

@mathiasbynens mathiasbynens merged commit 4a7b332 into mathiasbynens:main Dec 28, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the set-notation branch December 28, 2021 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants