From 0e3c7b6a8221895c53f7e61593ef8560ce5df117 Mon Sep 17 00:00:00 2001 From: arusakov Date: Mon, 20 Feb 2017 01:08:53 +0300 Subject: [PATCH 1/2] no-non-null-assertion rule --- src/language/walker/syntaxWalker.ts | 8 ++++ src/rules/noNonNullAssertionRule.ts | 47 +++++++++++++++++++ test/rules/no-non-null-assertion/test.ts.lint | 4 ++ test/rules/no-non-null-assertion/tslint.json | 5 ++ 4 files changed, 64 insertions(+) create mode 100644 src/rules/noNonNullAssertionRule.ts create mode 100644 test/rules/no-non-null-assertion/test.ts.lint create mode 100644 test/rules/no-non-null-assertion/tslint.json diff --git a/src/language/walker/syntaxWalker.ts b/src/language/walker/syntaxWalker.ts index acee72b0e09..09d22289810 100644 --- a/src/language/walker/syntaxWalker.ts +++ b/src/language/walker/syntaxWalker.ts @@ -234,6 +234,10 @@ export class SyntaxWalker { this.walkChildren(node); } + protected visitNonNullExpression(node: ts.NonNullExpression) { + this.walkChildren(node); + } + protected visitNumericLiteral(node: ts.NumericLiteral) { this.walkChildren(node); } @@ -560,6 +564,10 @@ export class SyntaxWalker { this.visitNewExpression(node as ts.NewExpression); break; + case ts.SyntaxKind.NonNullExpression: + this.visitNonNullExpression(node as ts.NonNullExpression); + break; + case ts.SyntaxKind.NumericLiteral: this.visitNumericLiteral(node as ts.NumericLiteral); break; diff --git a/src/rules/noNonNullAssertionRule.ts b/src/rules/noNonNullAssertionRule.ts new file mode 100644 index 00000000000..1fa1b6f80d9 --- /dev/null +++ b/src/rules/noNonNullAssertionRule.ts @@ -0,0 +1,47 @@ +/** + * @license + * Copyright 2013 Palantir Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as ts from "typescript"; + +import * as Lint from "../index"; + +export class Rule extends Lint.Rules.AbstractRule { + /* tslint:disable:object-literal-sort-keys */ + public static metadata: Lint.IRuleMetadata = { + ruleName: "no-non-null-assertion", + description: "Disallows non null assertions.", + optionsDescription: "Not configurable.", + options: null, + optionExamples: ["true"], + type: "typescript", + typescriptOnly: true, + }; + /* tslint:enable:object-literal-sort-keys */ + + public static FAILURE_STRING = "Forbidden non null assertion"; + + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + return this.applyWithWalker(new NoNonNullAssertionWalker(sourceFile, this.getOptions())); + } +} + +class NoNonNullAssertionWalker extends Lint.RuleWalker { + public visitNonNullExpression(node: ts.NonNullExpression) { + this.addFailureAtNode(node, Rule.FAILURE_STRING); + super.visitNonNullExpression(node); + } +} diff --git a/test/rules/no-non-null-assertion/test.ts.lint b/test/rules/no-non-null-assertion/test.ts.lint new file mode 100644 index 00000000000..12bb8af725d --- /dev/null +++ b/test/rules/no-non-null-assertion/test.ts.lint @@ -0,0 +1,4 @@ +var x = null + +x!.y = null +~~ [Forbidden non null assertion] diff --git a/test/rules/no-non-null-assertion/tslint.json b/test/rules/no-non-null-assertion/tslint.json new file mode 100644 index 00000000000..6359b34ac55 --- /dev/null +++ b/test/rules/no-non-null-assertion/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-non-null-assertion": true + } +} From a433216ca6e99dc7d14076519422ad41a29b9a20 Mon Sep 17 00:00:00 2001 From: arusakov Date: Wed, 22 Feb 2017 01:05:16 +0300 Subject: [PATCH 2/2] copyright year fixed, rationale added --- src/rules/noNonNullAssertionRule.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rules/noNonNullAssertionRule.ts b/src/rules/noNonNullAssertionRule.ts index 1fa1b6f80d9..015f3bdcbea 100644 --- a/src/rules/noNonNullAssertionRule.ts +++ b/src/rules/noNonNullAssertionRule.ts @@ -1,6 +1,6 @@ /** * @license - * Copyright 2013 Palantir Technologies, Inc. + * Copyright 2017 Palantir Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,8 @@ export class Rule extends Lint.Rules.AbstractRule { /* tslint:disable:object-literal-sort-keys */ public static metadata: Lint.IRuleMetadata = { ruleName: "no-non-null-assertion", - description: "Disallows non null assertions.", + description: "Disallows non-null assertions.", + rationale: "Using non-null assertion cancels the benefits of the strict null checking mode.", optionsDescription: "Not configurable.", options: null, optionExamples: ["true"],