|
| 1 | +import { stripIndent } from 'common-tags' |
| 2 | +import { describe, it, assert, expect } from 'vitest' |
| 3 | + |
| 4 | +import ShPlugin from 'prettier-plugin-sh' |
| 5 | + |
| 6 | +describe('parser', () => { |
| 7 | + const hasPragma = ShPlugin.parsers?.sh?.hasPragma |
| 8 | + assert(hasPragma != null) |
| 9 | + |
| 10 | + describe('should detect pragmas', () => { |
| 11 | + it('at the top of the file', () => { |
| 12 | + expect( |
| 13 | + hasPragma(stripIndent` |
| 14 | + # @prettier |
| 15 | + FOO="bar" |
| 16 | + `), |
| 17 | + ).toBeTruthy() |
| 18 | + }) |
| 19 | + |
| 20 | + it('with extra leading spaces', () => { |
| 21 | + expect( |
| 22 | + hasPragma(stripIndent` |
| 23 | + # @prettier |
| 24 | + FOO="bar" |
| 25 | + `), |
| 26 | + ).toBeTruthy() |
| 27 | + }) |
| 28 | + |
| 29 | + it('with no leading space', () => { |
| 30 | + expect( |
| 31 | + hasPragma(stripIndent` |
| 32 | + #@prettier |
| 33 | + FOO="bar" |
| 34 | + `), |
| 35 | + ).toBeTruthy() |
| 36 | + }) |
| 37 | + |
| 38 | + it('with "format" pragma instead', () => { |
| 39 | + expect( |
| 40 | + hasPragma(stripIndent` |
| 41 | + # @format |
| 42 | + FOO="bar" |
| 43 | + `), |
| 44 | + ).toBeTruthy() |
| 45 | + }) |
| 46 | + |
| 47 | + it('after leading whitespace', () => { |
| 48 | + expect( |
| 49 | + hasPragma(stripIndent` |
| 50 | +
|
| 51 | +
|
| 52 | + # @prettier |
| 53 | + FOO="bar" |
| 54 | + `), |
| 55 | + ).toBeTruthy() |
| 56 | + }) |
| 57 | + |
| 58 | + it('after leading comments', () => { |
| 59 | + expect( |
| 60 | + hasPragma(stripIndent` |
| 61 | + # Testing! |
| 62 | +
|
| 63 | + # |
| 64 | + # |
| 65 | + # @prettier |
| 66 | + FOO="bar" |
| 67 | + `), |
| 68 | + ).toBeTruthy() |
| 69 | + }) |
| 70 | + |
| 71 | + it('after a shebang', () => { |
| 72 | + expect( |
| 73 | + hasPragma(stripIndent` |
| 74 | + #!/bin/bash |
| 75 | + # |
| 76 | +
|
| 77 | + # @prettier |
| 78 | + FOO="bar" |
| 79 | + `), |
| 80 | + ).toBeTruthy() |
| 81 | + }) |
| 82 | + |
| 83 | + it('unless none exist', () => { |
| 84 | + expect( |
| 85 | + hasPragma(stripIndent` |
| 86 | + FOO="bar" |
| 87 | + `), |
| 88 | + ).toBeFalsy() |
| 89 | + }) |
| 90 | + |
| 91 | + it('unless the file is empty', () => { |
| 92 | + expect(hasPragma('')).toBeFalsy() |
| 93 | + }) |
| 94 | + |
| 95 | + it('unless it comes after real content', () => { |
| 96 | + expect( |
| 97 | + hasPragma(stripIndent` |
| 98 | + FOO="bar" |
| 99 | + # @prettier |
| 100 | + `), |
| 101 | + ).toBeFalsy() |
| 102 | + }) |
| 103 | + |
| 104 | + it('unless it comes after real content and comments', () => { |
| 105 | + expect( |
| 106 | + hasPragma(stripIndent` |
| 107 | +
|
| 108 | + # Test |
| 109 | + #! |
| 110 | + FOO="bar" |
| 111 | + # @prettier |
| 112 | + `), |
| 113 | + ).toBeFalsy() |
| 114 | + }) |
| 115 | + }) |
| 116 | +}) |
0 commit comments