Skip to content

Commit 75a8ae7

Browse files
author
Aurelien Guerre Chaley
committed
fix the case with escaped quote and bad comment after the end of value
1 parent 33bf94a commit 75a8ae7

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

js/DotEnvParser.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ class DotEnvParser {
1313
}
1414

1515
static removeComments(str) {
16-
if (str.includes('="') && str.endsWith('"')) {
17-
return str;
16+
if (str.includes('="')) {
17+
if(str.endsWith('"')) {
18+
return str;
19+
} else {
20+
const values = /(.*="(?:[^"\\]|\\.)*")(.*)/.exec(str);
21+
if (!values[2].trim().startsWith('#')) {
22+
throw new Error(`incorrect line ${str}`)
23+
}
24+
return values[1].replace('\\"','"');
25+
}
1826
} else {
1927
return str.replace(/#.*/, '');
2028
}

test/envsub-global/envFile9.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
NOT_A_COMMENT="Daniel#not_a_comment"
1+
NOT_A_COMMENT="Daniel#not_a_comment\"" #comment
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
xxxDaniel#not_a_commentxxx xxxDaniel#not_a_commentxxx
2-
xxxDaniel#not_a_commentxxx xxxDaniel#not_a_commentxxx
1+
xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx
2+
xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx

0 commit comments

Comments
 (0)