Skip to content

Commit

Permalink
feat: unescape escaped characters when value is not quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 15, 2021
1 parent 9e101a5 commit 4b1899f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/dotenv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,9 @@ def parse_line(line)
def parse_value(value)
# Remove surrounding quotes
value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')

if Regexp.last_match(1) == '"'
value = unescape_characters(expand_newlines(value))
end

if Regexp.last_match(1) != "'"
self.class.substitutions.each do |proc|
value = proc.call(value, @hash, @is_load)
end
end
maybe_quote = Regexp.last_match(1)
value = unescape_value(value, maybe_quote)
value = perform_substitutions(value, maybe_quote)
value
end

Expand All @@ -94,5 +87,24 @@ def expand_newlines(value)
def variable_not_set?(line)
!line.split[1..-1].all? { |var| @hash.member?(var) }
end

def unescape_value(value, maybe_quote)
if maybe_quote == '"'
unescape_characters(expand_newlines(value))
elsif maybe_quote.nil?
unescape_characters(value)
else
value
end
end

def perform_substitutions(value, maybe_quote)
if maybe_quote != "'"
self.class.substitutions.each do |proc|
value = proc.call(value, @hash, @is_load)
end
end
value
end
end
end
4 changes: 4 additions & 0 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def env(string)
expect(env("FOO= bar")).to eql("FOO" => "bar")
end

it "parses unquoted escape characters correctly" do
expect(env("FOO=bar\\ bar")).to eql("FOO" => "bar bar")
end

it "parses values with spaces around equal sign" do
expect(env("FOO =bar")).to eql("FOO" => "bar")
expect(env("FOO= bar")).to eql("FOO" => "bar")
Expand Down

0 comments on commit 4b1899f

Please sign in to comment.