| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {DiagnosticAllowlist} from '../types'; |
| 9 | |
| 10 | import {isBlockingDiagnostic} from '../Diagnostics'; |
| 11 | |
| 12 | const range = {startLine: 1, startCol: 1, endLine: 1, endCol: 1}; |
| 13 | |
| 14 | describe('Diagnostics', () => { |
| 15 | describe('isBlockingDiagnostic', () => { |
| 16 | it('handles allowlist', () => { |
| 17 | const policy: DiagnosticAllowlist = new Map([ |
| 18 | ['error', new Map([['foo', {allow: new Set(['code1'])}]])], |
| 19 | ]); |
| 20 | expect( |
| 21 | isBlockingDiagnostic( |
| 22 | { |
| 23 | message: 'sample', |
| 24 | range, |
| 25 | severity: 'error', |
| 26 | source: 'foo', |
| 27 | code: 'code1', |
| 28 | }, |
| 29 | policy, |
| 30 | ), |
| 31 | ).toBe(true); |
| 32 | expect( |
| 33 | isBlockingDiagnostic( |
| 34 | { |
| 35 | message: 'sample', |
| 36 | range, |
| 37 | severity: 'error', |
| 38 | source: 'foo', |
| 39 | code: 'code3', |
| 40 | }, |
| 41 | policy, |
| 42 | ), |
| 43 | ).toBe(false); |
| 44 | }); |
| 45 | |
| 46 | it('handles blocklist', () => { |
| 47 | const policy: DiagnosticAllowlist = new Map([ |
| 48 | ['error', new Map([['foo', {block: new Set(['code1'])}]])], |
| 49 | ]); |
| 50 | expect( |
| 51 | isBlockingDiagnostic( |
| 52 | { |
| 53 | message: 'sample', |
| 54 | range, |
| 55 | severity: 'error', |
| 56 | source: 'foo', |
| 57 | code: 'code3', |
| 58 | }, |
| 59 | policy, |
| 60 | ), |
| 61 | ).toBe(true); |
| 62 | expect( |
| 63 | isBlockingDiagnostic( |
| 64 | { |
| 65 | message: 'sample', |
| 66 | range, |
| 67 | severity: 'error', |
| 68 | source: 'foo', |
| 69 | code: 'code1', |
| 70 | }, |
| 71 | policy, |
| 72 | ), |
| 73 | ).toBe(false); |
| 74 | }); |
| 75 | |
| 76 | it('handles wildcard', () => { |
| 77 | const policy: DiagnosticAllowlist = new Map([ |
| 78 | ['error', new Map([['foo', {block: new Set([])}]])], |
| 79 | ]); |
| 80 | expect( |
| 81 | isBlockingDiagnostic( |
| 82 | { |
| 83 | message: 'sample', |
| 84 | range, |
| 85 | severity: 'error', |
| 86 | source: 'foo', |
| 87 | code: 'code1', |
| 88 | }, |
| 89 | policy, |
| 90 | ), |
| 91 | ).toBe(true); |
| 92 | }); |
| 93 | |
| 94 | it('ignores non-errors', () => { |
| 95 | const policy: DiagnosticAllowlist = new Map([ |
| 96 | ['error', new Map([['foo', {block: new Set([])}]])], |
| 97 | ]); |
| 98 | expect( |
| 99 | isBlockingDiagnostic( |
| 100 | { |
| 101 | message: 'sample', |
| 102 | range, |
| 103 | severity: 'hint', |
| 104 | source: 'foo', |
| 105 | code: 'code1', |
| 106 | }, |
| 107 | policy, |
| 108 | ), |
| 109 | ).toBe(false); |
| 110 | }); |
| 111 | |
| 112 | it('accepts warnings', () => { |
| 113 | const policy: DiagnosticAllowlist = new Map([ |
| 114 | ['warning', new Map([['foo', {block: new Set([])}]])], |
| 115 | ]); |
| 116 | expect( |
| 117 | isBlockingDiagnostic( |
| 118 | { |
| 119 | message: 'sample', |
| 120 | range, |
| 121 | severity: 'warning', |
| 122 | source: 'foo', |
| 123 | code: 'code1', |
| 124 | }, |
| 125 | policy, |
| 126 | ), |
| 127 | ).toBe(true); |
| 128 | }); |
| 129 | |
| 130 | it('handles undefined source', () => { |
| 131 | const policy: DiagnosticAllowlist = new Map([ |
| 132 | ['warning', new Map([['undefined', {block: new Set(['foo'])}]])], |
| 133 | ]); |
| 134 | expect( |
| 135 | isBlockingDiagnostic( |
| 136 | { |
| 137 | message: 'sample', |
| 138 | range, |
| 139 | severity: 'warning', |
| 140 | source: undefined, |
| 141 | code: 'foo', |
| 142 | }, |
| 143 | policy, |
| 144 | ), |
| 145 | ).toBe(false); |
| 146 | expect( |
| 147 | isBlockingDiagnostic( |
| 148 | { |
| 149 | message: 'sample', |
| 150 | range, |
| 151 | severity: 'warning', |
| 152 | source: undefined, |
| 153 | code: 'something_else', |
| 154 | }, |
| 155 | policy, |
| 156 | ), |
| 157 | ).toBe(true); |
| 158 | }); |
| 159 | |
| 160 | it('handles undefined code', () => { |
| 161 | const policy: DiagnosticAllowlist = new Map([ |
| 162 | ['warning', new Map([['foo', {block: new Set(['undefined'])}]])], |
| 163 | ]); |
| 164 | expect( |
| 165 | isBlockingDiagnostic( |
| 166 | { |
| 167 | message: 'sample', |
| 168 | range, |
| 169 | severity: 'warning', |
| 170 | source: 'foo', |
| 171 | code: undefined, |
| 172 | }, |
| 173 | policy, |
| 174 | ), |
| 175 | ).toBe(false); |
| 176 | }); |
| 177 | }); |
| 178 | }); |
| 179 | |