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

Add prefer-replace-all rule #488

Merged
merged 20 commits into from Feb 1, 2020
Merged

Add prefer-replace-all rule #488

merged 20 commits into from Feb 1, 2020

Conversation

jmoore914
Copy link
Contributor

Fixes #433

rules/prefer-replace-all.js Outdated Show resolved Hide resolved
rules/prefer-replace-all.js Outdated Show resolved Hide resolved
rules/prefer-replace-all.js Outdated Show resolved Hide resolved

function checkGlobalFlag(node) {
const searchPattern = node.arguments[0];
return Object.prototype.hasOwnProperty.call(searchPattern, 'regex') && searchPattern.regex.flags === 'g';
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not just searchPattern && searchPattern.regex && searchPattern.regex.flags === 'g'.
I understand flags with i should not pass, but how about other flags like gu?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the built in replace method have full unicode support? From what I'm reading it seems like it doesn't. And all of the other flags would change the search behavior as well.


function replaceNode(node, fixer) {
const stringName = node.callee.object.name;
const searchPattern = node.arguments[0].regex.pattern.replace(/\\(.)/g, '$1');
Copy link
Collaborator

@fisker fisker Dec 30, 2019

Choose a reason for hiding this comment

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

This line is not right, /\\a/g is searching for \a not a, maybe .replace(/\\\\(.)/g, '\\$1')?.

Also, funny thing.

I think we can also replace it with .replace(/\\\\/g, '\\'), then this line will against this rule itself, so maybe .replaceAll('\\\\', '\\')?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. On second thought, that replace shouldn't be necessary at all. However, I do need to add an escape for any double quotes in the original regex expression.

Yeah, that ocurred to me while I was writing it. Very ironic. I decided not to use replaceAll since it doesn't have support in node yet and it would be transpiled back to the expression I have if we ran it through babel.

rules/prefer-replace-all.js Outdated Show resolved Hide resolved
rules/prefer-replace-all.js Outdated Show resolved Hide resolved
@fisker
Copy link
Collaborator

fisker commented Jan 3, 2020

@jmoore914 Can you check 715eb67 ? Make sure I didn't miss any test cases

Comment on lines 80 to 85
// Escaped symbols
{
code: 'foo.replace(/searchPattern/g, \'\\\'escapedQuotes\\\'\')',
output: 'foo.replaceAll(\'searchPattern\', \'\\\'escapedQuotes\\\'\')',
code: 'foo.replace(/\\./g, bar)',
output: 'foo.replaceAll(\'.\', bar)',
errors: [error]
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

@jmoore914 I think I found a broken case, we still need replace \

Copy link
Collaborator

@fisker fisker Jan 3, 2020

Choose a reason for hiding this comment

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

Maybe it's not that simple at all, I think we should test all these cases

/\./g 
/\\./g 
/\\\./g 

rules/prefer-replace-all.js Outdated Show resolved Hide resolved
rules/prefer-replace-all.js Outdated Show resolved Hide resolved
jmoore914 and others added 2 commits January 4, 2020 10:21
@fisker
Copy link
Collaborator

fisker commented Jan 11, 2020

@jmoore914 see

#488 (comment)

@jmoore914
Copy link
Contributor Author

@jmoore914 see

#488 (comment)

Newest PR should fix this comment

Copy link
Collaborator

@fisker fisker left a comment

Choose a reason for hiding this comment

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

LGTM

@sindresorhus sindresorhus merged commit d98c277 into sindresorhus:master Feb 1, 2020
@sindresorhus
Copy link
Owner

Thanks :)

@jmoore914 jmoore914 deleted the prefer-replace-all branch February 3, 2020 22:34
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.

Rule proposal: prefer-replace-all
3 participants