简体   繁体   English

WebEssentials tslint自定义规则

[英]WebEssentials tslint custom rules

I have a tslint.json file in my solution directory and I'm trying to create a custom rule following the guidelines on https://www.npmjs.com/package/tslint 我的解决方案目录中有一个tslint.json文件,我正在尝试按照https://www.npmjs.com/package/tslint上的指南创建自定义规则

I have created a "nonImportsRule.ts", have copied the code from the link and have added "no-imports": true to my tslint.json file however the rule is not being picked up. 我创建了一个“ nonImportsRule.ts”,从链接中复制了代码,并在“ tslint.json”文件中添加了“ no-imports”:true,但是该规则未得到使用。

The guide says that a rulesDirectory needs to be specified, but I have no idea where this should be configured? 该指南说需要指定rulesDirectory,但是我不知道应该在哪里配置它?

Also - is it possible to setup Web Essentials to break the build if tslint rules are violated? 另外-如果违反tslint规则,是否可以设置Web Essentials破坏构建?

I had a same kind of a problem. 我有同样的问题。 I wanted to use the TSLint extensions, tslint-microsoft-contrib and codelyzer, together with Web Analyzer. 我想将TSLint扩展,tslint-microsoft-contrib和codelyzer与Web Analyzer一起使用。 This did not work. 这没有用。 The first step to figure out why was to make an adaptation in server.js which can be found in C:\\Users\\[User]\\AppData\\Local\\Temp\\WebAnalyzer1.7.75. 找出原因的第一步是对server.js进行改编,该改编可以在C:\\ Users \\ [User] \\ AppData \\ Local \\ Temp \\ WebAnalyzer1.7.75中找到。 I changed the TSLint function into: 我将TSLint函数更改为:

tslint: function (configFile, files) {
    // Try catch tslint errors 
    try {

        var tslint = require("tslint");
        var options = {
            formatter: "json",
            configuration: JSON.parse(fs.readFileSync(configFile, "utf8").trim())
        };

        var results = [];

        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var ll = new tslint(file, fs.readFileSync(file, "utf8"), options);
            results = results.concat(JSON.parse(ll.lint().output));
        }

    } catch(error) {
        // Return tslint error to visual studio so we can get some ideas for counter measures.
        var result = JSON.parse('[{"endPosition": {"character": 0,"line": 0,"position": 0},"failure": "INSTALL ERROR","name": "/","ruleName": "INSTALL ERROR","startPosition": {"character": 0,"line": 0,"position": 0}}]');
        result[0].failure = error.message;
        return result;
    }

    return results;
},

The alternation resulted in error feedback in the visual studio error list when I run the Web Analyzer. 当我运行Web分析器时,这种交替导致Visual Studio错误列表中的错误反馈。 Do not forget to force a new instance of node.exe with the task manager after you have applied the alternation. 应用更改后,不要忘记在任务管理器中强制使用新的node.exe实例。 The feedback leaded, for my particular situation, to the following installation of npm packages in the following directories: 对于我的特殊情况,反馈导致在以下目录中安装了npm软件包:

Packages: 包装方式:

  • "codelyzer": "0.0.12" “ codelyzer”:“ 0.0.12”
  • "tslint": "^3.7.3" “ tslint”:“ ^ 3.7.3”
  • "tslint-microsoft-contrib": "^2.0.2" “ tslint-microsoft-contrib”:“ ^ 2.0.2”
  • "typescript": "^1.8.9" “ typescript”:“ ^ 1.8.9”

Directories: 目录:

  • C:\\Users\\[User]\\AppData\\Local\\Temp\\WebAnalyzer1.7.75 C:\\ Users \\ [User] \\ AppData \\ Local \\ Temp \\ WebAnalyzer1.7.75
  • C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE C:\\ Program Files(x86)\\ Microsoft Visual Studio 14.0 \\ Common7 \\ IDE

After this, Web Analyzer was able to use the same tslint rules as my grunt task. 此后,Web Analyzer能够使用与我的grunt任务相同的tslint规则。 Hopefully a newer version of Web Analyzer will solve my problems more elegantly. 希望较新版本的Web Analyzer可以更优雅地解决我的问题。

Okay, i'm not using Web Essentials extension but Web Analyzer : https://visualstudiogallery.msdn.microsoft.com/6edc26d4-47d8-4987-82ee-7c820d79be1d 好的,我不使用Web Essentials扩展程序,而是使用Web分析器: https : //visualstudiogallery.msdn.microsoft.com/6edc26d4-47d8-4987-82ee-7c820d79be1d

So i won't be able to answer on this question 100%, but i want to summarize here my experience with custom tslint rules. 因此,我将无法100%回答这个问题,但我想在此总结我对自定义tslint规则的经验。 First of all, what is not completely clear from documentation is that the whole thing depends on node.js. 首先,从文档中不能完全清楚的是,整个过程取决于node.js。

  1. So first of all you need to install node js. 因此,首先您需要安装node js。 This will give you npm command to your command line. 这会将npm命令带到命令行。
  2. After install with npm tslint and typescript. 用npm tslint和打字稿安装后。 https://github.com/palantir/tslint here are examples. https://github.com/palantir/tslint这是示例。 These will create files in : "c:\\Users[Username]\\AppData\\Roaming\\npm\\node_modules" 这些将在以下位置创建文件:“ c:\\ Users [用户名] \\ AppData \\ Roaming \\ npm \\ node_modules”
  3. Go into "c:\\Users[Username]\\AppData\\Roaming\\npm\\node_modules\\tslint\\lib\\rules\\". 进入“ c:\\ Users [用户名] \\ AppData \\ Roaming \\ npm \\ node_modules \\ tslint \\ lib \\ rules \\”。 Create here noImportRule.ts. 在此处创建noImportRule.ts。 Copy the following content: 复制以下内容:

     import * as ts from "typescript"; import * as Lint from "../lint"; export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING = "import statement forbidden EDE"; public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { return this.applyWithWalker(new NoImportsWalker(sourceFile, this.getOptions())); } } // The walker takes care of all the work. class NoImportsWalker extends Lint.RuleWalker { public visitImportDeclaration(node: ts.ImportDeclaration) { // create a failure at the current position this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)); // call the base version of this visitor to actually parse this node super.visitImportDeclaration(node); } } 

Note that in the example import lint is not given with relative path that won't work with this approach. 请注意,在示例中,没有为import lint提供相对路径,该路径不适用于此方法。 4. Fire the command : "tsc -m commonjs --noImplicitAny .\\noImportsRule.ts" . 4. 触发命令: “ tsc -m commonjs --noImplicitAny。\\ noImportsRule.ts” This will compile your custom rule's ts. 这将编译您的自定义规则的ts。 You will get bunch of compilation errors, such as: ../enableDisableRules.d.ts(1,21): error TS2307: Cannot find module 'typescript'. 您会遇到一堆编译错误,例如:../enableDisableRules.d.ts(1,21):错误TS2307:找不到模块'typescript'。 That's a good question why are these thrown, but forget about them, js file will be generated anyway. 这是一个很好的问题,为什么会抛出这些错误,但是忘记了,无论如何都会生成js文件。 5. Put "no-imports": true to your tslint.json(for now this should be custom one). 5.在您的tslint.json中输入“ no-imports”:true (现在这应该是自定义的)。 With this command from command line: tslint -c 'sample.tslint.json' test.ts you will get: test.ts[1, 1]: import statement forbidden. 通过从命令行tslint -c'sample.tslint.json'test.ts使用此命令,您将获得:test.ts [1,1]:禁止导入语句。 So you made the custom rule working!!! 因此,您使自定义规则生效了!!! :) :)

That's all for working from command line. 这就是从命令行工作的全部。 In addition I made custom rules working with WebAnalyzer, at least temporary. 另外,我制定了与WebAnalyzer一起使用的自定义规则,至少是临时的。 I needed to copy my custom rule's files here: c:\\Users[Username]\\AppData\\Local\\Temp\\WebAnalyzer1.6.65\\node_modules\\tslint\\lib\\rules\\ and of course configure WebAnalyzer tslint.json to include custom rules. 我需要在此处复制自定义规则的文件:c:\\ Users [用户名] \\ AppData \\ Local \\ Temp \\ WebAnalyzer1.6.65 \\ node_modules \\ tslint \\ lib \\ rules \\,当然还要配置WebAnalyzer tslint.json以包括自定义规则。 I have no idea how Web Essentials extension makes this whole thing working with tslint, but i guess some way similar :). 我不知道Web Essentials扩展如何使整个事情与tslint一起工作,但我想有些类似的方法:)。 Somewhere there should be a folder (node_modules\\tslint\\lib\\rules) with rules what tslint uses. 某处应该有一个文件夹(node_modules \\ tslint \\ lib \\ rules),其中包含tslint使用的规则。 There you need to copy your custom ones. 在那里,您需要复制自定义的。 Of course the most elegant solution would be to modify Web Essentials extension itself and make the tslint's custom rules directory configurable from visual studio. 当然,最优雅的解决方案是修改Web Essentials扩展本身,并使tslint的自定义规则目录可从Visual Studio配置。 (so my solution is just a workaround) (所以我的解决方案只是一种解决方法)

Here is my custom rule example in the visual studio warning's list: 这是Visual Studio警告列表中的自定义规则示例:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM