k0pernikus
k0pernikus

Reputation: 66510

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.

I want to base my guideline on 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.

Can such a project consists of merely a tslint.json itself or do I need to export a module?

I want the package to then be hosted on npm/sinopia instance, yet am unsure how to proceed.

Upvotes: 3

Views: 1987

Answers (1)

cartant
cartant

Reputation: 58400

Yes. You can create an NPM module that contains your rule set and can reference that in a tslint.json file's extends setting.

For example, you can create an NPM module named my-rule-set with this package.json:

{
    "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):

{
    "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:

{
    "extends": "my-rule-set"
}

There is more information on sharable configurations in this TSLint blog post.

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. So for example, it should also have something like: "rulesDirectory": "./rules". Then the ./rules directory should contain the compiled .js versions of your rules.

Upvotes: 8

Related Questions