-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix Regexes for JavaScript objects #1
Comments
@baseplate-admin I want to this one :) |
Hi there. Thanks for replying. Do you have a working regex? If you do please put a pull request and i will happily merge it. 🥳 ( if you dont understand any part of my code. Please do tell me ) |
I am a bit familiar with regex. I work on this for a little bit and If I get stuck I ask you for help. Happy coding :) |
Thank you very much. Since this is one of my very first project i might've not followed the best practices. But thank you again for your contribution. I will be waiting for your pull request . Cheers🍻 Edit : I am assigning you for this issue |
@baseplate-admin I am pretty sure the issue is with this part of regex Instead of this we can modify this one |
I think i kinda see your point.
But lets take a look here. Copied from here So here you can see that there are JS specific words like break , try , case. Also there are numbers. So if we try your approach it might not work all the time. For example your approach won't work on this: const superCoolNumber = 1
const carsNumbers = 12 Because what we want is ( valid one line JS ) : const superCoolNumber = 1;const carsNumbers = 12
This regex probably wont work in our caseFor example : const hello = 'world'
const foo = {
str : 'bar'
} Will turn into : const hello = 'world';
const foo = {
str : 'bar'; // See the problem ?
} Also the regex should match ` ( backticks ) because they are common in JS apps. (?<=(["`']\b))(?:(?=(\\?))\2.)*?(?=\1) Now my approach here is to check if theres a closing bracket } after the newline Character. If there is dont add whitespace. anime(
{
targets: '.animejs__facebook__button',
translateX: 40 * 2,
easing: 'easeOutSine',
duration: 150,
opacity: 0,
scale: 0.2 // Dont add a line break here
} // Capture this bracket right here, if there is a bracket dont add a line break inside. Instead preferably add a comma
); But i just cant figure that regex out |
Also on another note. Do you think i should add testing? Will it help you in any way? If you do, could you please tell me what testing lib i should use? |
What about positive and negative lookaheads like these two: |
I think Unit testing is quite important for this project. |
Thanks for recommendation. So should i use pytest or unittest? I will work on the testing today :) |
Your approach is correct 🥳. Yea positive lookahead is the right way to go. But you have to ignore the whitespaces before the } ( bracket ). which i can't figure out 🤔 But don’t feel pressured. 😄 Take as much time as you need |
Currently, I am studying pytest. In this other project I did a little contribution https://github.com/neurodata/hyppo they validate everything with pytest. That could be a suitable choice. Yeah, I keep working on the regex :) 😄 |
@najmieh Hey do you think i should add this into the test ? anime(
{
targets: '.animejs__facebook__button',
translateX: 40 * 2,
easing: 'easeOutSine',
duration: 150,
opacity: 0,
scale: 0.2 // Dont add a line break here
} // Capture this bracket right here, if there is a bracket dont add a line break inside. Instead preferably add a comma
); So that when it finally works we know it works . ( Which means it will always fail unless we finish this error )
Wooh 😮 !!! Very cool project 🥇 |
Yes, Good idea! |
@najmieh So basically yea. We should ignore the whitespace and get the next character after newline. And then merge that into anime(
{
targets: '.animejs__facebook__button',
translateX: 40 * 2,
easing: 'easeOutSine',
duration: 150,
opacity: 0,
scale: 0.2
} // Ignore the whitespaces and capture the brackets after /n [ newline ]
); Edit : I will have the tests ready by tomorrow |
@najmieh Hey i added the tests. But the test's EXPECTED_JS may look ugly.Run it with :
But for now i am doing a fallback method to keep the extension running :for i in regex_occurances:
minified_js = minify_html.minify(
i,
minify_js=True,
)
replaced_regex_occurances.append(minified_js) When this issue is closed it will be : for i in regex_occurances:
substituated_regex = sub(NEW_LINE_REPLACE_PATTERN, r";", i)
replaced_regex_occurances.append(substituated_regex) |
This looks good! I've been out of self-learning for couple days :( |
Thank you 😄 🥳
Take your time 😁. No worries 😉. In the mean time I will be working on other stuffs 😛. |
@baseplate-admin this regex identifies all the white spaces except for the last one! |
Hi there, @najmieh var x = {
hello: 'world',
list: ['python', 'django', 'js']
};
var y = {
hello:'world',
list: {
test: '.py'
}
} Your approach wont work on all the above methods. |
Yeah, I see your point. I come up with a better idea. 🗿🗿🗿 |
@najmieh Thank you for contributing here. Please read this discussion here. If the other library is chosen, you might need to contribute there. Because I think a library by an official django developer would be better than my poorly written library. I thank you again for working with me. This is my very first open source project and so it meant a lot working with you here. |
yeah, needs a meticulous understanding! I was not aware. |
Yep spending developer time in solving the same issue in 2 different way will produce no result (I am looking at you flask) Instead we should look forward to improving functionality, not reinventing the wheel. Because if we dont focus on improving functionality we will have 10 packages that are not feature complete and they essentially do the same thing. Lets hear from @adamchainz before taking any step. I worked with him today and he seems a nice guy. |
I am afraid I'm not interested in attempting to parse JS with a regex. I feel like it's unlikely to be possible. Only "regular" languages can be parsed with a regex, and JavaScript has so many features and quirks I believe it's unlikely to be possible. You need a real parser. I'm going to unsub from this issue now. |
Got it. I am gonna work on rust esbuild. Thanks 🥳. Also @najmieh Thank you for spending some time on this hobby project of mine. |
Currently the regex can't add comma to JavaScript objects which has more than 2 variables
For example :
This will turn into :
Now the python regex that i am using is :
The text was updated successfully, but these errors were encountered: