te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Is there an option in TypeScript so that a compiled JavaScript file is JSHint compatible? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there an option in TypeScript so that a compiled JavaScript file is JSHint compatible? - Stack Overflow

programmeradmin3浏览0评论

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
 |  Show 5 more ments

5 Answers 5

Reset to default 9 +25

It'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.

发布评论

评论列表(0)

  1. 暂无评论