Is it possible and reasonable to convince JSHint to accept a single line `do` loop? - jshint

This is clear (IMO) and valid javascript:
do var playerId = newPlayerId();
while(playerId in players);
JSHint complains that it expected a { before playerId. Is there any way to disable this warning, without disabling all curly-brace related warnings? My style is cool, right? No ambiguities or obscurities?

For some reason, this does not appear to be possible. It is possible with other similar statements (such as if and while) by setting the curly option to false:
/*jshint curly: false */
var x = 10;
if (x < 20)
x = 30;
I see no reason why this rule should not apply to the do statement, so I've opened a pull request to add this functionality to JSHint.

Related

I need help for gamemaker 2.3

Pls help me
A few weeks ago it came out of gamemaker 2.3, practically in the gamemaker language they changed the scripts into functions, but now after converting the files to be able to reopen them, I double-checked all the scripts and etc but anyway when I start it it remains a black screen, however it doesn't give me any compilation errors or whatever, what could be the problem?
Ps.
I might sound stupid, but if someone has the same program as me I can pass the project to them so they can see the scripts for themselves, so basically it's just the base and there is only the script to make the player walk and for collisions, I know that no one would want to waste time, but I ask the same
Its possible that your code is stuck in an infinite loop, here's an example of what that might look like:
var doloop = true
while(doloop == true){
x += 1
y += 1
}
the "doloop" variable is never changed within the while loop, so it is always equal to true and the loop never ends. Because the code never finishes looping, it can never get around to drawing anything, so you end up with a black screen. The easiest way to check for these is to put a breakpoint/debugging point at the beginning and just after every while/for/do/ect loop and debug it. e.g. (I am using asterisks "*" to represent breakpoints)
var doloop = true
* while(doloop == true){
x += 1
y += 1
}
*
When you get to one of the loops remove the first breakpoint and hit the "continue" button in the debugger. If it (it being the computer) takes an longer than it should to hit the second breakpoint (as in, you wait for a ten seconds to or two minutes (depends on how complex the code is) and it still hasn't hit the second breakpoint), then you should replace the breakpoint at the beginning of the loop to check and make sure it is still in there. If it is still in the loop, then that is likely where the code is getting stuck. Review the loop and everywhere any associated variables are set/changed, and you should be able to find the problem (even if it takes a while).
Majestic_Monkey_ and the commentors are correct: use the debugger. It's easy and it's your friend. Just place a red circle on the very first line of code that runs, and click the little bug icon and you can step through your code easily.
But to address your specific issue (or if anyone in the future has this issue): scripts have changed into files that can have many functions. Where you used to have
//script_name
var num = argument0 + argument1;
return num;
You would now have
function script_name(a, b) {
var num = a + b;
return num;
}
All you have to do is create a decleration for your new function:
function my_function_name(argument_names, etc...)
Then wrap all your old code in { }, and replace all those ugly "argument0" things with actual names. It's that easy. Plus you can have more than one function per script!

LESS addition operator (+) is appending instead of adding

I'm having a strange issue I haven't seen occur. I am trying to do some basic addition to some variables like this:
#screen-break-sm: 768px;
#screen-break-md: 992px;
#screen-max-mobile: #screen-break-sm;
#screen-min-desktop: #screen-break-sm + 1;
Then, those values are being used in some media queries. When it is compiled using gulp-less (version ~3.0.0 in package.json) via Gulp, the output ends up being something like:
#media (min-width:768px + 1) {
// CSS
}
I'm expecting:
#media (min-width:769px) {
// CSS
}
I have tried doing the addition as both #screen-break-sm + 1 and also screen-break-sm + 1px. I've also tried removing the px part of the original values and doing the add and appending the px afterwards, but that doesn't add either.
In case it is relevant, this is one of the gulp scripts that builds a section where I first ran into this issue:
module.exports = function (build) {
return function () {
var pagesPath = build.options.styles.buildPath + '/pages/';
return build.gulp.src('./resources/assets/less/pages/**/*')
.pipe(build.plugins.less({
paths: [ build.plugins.path.join(__dirname, 'less', 'vendor', 'theme', 'bootstrap') ]
})).on('error', build.errorHandler)
.pipe(build.plugins.minifyCss()).on('error', build.errorHandler)
.pipe(build.plugins.extReplace('.min.css')).on('error', build.errorHandler)
.pipe(build.gulp.dest(pagesPath));
};
};
Any ideas why LESS is concatenating/appending instead of performing addition?
[EDIT]
While the solution is the same as the other question that was identified as a possible duplicate, that question does not discuss the problem that users will encounter directly, and therefore I think this question is much better suited for searching purposes. I never found that solution after an hour of Googling and only after getting the answer and the "strict math" verbiage did that other question show up.
Look at strict math option which default value is OFF. Are you sure that for some reason you don't have it set to ON?
lessc -sm=on
lessc --strict-math=on

Grunt jshint disable Expected a conditional expression and instead saw an assignment

I am using grunt-contrib-jshint and it finds the following error in my JS file:
line 5 col 70 Expected a conditional expression and instead saw an assignment.
I know the reason of this error, but all I want is to disable it. Looking here it looks like I can either use no-cond-assign to 0 in my jshintrc or by adding -W084 : true in my options.
The problem is that the first solution ended up in a corrupted jshintrc and the second one does not solve the problem. Another option is to add -W022 : true (which solution I found in the comments) also does not work.
Use the ignore pragma to skip this line:
/* jshint ignore:start */
if(this = Infinity)
/* jshint ignore:end */
{
return;
}

Problems with JSLint options in Aptana Studio 3

I have configured Aptana Studio 3 to use JSLint for validation. At least some of the JSLint options don't seem to work correctly, however. For example, the code below produces errors on the line f = function() { -- because of the missing space after function -- and on the line j = i++; -- because of the use of ++. Both of these errors should have been suppressed by the options. If I run this same code through the jslint.org site, I get no errors. If I reverse the options (put false in place of true) the errors go away in Aptana but now jslint.org shows errors. So it appears the options are backwards. In addition, in Aptana the absence of use strict is not flagged as an error, regardless of the "sloppy" option.
/*jslint white:true, plusplus:true*/
var f;
f = function() {
var i, j;
j = i++;
};
I've created a bug report with Aptana on this issue.
http://jira.appcelerator.org/browse/APSTUD-4129

Use of console.time

I'm creating an application with Google Closure Library and its Compiler. To debug values I use console.log(). Compiling this will throw the following exception JSC_UNDEFINED_VARIABLE. variable console is undeclared at .... To solve this error, I just had to use window.console.log() instead.
I also want to measure the time that a function takes. Firebug has two nice functions console.time(name) and console.timeEnd(name) to do that very easily. Unfortunately the Closure Compiler does not support these functions throwing the following warning JSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at .... Unfortunately you cannot solve this warning with prepending window.
I also had a look at the library, but goog.debug.Console has not the function that I need.
Another solution I have used before was something like the following
var start = new Date();
// do something
var end = new Date();
// do some calculation to get the ms for both start and end
var startMS = ....;
var endMS = .....;
// get the difference and print it
var difference = (endMS - startMS) / 1000;
console.log('Time taken for something: ' + difference);
This is a little bit too much code, if you use it very often, and the version with the two functions would be great:
window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");
This prints out the MS between start and end. But as mentioned above, this doesn't work with the closure compiler. Does anyone have a solution for this, how I can use these two functions window.console.time() and window.console.timeEnd()? Or maybe another solution that goog.closure provides, but I haven't found?
You just need to added them to the externs you are using.
If you don't want to/can't use externs, you can easily reference "undeclared" objects with the string-based properties:
window['console']['log']('Hello Console!');
window['console']['time']('timeTaken');
...
But you have to be careful, because the second line might throw an error if the time property does not exist or it's not a function.

Resources