Could Typescript generates code that makes JSHint happy?

Topics: Code Checkins, General
Jul 9, 2013 at 10:57 AM
Edited Jul 9, 2013 at 10:57 AM
Hello everybody,

I've written the following typescript-Code
/*jslint node: true */
///<reference path='../../typescript/node/node.d.ts'/>

'use strict';

import fs = module('fs');

export function fetch(repository) {
    if (!fs.existsSync(repository.path)) {
      return false;
    }
    return true;
};

export function pull(repository) {
    if (!fs.existsSync(repository.path)) {
      return false;
    }
    return true;
};
The typescript compiler generates now the following javascript code:
'use strict';

var fs = require('fs');

function fetch(repository) {
    if (!fs.existsSync(repository.path)) {
        return false;
    }
    return true;
}
exports.fetch = fetch;
;

function pull(repository) {
    if (!fs.existsSync(repository.path)) {
        return false;
    }
    return true;
}
exports.pull = pull;
;
There are some problems that are not valid for JSLint.
  • The first is the removed comment in the first line.
  • The next problem are the indentation in line 7 and 16 (return false)
  • The last problems are the unnecessary semicolon in line 12 and 21.
This generates unnecessary JSHint errors. Is it possible to configure, what the Typescript compiler should make and what not? For instance my comments or the code indentation?
Jul 9, 2013 at 11:01 AM
It seems to me that once you use a tool to generate JS for you, you no longer care about any "code conventions" being enforced.

The unnecessary semicolons are there because you added them yourself. export function Foo() { } is fine, no semicolon needed.
Jul 9, 2013 at 11:27 AM
Thanks for your hint with the semicolon. It has solved the third problem.

But there are still the first and the second problem. Even if Typescript generates the code for me, my grunt-jshint task would be happy, when the code could be JSHint compliant.
Jul 9, 2013 at 12:12 PM
Have you tried the --comments command line option to preserve comments?

As for whitespace, the compiler front-end was rewritten for 0.9. This now tokenizes whitespace similar to the Roslin C# compiler. However, it still uses the old backend which erases whitespace.

Perhaps in a future (post-1.0) release, whitespace will be transpiled out to JavaScript.


Richard


Jul 9, 2013 at 12:57 PM
Thanks, this command line option has solved the problem. I've used this option also from the grunt-typescript task and it works!