|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { expect } from 'chai'; |
| 7 | +import { |
| 8 | + filterSuppressedDiagnostics, |
| 9 | + YAML_DISABLE_PATTERN, |
| 10 | + parseDisableSpecifiers, |
| 11 | + shouldSuppressDiagnostic, |
| 12 | + GetLineText, |
| 13 | +} from '../src/languageservice/utils/diagnostic-filter'; |
| 14 | + |
| 15 | +function makeDiag(startLine: number, message: string): { startLine: number; message: string } { |
| 16 | + return { startLine, message }; |
| 17 | +} |
| 18 | + |
| 19 | +function linesOf(lines: string[]): GetLineText { |
| 20 | + return (line: number) => (line >= 0 && line < lines.length ? lines[line] : undefined); |
| 21 | +} |
| 22 | + |
| 23 | +describe('YAML_DISABLE_PATTERN', () => { |
| 24 | + it('should capture specifiers in group 1', () => { |
| 25 | + const match = YAML_DISABLE_PATTERN.exec('# yaml-language-server-disable Incorrect type, not accepted'); |
| 26 | + expect(match).to.not.be.null; |
| 27 | + expect(match[1].trim()).to.equal('Incorrect type, not accepted'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should capture empty group 1 when no specifiers given', () => { |
| 31 | + const match = YAML_DISABLE_PATTERN.exec('# yaml-language-server-disable'); |
| 32 | + expect(match).to.not.be.null; |
| 33 | + expect(match[1].trim()).to.equal(''); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('parseDisableSpecifiers', () => { |
| 38 | + it('should return empty array for empty string', () => { |
| 39 | + expect(parseDisableSpecifiers('')).to.deep.equal([]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return empty array for whitespace-only string', () => { |
| 43 | + expect(parseDisableSpecifiers(' ')).to.deep.equal([]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should parse a single specifier', () => { |
| 47 | + expect(parseDisableSpecifiers('Incorrect type')).to.deep.equal(['incorrect type']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should parse comma-separated specifiers', () => { |
| 51 | + expect(parseDisableSpecifiers('Incorrect type, not accepted')).to.deep.equal(['incorrect type', 'not accepted']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should trim whitespace around specifiers', () => { |
| 55 | + expect(parseDisableSpecifiers(' foo , bar ')).to.deep.equal(['foo', 'bar']); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should ignore empty entries from trailing commas', () => { |
| 59 | + expect(parseDisableSpecifiers('foo,')).to.deep.equal(['foo']); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should lower-case all specifiers', () => { |
| 63 | + expect(parseDisableSpecifiers('Value Is NOT Accepted')).to.deep.equal(['value is not accepted']); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('shouldSuppressDiagnostic', () => { |
| 68 | + it('should suppress when specifiers is empty (suppress all)', () => { |
| 69 | + expect(shouldSuppressDiagnostic([], 'any message')).to.be.true; |
| 70 | + }); |
| 71 | + |
| 72 | + it('should suppress when message contains the specifier (case-insensitive)', () => { |
| 73 | + expect(shouldSuppressDiagnostic(['incorrect type'], 'Incorrect type. Expected string.')).to.be.true; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not suppress when message does not contain the specifier', () => { |
| 77 | + expect(shouldSuppressDiagnostic(['not accepted'], 'Incorrect type. Expected string.')).to.be.false; |
| 78 | + }); |
| 79 | + |
| 80 | + it('should suppress when any of multiple specifiers matches', () => { |
| 81 | + expect(shouldSuppressDiagnostic(['not accepted', 'incorrect type'], 'Incorrect type. Expected string.')).to.be.true; |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not suppress when none of multiple specifiers match', () => { |
| 85 | + expect(shouldSuppressDiagnostic(['not accepted', 'missing property'], 'Incorrect type. Expected string.')).to.be.false; |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('filterSuppressedDiagnostics', () => { |
| 90 | + const filter = (diagnostics: ReturnType<typeof makeDiag>[], lines: GetLineText): ReturnType<typeof makeDiag>[] => |
| 91 | + filterSuppressedDiagnostics( |
| 92 | + diagnostics, |
| 93 | + (d) => d.startLine, |
| 94 | + (d) => d.message, |
| 95 | + lines |
| 96 | + ); |
| 97 | + |
| 98 | + it('should return all diagnostics when there are no suppression comments', () => { |
| 99 | + const lines = linesOf(['key: value', 'other: 123']); |
| 100 | + const diagnostics = [makeDiag(0, 'error on line 0'), makeDiag(1, 'error on line 1')]; |
| 101 | + |
| 102 | + const result = filter(diagnostics, lines); |
| 103 | + |
| 104 | + expect(result).to.have.length(2); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should suppress all diagnostics when no specifiers are given', () => { |
| 108 | + const lines = linesOf(['name: hello', '# yaml-language-server-disable', 'age: not-a-number']); |
| 109 | + const diagnostics = [makeDiag(2, 'Incorrect type'), makeDiag(2, 'Value not accepted')]; |
| 110 | + |
| 111 | + const result = filter(diagnostics, lines); |
| 112 | + |
| 113 | + expect(result).to.be.empty; |
| 114 | + }); |
| 115 | + |
| 116 | + it('should suppress only matching diagnostics when specifiers are given', () => { |
| 117 | + const lines = linesOf(['name: hello', '# yaml-language-server-disable Incorrect type', 'age: not-a-number']); |
| 118 | + const diagnostics = [makeDiag(2, 'Incorrect type. Expected string.'), makeDiag(2, 'Value is not accepted.')]; |
| 119 | + |
| 120 | + const result = filter(diagnostics, lines); |
| 121 | + |
| 122 | + expect(result).to.have.length(1); |
| 123 | + expect(result[0].message).to.equal('Value is not accepted.'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should suppress diagnostics matching any of multiple comma-separated specifiers', () => { |
| 127 | + const lines = linesOf(['# yaml-language-server-disable Incorrect type, not accepted', 'key: bad']); |
| 128 | + const diagnostics = [ |
| 129 | + makeDiag(1, 'Incorrect type. Expected string.'), |
| 130 | + makeDiag(1, 'Value is not accepted.'), |
| 131 | + makeDiag(1, 'Missing required property "name".'), |
| 132 | + ]; |
| 133 | + |
| 134 | + const result = filter(diagnostics, lines); |
| 135 | + |
| 136 | + expect(result).to.have.length(1); |
| 137 | + expect(result[0].message).to.equal('Missing required property "name".'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should match specifiers case-insensitively', () => { |
| 141 | + const lines = linesOf(['# yaml-language-server-disable incorrect TYPE', 'key: bad']); |
| 142 | + const diagnostics = [makeDiag(1, 'Incorrect type. Expected string.')]; |
| 143 | + |
| 144 | + const result = filter(diagnostics, lines); |
| 145 | + |
| 146 | + expect(result).to.be.empty; |
| 147 | + }); |
| 148 | + |
| 149 | + it('should keep diagnostics on lines NOT preceded by a disable comment', () => { |
| 150 | + const lines = linesOf(['name: hello', '# yaml-language-server-disable', 'age: bad', 'score: bad']); |
| 151 | + const diagnostics = [makeDiag(2, 'error on line 2'), makeDiag(3, 'error on line 3')]; |
| 152 | + |
| 153 | + const result = filter(diagnostics, lines); |
| 154 | + |
| 155 | + expect(result).to.have.length(1); |
| 156 | + expect(result[0].message).to.equal('error on line 3'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not filter a diagnostic on line 0 (no preceding line)', () => { |
| 160 | + const lines = linesOf(['bad: value']); |
| 161 | + const diagnostics = [makeDiag(0, 'error on first line')]; |
| 162 | + |
| 163 | + const result = filter(diagnostics, lines); |
| 164 | + |
| 165 | + expect(result).to.have.length(1); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should handle indented disable comments', () => { |
| 169 | + const lines = linesOf(['root:', ' # yaml-language-server-disable', ' child: bad-value']); |
| 170 | + const diagnostics = [makeDiag(2, 'invalid value')]; |
| 171 | + |
| 172 | + const result = filter(diagnostics, lines); |
| 173 | + |
| 174 | + expect(result).to.be.empty; |
| 175 | + }); |
| 176 | + |
| 177 | + it('should not suppress when the disable comment is two lines above', () => { |
| 178 | + const lines = linesOf(['# yaml-language-server-disable', 'good: value', 'bad: value']); |
| 179 | + const diagnostics = [makeDiag(2, 'error on line 2')]; |
| 180 | + |
| 181 | + const result = filter(diagnostics, lines); |
| 182 | + |
| 183 | + expect(result).to.have.length(1); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should handle multiple disable comments for different lines', () => { |
| 187 | + const lines = linesOf([ |
| 188 | + '# yaml-language-server-disable', |
| 189 | + 'line1: bad', |
| 190 | + 'line2: ok', |
| 191 | + '# yaml-language-server-disable', |
| 192 | + 'line4: also-bad', |
| 193 | + ]); |
| 194 | + const diagnostics = [makeDiag(1, 'error on line 1'), makeDiag(2, 'error on line 2'), makeDiag(4, 'error on line 4')]; |
| 195 | + |
| 196 | + const result = filter(diagnostics, lines); |
| 197 | + |
| 198 | + expect(result).to.have.length(1); |
| 199 | + expect(result[0].message).to.equal('error on line 2'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should return all diagnostics when the document cannot be read', () => { |
| 203 | + const noDocument: GetLineText = () => undefined; |
| 204 | + const diagnostics = [makeDiag(1, 'some error')]; |
| 205 | + |
| 206 | + const result = filter(diagnostics, noDocument); |
| 207 | + |
| 208 | + expect(result).to.have.length(1); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should return an empty array when given no diagnostics', () => { |
| 212 | + const lines = linesOf(['# yaml-language-server-disable', 'key: value']); |
| 213 | + |
| 214 | + const result = filter([], lines); |
| 215 | + |
| 216 | + expect(result).to.be.empty; |
| 217 | + }); |
| 218 | + |
| 219 | + it('should not treat a non-comment line containing the keyword as suppression', () => { |
| 220 | + const lines = linesOf(['key: yaml-language-server-disable', 'other: bad']); |
| 221 | + const diagnostics = [makeDiag(1, 'error on line 1')]; |
| 222 | + |
| 223 | + const result = filter(diagnostics, lines); |
| 224 | + |
| 225 | + expect(result).to.have.length(1); |
| 226 | + }); |
| 227 | +}); |
0 commit comments