How to configure Visual Studio Code and tslint?
Asked Answered
S

2

29

In 0.3.0, I'm seeing intellisense for typescript. However, I was also expecting to see some tslinting as I have a tslint.json. Does VSC support linting natively or do I just need to lean on gulp?

If the latter, is it configurable to run as files are changed or does it need to be a manual task that is launched explicitly.

Scottscotti answered 3/6, 2015 at 23:23 Comment(3)
Based on the web page they say there is support for linting for TypeScript and C#Catalano
Sami, what am I missing? I see "Intelliense, Linting, outling for CSS, HTML, Javascript, JSON, Less and Sass" only - right below "refactoring, find all references for C#, Typescript". No linting for c#/ts and I'm not seeing it applying my tslint.json rules in the IDE, so I'm assuming it isn't wired up yet. But, wanted to see if I missed a toggle somewhere.Scottscotti
"No intent to fix" code.visualstudio.com/issues/Detail/16540 Suggest we lean on a task code.visualstudio.com/Docs/tasksScottscotti
M
5

You can add a linting task to your gulpfile like below. Or even a watcher task. Notice I just use TypeScript, not gulp plug in nor tslint, though they are fine too.

gulp.task('ts-watcher', function() {
    gulp.watch('./src/**/*.ts', ['ts-compile']);
});

gulp.task('ts-compile', function(done) {    
    runTSC('src/client', done);
});

function runTSC(directory, done) {
    var tscjs = path.join(process.cwd(), 'node_modules/typescript/bin/tsc.js');
    var childProcess = cp.spawn('node', [tscjs, '-p', directory], { cwd: process.cwd() });
    childProcess.stdout.on('data', function (data) {
        // Code will read the output
        console.log(data.toString());
    });
    childProcess.stderr.on('data', function (data) {
        // Code will read the output
        console.log(data.toString());
    });
    childProcess.on('close', function () {
        done();
    });
}
Mcnully answered 8/6, 2015 at 1:54 Comment(5)
Thanks John. I'm using a watcher now that uses gulp-notify. I was hoping for tighter integration - inline notification, etc. I imagine it is coming.Scottscotti
I agree. The new WebStorm 11 now has tight integration of TSX with in-line linting. I have gone back and forth between VSC and WS, but am currently sticking with Webstorm until VSC has inline notification.Makings
I actually dont use any of these. VS Code shows TypeScript issue in the editor without having any of these tasks. What additional things are you wanting? Im curious.Mcnully
Getting on this a bit late, but I think @digger69 was asking about tslint annotations in the editor. I personally like the idea of VS Code, but the lack of tslint annotations is preventing me from moving away from sublime.Saturated
@AndrewEisenberg There's some way now to get TsLint analyze in VSCode "Problems" view. Linking tslint report with a problem analyzer. Tested it here: michael.laffargue.fr/blog/2016/09/16/… and it works quite wellTetryl
C
34

Short answer

Does VSC support linting natively or do I just need to lean on gulp?

Yes. VS Code supports linting with the TSLint extension. There is no need for gulp.

Steps to integrate TSLint into VS Code

First, install prerequisites: TSLint and TypeScript.

npm install -g tslint typescript

Second, add a tslint.json file to your project root. You can do this with tslint --init, which gives you nice defaults. Alternatively, create the file and use this minimal config, which inherits recommended rules.

// tslint.json 
{
  "extends": "tslint:recommended",
  "rules": {}
}

Third, install the TSLint extension for VS Code.

  1. Open VS Code in your project's root.
  2. Open the command palette CTRL + P
  3. ext install tslint.
  4. Choose Install, then choose Enable, finally restart VS Code.

Fourth, enjoy your integrated TS Lint.

TSLint in action in VS Code.

Cassette answered 9/8, 2016 at 23:44 Comment(2)
I would also add that if using a custom tsconfig.json file try to run it in the console (i.e. tslint */.ts) to make sure that it is a valid file otherwise it won't run in VSCode.Balthazar
Note that this will only show opened file problems. To get the full list in the "Problems" view, you'll need some more steps I wrote there : michael.laffargue.fr/blog/2016/09/16/…Tetryl
M
5

You can add a linting task to your gulpfile like below. Or even a watcher task. Notice I just use TypeScript, not gulp plug in nor tslint, though they are fine too.

gulp.task('ts-watcher', function() {
    gulp.watch('./src/**/*.ts', ['ts-compile']);
});

gulp.task('ts-compile', function(done) {    
    runTSC('src/client', done);
});

function runTSC(directory, done) {
    var tscjs = path.join(process.cwd(), 'node_modules/typescript/bin/tsc.js');
    var childProcess = cp.spawn('node', [tscjs, '-p', directory], { cwd: process.cwd() });
    childProcess.stdout.on('data', function (data) {
        // Code will read the output
        console.log(data.toString());
    });
    childProcess.stderr.on('data', function (data) {
        // Code will read the output
        console.log(data.toString());
    });
    childProcess.on('close', function () {
        done();
    });
}
Mcnully answered 8/6, 2015 at 1:54 Comment(5)
Thanks John. I'm using a watcher now that uses gulp-notify. I was hoping for tighter integration - inline notification, etc. I imagine it is coming.Scottscotti
I agree. The new WebStorm 11 now has tight integration of TSX with in-line linting. I have gone back and forth between VSC and WS, but am currently sticking with Webstorm until VSC has inline notification.Makings
I actually dont use any of these. VS Code shows TypeScript issue in the editor without having any of these tasks. What additional things are you wanting? Im curious.Mcnully
Getting on this a bit late, but I think @digger69 was asking about tslint annotations in the editor. I personally like the idea of VS Code, but the lack of tslint annotations is preventing me from moving away from sublime.Saturated
@AndrewEisenberg There's some way now to get TsLint analyze in VSCode "Problems" view. Linking tslint report with a problem analyzer. Tested it here: michael.laffargue.fr/blog/2016/09/16/… and it works quite wellTetryl

© 2022 - 2024 — McMap. All rights reserved.