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

Allow null keyword in a type position #1277

Merged
merged 1 commit into from
May 27, 2016
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
12 changes: 11 additions & 1 deletion src/rules/noNullKeywordRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ export class Rule extends Lint.Rules.AbstractRule {
class NullWalker extends Lint.RuleWalker {
public visitNode(node: ts.Node) {
super.visitNode(node);
if (node.kind === ts.SyntaxKind.NullKeyword) {
if (node.kind === ts.SyntaxKind.NullKeyword && !isPartOfType(node)) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}
}

function isPartOfType({ parent }: ts.Node) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe isPartOfTypeLiteral?

while (parent != null) {
if (ts.SyntaxKind.FirstTypeNode <= parent.kind && parent.kind <= ts.SyntaxKind.LastTypeNode) {
return true;
}
parent = parent.parent;
}
return false;
}
5 changes: 5 additions & 0 deletions test/rules/no-null-keyword/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ var x = null; // error
~~~~ [Use 'undefined' instead of 'null']
console.log(null, x); // error
~~~~ [Use 'undefined' instead of 'null']

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