Skip to content

Use replaceAll when replacing separators #15288

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
karthiknadig opened this issue Feb 1, 2021 · 14 comments · Fixed by #17582
Closed

Use replaceAll when replacing separators #15288

karthiknadig opened this issue Feb 1, 2021 · 14 comments · Fixed by #17582
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug good first issue regression Bug didn't exist in a previous release

Comments

@karthiknadig karthiknadig added bug Issue identified by VS Code Team member as probable bug good first issue needs PR triage-needed Needs assignment to the proper sub-team labels Feb 1, 2021
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Feb 1, 2021
@brettcannon brettcannon added the regression Bug didn't exist in a previous release label Feb 2, 2021
@KamalSinghKhanna
Copy link

Hi, I would like to work on this issue,can you suggest me ways to get started?

@karthiknadig
Copy link
Member Author

@KamalSinghKhanna go ahead

@vijaya-lakshmi-venkatraman

Hello,
I would like to pick this issue if still available.

So the required changes are :

  1. In /pythonEnvironments/base/info/interpreter.ts (Line 73)
// Concat these together to make a set of quoted strings
    const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replaceAll('\\', '\\\\')}"`), '');
  1. In /pythonEnvironments/info/interpreter.ts (Line 60)
// Concat these together to make a set of quoted strings
    const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '\\\\')}"`), '');

Is that right?

@karthiknadig
Copy link
Member Author

karthiknadig commented May 5, 2021

@vijaya-lakshmi-venkatraman That is correct.

Edit: You may have to add the replaceAll API to the string prototype. src\client\common\extensions.ts

@vijaya-lakshmi-venkatraman

Thanks @karthiknadig.
I would like to take this up if @KamalSinghKhanna is not working on this anymore.

@karthiknadig
Copy link
Member Author

@vijaya-lakshmi-venkatraman go ahead.

@karrtikr karrtikr mentioned this issue May 7, 2021
@KamalSinghKhanna
Copy link

KamalSinghKhanna commented Jun 1, 2021

Is this issue still open or @vijaya-lakshmi-venkatraman is still working on this issue

@karthiknadig
Copy link
Member Author

One thing to note. there is no replaceAll API. There are two ways one could do this. Either extend string prototype to add a replaceAll. Or use pattern matching to replace all.

if you decide to extend string, you can do that here src\client\common\extensions.ts . Then all you have to do is include it in that file where you consume replaceAll

@IceJinx33
Copy link

Hi, is this issue still open or is @KamalSinghKhanna or @vijaya-lakshmi-venkatraman working on this? If not could I try working on this?

@KamalSinghKhanna
Copy link

Hi @IceJinx33 , feel free to work on this.

@IceJinx33
Copy link

@karthiknadig, The original Code QL issue link link doesn't seem to be working. Where can I verify the correct requirements of the code?

@karthiknadig
Copy link
Member Author

@IceJinx33 This is what the CodeQL link says:
image

@IceJinx33
Copy link

IceJinx33 commented Oct 1, 2021

@karthiknadig, I extended the string in src\client\common\extensions.ts to have a replaceAll function, which simply uses a RegExp global search 'g' to replace an old substring with a new one. However, the old substring needs to be escaped properly to get a correct RegExp, and the escaping function from MDN Web Docs escapes a limited set of special characters.

replaceAll

replaceAll is used in the following files for replacing '\\' with '\\\\'
https://github.com/microsoft/vscode-python/blob/2021.1.502429796/src/client/pythonEnvironments/base/info/interpreter.ts
https://github.com/microsoft/vscode-python/blob/2021.1.502429796/src/client/pythonEnvironments/info/interpreter.ts

In the extensions.unit.tests.ts, I added the following tests for the above use case and they pass when tested using npm run test:unittests -- --grep='String Extensions' after compilation.

test

output

Are there any test cases I could be missing? I'm also not really sure if the escaping function covers all cases, so is the way I've documented it okay? Or should I think of a different approach?

@karthiknadig
Copy link
Member Author

@IceJinx33 This is fantastic. Can you open a PR.

@karthiknadig karthiknadig linked a pull request Oct 1, 2021 that will close this issue
@luabud luabud closed this as completed Nov 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug good first issue regression Bug didn't exist in a previous release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants