Repository: nzakas/eslint-plugin-typescript
Pull request #108 (2018-02-24 18:11:37)

New: Add no-non-null-assertion
This PR adds the [`no-non-null-assertion`](https://palantir.github.io/tslint/rules/no-non-null-assertion/) rule as listed in the description of #5.

Please let me know if there is any wording or formatting that should be adjusted. This is my first time contributing to this codebase, and I tried to match this codebase's conventions. 🙂 


Patch:
diff --git a/README.md b/README.md
index 7939658..ccc1723 100644
--- a/README.md
+++ b/README.md
@@ -64,6 +64,7 @@ Then configure the rules you want to use under the rules section.
 * [`typescript/no-empty-interface`](./docs/rules/no-empty-interface.md) — Disallow the declaration of empty interfaces (`no-empty-interface` from TSLint)
 * [`typescript/no-explicit-any`](./docs/rules/no-explicit-any.md) — Disallow usage of the `any` type (`no-any` from TSLint)
 * [`typescript/no-namespace`](./docs/rules/no-namespace.md) — Disallow the use of custom TypeScript modules and namespaces
+* [`typescript/no-non-null-assertion`](./docs/rules/no-non-null-assertion.md) — Disallows non-null assertions using the `!` postfix operator (`no-non-null-assertion` from TSLint)
 * [`typescript/no-parameter-properties`](./docs/rules/no-parameter-properties.md) — Disallow the use of parameter properties in class constructors. (`no-parameter-properties` from TSLint)
 * [`typescript/no-triple-slash-reference`](./docs/rules/no-triple-slash-reference.md) — Disallow `/// <reference path="" />` comments (`no-reference` from TSLint)
 * [`typescript/no-type-alias`](./docs/rules/no-type-alias.md) — Disallow the use of type aliases (`interface-over-type-literal` from TSLint)
diff --git a/docs/rules/no-non-null-assertion.md b/docs/rules/no-non-null-assertion.md
new file mode 100644
index 0000000..5e9f2e3
--- /dev/null
+++ b/docs/rules/no-non-null-assertion.md
@@ -0,0 +1,35 @@
+# Disallows non-null assertions using the `!` postfix operator (no-non-null-assertion)
+
+## Rule Details
+
+Using non-null assertions cancels the benefits of the strict null-checking mode.
+
+Examples of **incorrect** code for this rule:
+
+```ts
+interface Foo {
+    bar?: string;
+}
+
+const foo: Foo = getFoo();
+const includesBaz: boolean = foo.bar!.includes("baz");
+```
+
+Examples of **correct** code for this rule:
+
+```ts
+interface Foo {
+    bar?: string;
+}
+
+const foo: Foo = getFoo();
+const includesBaz: boolean = foo.bar && foo.bar.includes("baz");
+```
+
+## When Not To Use It
+
+If you don't care about strict null-checking, then you will not need this rule.
+
+## Further Reading
+
+* [`no-non-null-assertion`](https://palantir.github.io/tslint/rules/no-non-null-assertion/) in [TSLint](https://palantir.github.io/tslint/)
diff --git a/lib/rules/no-non-null-assertion.js b/lib/rules/no-non-null-assertion.js
new file mode 100644
index 0000000..3520462
--- /dev/null
+++ b/lib/rules/no-non-null-assertion.js
@@ -0,0 +1,39 @@
+/**
+ * @fileoverview Disallows non-null assertions using the `!` postfix operator.
+ * @author Macklin Underdown
+ */
+"use strict";
+
+const util = require("../util");
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+    meta: {
+        docs: {
+            description:
+                "Disallows non-null assertions using the `!` postfix operator",
+            extraDescription: [util.tslintRule("no-non-null-assertion")],
+            category: "TypeScript",
+            url:
+                "https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/no-non-null-assertion.md"
+        },
+        schema: []
+    },
+    create(context) {
+        //----------------------------------------------------------------------
+        // Public
+        //----------------------------------------------------------------------
+
+        return {
+            TSNonNullExpression(node) {
+                context.report({
+                    node,
+                    message: "Forbidden non-null assertion"
+                });
+            }
+        };
+    }
+};


Test patch:
diff --git a/tests/lib/rules/no-non-null-assertion.js b/tests/lib/rules/no-non-null-assertion.js
new file mode 100644
index 0000000..10b65a2
--- /dev/null
+++ b/tests/lib/rules/no-non-null-assertion.js
@@ -0,0 +1,32 @@
+/**
+ * @fileoverview Disallows non-null assertions using the `!` postfix operator.
+ * @author Macklin Underdown
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Requirements
+//------------------------------------------------------------------------------
+
+const rule = require("../../../lib/rules/no-non-null-assertion"),
+    RuleTester = require("eslint").RuleTester;
+
+//------------------------------------------------------------------------------
+// Tests
+//------------------------------------------------------------------------------
+
+const ruleTester = new RuleTester({
+    parser: "typescript-eslint-parser"
+});
+
+ruleTester.run("no-non-null-assertion", rule, {
+    valid: ["const x = { y: 1 }; x.y;"],
+    invalid: [
+        {
+            code: "const x = null; x!.y;",
+            errors: [
+                { message: "Forbidden non-null assertion", line: 1, column: 17 }
+            ]
+        }
+    ]
+});