@futpib/parser 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/bashParser.js +317 -75
- package/build/bashParser.test.js +71 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/predicateElementParser.d.ts +3 -0
- package/build/predicateElementParser.js +10 -0
- package/package.json +1 -1
- package/src/bashParser.test.ts +138 -0
- package/src/bashParser.ts +467 -139
- package/src/index.ts +4 -0
- package/src/predicateElementParser.ts +22 -0
package/src/bashParser.test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import test from 'ava';
|
|
|
2
2
|
import { runParser, runParserWithRemainingInput } from './parser.js';
|
|
3
3
|
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
4
4
|
import { bashScriptParser, bashWordParser, bashSimpleCommandParser } from './bashParser.js';
|
|
5
|
+
import type { BashSimpleCommand, BashWordPartLiteral } from './bash.js';
|
|
5
6
|
|
|
6
7
|
test('simple command parser - single word', async t => {
|
|
7
8
|
const result = await runParser(
|
|
@@ -588,3 +589,140 @@ test('if treated as command name', async t => {
|
|
|
588
589
|
t.deepEqual(cmd.name, { parts: [{ type: 'literal', value: 'if' }] });
|
|
589
590
|
}
|
|
590
591
|
});
|
|
592
|
+
|
|
593
|
+
test('find -exec with {} placeholder', async t => {
|
|
594
|
+
const result = await runParser(
|
|
595
|
+
bashScriptParser,
|
|
596
|
+
'find . -name "*.tmp" -exec rm {} \\;',
|
|
597
|
+
stringParserInputCompanion,
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
601
|
+
t.is(cmd.name!.parts[0].type, 'literal');
|
|
602
|
+
t.is((cmd.name!.parts[0] as BashWordPartLiteral).value, 'find');
|
|
603
|
+
// {} should be parsed as a literal word argument
|
|
604
|
+
const braceArg = cmd.args[5]; // ., -name, "*.tmp", -exec, rm, {}, \;
|
|
605
|
+
t.is(braceArg.parts[0].type, 'literal');
|
|
606
|
+
t.is((braceArg.parts[0] as BashWordPartLiteral).value, '{}');
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
test('lone open brace as argument', async t => {
|
|
610
|
+
const result = await runParser(
|
|
611
|
+
bashScriptParser,
|
|
612
|
+
'echo {',
|
|
613
|
+
stringParserInputCompanion,
|
|
614
|
+
);
|
|
615
|
+
|
|
616
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
617
|
+
t.deepEqual(cmd.args[0], {
|
|
618
|
+
parts: [{ type: 'literal', value: '{' }],
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
test('close brace mid-word', async t => {
|
|
623
|
+
const result = await runParser(
|
|
624
|
+
bashScriptParser,
|
|
625
|
+
'echo foo}bar',
|
|
626
|
+
stringParserInputCompanion,
|
|
627
|
+
);
|
|
628
|
+
|
|
629
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
630
|
+
t.is(cmd.args.length, 1);
|
|
631
|
+
t.is(cmd.args[0].parts[0].type, 'literal');
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
test('open brace mid-word', async t => {
|
|
635
|
+
const result = await runParser(
|
|
636
|
+
bashScriptParser,
|
|
637
|
+
'echo foo{bar',
|
|
638
|
+
stringParserInputCompanion,
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
642
|
+
t.is(cmd.args.length, 1);
|
|
643
|
+
t.is(cmd.args[0].parts[0].type, 'literal');
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
test('braces mid-word like brace expansion', async t => {
|
|
647
|
+
const result = await runParser(
|
|
648
|
+
bashScriptParser,
|
|
649
|
+
'echo file.{c,h}',
|
|
650
|
+
stringParserInputCompanion,
|
|
651
|
+
);
|
|
652
|
+
|
|
653
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
654
|
+
t.is(cmd.args.length, 1);
|
|
655
|
+
t.is(cmd.args[0].parts[0].type, 'literal');
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
test('find -exec with {.} placeholder variant', async t => {
|
|
659
|
+
const result = await runParser(
|
|
660
|
+
bashScriptParser,
|
|
661
|
+
'echo {.}',
|
|
662
|
+
stringParserInputCompanion,
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
666
|
+
t.is(cmd.args.length, 1);
|
|
667
|
+
t.is(cmd.args[0].parts[0].type, 'literal');
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
test('lone close brace as argument', async t => {
|
|
671
|
+
const result = await runParser(
|
|
672
|
+
bashScriptParser,
|
|
673
|
+
'echo }',
|
|
674
|
+
stringParserInputCompanion,
|
|
675
|
+
);
|
|
676
|
+
|
|
677
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
678
|
+
t.deepEqual(cmd.args[0], {
|
|
679
|
+
parts: [{ type: 'literal', value: '}' }],
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
test('close brace at start of word', async t => {
|
|
684
|
+
const result = await runParser(
|
|
685
|
+
bashScriptParser,
|
|
686
|
+
'echo }hello',
|
|
687
|
+
stringParserInputCompanion,
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
691
|
+
t.is(cmd.args.length, 1);
|
|
692
|
+
t.is(cmd.args[0].parts[0].type, 'literal');
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
test('multi-line script with blank lines', async t => {
|
|
696
|
+
const result = await runParser(
|
|
697
|
+
bashScriptParser,
|
|
698
|
+
'echo hello\n\necho world',
|
|
699
|
+
stringParserInputCompanion,
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
t.is(result.entries.length, 2);
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
test('mid-script comment', async t => {
|
|
706
|
+
const result = await runParser(
|
|
707
|
+
bashScriptParser,
|
|
708
|
+
'echo hello\n# comment\necho world',
|
|
709
|
+
stringParserInputCompanion,
|
|
710
|
+
);
|
|
711
|
+
|
|
712
|
+
t.is(result.entries.length, 2);
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
test('nested parentheses in arithmetic expansion', async t => {
|
|
716
|
+
const result = await runParser(
|
|
717
|
+
bashScriptParser,
|
|
718
|
+
'echo $((1 + (2 * 3)))',
|
|
719
|
+
stringParserInputCompanion,
|
|
720
|
+
);
|
|
721
|
+
|
|
722
|
+
const cmd = result.entries[0].pipeline.commands[0] as BashSimpleCommand;
|
|
723
|
+
const arith = cmd.args[0].parts[0];
|
|
724
|
+
t.is(arith.type, 'arithmeticExpansion');
|
|
725
|
+
if (arith.type === 'arithmeticExpansion') {
|
|
726
|
+
t.is(arith.expression, '1 + (2 * 3)');
|
|
727
|
+
}
|
|
728
|
+
});
|