|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import type { RuleTester } from '@typescript-eslint/rule-tester'; |
| 5 | + |
| 6 | +import { getRuleTesterWithoutProject } from './ruleTester'; |
| 7 | +import { noNullRule } from '../no-null'; |
| 8 | + |
| 9 | +const ruleTester: RuleTester = getRuleTesterWithoutProject(); |
| 10 | + |
| 11 | +ruleTester.run('no-null', noNullRule, { |
| 12 | + invalid: [ |
| 13 | + { |
| 14 | + // Assigning null to a variable |
| 15 | + code: 'let x = null;', |
| 16 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 17 | + }, |
| 18 | + { |
| 19 | + // Passing null as a function argument (not Object.create) |
| 20 | + code: 'foo(null);', |
| 21 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 22 | + }, |
| 23 | + { |
| 24 | + // Returning null |
| 25 | + code: 'function f() { return null; }', |
| 26 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 27 | + }, |
| 28 | + { |
| 29 | + // Using null in a ternary |
| 30 | + code: 'let x = true ? null : undefined;', |
| 31 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 32 | + }, |
| 33 | + { |
| 34 | + // Using null as a second argument to Object.create |
| 35 | + code: 'Object.create(proto, null);', |
| 36 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 37 | + }, |
| 38 | + { |
| 39 | + // Using null on a different method of Object |
| 40 | + code: 'Object.assign(null);', |
| 41 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 42 | + }, |
| 43 | + { |
| 44 | + // Using null with a different object's create method |
| 45 | + code: 'NotObject.create(null);', |
| 46 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 47 | + }, |
| 48 | + { |
| 49 | + // Computed property access: Object["create"](null) is NOT exempted |
| 50 | + code: 'Object["create"](null);', |
| 51 | + errors: [{ messageId: 'error-usage-of-null' }] |
| 52 | + } |
| 53 | + ], |
| 54 | + valid: [ |
| 55 | + { |
| 56 | + // Comparison with === is allowed |
| 57 | + code: 'if (x === null) {}' |
| 58 | + }, |
| 59 | + { |
| 60 | + // Comparison with !== is allowed |
| 61 | + code: 'if (x !== null) {}' |
| 62 | + }, |
| 63 | + { |
| 64 | + // Comparison with == is allowed |
| 65 | + code: 'if (x == null) {}' |
| 66 | + }, |
| 67 | + { |
| 68 | + // Comparison with != is allowed |
| 69 | + code: 'if (x != null) {}' |
| 70 | + }, |
| 71 | + { |
| 72 | + // Object.create(null) is allowed for creating prototype-less dictionary objects |
| 73 | + code: 'const dict = Object.create(null);' |
| 74 | + }, |
| 75 | + { |
| 76 | + // Object.create(null) with type annotation |
| 77 | + code: 'const dict: Record<string, number> = Object.create(null);' |
| 78 | + }, |
| 79 | + { |
| 80 | + // Object.create(null) inside a function |
| 81 | + code: 'function createDict() { return Object.create(null); }' |
| 82 | + } |
| 83 | + ] |
| 84 | +}); |
0 commit comments