Parser accepts simple unquoted vars with spaces

master
Dominik Dzienia 2 years ago
parent fbff65b29d
commit 2fcea26577

@ -1,4 +1,4 @@
The command line tool to read, manipulate and update .env files.
The command line tool and library to read, manipulate and update .env files.
## Features

@ -9,7 +9,11 @@ describe('MultiLine format parser', () => {
expect(parseMultiLine('VARNAME=value \t ')).toEqual([vtoken('VARNAME', 'value', '', '', '', ' \t ')]);
expect(parseMultiLine('VARNAME=A:\\DYNA')).toEqual([vtoken('VARNAME', 'A:\\DYNA')]);
expect(parseMultiLine('VARNAME=quote\'is"ok')).toEqual([vtoken('VARNAME', 'quote\'is"ok')]);
expect(() => parseMultiLine('VARNAME=value not escaped')).toThrow('PARSING FAILED');
expect(parseMultiLine('VARNAME=value not escaped')).toEqual([vtoken('VARNAME', 'value not escaped')]);
expect(parseMultiLine('VARNAME= value not escaped')).toEqual([vtoken('VARNAME', 'value not escaped', '', '', '', '', '= ')]);
expect(parseMultiLine('VARNAME=value not escaped ')).toEqual([vtoken('VARNAME', 'value not escaped', '', '', '', ' ')]);
expect(parseMultiLine('VARNAME=this# is# value')).toEqual([vtoken('VARNAME', 'this# is# value')]);
expect(parseMultiLine('VARNAME= this# is# value # and comment')).toEqual([vtoken('VARNAME', 'this# is# value', '', ' # and comment', '', '', '= ')]);
});
it('prop have defined names', () => {

@ -13,15 +13,14 @@ const escapedStrSQ = nonEscapedValuePartSQ.or(escapedChar.map((v: string) => v.r
const nonEscapedValuePartDQ = Parsimmon.regexp(/[^"\\]+/);
const escapedStrDQ = nonEscapedValuePartDQ.or(escapedChar.map((v: string) => v.replace('\\"', '"').replace('\\\\', '\\'))).many();
// to alow spaces in simple values: [^"'\s][^\s]*([^\S\r\n]+\S+)* -- but that wont work in BASH so we do not allowed unescaped spaces!
const simpleValueWithoutSpacesAndQuotes = Parsimmon.regexp(/[^"'\s][^\s]*/);
const simpleValueNotQuoted = Parsimmon.regexp(/[^"'\s][^\s]*([^\S\r\n]+[^\s#][^\s]*)*/);
const valueWithSpacesDoubleQuoted = Parsimmon.seqObj<string, any>(['quote', doubleQuote], ['value', escapedStrDQ.map((v) => v.join(''))], doubleQuote);
const valueWithSpacesSingleQuoted = Parsimmon.seqObj<string, any>(['quote', singleQuote], ['value', escapedStrSQ.map((v) => v.join(''))], singleQuote);
const value = Parsimmon.alt(
valueWithSpacesSingleQuoted,
valueWithSpacesDoubleQuoted,
simpleValueWithoutSpacesAndQuotes.map((v) => ({ quote: '', value: v })),
simpleValueNotQuoted.map((v) => ({ quote: '', value: v })),
).desc('property value');
const commentOpt = Parsimmon.regexp(/[^\S\r\n]+#[^\r\n]*/);
const comment = Parsimmon.regexp(/[\n\r]*[^\S\r\n]*#[^\r\n]*/);

Loading…
Cancel
Save