Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

New Rule: no-non-null-assertion #2221

Merged
merged 2 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/language/walker/syntaxWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions src/rules/noNonNullAssertionRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license
* 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.
* 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.",
rationale: "Using non-null assertion cancels the benefits of the strict null checking mode.",
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);
}
}
4 changes: 4 additions & 0 deletions test/rules/no-non-null-assertion/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var x = null

x!.y = null
~~ [Forbidden non null assertion]
5 changes: 5 additions & 0 deletions test/rules/no-non-null-assertion/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-non-null-assertion": true
}
}