Skip to content

Consider stripping a leading and trailing slash from configuration property keys #6910

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

Closed
remcohaszing opened this issue Aug 29, 2018 · 9 comments

Comments

@remcohaszing
Copy link
Contributor

🚀 Feature Proposal

Consider stripping a leading and trailing slash (/) from configuration property keys if the key represents a regular expression.

Motivation

This would allow to use an actual regular expression in several configuration locations. This adds syntax highlighting and linting capabilities to regular expressions used in Jest configuration, and removes the need to use double backslashes.

Example

From the moduleNameMapper example configuration

{
  "moduleNameMapper": {
    "^image![a-zA-Z0-9$_-]+$": "GlobalImageStub",
    "^[./a-zA-Z0-9$_-]+\\.png$": "<rootDir>/RelativeImageStub.js",
    "module_name_(.*)": "<rootDir>/substituted_module_$1.js"
  }
}

In JavaScript, this could be written as

module.exports = {
  moduleNameMapper: {
    [/image![a-zA-Z0-9$_-]+$/.source]: 'GlobalImageStub',
    [/^[./a-zA-Z0-9$_-]+\.png$/.source]: '<rootDir>/RelativeImageStub.js',
    [/module_name_(.*)/.source]: '<rootDir>/substituted_module_$1.js'
  }
}

It would be great if the .source could simply be removed

module.exports = {
  moduleNameMapper: {
    [/image![a-zA-Z0-9$_-]+$/]: 'GlobalImageStub',
    [/^[./a-zA-Z0-9$_-]+\.png$/]: '<rootDir>/RelativeImageStub.js',
    [/module_name_(.*)/]: '<rootDir>/substituted_module_$1.js'
  }
}

However, the last example won’t work currently, because RegExp.prototype.toString() adds a preceding and a trailing slash.

Pitch

This implements a feature regarding the parsing of jest.config.js.

@SimenB
Copy link
Member

SimenB commented Aug 29, 2018

Definitely! Right now we have quite a bit of new RegExp spread around, would be great to consolidate it all to normalize in jest-config. Then we could do a check if the input is already a regex before calling new RegExp on it. I'm not sure how much work that would be, but just greping for new RegExp didn't show too much so it might not be too bad.

Wanna send a PR for it?

@alexanderbird
Copy link

For what it's worth: I've written a failing test for the moduleNameMapper example.

master...alexanderbird:support-regexp-configuration-keys

Not sure when I'll be able to come back to this, so if anyone else wants to run with it don't let me stop you :)


Incidentally, that unit test I added to is looking pretty long, is there any reason I shouldn't split it up into separate tests for each expectation?

@SimenB
Copy link
Member

SimenB commented Oct 6, 2018

Incidentally, that unit test I added to is looking pretty long, is there any reason I shouldn't split it up into separate tests for each expectation?

Feel free to split it up into logical parts!

@alexanderbird
Copy link

@SimenB it looks like RegExp literals get toString()ed when the object is initialized, so when we normalize it it's already a string -- so there's no way to tell between a RegExp literal and a string surrounded by slashes.

I can update the documentation to warn about this, but before I continue I want to confirm that's an ok solution.

I've written a minimal solution to normalize the strings:
master...alexanderbird:support-regexp-configuration-keys

@SimenB
Copy link
Member

SimenB commented Oct 7, 2018

it looks like RegExp literals get toString()ed when the object is initialized

Can we change that?

@remcohaszing
Copy link
Contributor Author

No,sorry, I thought this was clear.

Object keys are always strings. The string representation of a RegExp contains a leading and a trailing slash. So my idea was to strip the leading and trailing space if the object key looks like a string representation of a RegExp object.

So this:

module.exports = {
  moduleNameMapper: {
    [/foo/]: 'bar',
  }
}

Equivalent to:

module.exports = {
  moduleNameMapper: {
    '/foo/': 'bar',
  }
}

Would be interpreted as this:

module.exports = {
  moduleNameMapper: {
    'foo': 'bar',
  }
}

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants