@@ -12,26 +12,37 @@ export function parseDuckyScript(input: string): DuckyAst {
1212
1313 for ( let i = 0 ; i < lines . length ; i ++ ) {
1414 const raw = lines [ i ] ;
15+ if ( raw == null ) continue ;
1516 const line = raw . trim ( ) ;
1617 if ( ! line || line . startsWith ( 'REM' ) ) continue ;
1718
1819 const [ cmd , ...rest ] = line . split ( / \s + / ) ;
20+ if ( ! cmd ) continue ;
1921 const upper = cmd . toUpperCase ( ) ;
2022
2123 if ( upper === 'STRING' ) {
22- const args = [ raw . slice ( raw . toUpperCase ( ) . indexOf ( 'STRING' ) + 6 ) . trimStart ( ) ] ;
24+ const idx = raw . toUpperCase ( ) . indexOf ( 'STRING' ) ;
25+ const args = [ idx >= 0 ? raw . slice ( idx + 6 ) . trimStart ( ) : '' ] ;
2326 commands . push ( { type : 'STRING' , args } ) ;
2427 continue ;
2528 }
2629
2730 if ( upper === 'DELAY' ) {
31+ if ( ! rest [ 0 ] ) {
32+ errors . push ( `Line ${ i + 1 } : DELAY requires a number` ) ;
33+ continue ;
34+ }
2835 const ms = parseInt ( rest [ 0 ] , 10 ) ;
2936 if ( Number . isFinite ( ms ) ) commands . push ( { type : 'DELAY' , args : [ String ( ms ) ] } ) ;
3037 else errors . push ( `Line ${ i + 1 } : DELAY requires a number` ) ;
3138 continue ;
3239 }
3340
3441 if ( upper === 'REPEAT' ) {
42+ if ( ! rest [ 0 ] ) {
43+ errors . push ( `Line ${ i + 1 } : REPEAT requires positive number` ) ;
44+ continue ;
45+ }
3546 const n = parseInt ( rest [ 0 ] , 10 ) ;
3647 if ( ! Number . isFinite ( n ) || n < 1 ) {
3748 errors . push ( `Line ${ i + 1 } : REPEAT requires positive number` ) ;
@@ -42,7 +53,7 @@ export function parseDuckyScript(input: string): DuckyAst {
4253 continue ;
4354 }
4455 const last = commands [ commands . length - 1 ] ;
45- for ( let j = 0 ; j < n ; j ++ ) commands . push ( { ... last } ) ;
56+ for ( let j = 0 ; j < n ; j ++ ) commands . push ( { type : last . type , args : [ ... last . args ] } ) ;
4657 continue ;
4758 }
4859
0 commit comments