@@ -3,41 +3,45 @@ import type { Node } from 'sh-syntax'
33
44import { parsers } from 'prettier-plugin-sh'
55
6+ const parseScript = async ( script : string ) =>
7+ parsers . sh . parse ( script , {
8+ filepath : 'test.sh' ,
9+ } as ParserOptions < Node > )
10+
611describe ( 'Parser location functions' , ( ) => {
712 const { locStart, locEnd } = parsers . sh
813
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- } as ParserOptions < Node > )
14+ it ( 'should handle simple commands' , async ( ) => {
15+ const script = 'echo "simple"'
16+ const node = await parseScript ( script )
1417
15- // Root node should start at position 0
1618 expect ( locStart ( node ) ) . toBe ( 0 )
17-
18- // Root node should end at the script length
1919 expect ( locEnd ( node ) ) . toBe ( script . length )
2020 } )
2121
22- it ( 'should handle scripts with various syntax constructs' , async ( ) => {
23- const scripts = [
24- // Simple command
25- 'echo "simple"' ,
22+ it ( 'should handle if statements' , async ( ) => {
23+ const script = 'if [ "$1" = "test" ]; then\n echo "test"\nfi'
24+ const node = await parseScript ( script )
2625
27- // If statement
28- 'if [ "$1" = "test" ]; then\n echo "test"\nfi' ,
26+ expect ( locStart ( node ) ) . toBe ( 0 )
27+ expect ( locEnd ( node ) ) . toBe ( script . length )
28+ } )
2929
30- // Function definition
31- 'function say_hello() {\n echo "Hello"\n}' ,
32- ]
30+ it ( 'should handle function definitions' , async ( ) => {
31+ const script = 'function say_hello() {\n echo "Hello"\n}'
32+ const node = await parseScript ( script )
3333
34- for ( const script of scripts ) {
35- const node = await parsers . sh . parse ( script , {
36- filepath : 'test.sh' ,
37- } as ParserOptions < Node > )
34+ expect ( locStart ( node ) ) . toBe ( 0 )
35+ expect ( locEnd ( node ) ) . toBe ( script . length )
36+ } )
37+
38+ it ( 'should handle more complex constructs' , async ( ) => {
39+ // Add test for a more complex example, e.g. with loops and nesting
40+ const script =
41+ 'for i in $(seq 1 3); do\n if [ "$i" -eq 2 ]; then\n echo "Found $i"\n fi\ndone'
42+ const node = await parseScript ( script )
3843
39- expect ( locStart ( node ) ) . toBe ( 0 )
40- expect ( locEnd ( node ) ) . toBe ( script . length )
41- }
44+ expect ( locStart ( node ) ) . toBe ( 0 )
45+ expect ( locEnd ( node ) ) . toBe ( script . length )
4246 } )
4347} )
0 commit comments