-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Consider those who doesn't use semicolons #171
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
Conversation
Thank you @icamys , can you please add a test that reproduces the problem when js-cookie is included after another JavaScript file that doesn't use semicolons? |
Here's an example: In my project I use gulp and asset builder. As you probably know, when we execute gulp command, firstly all 'main' files from bower_components directory are concatenated, after that all client code is appended to it. I found this problem, when during the concatination js-cookie main file was appended after JavaScript Canvas to Blob main file. |
@icamys Sorry, but I meant a test here. We need a test that runs when the build runs on CI to prevent regressions. Even though it is just a semicolon, we need a testable example for posterity. The Contributing Guidelines shows how to contribute to js-cookie, and there it is said that we need a test when proposing a feature through a Pull Request. The best way to do it would be to simulate the concatenation or inclusion of the js-cookie file after another one that doesn't end with a semicolon and ends with an expression. Something like this would be enough: var expression = function() {}
(function (factory) { ... Can you do that and amend on this PR? Otherwise you can create an issue about this instead, a PR is meant to be just the implementation of the feature. |
@FagnerMartinsBrack Today I've added a test, but I'm getting a TypeError from PhantomJS:
I'm virgin with PhantomJS, that's why I don't really know how to fix this error, but it's definitely a real situation, that reproduces the problem. The test is pretty simple. In HTML using ajax request I'm getting the contents of js-cookie.js file, then prepending to it an expression that reproduces the said error:
If you can give me the cue how to bypass this error, I would be grateful. By the way, here is the QUnit test itself:
|
@icamys There is no need to, you can use that test in this Pull Request, maybe just changing the message to explain what is going on. js-cookie is trying to execute the Object as a function because of the lack of semicolon, if you add the semicolon in the beginning of the file, it will pass. Can you Thank you! |
@FagnerMartinsBrack I've amended this PR.
Agree, though we can't be sure about the code, that is executed before the module from time to time. That's why there is a need for an extra semicolon. |
I don't understand what you mean, could you please elaborate? Thanks. |
xmlHttp.send( null ); | ||
window.scriptContent = '(function (){ return {}; })() '; | ||
window.scriptContent += xmlHttp.responseText; | ||
window.scriptContent += ';window.ok = true;'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there really a reason for changing window.ok = true
after it is loaded? Can't we just check if the test throws an Error
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah you can't, since it is inside an iframe xD, correct me if I am wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I guess something like window.jsCookieLoadedSuccessfully
will explain what this ok
means when reading the test. Not a big deal though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a good resource in how to get errors from iframe in case you are interested: http://stackoverflow.com/a/14041297
@icamys Thank you very much for the quality of this Pull Request :O. I have made some comments, looking forward on your feedback. |
@FagnerMartinsBrack I've updated PR again considering your comments. |
iframe.contentWindow.onerror = function () { | ||
loadedSuccessfully = false; | ||
}; | ||
assert.strictEqual(loadedSuccessfully, true, 'can\'t throw Object is not a function error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually don't escape single quotes, we use double quotes instead (yeah, pretty crazy, but the code was like this since forever xD)
@FagnerMartinsBrack I still hope that PR would be accepted =) |
Added a test that triggers an error if before module there is no extra semicolon
Ugh! It seems I made a mistake with the double quotes! see here, we are not using double quotes anymore it seems =(. Can you please revert that, to fix the code style error on CI? Sorry for that =/ |
Nevermind, I can fix this. Thank you for the tests! |
Avoiding "intermediate value" error when used with other modules.
See http://stackoverflow.com/questions/23370269/jquery-autosize-plugin-error-intermediate-value-is-not-a-function