Skip to content

Commit

Permalink
[Fix] parse: preserve whitespace in comments
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
ljharb committed Apr 7, 2023
1 parent 1d58679 commit ecf2a60
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
30 changes: 25 additions & 5 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ for (var i = 0; i < 4; i++) {
}
var startsWithToken = new RegExp('^' + TOKEN);

function matchAll(s, r) {
var origIndex = r.lastIndex;

var matches = [];
var matchObj;

while ((matchObj = r.exec(s))) {
matches.push(matchObj);
if (r.lastIndex === matchObj.index) {
r.lastIndex += 1;
}
}

r.lastIndex = origIndex;

return matches;
}

function parseInternal(string, env, opts) {
if (!opts) {
opts = {};
Expand All @@ -43,9 +61,9 @@ function parseInternal(string, env, opts) {
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
].join('|'), 'g');

var matches = string.match(chunker);
var matches = matchAll(string, chunker);

if (!matches) {
if (matches.length === 0) {
return [];
}
if (!env) {
Expand All @@ -68,8 +86,9 @@ function parseInternal(string, env, opts) {
return pre + r;
}

return matches.filter(Boolean).map(function (s, j, match) {
if (commented) {
return matches.map(function (match) {
var s = match[0];
if (!s || commented) {
return void undefined;
}
if (controlRE.test(s)) {
Expand Down Expand Up @@ -157,7 +176,7 @@ function parseInternal(string, env, opts) {
return { op: s };
} else if (hash.test(c)) {
commented = true;
var commentObj = { comment: s.slice(i + 1) + match.slice(j + 1).join(' ') };
var commentObj = { comment: string.slice(match.index + i + 1) };
if (out.length) {
return [out, commentObj];
}
Expand All @@ -177,6 +196,7 @@ function parseInternal(string, env, opts) {

return out;
}).reduce(function (prev, arg) { // finalize parsed arguments
// TODO: replace this whole reduce with a concat
return typeof arg === 'undefined' ? prev : prev.concat(arg);
}, []);
}
Expand Down
8 changes: 4 additions & 4 deletions test/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var parse = require('../').parse;
test('comment', function (t) {
t.same(parse('beep#boop'), ['beep', { comment: 'boop' }]);
t.same(parse('beep #boop'), ['beep', { comment: 'boop' }]);
t.same(parse('beep # boop'), ['beep', { comment: 'boop' }]);
t.same(parse('beep # > boop'), ['beep', { comment: '> boop' }]);
t.same(parse('beep # "> boop"'), ['beep', { comment: '"> boop"' }]);
t.same(parse('beep # boop'), ['beep', { comment: ' boop' }]);
t.same(parse('beep # > boop'), ['beep', { comment: ' > boop' }]);
t.same(parse('beep # "> boop"'), ['beep', { comment: ' "> boop"' }]);
t.same(parse('beep "#"'), ['beep', '#']);
t.same(parse('beep #"#"#'), ['beep', { comment: '"#"#' }]);
t.same(parse('beep > boop # > foo'), ['beep', { op: '>' }, 'boop', { comment: '> foo' }]);
t.same(parse('beep > boop # > foo'), ['beep', { op: '>' }, 'boop', { comment: ' > foo' }]);
t.end();
});
3 changes: 3 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ test('parse shell commands', function (t) {
t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);
t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);

t.deepEqual(parse('# abc def ghi'), [{ comment: ' abc def ghi' }], 'start-of-line comment content is unparsed');
t.deepEqual(parse('xyz # abc def ghi'), ['xyz', { comment: ' abc def ghi' }], 'comment content is unparsed');

t.end();
});

0 comments on commit ecf2a60

Please sign in to comment.