forked from un-ts/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sh): add support for file pragmas
Closes un-ts#377
- Loading branch information
1 parent
a818d8f
commit 6a3e2a9
Showing
5 changed files
with
238 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'prettier-plugin-sh': minor | ||
--- | ||
|
||
add support for file pragmas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { stripIndent } from 'common-tags' | ||
import { describe, it, assert, expect } from 'vitest' | ||
|
||
import ShPlugin from 'prettier-plugin-sh' | ||
|
||
describe('parser', () => { | ||
const hasPragma = ShPlugin.parsers?.sh?.hasPragma | ||
assert(hasPragma != null) | ||
|
||
describe('should detect pragmas', () => { | ||
it('at the top of the file', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# @prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('with extra leading spaces', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# @prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('with no leading space', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
#@prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('with "format" pragma instead', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# @format | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('after leading whitespace', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# @prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('after leading comments', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# Testing! | ||
# | ||
# | ||
# @prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('after a shebang', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
#!/bin/bash | ||
# | ||
# @prettier | ||
FOO="bar" | ||
`), | ||
).toBeTruthy() | ||
}) | ||
|
||
it('unless none exist', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
FOO="bar" | ||
`), | ||
).toBeFalsy() | ||
}) | ||
|
||
it('unless the file is empty', () => { | ||
expect(hasPragma('')).toBeFalsy() | ||
}) | ||
|
||
it('unless it comes after real content', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
FOO="bar" | ||
# @prettier | ||
`), | ||
).toBeFalsy() | ||
}) | ||
|
||
it('unless it comes after real content and comments', () => { | ||
expect( | ||
hasPragma(stripIndent` | ||
# Test | ||
#! | ||
FOO="bar" | ||
# @prettier | ||
`), | ||
).toBeFalsy() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.