Skip to content

Commit bb98cb9

Browse files
committed
test: add loc-functions cases
1 parent 0bfcefa commit bb98cb9

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎packages/sh/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing, taking various rules into account.
1010

11-
This plugin adds support for a lot of files through [mvdan-sh][] via [sh-syntax][] and [dockerfmt][].
11+
This plugin adds support for various file formats through [mvdan-sh][] via [sh-syntax][] and [dockerfmt][].
1212

1313
## Notice
1414

@@ -18,7 +18,7 @@ Of course it should just work, but may not match [prettier][]'s format sometimes
1818
> [!WARNING]
1919
>
2020
> `ignore` files are not officially supported by [mvdan-sh][] what means only basic and simple `ignore` usage cases can be handled correctly.
21-
> We can not do much on our side. See also <https://github.com/un-ts/prettier/issues/336>.
21+
> We cannot do much on our side. See also <https://github.com/un-ts/prettier/issues/336>.
2222
2323
## Requirements
2424

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { ParserOptions } from 'prettier'
2+
import type { Node } from 'sh-syntax'
3+
4+
import { parsers } from 'prettier-plugin-sh'
5+
6+
describe('Parser location functions', () => {
7+
const { locStart, locEnd } = parsers.sh
8+
9+
it('should return correct offsets for simple shell scripts', async () => {
10+
const script = '#!/bin/sh\necho "Hello, world!"'
11+
const node = await parsers.sh.parse(script, {
12+
filepath: 'test.sh',
13+
locStart,
14+
locEnd,
15+
} as ParserOptions<Node>)
16+
17+
// Root node should start at position 0
18+
expect(locStart(node)).toBe(0)
19+
20+
// Root node should end at the script length
21+
expect(locEnd(node)).toBe(script.length)
22+
})
23+
24+
it('should handle scripts with various syntax constructs', async () => {
25+
const scripts = [
26+
// Simple command
27+
'echo "simple"',
28+
29+
// If statement
30+
'if [ "$1" = "test" ]; then\n echo "test"\nfi',
31+
32+
// Function definition
33+
'function say_hello() {\n echo "Hello"\n}',
34+
]
35+
36+
for (const script of scripts) {
37+
const node = await parsers.sh.parse(script, {
38+
filepath: 'test.sh',
39+
locStart,
40+
locEnd,
41+
} as ParserOptions<Node>)
42+
43+
expect(locStart(node)).toBe(0)
44+
expect(locEnd(node)).toBe(script.length)
45+
}
46+
})
47+
})

0 commit comments

Comments
 (0)