forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-null.ts
More file actions
49 lines (43 loc) · 1.5 KB
/
no-null.ts
File metadata and controls
49 lines (43 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
TSESTree,
TSESLint
} from '@typescript-eslint/experimental-utils';
type MessageIds = 'error-usage-of-null';
type Options = [ ];
const noNullRule: TSESLint.RuleModule<MessageIds,Options> = {
meta: {
type: 'problem',
messages: {
'error-usage-of-null':
'Usage of "null" is deprecated except when received from legacy APIs; use "undefined" instead'
},
schema: [ ],
docs: {
description: 'Prevent usage of JavaScript\'s "null" keyword',
category: 'Stylistic Issues',
recommended: "error",
url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin'
}
},
create: (context: TSESLint.RuleContext<MessageIds, Options>) => {
return {
Literal: function(node: TSESTree.Literal) {
// Is it a "null" literal?
if (node.value === null) {
// Does the "null" appear in a comparison such as "if (x === null)"?
let isComparison: boolean = false;
if (node.parent && node.parent.type === 'BinaryExpression') {
const operator: string = node.parent.operator;
isComparison = operator === '!==' || operator === '===' || operator === '!=' || operator === '==';
}
if (!isComparison) {
context.report({ node, messageId: 'error-usage-of-null' });
}
}
}
};
}
};
export { noNullRule };