Skip to content

Commit

Permalink
[New] Add support for here strings (<<<)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkoskela authored and ljharb committed Jan 29, 2023
1 parent 216b198 commit 9802fb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// '<(' is process substitution operator and
// can be parsed the same as control operator
var CONTROL = '(?:' + [
'\\|\\|', '\\&\\&', ';;', '\\|\\&', '\\<\\(', '>>', '>\\&', '<\\&', '[&;()|<>]'
'\\|\\|', '\\&\\&', ';;', '\\|\\&', '\\<\\(', '\\<\\<\\<', '>>', '>\\&', '<\\&', '[&;()|<>]'
].join('|') + ')';
var META = '|&;()<> \\t';
var BAREWORD = '(\\\\[\'"' + META + ']|[^\\s\'"' + META + '])+';
Expand Down
9 changes: 9 additions & 0 deletions test/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ test('expand environment variables', function (t) {
t.end();
});

test('expand environment variables within here-strings', function (t) {
t.same(parse('a <<< $x', { x: 'Joe' }), ['a', { op: '<<<' }, 'Joe']);
t.same(parse('a <<< ${x}', { x: 'Joe' }), ['a', { op: '<<<' }, 'Joe']);
t.same(parse('a <<< "$x"', { x: 'Joe' }), ['a', { op: '<<<' }, 'Joe']);
t.same(parse('a <<< "${x}"', { x: 'Joe' }), ['a', { op: '<<<' }, 'Joe']);

t.end();
});

test('environment variables with metacharacters', function (t) {
t.same(parse('a $XYZ c', { XYZ: '"b"' }), ['a', '"b"', 'c']);
t.same(parse('a $XYZ c', { XYZ: '$X', X: 5 }), ['a', '$X', 'c']);
Expand Down
9 changes: 9 additions & 0 deletions test/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ test('duplicating input file descriptors', function (t) {
t.end();
});

test('here strings', function (t) {
t.same(parse('cat <<< "hello world"'), ['cat', { op: '<<<' }, 'hello world']);
t.same(parse('cat <<< hello'), ['cat', { op: '<<<' }, 'hello']);
t.same(parse('cat<<<hello'), ['cat', { op: '<<<' }, 'hello']);
t.same(parse('cat<<<"hello world"'), ['cat', { op: '<<<' }, 'hello world']);

t.end();
});

test('glob patterns', function (t) {
t.same(
parse('tap test/*.test.js'),
Expand Down

0 comments on commit 9802fb3

Please sign in to comment.