tslint-immutable
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -9,5 +9,9 @@ # Change Log | ||
## [v4.2.0] - 2017-09-14 | ||
### Added | ||
- New option `ignore-prefix` for the `no-expression-statement` rule. See [#39](https://github.com/jonaskello/tslint-immutable/issues/39) for background. Thanks to [@algesten](https://github.com/algesten) for this option! (See PR [#42](https://github.com/jonaskello/tslint-immutable/pull/42)) | ||
## [v4.1.0] - 2017-08-21 | ||
### Added | ||
- New rule `no-object-mutation`. See [#36](https://github.com/jonaskello/tslint-immutable/pull/36) for background. Thanks to [@miangraham](https://github.com/miangraham) for this rule! (See PR [#37](https://github.com/jonaskello/tslint-immutable/pull/37)) | ||
- New rule `no-object-mutation`. See [#36](https://github.com/jonaskello/tslint-immutable/issues/36) for background. Thanks to [@miangraham](https://github.com/miangraham) for this rule! (See PR [#37](https://github.com/jonaskello/tslint-immutable/pull/37)) | ||
@@ -134,3 +138,4 @@ ## [v4.0.2] - 2017-07-16 | ||
[Unreleased]: https://github.com/jonaskello/tslint-immutable/compare/v4.1.0...master | ||
[Unreleased]: https://github.com/jonaskello/tslint-immutable/compare/v4.2.0...master | ||
[v4.2.0]: https://github.com/jonaskello/tslint-immutable/compare/v4.1.0...v4.2.0 | ||
[v4.1.0]: https://github.com/jonaskello/tslint-immutable/compare/v4.0.2...v4.1.0 | ||
@@ -137,0 +142,0 @@ [v4.0.2]: https://github.com/jonaskello/tslint-immutable/compare/v4.0.1...v4.0.2 |
{ | ||
"name": "tslint-immutable", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "TSLint rules to disable mutation in TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "tslint-immutable.json", |
@@ -87,3 +87,4 @@ # tslint-immutable | ||
```typescript | ||
let foo: { readonly [key:string]: number }; | ||
const foo: { readonly [key: string]: number } = { "a": 1, "b": 2 }; | ||
foo["a"] = 3; // Error: Index signature only permits reading | ||
``` | ||
@@ -226,2 +227,19 @@ | ||
#### Options | ||
- [ignore-prefix](#using-the-ignore-prefix-option-with-no-expression-statement) | ||
#### Example config | ||
```javascript | ||
"no-expression-statement": true | ||
``` | ||
```javascript | ||
"no-expression-statement": [true, {"ignore-prefix": "console."}] | ||
``` | ||
```javascript | ||
"no-expression-statement": [true, {"ignore-prefix": ["console.log", "console.error"]}] | ||
``` | ||
## Options | ||
@@ -260,2 +278,21 @@ | ||
### Using the `ignore-prefix` option with `no-expression-statement` | ||
Expression statements typically cause side effects, however not all side effects are undesirable. One example of a helpful side effect is logging. To not get warning of every log statement, we can configure the linter to ignore well known expression statement prefixes. | ||
One such prefix could be `console.`, which would cover both these cases: | ||
```typescript | ||
const doSomething(arg:string) => { | ||
if (arg) { | ||
console.log("Argument is", arg); | ||
} else { | ||
console.warn("Argument is empty!"); | ||
} | ||
return `Hello ${arg}`; | ||
} | ||
``` | ||
## Recommended built-in rules | ||
@@ -262,0 +299,0 @@ |
@@ -15,2 +15,14 @@ "use strict"; | ||
var Lint = require("tslint"); | ||
var OPTION_IGNORE_PREFIX = "ignore-prefix"; | ||
function parseOptions(options) { | ||
var ignorePrefix; | ||
for (var _i = 0, options_1 = options; _i < options_1.length; _i++) { | ||
var o = options_1[_i]; | ||
if (typeof o === "object" && o[OPTION_IGNORE_PREFIX] !== null) { | ||
ignorePrefix = o[OPTION_IGNORE_PREFIX]; | ||
break; | ||
} | ||
} | ||
return { ignorePrefix: ignorePrefix }; | ||
} | ||
var Rule = (function (_super) { | ||
@@ -31,4 +43,6 @@ __extends(Rule, _super); | ||
__extends(NoExpressionStatementWalker, _super); | ||
function NoExpressionStatementWalker() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
function NoExpressionStatementWalker(sourceFile, options) { | ||
var _this = _super.call(this, sourceFile, options) || this; | ||
Object.assign(_this, parseOptions(options.ruleArguments)); | ||
return _this; | ||
} | ||
@@ -38,3 +52,6 @@ NoExpressionStatementWalker.prototype.visitNode = function (node) { | ||
var children = node.getChildren(); | ||
if (children.every(function (n) { return n.kind !== ts.SyntaxKind.YieldExpression; })) { | ||
var text = node.getText(this.getSourceFile()); | ||
var isYield = children.every(function (n) { return n.kind === ts.SyntaxKind.YieldExpression; }); | ||
var isIgnored = this.isIgnored(text); | ||
if (!isYield && !isIgnored) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)); | ||
@@ -45,3 +62,19 @@ } | ||
}; | ||
NoExpressionStatementWalker.prototype.isIgnored = function (text) { | ||
if (!this.ignorePrefix) { | ||
return false; | ||
} | ||
if (Array.isArray(this.ignorePrefix)) { | ||
if (this.ignorePrefix.find(function (pfx) { return text.indexOf(pfx) === 0; })) { | ||
return true; | ||
} | ||
} | ||
else { | ||
if (text.indexOf(this.ignorePrefix) === 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
return NoExpressionStatementWalker; | ||
}(Lint.RuleWalker)); |
62267
776
367