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

Commit

Permalink
no-null-keyword: Allow strict comparison (#2802)
Browse files Browse the repository at this point in the history
Allows `x === null`, but disallows `x == null`. The latter will be automatically fixed to `x == undefined`.

[new-fixer] `no-null-keyword`: fix `x == null` to `x == undefined`
[enhancement] `no-null-keyword` allows strict comparison
Fixes: #2782
  • Loading branch information
ajafff authored and adidahiya committed Jul 31, 2017
1 parent 42ff133 commit ace23ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/rules/noNullKeywordRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// with due reference to https://github.com/Microsoft/TypeScript/blob/7813121c4d77e50aad0eed3152ef1f1156c7b574/scripts/tslint/noNullRule.ts

import { isBinaryExpression, isTypeNodeKind } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand All @@ -34,6 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
hasFix: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand All @@ -47,12 +49,21 @@ export class Rule extends Lint.Rules.AbstractRule {
function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, cb);
function cb(node: ts.Node): void {
if (node.kind >= ts.SyntaxKind.FirstTypeNode && node.kind <= ts.SyntaxKind.LastTypeNode) {
if (isTypeNodeKind(node.kind)) {
return; // skip type nodes
}
if (node.kind === ts.SyntaxKind.NullKeyword) {
return ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
if (node.kind !== ts.SyntaxKind.NullKeyword) {
return ts.forEachChild(node, cb);
}
const parent = node.parent!;
let eq: Lint.EqualsKind | undefined;
if (isBinaryExpression(parent)) {
eq = Lint.getEqualsKind(parent.operatorToken);
}
if (eq === undefined) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
} else if (!eq.isStrict) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, Lint.Replacement.replaceNode(node, "undefined", ctx.sourceFile));
}
return ts.forEachChild(node, cb);
}
}
12 changes: 12 additions & 0 deletions test/rules/no-null-keyword/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var x = null; // error
console.log(null, x); // error

let match: string | null;
interface foo {
bar: [number, null, string];
}

if (document.querySelector('.foo') === null) {}
if (document.querySelector('.foo') == undefined) {}
if (null !== document.querySelector('.foo')) {}
if (undefined != document.querySelector('.foo')) {}
15 changes: 12 additions & 3 deletions test/rules/no-null-keyword/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
var x = null; // error
~~~~ [Use 'undefined' instead of 'null']
~~~~ [0]
console.log(null, x); // error
~~~~ [Use 'undefined' instead of 'null']
~~~~ [0]

let match(): string | null;
let match: string | null;
interface foo {
bar: [number, null, string];
}

if (document.querySelector('.foo') === null) {}
if (document.querySelector('.foo') == null) {}
~~~~ [0]
if (null !== document.querySelector('.foo')) {}
if (null != document.querySelector('.foo')) {}
~~~~ [0]

[0]: Use 'undefined' instead of 'null'

0 comments on commit ace23ce

Please sign in to comment.