|
| 1 | +import { parseShellCheckDirective } from '../directive' |
| 2 | + |
| 3 | +describe('parseShellCheckDirective', () => { |
| 4 | + it('parses a disable directive', () => { |
| 5 | + expect(parseShellCheckDirective('# shellcheck disable=SC1000')).toEqual([ |
| 6 | + { |
| 7 | + type: 'disable', |
| 8 | + rules: ['SC1000'], |
| 9 | + }, |
| 10 | + ]) |
| 11 | + }) |
| 12 | + |
| 13 | + it('parses a disable directive with multiple args', () => { |
| 14 | + expect(parseShellCheckDirective('# shellcheck disable=SC1000,SC1001')).toEqual([ |
| 15 | + { |
| 16 | + type: 'disable', |
| 17 | + rules: ['SC1000', 'SC1001'], |
| 18 | + }, |
| 19 | + ]) |
| 20 | + |
| 21 | + expect( |
| 22 | + parseShellCheckDirective( |
| 23 | + '# shellcheck disable=SC1000,SC2000-SC2002,SC1001 # this is a comment', |
| 24 | + ), |
| 25 | + ).toEqual([ |
| 26 | + { |
| 27 | + type: 'disable', |
| 28 | + rules: ['SC1000', 'SC2000', 'SC2001', 'SC2002', 'SC1001'], |
| 29 | + }, |
| 30 | + ]) |
| 31 | + |
| 32 | + expect(parseShellCheckDirective('# shellcheck disable=SC1000,SC1001')).toEqual([ |
| 33 | + { |
| 34 | + type: 'disable', |
| 35 | + rules: ['SC1000', 'SC1001'], |
| 36 | + }, |
| 37 | + ]) |
| 38 | + |
| 39 | + expect(parseShellCheckDirective('# shellcheck disable=SC1000,SC1001')).toEqual([ |
| 40 | + { |
| 41 | + type: 'disable', |
| 42 | + rules: ['SC1000', 'SC1001'], |
| 43 | + }, |
| 44 | + ]) |
| 45 | + }) |
| 46 | + |
| 47 | + // SC1000-SC9999 |
| 48 | + it('parses a disable directive with a range', () => { |
| 49 | + expect(parseShellCheckDirective('# shellcheck disable=SC1000-SC1005')).toEqual([ |
| 50 | + { |
| 51 | + type: 'disable', |
| 52 | + rules: ['SC1000', 'SC1001', 'SC1002', 'SC1003', 'SC1004', 'SC1005'], |
| 53 | + }, |
| 54 | + ]) |
| 55 | + }) |
| 56 | + |
| 57 | + it('parses a disable directive with all', () => { |
| 58 | + expect(parseShellCheckDirective('# shellcheck disable=all')).toEqual([ |
| 59 | + { |
| 60 | + type: 'disable', |
| 61 | + rules: ['all'], |
| 62 | + }, |
| 63 | + ]) |
| 64 | + }) |
| 65 | + |
| 66 | + it('parses an enable directive', () => { |
| 67 | + expect( |
| 68 | + parseShellCheckDirective('# shellcheck enable=require-variable-braces'), |
| 69 | + ).toEqual([ |
| 70 | + { |
| 71 | + type: 'enable', |
| 72 | + rules: ['require-variable-braces'], |
| 73 | + }, |
| 74 | + ]) |
| 75 | + }) |
| 76 | + |
| 77 | + it('parses source directive', () => { |
| 78 | + expect(parseShellCheckDirective('# shellcheck source=foo.sh')).toEqual([ |
| 79 | + { |
| 80 | + type: 'source', |
| 81 | + path: 'foo.sh', |
| 82 | + }, |
| 83 | + ]) |
| 84 | + |
| 85 | + expect(parseShellCheckDirective('# shellcheck source=/dev/null # a comment')).toEqual( |
| 86 | + [ |
| 87 | + { |
| 88 | + type: 'source', |
| 89 | + path: '/dev/null', |
| 90 | + }, |
| 91 | + ], |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('parses source-path directive', () => { |
| 96 | + expect(parseShellCheckDirective('# shellcheck source-path=src/examples')).toEqual([ |
| 97 | + { |
| 98 | + type: 'source-path', |
| 99 | + path: 'src/examples', |
| 100 | + }, |
| 101 | + ]) |
| 102 | + |
| 103 | + expect(parseShellCheckDirective('# shellcheck source-path=SCRIPTDIR')).toEqual([ |
| 104 | + { |
| 105 | + type: 'source-path', |
| 106 | + path: 'SCRIPTDIR', |
| 107 | + }, |
| 108 | + ]) |
| 109 | + }) |
| 110 | + |
| 111 | + it('supports multiple directives on the same line', () => { |
| 112 | + expect( |
| 113 | + parseShellCheckDirective( |
| 114 | + `# shellcheck cats=dogs disable=SC1234,SC2345 enable="foo" shell=bash`, |
| 115 | + ), |
| 116 | + ).toEqual([ |
| 117 | + { |
| 118 | + type: 'disable', |
| 119 | + rules: ['SC1234', 'SC2345'], |
| 120 | + }, |
| 121 | + { |
| 122 | + type: 'enable', |
| 123 | + rules: ['"foo"'], |
| 124 | + }, |
| 125 | + { |
| 126 | + type: 'shell', |
| 127 | + shell: 'bash', |
| 128 | + }, |
| 129 | + ]) |
| 130 | + }) |
| 131 | + |
| 132 | + it('parses a line with no directive', () => { |
| 133 | + expect(parseShellCheckDirective('# foo bar')).toEqual([]) |
| 134 | + }) |
| 135 | +}) |
0 commit comments