Is there a JHint / JSLint type linting and/or beautifier for TypeScript?

Topics: General
Mar 28, 2013 at 3:30 PM
I use JSHint and jsbeautifier for node.js (using a grunt.js build file) to clean up our javascript, and I was wondering if there's similar tools for TypeScript as well.

Ideally I want the buildfile to be able to lint/clean up for every user on every platform so I'd rather not solely depend on the editor tooling.
Mar 28, 2013 at 11:38 PM
I'm not aware of any such tool but I would certainly love to see one.

I experimented with applying JsHint to the generated code. Having to trace the issue back from the generated code is a bit of a pain so I only wanted the most egregious errors pointed out. Unfortunately a lot of warnings can't be turned off for JsHint and some of them are fundamental to the generated code so I gave up on that.
Mar 29, 2013 at 6:16 PM
I did that also and it was not a great success either, it generates lots of minor lint. I created this issue for it: http://typescript.codeplex.com/workitem/850

But what I'd really like is linting and beautifying for pure TypeScript. As it;s so similar I'm looking into the JSHint sources now but I'm not confident enough for taking this on myself at acceptable quality level.
Apr 1, 2013 at 5:58 PM
@Bartvds, what exactly is lacking in TypeScript that makes one want to use JSHint? (ignoring the bugs in what is in fact an alpha version at present)
Apr 1, 2013 at 8:00 PM
nabog wrote:
@Bartvds, what exactly is lacking in TypeScript that makes one want to use JSHint? (ignoring the bugs in what is in fact an alpha version at present)
Linters and compilers really have separate jobs. Compilers follow the letter of the law where a linter warns about things that are technically valid but likely signs of code quality issues. Assignments inside if statements and unused variables are two of the most common mistakes. ReSharper's linter for JavaScript warns about unreachable code which is awesome. If they add TypeScript support I'll be very happy.
Apr 1, 2013 at 8:03 PM
Edited Apr 1, 2013 at 8:05 PM
There's a few different things here. I'm not sure where they belong (in core or in wider community) but I'd ask here first:
  • I would like to be able to enforce a strict linting on the TypeScript files itself. Similar in the way we now use JSHint on Javascript, but I would like something like that for TypeScript itself (eg: it understands TS). Since TS is a superset of JS this would be the same type of lint and possibly more.
  • I would like to be able to auto-format TypeScript using a node.js based module, similar to jsbeautifier, that we can use to standardise the code formatting without conflicting settings between editors. Visual Studio and WebStorm and such have good formatters but we don't want to depend on editor settings in shared project where people might even code in notepad or whatever. This way it could be integrated in a automated build/commit tasks.
  • I don't use JSHint specifically on TypeScript code, it makes not a lot of sense, but we would like to have the TS generated JS modules (AMD and node) to live between the 'vanilla' JS files from other projects/members without tripping the JSHint. Since TS's code is machine generated it should be 'perfect' code that makes Crockford drool.
Apr 3, 2013 at 3:30 PM
JSHint/JSLint is a kind of hybrid tool that checks for both errors in the code and also attempts to enforce a redundant style on the developer.

It doesn't make much sense to want something similar for TypeScript.

There are a couple of tools that are rather useful for C#: FxCop and StyleCop. I think something along those lines would make more sense.

Noel
Apr 5, 2013 at 8:51 AM
Why doesn't this make sense for TypeScript?

TypeScript is the same code as JavaScript, so I say if it's useful for JavaScript then it's also useful for TypeScript, since you can make EXACT the same type of lint, and MORE. Unless you disregard everything we've learned over past years of JavaScripting.
Apr 5, 2013 at 4:22 PM
Edited Apr 5, 2013 at 4:33 PM
Because TypeScript is a statically typed language, unlike JavaScript. Furthermore, it supports ECMAScript 6 features that makes it possible to write object-oriented code. That provides a number of assurances with regard to removing errors from written code at compile time.

That seems pretty obvious, so perhaps you are getting at something else?

I agree with you that we need a linter (like FxCop) to warn about correct but potentially harmful or redundant code, for example as mentioned by @Grajkowski it should be useful to have warnings about unused variables.

We also need a tool that can enforce a certain style of coding, for example "All private methods must be placed after all public methods" or "The body of the if statement must be wrapped in opening and closing curly brackets." (That is what StyleCop does for C#.)

The code we write with TypeScript is generally more stable than any JavaScript. I don't see what further use JSLint (with its "Unexpected '++'" and other lovely nuggets) has in this environment.
Aug 8, 2013 at 12:45 PM
Any beautifiers out there yet? Would love to automate code formatting rules but don't have the time/skill to write my own beautifier.
Coordinator
Aug 8, 2013 at 3:54 PM
On a lark, I search for "tslint". I have only used this for a couple minutes, but might be something to explore:

https://github.com/palantir/tslint (with grunt support: https://npmjs.org/package/grunt-tslint)
Aug 8, 2013 at 6:57 PM
We also just released an Eclipse tslint plug-in so you can get errors as you type:
https://github.com/palantir/eclipse-tslint

In terms of formatting code, we made a separate Eclipse plug-in which supports that:
https://github.com/palantir/eclipse-typescript
Apr 1 at 11:42 PM
I would like to see an extension for visual studio that provides TypeScript linting, primarily for naming conventions and strict type comparisons. I know the type comparisons are already checked by the compiler but should I need to fall back to editing the generated JS in the future without TypeScript, it would be better if the proper strict comparison operators were already in place. I'm fairly diligent about it but I miss one once in a while or sometimes I will paste in someone else's code and it helps to clean things up quickly.
Apr 2 at 12:48 AM
I was re-reading this old thread and since I started it I'll report back after these months:

TSLint is definitely established by now, top stuff: https://github.com/palantir/tslint (has grunt/gulp plugins, and some fancy reporters on npm).

For formatting there is typescript-formatter: https://github.com/vvakame/typescript-formatter/ (still young, needs some grunt/gulp wrappers etc but is very active developer)

I know nothing about visual studio though (I use WebStorm)