[英]How to create my custom tslint rule set?
I want to introduce a typescript coding guideline that can be shared accross multiple projects. 我想介绍一个可以在多个项目中共享的打字稿编码指南。 I don't want to copy paste a
tslint.json
over multiple times, it has happened currently and we have diverged version of it going around. 我不想复制粘贴
tslint.json
多次,它已经发生了,我们有它的分歧版本。
I want to base my guideline on tslint:recommended
. 我希望将我的指南建立在
tslint:recommended
。 And I see that the tslint syntax allows for extend
, yet I am unclear about its usage and how to structure such a package. 我发现tslint语法允许
extend
,但我不清楚它的用法以及如何构造这样的包。
Can such a project consists of merely a tslint.json
itself or do I need to export a module? 这样的项目可以只包含一个
tslint.json
本身还是我需要导出一个模块?
I want the package to then be hosted on npm/sinopia instance, yet am unsure how to proceed. 我希望包然后在npm / sinopia实例上托管,但我不确定如何继续。
Yes. 是。 You can create an NPM module that contains your rule set and can reference that in a
tslint.json
file's extends
setting. 您可以创建包含规则集的NPM模块,并可以在
tslint.json
文件的extends
设置中引用该模块。
For example, you can create an NPM module named my-rule-set
with this package.json
: 例如,您可以使用此
package.json
创建名为my-rule-set
的NPM模块:
{
"name": "my-rule-set",
"version": "0.0.0",
"main": "my-rule-set.json"
}
and this my-rule-set.json
(note that the main
in the package.json
references the my-rule-set.json
file): 这个
my-rule-set.json
(请注意, package.json
中的main
引用了my-rule-set.json
文件):
{
"extends": "tslint:recommended",
"rules":
{
...
}
}
Then, with my-rule-set
installed in node_modules
, you can extend a tslint.json
file with your own rule set: 然后,在
node_modules
安装my-rule-set
,您可以使用自己的规则集扩展tslint.json
文件:
{
"extends": "my-rule-set"
}
There is more information on sharable configurations in this TSLint blog post . 此TSLint博客文章中提供了有关可共享配置的更多信息。
If your rule set contains only configurations of existing rules, that's all you need to do. 如果您的规则集仅包含现有规则的配置,那么您只需要这样做。 However, if you intend to implement some custom rules, in
my-rule-set.json
you'll need to link to the directory where your custom rules are. 但是,如果您打算实现一些自定义规则,则在
my-rule-set.json
您需要链接到自定义规则所在的目录。 So for example, it should also have something like: "rulesDirectory": "./rules"
. 因此,例如,它也应该具有类似:
"rulesDirectory": "./rules"
。 Then the ./rules
directory should contain the compiled .js
versions of your rules. 然后
./rules
目录应包含规则的已编译.js
版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.