Allow type declaration with JSHint - javascript

I try to validate some server side javascript with JSHint.
Everything looks great so far, but i got problems with a syntax like this
var foo:type = bar;
The message is
Missing semicolon.
Expected '(end)' and instead saw ':'.
Is there any way to allow it?

JSHint says it is not valid JavaScript, which is true. You can suppress different warnings about coding style, but it does not (and really, should not) allow blatantly wrong code.
Anyway, you should try tslint. It was designed for TypeScript.

Related

When creating a linter, what is the expected output format?

Linting is an invaluable technique when crafting code. Yet I find myself wanting to understand the process of linting more. To do this, I am building a basic static code analysis tool with node.
What the linter should do is perform a regex check and if the regex matches, throw an error (or warning depending on the user's config).
I understand that linters traditionally parse code and some even execute checks on an AST, but I want to avoid this entirely. I also understand that my method is bypassing nearly every important part of linting by avoiding the parsed syntax altogether.
The goal is to be able to write a few dead-simple checks and have this as an accessory linter for rapid prototyping. (example: put ^$\n^$ into my linter config and an error would be thrown for two continuous blank lines)
The part of the process that seems undocumented is what type of output is expected to the command line. Here is example output from xo:
/Users/dawsonbotsford/code/regexLinter/cli.js
42:9 error Expected indentation of 6 space characters but found 8 indent
43:9 error Expected indentation of 6 space characters but found 8 indent
43:32 error Missing semicolon semi
And eslint example output:
/Users/dawsonbotsford/code/regexLinter/cli.js
3:1 error Parsing error: The keyword 'const' is reserved
How would I mimic this output with the correct types of shell errors/warnings such that it would be pluggable into sublime-contrib plugins, CI servers, etc.?
If you want it to be pluggable the simplest you can do is write a plugin for ESLint if you need to capture formatting issues. If you want to transform code use šŸŠPutout, the tool Iā€™m working on, it also has a plugin for ESLint so the messages you are going to show, will be accessible in terminal, IDE, CI etc.

JSCOMPILER_PRESERVE after compiling javascript with KJSCompiler

After compiling my JavaScript source with KJSCompiler (https://github.com/knyga/kjscompiler) I get this weird function wrapped around my code. If I try to include it in my live source code I get this error in the console "Uncaught ReferenceError: JSCOMPILER_PRESERVE is not defined ".
I thought that this problem occurred because I didn't add the "wrapper" in the JSON file of kjscompiler. After trying that it still occurs.
Does anyone have an idea how to fix this?
I came across this when using Closure Compiler directly via the Java API.
It seems to be caused by running the compiler in checks-only mode but with protect hidden side effects enabled. The late pass to remove protection of hidden side effects is skipped in checks-only mode.
I'm not sure about KJSCompiler specifically, but it might help to look at changing the compilation level to SIMPLE or ADVANCED, and/or how you might be able to control what checks and optimisations run to KJSCompiler, e.g. turning off CompilerOptions.protectHiddenSideEffects.
See https://github.com/google/closure-compiler/issues/1875
In addition to Steve S's answer:
set protectHiddenSideEffects after setting optimization level, as options.setProtectHiddenSideEffects(false); didn't work for me for GCC version v20180204 if I set it before setting CompilationLevel.
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(compilerOptions);
//To remove JSCOMPILER_PRESERVE error: https://github.com/google/closure-compiler/issues/1875
compilerOptions.setProtectHiddenSideEffects(false);

Why does Eclipse complain about missing semicolon in my javascript?

I'm having an issue with Eclipse where it's complaining about missing semicolons in javascript code in a jsp file. Given the two lines below, Eclipse complains about the first line and indicates that there's a missing semicolon right before the closing curly brace. It has no complaints about the second line. I'd prefer to use the first way, but I'm annoyed with the warnings. Is there a different syntax that I should be using? I'm also using JQuery, so I don't know if this is contributing to the parsing error in Eclipse.
var isFoo = ${actionBean.isFoo}; // javascript type is boolean
var isFoo = '${actionBean.isFoo}'; // javascript type is string
To answer some of the questions people have posed in the comments...
This code is from a JSP file and actionBean refers to the Java action bean for this page.
"isFoo" is a member variable of the action bean. The syntax ${actionBean.isFoo} is JSP Expression Language (or EL for short) and it's used to evaluate a java variable in a JSP. In this case, my code takes the value from the java variable and assigns it to the javascript variable. The code works just fine, but Eclipse complains.
var isFoo = ${actionBean.isFoo};
This is not valid JavaScript, which is why you're getting a syntax error. I'm assuming the ${...} is supposed to do some interpolation in NetBeans, but Eclipse has no way of knowing that, and just tries to parse it as JavaScript. Unless there's an Eclipse plugin or setting for dealing with mixed code like this, you may just have to deal with seeing syntax errors.
Eclipse is complaining because it is not really Javascript, and it is trying to parse it like Javascript. If it is really annoying you could disable the warning in the preferences, under your own risk to not be notified in other circumstances:
var isFoo = ${actionBean.isFoo};
is not javascript, it is JSF

Automatically Fix "missing semicolon before statement"

I have a large javascript file that is missing a lot of semicolons. It works fine as it is, but when I try to use a minifier = kaboom! Is there a way to automatically fix this? I tried to go manually through it but it's not humanly possible.
check out the JavaScript Lint application that checks your script for common errors (semicolon, curly brackets, trailing decimals, nested comments, etc.)

JavaScript: Ci is not defined

I just spent half an one our to find out what caused the Error-Message "Ci is not defined" in my JavaScript code. I finally found the reason:
It should be (jQuery):
$("asd").bla();
It was:
("asd").bla();
(Dollar sign gone missing)
Now after having fixed the problem I'd like to understand the message itself: What does Firefox mean when it tells me that "Ci" is not defined. What's "Ci"?
Update:
I'm using the current version of Firefox (3.0.3).
To reproduce, just use this HTML code:
<html><head><title>test</title>
<script>
("asd").bla();
</script>
</head><body></body></html>
To make it clear: I know what caused the error message. I'd just like to know what Firefox tries to tell me with "Ci"...
I don't know which version of FF you are using, but regardless, the message is probably referring to the fact that bla() is not a function available on the String object. Since you were missing the $, which means you were missing a function, ("asd") would evaluate to a string, and then the JavaScript interpreter would try to call bla() on that object. So, if you had the following code in your project:
String.prototype.bla = function() {};
// now this next line will execute without any problems:
("asd").bla();
So, it is possible that Ci is some internal Firefox symbol that simply refers to the idea of a function. That is my guess, I imagine you are going to need someone that knows something about Firefox's internals to get a better answer to this question...
UPDATE: I am running your example code in the exact same version of FF as you are, but it reports the error as:
Error: "asd".bla is not a function
Source File: file:///C:/test.html
Line: 3
Perhaps you have an extension/plug-in running that does something with this? Maybe some Greasemonkey script or something?
Jason seems to be right. Many plugins (e.g. Firebug, Geode) use Ci as a shortcut:
const Ci = Components.interfaces;
So the plugins seem to cause that strange error message.
Assuming it's CodeIngiter, it can't find the js file.

Categories

Resources