Skip to content

Commit

Permalink
Interpret # as start of comment only if preceded by whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Reisdorf authored and jonathan-reisdorf committed Jan 25, 2022
1 parent a1bf6c1 commit f0508c0
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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'}`)
Expand Down
2 changes: 1 addition & 1 deletion lib/main.js
Expand Up @@ -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/

Expand Down
1 change: 1 addition & 0 deletions tests/.env
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test-parse.js
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit f0508c0

Please sign in to comment.