UNPKG

17.4 kBMarkdownView Raw
1# Wotan
2
3Pluggable TypeScript and JavaScript linter
4
5[![npm version](https://img.shields.io/npm/v/@fimbul/wotan.svg)](https://www.npmjs.com/package/@fimbul/wotan)
6[![npm downloads](https://img.shields.io/npm/dm/@fimbul/wotan.svg)](https://www.npmjs.com/package/@fimbul/wotan)
7[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovateapp.com/)
8[![CircleCI](https://circleci.com/gh/fimbullinter/wotan/tree/master.svg?style=shield)](https://circleci.com/gh/fimbullinter/wotan/tree/master)
9[![Build status](https://ci.appveyor.com/api/projects/status/a28dpupxvjljibq3/branch/master?svg=true)](https://ci.appveyor.com/project/ajafff/wotan/branch/master)
10[![codecov](https://codecov.io/gh/fimbullinter/wotan/branch/master/graph/badge.svg)](https://codecov.io/gh/fimbullinter/wotan)
11[![Join the chat at https://gitter.im/fimbullinter/wotan](https://badges.gitter.im/fimbullinter/wotan.svg)](https://gitter.im/fimbullinter/wotan)
12
13Make sure to also read the [full documentation of all available modules](https://github.com/fimbullinter/wotan#readme).
14
15## Quick Start
16
17Install Wotan as a local dependency:
18
19```sh
20npm install --save-dev @fimbul/wotan
21# or
22yarn add -D @fimbul/wotan
23```
24
25Add a `.wotanrc.yaml` file to the root of your project with the following content:
26
27```yaml
28extends: wotan:recommended
29```
30
31That enables all recommended builtin rules. See the below for a list of [available rules](#available-rules) and [how the configuration works](#configuration).
32
33Now you can run the linter with one of the following commands depending on your use case:
34
35```sh
36wotan -p <path/to/tsconfig.json> # lint the whole project
37wotan 'src/**/*.ts' -e '**/*.d.ts' # lint all typescript files excluding declaration files
38wotan --fix # lint the whole project and fix all fixable errors
39wotan -p tsconfig.json -r # lint the specified project and all projects in its 'references'
40```
41
42## Editor Integration
43
44For instructions how to integrate the linter into your editor, see the [documentation of the `@fimbul/mithotyn` package](https://github.com/fimbullinter/wotan/blob/master/packages/mithotyn/README.md).
45For Visual Studio Code you can install the [official extension](https://marketplace.visualstudio.com/items?itemName=fimbullinter.vscode-plugin).
46
47## Available Rules
48
49For a list of available rules, see the [documentation of the `@fimbul/mimir` package](https://github.com/fimbullinter/wotan/blob/master/packages/mimir/README.md#rules).
50
51## Configuration
52
53Wotan is configured with a YAML, JSON5 or JSON file named `.wotanrc.yaml`, `.wotanrc.json5` or `.wotanrc.json`. By default the configuration file from the closest parent folder is used to lint each file.
54
55You can use different configurations for different directories. Consider the following setup:
56
57`.wotanrc.yaml` describes the rules that apply to every file and subdirectory (unless they contain a cofiguration file themselves):
58
59```yaml
60---
61extends: wotan:recommended # use all recommended rules
62```
63
64`test/.wotanrc.json` extends the base configuration and disables some rules that are not needed for tests:
65
66```json
67{
68 "extends": "../.wotanrc.yaml",
69 "rules": {
70 "no-useless-assertion": "off",
71 "await-only-promise": "warn"
72 }
73}
74```
75
76Note: this describes the default configuration file name and content. Plugin modules are able to override this behavior to read files with different name or content.
77
78### Overrides
79
80If you are more into having a single place for configuration, here's an alternative solution for the example above. The `.wotanrc.yaml` could look like this:
81
82```yaml
83---
84extends: wotan:recommended # use all recommended rules, could be an array to extend multiple configs
85rules: # could override some rules for all files here
86overrides:
87 - files: "test/**" # override the following rules for all files in the `test` folder
88 rules:
89 no-useless-assertion: off
90 await-only-promise: warn
91 - files: "*.spec.ts" # override the following rules for all *.spec.ts files in all directories
92 rules:
93 no-debugger: off
94```
95
96Overrides are processed in order and applied in order. The latter one overrides all prior overrides.
97
98Note that in the example above `*.spec.ts` matches in all directories. Normally patterns are matched relative to the configuration file they are specified in. Patterns without any slash are treated special. They will only be matched against the basename of every file in every directory.
99If you want to limit the pattern to the current directory, you can prefix it with `./` resulting in `./*.spec.ts`.
100
101`*` also matches the leading do if present, so you don't need a second glob pattern for dotfiles. That means `*.spec.ts` matches `.some.spec.ts` as well as `some.spec.ts`.
102
103### Configuring Rules
104
105Rules can have one of 4 different severities: `error`, `warning` (or `warn`), `suggestion` (or `hint`) and `off`.
106`error` is reported and causes the process to end with an exit code of 2. This is the default if not specified.
107`warning` is reported but doesn't cause a non-zero exit code.
108`suggestion` treated like `warning` but displayed differently.
109`off` turns the rule off, of course.
110
111Configurable rules get their options through an object. The content of the `"options"` property varies based on the rule.
112
113```js
114{
115 "rules": {
116 "some-rule": {
117 "severity": "error",
118 "options": {
119 "some-option": "some-option-value"
120 }
121 }
122 }
123}
124```
125
126`severity` and `options` are both optional. That allows you to extend a configuration and only change the severity of a rule without altering the options. Or you can change the rule's options without changing the severity inherited by the base config.
127
128### Display Configuration
129
130If the linter behaves somehow unexpected, it's probably because you configured it that way.
131You're lucky because there's a builtin command to diagnose this, so you don't need to know how the configuration file lookup and the handling of overrides, excludes and aliases works in detail.
132
133Just use `wotan show <filename>` to display the configuration file and the exact rule config used to lint this file. If there is no file found or the file is excluded, you will see that too.
134
135## Enable or disable rules with comments
136
137Sometimes you need to enable or disable a specific rule or all rules for a section of a file. This can be done using comments. It doesn't matter if you use `//` or `/* */`. Multiple rule names are separated by comma.
138It's not possible to enable a rule with a comment if that rule is not already enabled in the configuration for that file. That means comments can only enable rules that were previously disabled by a comment.
139
140* `// wotan-disable` disables all rules from the start of the comment until the end of the file (or until it is enabled again)
141* `// wotan-enable` enables all rules from the start of the comment until the end of the file. Enable comments have the same mechanics as disable comments.
142* `// wotan-disable-line` disables all rules in the current line (also works with enable)
143* `// wotan-disable-next-line` disables all rules in the next line (also works with enable)
144* `// wotan-disable-next-line bar, local/baz` disables the rules `bar` and `local/baz` in the next line
145* `// wotan-enable-line foo` enables the rule `foo` in the current line
146* `// wotan-enable-next-line bar, local/baz` enables the rules `bar` and `local/baz` in the next line
147
148This is the default behavior which can be overridden by plugin modules.
149
150To detect unused or redundant comments you can use the `--report-useless-directives` CLI option.
151
152## CLI Options
153
154* `-c --config <name>` specifies the configuration to use for all files instead of looking for configuration files in parent directories. This can either be a file name, the name of a node module containing a shareable config, or the name of a builtin config like `wotan:recommended`
155* `-e --exclude <glob>` excludes all files that match the given glob pattern from linting. This option can be used multiple times to specify multiple patterns. For example `-e '**/*.js' -e '**/*.d.ts'`. It is recommended to wrap the glob patterns in single quotes to prevent the shell from expanding them.
156* `--fix [true|false|number]` automatically fixes all fixable findings in your code and writes the result back to disk. Given a number it will at most use the specified number of iterations for fixing before returning the result. There are some precautions to prevent overlapping fixes from destroying you code. You should however commit your changes before using this feature. Files containing syntax errors are never fixed. If applying fixes would cause syntax errors, the fixes will not be applied.
157* `-f --formatter <name>` the name or path of a formatter. This can either be a file name, the name of a node module contianing a formatter, or the name of a builtin formatter. Currently available builtin formatters are `json` and `stylish` (default).
158* `-m --module <name>` specifies one or more packages with DI modules to load before starting the actual linter. These modules can be used to override the default behavior.
159* `-p --project <name>` specifies the path to the `tsconfig.json` file to use. This option is used to find all files contained in your project. It also enables rules that require type information. This option can be used multiple times to specify multiple projects to lint.
160* `-r --references [true|false]` enables project references. Starting from the project specified with `-p --project` or the `tsconfig.json` in the current directory it will recursively follow all `"references"` and lint those projects.
161* `--report-useless-directives [true|false|error|warning|suggestion]` reports `// wotan-disable` and `// wotan-enable` comments that are redundant (i.e. rules are already disabled) or unused (there are no findings for the specified rules). Useless directives are reported as lint findings with the specified severity (`true` is converted to `error`). Those findings cannot be disabled by a disable comment. The findings are fixable which allow autofixing when used with the `--fix` option.
162* `--cache` enables caching of lint results for projects. Can only be used with `-p --project` option. Read more about [caching](#caching).
163* `[...FILES]` specifies the files to lint. You can specify paths and glob patterns here.
164
165Note that all file paths are relative to the current working directory. Therefore `**/*.ts` doesn't match `../foo.ts`.
166
167### Examples
168
169The following examples are intended to be used as npm scripts. If you want to execute it directly on the command line, you need to use `./node_modules/.bin/wotan` instead of just `wotan`.
170
171```sh
172wotan # search the closest tsconfig.json and lint the whole project with type information
173wotan -c wotan:recommended # same as above, but uses the specified configuration for all files in the project
174wotan -c wotan:recommended --fix # same as above with automatic fixing
175wotan '**/*.ts' -e '**/*.d.ts' -e 'node_modules/**' # lint all typescript files excluding declaration files, also excludes node_modules just to be sure
176wotan -p . # lint the whole project configured by ./tsconfig.json, with type information, excludes node_modules by default
177wotan -p . 'src/**' # lint all files in src directory that are included in the project with type information
178wotan -p src -p test # lint all files in project 'src' and all files in project 'test'
179wotan -p . -r # lint the whole project configured by ./tsconfig.json all all of its project references
180wotan -m @fimbul/heimdall # enables TSLint rules and formatters to be used. for more information, see @fimbul/heimdall
181```
182
183### Adding CLI defaults to `.fimbullinter.yaml`
184
185If you find yourself using Wotan with the same CLI arguments over and over again, you can simply save them as defaults to a file called `.fimbullinter.yaml`. By default Wotan uses this file for CLI defaults if it's present in your current working directory.
186
187There's a subcommand to create and update this file, so you don't need to know any implementation details to guess the file structure.
188
189Let's assume you always use the following CLI arguments:
190
191```sh
192wotan -p tsconfig.build.json -c config/.wotanrc.yaml -e '**/*.d.ts'
193```
194
195To save these as defaults, simply use the `save` subcommand:
196
197```sh
198wotan save -p tsconfig.build.json -c config/.wotanrc.yaml -e '**/*.d.ts'
199```
200
201You just created a `.fimbullinter.yaml` file with the following contents:
202
203```yaml
204config: config/.wotanrc.yaml
205exclude:
206 - "**/*.d.ts"
207project: tsconfig.build.json
208```
209
210The next time you execute `wotan` in that directory, this default configuration is automatically picked up.
211
212Defaults can be overridden or cleared by explicitly specifying them as CLI arguments, for example:
213
214```sh
215wotan -p tsconfig.json -e '' # overrides 'project' and clears 'exclude'
216
217wotan save -c '' # clear 'config' option and update .fimbullinter.yaml
218```
219
220Note that `.fimbullinter.yaml` can also be used to store configuration for plugin modules. See the documentation of the plugins you use if this applies to you. In that case you need to edit the file manually. Using `wotan save` will not alter third party configuration.
221
222## Linting with Type Information
223
224When linting a project (`--project` CLI option) rules are able to use type information using TypeScript's API. Some rules report more findings with type information, some other rules require type information for each of their checks.
225If a rule cannot work properly without type information, you will see a warning like `Rule 'foo' requires type information.`
226
227### Special Handling of JavaScript Files
228
229TypeScript can analyze and check JavaScript files. However, it only does this if you explicitly ask for it using `"allowJs": true, "checkJs": true` in your `tsconfig.json` or by adding a `// @ts-check` comment on top of your JS files.
230A `// @ts-nocheck` comment excludes a file from type checking.
231More information is available in the official [TypeScript Handbook: Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html).
232
233Wotan respects these flags, too. That means it will not provide type information to rules executed on unchecked JS files.
234This ensures you won't get surprising lint findings caused by funky type inference in those files.
235You will still get reports for purely syntactic findings, i.e. rules that don't require type information.
236
237### Caching
238
239Caching is done per project. Hence it requires type information. For every `tsconfig.json` it creates a `tsconfig.fimbullintercache` file that contains the state of the previous run.
240The content of this file is not intended for use by other tools. All cache information is relative to the project directory. That means the cache can be checked into your VCS and reused by CI or your collegues.
241
242If a cache is available for a given project, Wotan can avoid linting files that are known to be unchanged. This can significantly reduce execution time.
243
244A file is treated as outdated and therefore linted if one of the following is true:
245
246* TypeScript version changed
247* compilerOptions changed
248* added, removed or outdated files that affect the global scope
249* linter configuration for the file changed
250* file has no cached lint result
251* file content changed
252* module resolution of imports or exports has changed
253 * dependency is added or removed
254 * ambient module or module augmentation is added, removed
255 * dependency is outdated
256 * if compilerOption `assumeChangesOnlyAffectDirectDependencies` is enabled, only checks direct dependencies
257 * otherwise recursively checks all transitive dependencies
258
259The following cases don't work well with caching:
260
261* rules accessing the file system or environment variables
262* rules accessing other files in the project that are not global or dependencies of the current file
263* linting the same project with different configurations -> only use caching for one of the configurations
264* projects where all files are in the global scope
265* updating the linter, rules or their (transitive) dependencies -> you need to manually remove the cache if you expect it to affect the lint result
266
267### Excluded Files
268
269If type information is available Wotan excludes all files you haven't written yourself. The following files are always excluded so you cannot explicitly include them:
270
271* any files of dependencies in `node_modules` (unless imported using a relative path, e.g. `./node_modules/foo/index`)
272* declaration files from `@types` (or `typeRoots` declared in your `tsconfig.json`)
273* declaration files included by TypeScript, e.g. `lib.es5.d.ts`
274* declaration files of project references (`references` in `tsconfig.json`)
275
276This is the default behavior which can be overridden by plugin modules.
277
278If you lint individual files without type information using the file's path or a glob pattern, you are responsible for excluding all files you don't want to lint.
279
280## Diagnosing Misbehavior
281
282Catching bugs by just looking at an exception is hard. That's why Wotan produces debug output for certain events. You only need to enable it via environment variable `DEBUG=wotan:*` and run the previous command again.
283See the [detailed documentation](https://github.com/visionmedia/debug#wildcards) on how to use the wildcards.
284
285## License
286
287Apache-2.0 © [Klaus Meinhardt](https://github.com/ajafff)