75

Is that possible to lint entire folder using tslint?

Using eslint it is possible to do eslint ./src to validate whole folder.

When i try to do the same for tslint - i am getting an error Error: EISDIR: illegal operation on a directory. In their examples on the site - they show how to validate single file, which is not the case usually.

Is that possible to validate my project without extra things like gulp-tslint, just from the command line?

2
  • 4
    Figured it out, that its possible to use wildcards - tslint src/**/*.ts Apr 6, 2016 at 10:08
  • Thanks for the comment. I was wondering how exactly to use wildcards.
    – mihkov
    Jan 2, 2017 at 15:11

3 Answers 3

111

You can use a glob to lint multiple files.

Normally, if you just pass a glob as is, your shell will expand it and pass the resulting files to TSLint. So for example, in bash 4+ with the globstar option enabled, you could do the following to lint all .ts and .tsx files:

tslint src/**/*.ts{,x}

You're probably better off though using a command that will work consistently across platforms and shells though. For this, you can pass the glob in quotes. When in quotes, the glob will get passed as is to TSLint which will handle it with node-glob. You could then run the following command to get the same results as above:

tslint 'src/**/*.ts?(x)'
6
  • Indeed, it seems like no-quotes only works on Windows. Thanks! Apr 12, 2017 at 20:38
  • so this does not work if I have a file a direct child of src. say src/index.ts, which is a common use case I suppose
    – aitchkhan
    Feb 12, 2018 at 5:02
  • @HaroonKhan both should work. I'm assuming in your case you were using the bash version of the glob. You likely did not have the globstar option set. Try switching to quoted version to take advantage of node-glob, which also has the nice side-effect of making your script more platform-independent
    – JKillian
    Feb 12, 2018 at 5:21
  • worked like a charm, but now failing in lint-staged :) will have a look at it ina while
    – aitchkhan
    Feb 12, 2018 at 5:42
  • helpful! for those unfamiliar with what a glob is
    – heug
    Nov 27, 2018 at 15:47
29

There is now a --project option for this. Example:

tslint --project .

From the docs:

-p, --project:

The path or directory containing a tsconfig.json file that will be used to determine which files will be linted. This flag also enables rules that require the type checker.

3
  • 1
    This works :) Thank you. The other wild card method wasn't finding all of the files.
    – Bodman
    Mar 21, 2018 at 16:50
  • I don't really understand the --project argument. Do you know how can I exclude files? May 15, 2018 at 18:33
  • The --project argument simply tells it to use the settings in the tsconfig.json file. So if you want to exclude files you'd need to configure that in tsconfig.json. May 15, 2018 at 19:52
11

If your project has a tsconfig.json you can aproach --project advange.

In my case I have a nativescript project wich includes a tsconfig.json at project root, so I have this:

enter image description here

Note that in the json 'node_modules' and 'platforms' directories are excluded. This means that all files in the project are going to be mapped except 'node_modules' and 'platforms' directories.

I have added this scripts at my package.json, 'tslint' to log the errors and 'tslint-fix' to fix the errors.

"scripts": {
    "tslint": "tslint -p tsconfig.json",
    "tslint-fix": "tslint --fix -p tsconfig.json"
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.