A collection of useful TSLint rules.
no-catch-expect
: Prevent calling Jasmine'sexpect
andexpectAsync
functions within acatch
block. If your tested code should throw an exception and you check that by callingexpect
within acatch
block, then your test might incorrectly succeed if your tested code does not throw an exception. Using this rule forces you to move theexpect
after thecatch
block.no-empty-imports
: Preventimport
statements from not importing any symbols.require-component-view-encapsulation
: Require Angular components to declare view encapsulation. When using ahead-of-time compilation, you cannot override the default Emulated encapsulation option. If you want ShadowDom encapsulation you must remember to add this option to each component. This rule forces you to add a specific encapsulation option.
This package is an extension of TSLint, therefore you must already have TSLint installed in your project. See https://github.com/palantir/tslint.
To consume these rules in your project, first install the module:
$ npm install --save-dev pslint
Then add the rules' location and configurations to your tslint.json:
{
"rulesDirectory": [
"./node_modules/pslint"
],
"rules": {
"no-catch-expect": true,
"no-empty-imports": true,
"require-component-view-encapsulation": [true, "ShadowDom"]
}
}
Note the peerDependencies
in package.json.
pslint
uses Gulp as its task runner. There's no need to install Gulp globally, since it appears in the package.json as a script. All Gulp tasks are written in TypeScript and you can find them in gulpfile.ts.
To fully build the project, use the default Gulp task:
$ npm run gulp
To run an individual task, for example, to transpile the TypeScript:
$ npm run gulp -- transpile
Each rule gets its own directory within test/rules. Name the directory the same as the rule file, except drop Rule from the end and convert camel case to kebab case. Example: noCatchExpectRule.ts → no-catch-expect. Each directory for rules-under-test needs test.ts.lint to contain the tests and tslint.json to configure TSLint. Test directories can contain arbitrary nesting of sub-test directories so long as each sub-directory has these two files. This is useful for testing multiple configurations of the same rule.
Run the tests:
$ npm run test
To debug a rule, set a breakpoint using the debugger
statement then run
$ npm run test-debug
In Chrome, navigate to chrome://inspect where you'll see the app listed as available for debugging.