在 Visual Studio 2015 年设置 TSLint

Setup TSLint in Visual Studio 2015

我在 Visual Studio 2013 年使用 Typescript 进行开发,并且始终在底部打开我的错误列表。当我编写代码时,TSLint 会告诉我代码是 messy/incorrect。我认为除了安装 Web Essentials 插件外,我不需要做任何其他事情。

我最近安装了 Visual Studio 2015,但我无法让 TSLint 像在 Visual Studio 2013 中那样工作。我是否遗漏了一些明显的东西?

这里好像有nuget包可以帮到你。如果您右键单击该项目并 select "Manage NuGet Packages" 并搜索 tslint,您将找到一个名为 "NCapsulateExtensions.TsLint" 的包,它应该适合您。

不过,我个人无法验证这一点,因为该软件包需要 System.Web.Helpers.dll 而我的机器上没有(我也无法在任何地方找到它)。因此,我查看了 git 存储库,发现 nuget 包实际上并未使用此 dll,并提交了一个拉取请求以将其删除。与此同时,我的叉子可以在这里找到:

https://github.com/mbraude/NCapsulateExtensions.TsLint

希望您或其他人知道 System.Web.Helpers 在哪里,以便您可以安装并尝试一下,或者作者接受拉取请求并发布新版本。

如果这最终没有奏效,您需要在您的项目中执行类似的操作 - 从自定义 msbuild 任务调用 tslint。您也可以克隆此解决方案并在没有 nuget 的情况下手动设置它 - 这只会增加很多工作量。

您现在必须使用 gulp 任务来完成此操作。如果你问我这有点痛苦,但它确实有效!我们是这样做的:

  1. 安装节点:https://nodejs.org
  2. 安装Gulp:http://gulpjs.com
  3. 安装 gulp 包到项目根目录:
    • 在命令行中cd C:\path\to\project
    • npm install gulp-tslint --save-dev
    • npm install gulp-plumber --save-dev

可选:

  • npm install gulp-newer --save-dev
  • npm install gulp-watch --save-dev

现在一切都安装好了!打开 Visual Studio 并添加一个新的 gulpfile.js 到您的项目根目录,内容如下:

/// <binding AfterBuild='TSLint:All' ProjectOpened='TSLint:Watch' />
//Node packages
var gulp    = require("gulp");
var plumber = require("gulp-plumber");
var tslint  = require("gulp-tslint");

//Only include these 2 packages if you've installed them
var newer   = require("gulp-newer");
var watch   = require("gulp-watch");


//Paths to include/exclude
var TYPE_SCRIPT_FILES = ["app/**/*.ts", "!app/core/Events.ts"];

//Our TSLint settings
var TYPE_SCRIPT_REPORT = tslint.report("prose", {
    emitError: false,
    reportLimit: 50
});

//The actual task to run
gulp.task("TSLint:All", function () {
    return gulp.src(TYPE_SCRIPT_FILES)
        .pipe(plumber())
        .pipe(tslint())
        .pipe(TYPE_SCRIPT_REPORT);
});


//===== ONly include the below code if needed
// File locations
var BIN = "bin";

// Listens for new (updated) typescript files and runs through TSlint
gulp.task("TSLint:Newer", [], function (done) {
    return gulp.src(TYPE_SCRIPT_FILES)
        .pipe(plumber())
        .pipe(newer(BIN))
        .pipe(tslint())
        .pipe(TYPE_SCRIPT_REPORT)
        .pipe(gulp.dest(BIN));
});

//This task runs when the project opens. When any file changes, it will be run through TSLint
gulp.task('TSLint:Watch', function () {
    return gulp.src(TYPE_SCRIPT_FILES)
        .pipe(watch(TYPE_SCRIPT_FILES))
        .pipe(plumber())
        .pipe(tslint())
        .pipe(TYPE_SCRIPT_REPORT)
        .pipe(gulp.dest(BIN));
});

好了,现在万事俱备了!现在在 Visual Studio 中打开 Task Runner Explorer。任务应该出现在这里。

并不是说您可以告诉每个任务何时 运行。您可以通过右键单击每个任务来设置它,它只会添加到 gulpfile.js

的第一行


  • TSLint:All 任务将在所有指定文件上 运行 TSLint。
  • TSLint:Newer 任务将 运行 TSLint 对自上次检查以来已更改的所有文件进行 TSLint。
  • TSLint:Watch 任务将保留 运行ning 并在文件保存时自动检查文件!

现在看来最简单的方法是从 Visual Studio 库下载 Mads Kristensen 的 Web Analyzer,它包括 TSLint 支持

https://visualstudiogallery.msdn.microsoft.com/6edc26d4-47d8-4987-82ee-7c820d79be1d

(免费)