Consider the following TypeScript code sample.
var checkEqual = function (a:any, b:any) {
if(a == null)
{
return false;
}
}
When I will pile it, it will generate the corresponding JavaScript file without any pile time error as below:
var checkEqual = function (a, b) {
if (a == null) {
return false;
}
};
But when I run JSHint on the piled JavaScript code/file I will have following error/warning:
Use === to pare with null
I want the piled JavaScript code from TypeScript to be JSHint patible (no errors and warnings should be there). That means either it should generate the correct code or it should give pile time error.
PS: I did not know about tslint before, but even after using it, it is not piling JavaScript patible with JSHint (JSHint is still throwing warnings).
Consider the following TypeScript code sample.
var checkEqual = function (a:any, b:any) {
if(a == null)
{
return false;
}
}
When I will pile it, it will generate the corresponding JavaScript file without any pile time error as below:
var checkEqual = function (a, b) {
if (a == null) {
return false;
}
};
But when I run JSHint on the piled JavaScript code/file I will have following error/warning:
Use === to pare with null
I want the piled JavaScript code from TypeScript to be JSHint patible (no errors and warnings should be there). That means either it should generate the correct code or it should give pile time error.
PS: I did not know about tslint before, but even after using it, it is not piling JavaScript patible with JSHint (JSHint is still throwing warnings).
Share Improve this question edited Dec 27, 2016 at 10:35 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Aug 22, 2016 at 10:48 sumisumi 4673 silver badges15 bronze badges 10-
3
use === in typescript ... because
== null
is perfectly acceptable code in circumstances where you don't want=== null
– Jaromanda X Commented Aug 22, 2016 at 10:51 - Code snippet was just an example, the main concern of mine is that typescript should consider JSHint errors while generating piled javascript files – sumi Commented Aug 22, 2016 at 11:19
- 3 why not use tslint instead? – qballer Commented Aug 22, 2016 at 13:26
- 9 A linter is supposed to be used to help programmers follow good coding standards and guidelines. "Validating" generated code with a linter doesn't make any sense. – JJJ Commented Aug 23, 2016 at 13:33
- 2 You've typed valid TypeScript/JavaScript code that is not "patible" with jshint, and you expect TypeScript to miraculously know that what you typed (which again, is valid code) is not what you meant, but rather is pletely different, valid code? Change your expectations. – Heretic Monkey Commented Sep 1, 2016 at 14:58
5 Answers
Reset to default 9 +25It's simply not possible! TypeScript was never built with the goal to create code that is pliant with a linter. When using TypeScript you have to think of the generated JavaScript as bytecode. There is no reason to look at your bytecode.
Some linters may even bother you with problematic use of this
. Which in fact should be used very carefully in ES5. But when using TypeScript/ES2015 class definitions you need to use this
a lot and transpiling to ES5 will create a lot of them. So creating code that matches your linter will force you to not use a lot of features of TypeScript.
So if you want to lint bytecode, then you should write bytecode ;)
This isn't the fault of the piler.
a == null
differs from a === null
in both TypeScript and JavaScript.
a == null
will return true if a is null or undefined, but a === null
will only return true if a is null.
Rather than linting the generated files, you could lint the TypeScript files themselves, using tools such as tslint. There is even a gulp wrapper for this to integrate into your build chain gulp-tslint.
TLDR takeaway:
If you're looking for a very high standard for yourself or team, JSLint. But it's not necessarily THE standard, just a standard, some of which es to us dogmatically from a JavaScript god named Doug Crockford. If you want to be a bit more flexible, or have some old pros on your team that don't buy into JSLint's opinions, or are going back and forth between JavaScript and other C-family languages on a regular basis, try JSHint.
Long version:
The reasoning behind the fork explains pretty well why JSHint exists:
http://badassjs./post/3364925033/jshint-an-munity-driven-fork-of-jslint http://anton.kovalyov/2011/02/20/why-i-forked-jslint-to-jshint/
So I guess the idea is that it's "munity-driven" rather than Crockford-driven. In practicality, JSHint is generally a bit more lenient (or at least configurable or agnostic) on a few stylistic and minor syntactical "opinions" that JSLint is a stickler on.
As an example, if you think both the A and B below are fine, or if you want to write code with one or more of the aspects of A that aren't available in B, JSHint is for you. If you think B is the only correct option... JSLint. I'm sure there are other differences, but this highlights a few.
A) Passes JSHint out of the box - fails JSLint
(function() {
"use strict";
var x=0, y=2;
function add(val1, val2){
return val1 + val2;
}
var z;
for (var i=0; i<2; i++){
z = add(y, x+i);
}
})();
B) Passes Both JSHint and JSLint
(function () {
"use strict";
var x = 0, y = 2, i, z;
function add(val1, val2) {
return val1 + val2;
}
for (i = 0; i < 2; i += 1) {
z = add(y, x + i);
}
}());
Personally I find JSLint code very nice to look at, and the only hard features of it that I disagree with are its hatred of more than one var declaration in a function and of for-loop var i = 0 declarations, and some of the whitespace enforcements for function declarations.
A few of the whitespace things that JSLint enforces, I find to be not necessarily bad, but out of sync with some pretty standard whitespace conventions for other languages in the family (C, Java, Python, etc...), which are often followed as conventions in JavaScript as well. Since I'm writing in various of these languages throughout the day, and working with team members who don't like lint-style whitespace in our code, I find JSHint to be a good balance. It catches stuff that's a legitimate bug or really bad form, but doesn't bark at me like JSLint does (sometimes, in ways I can't disable) for the stylistic opinions or syntactic nitpicks that I don't care for.
A lot of good libraries aren't lint'able, which to me demonstrates that there's some truth to the idea that some of JSLint is simply just about pushing one version of "good code" (which is, indeed, good code). But then again, the same libraries (or other good ones) probably aren't Hint'able either, so, touché.
To put in frank and simple:
What you are asking is impossible. And let me be absolutely clear: IMPOSSIBLE. Unless you want to re-engineer the whole system.