From 7392c09323921844e04d4aec1d5a76a98e5523ab Mon Sep 17 00:00:00 2001 From: Jonathan Reisdorf Date: Tue, 25 Jan 2022 11:08:32 +0100 Subject: [PATCH] Interpret # as start of comment only if preceded by whitespace --- CHANGELOG.md | 2 ++ README.md | 1 + lib/main.js | 2 +- tests/.env | 1 + tests/test-parse.js | 4 +++- 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a2cac42..74484259 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. See [standa ## [Unreleased](https://github.com/motdotla/dotenv/compare/v14.3.0...master) +- Interpret `#` as start of comment only if preceded by whitespace + ## [14.3.0](https://github.com/motdotla/dotenv/compare/v14.2.0...v14.3.0) - Add `multiline` option 🎉 ([#486](https://github.com/motdotla/dotenv/pull/486)) diff --git a/README.md b/README.md index 87f8032e..86b67b4b 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,7 @@ The parsing engine currently supports the following rules: - `BASIC=basic` becomes `{BASIC: 'basic'}` - empty lines are skipped - lines beginning with `#` are treated as comments +- whitespace followed by `#` marks the beginning of an inline comment (unless when the value is wrapped in quotes) - empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`) - inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`) - whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`) diff --git a/lib/main.js b/lib/main.js index d63b2edd..5eb37060 100644 --- a/lib/main.js +++ b/lib/main.js @@ -7,7 +7,7 @@ function log (message) { } const NEWLINE = '\n' -const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*("[^"]*"|'[^']*'|[^#]*)?(\s*|\s*#.*)?$/ +const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*("[^"]*"|'[^']*'|.*?)(\s+#.*)?$/ const RE_NEWLINES = /\\n/g const NEWLINES_MATCH = /\r\n|\n|\r/ diff --git a/tests/.env b/tests/.env index 0fdabc97..30992572 100644 --- a/tests/.env +++ b/tests/.env @@ -16,6 +16,7 @@ DONT_EXPAND_SQUOTED='dontexpand\nnewlines' INLINE_COMMENTS=inline comments # work #very #well INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work +INLINE_COMMENTS_SPACE=inline comments must start with#space EQUAL_SIGNS=equals== RETAIN_INNER_QUOTES={"foo": "bar"} RETAIN_LEADING_DQUOTE="retained diff --git a/tests/test-parse.js b/tests/test-parse.js index 7d0d3bc3..f6859353 100644 --- a/tests/test-parse.js +++ b/tests/test-parse.js @@ -7,7 +7,7 @@ const dotenv = require('../lib/main') const parsed = dotenv.parse(fs.readFileSync('tests/.env', { encoding: 'utf8' })) -t.plan(34) +t.plan(35) t.type(parsed, Object, 'should return an object') @@ -43,6 +43,8 @@ t.equal(parsed.INLINE_COMMENTS_SINGLE_QUOTES, 'inline comments outside of #singl t.equal(parsed.INLINE_COMMENTS_DOUBLE_QUOTES, 'inline comments outside of #doublequotes', 'ignores inline comments, but respects # character inside of double quotes') +t.equal(parsed.INLINE_COMMENTS_SPACE, 'inline comments must start with#space', 'respects # character in values when it is not preceded by a space character') + t.equal(parsed.EQUAL_SIGNS, 'equals==', 'respects equals signs in values') t.equal(parsed.RETAIN_INNER_QUOTES, '{"foo": "bar"}', 'retains inner quotes')