-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathno-null.test.ts
More file actions
97 lines (93 loc) · 2.79 KB
/
no-null.test.ts
File metadata and controls
97 lines (93 loc) · 2.79 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { RuleTester } from '@typescript-eslint/rule-tester';
import { getRuleTesterWithoutProject } from './ruleTester';
import { noNullRule } from '../no-null';
const ruleTester: RuleTester = getRuleTesterWithoutProject();
ruleTester.run('no-null', noNullRule, {
invalid: [
{
// Assigning null to a variable
code: 'let x = null;',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Passing null as a function argument (not Object.create)
code: 'foo(null);',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Returning null
code: 'function f() { return null; }',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Using null in a ternary
code: 'let x = true ? null : undefined;',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Using null as a second argument to Object.create
code: 'Object.create(proto, null);',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Using null on a different method of Object
code: 'Object.assign(null);',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Using null with a different object's create method
code: 'NotObject.create(null);',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// Computed property access: Object["create"](null) is NOT exempted
code: 'Object["create"](null);',
errors: [{ messageId: 'error-usage-of-null' }]
},
{
// null on a non-__proto__ property is still flagged
code: 'const x = { foo: null };',
errors: [{ messageId: 'error-usage-of-null' }]
}
],
valid: [
{
// Comparison with === is allowed
code: 'if (x === null) {}'
},
{
// Comparison with !== is allowed
code: 'if (x !== null) {}'
},
{
// Comparison with == is allowed
code: 'if (x == null) {}'
},
{
// Comparison with != is allowed
code: 'if (x != null) {}'
},
{
// Object.create(null) is allowed for creating prototype-less dictionary objects
code: 'const dict = Object.create(null);'
},
{
// Object.create(null) with type annotation
code: 'const dict: Record<string, number> = Object.create(null);'
},
{
// Object.create(null) inside a function
code: 'function createDict() { return Object.create(null); }'
},
{
// __proto__: null is allowed in object literals
code: 'const obj = { __proto__: null, a: 1 };'
},
{
// __proto__: null as string key is also allowed
code: 'const obj = { "__proto__": null, a: 1 };'
}
]
});