Skip to content

Commit

Permalink
src: remove comments from evn file
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Mar 16, 2024
1 parent 97b2c53 commit 4d101fc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
27 changes: 23 additions & 4 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ using v8::String;
*/
const std::regex LINE(
"\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']"
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?"
":#.*)?"); // NOLINT(whitespace/line_length)
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*");
// NOLINT(whitespace/line_length)

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
Expand Down Expand Up @@ -104,6 +104,7 @@ Local<Object> Dotenv::ToObject(Environment* env) {
void Dotenv::ParseContent(const std::string_view content) {
std::string lines = std::string(content);
lines = std::regex_replace(lines, std::regex("\r\n?"), "\n");
lines = RemoveComments(lines);

std::smatch match;
while (std::regex_search(lines, match, LINE)) {
Expand All @@ -126,7 +127,7 @@ void Dotenv::ParseContent(const std::string_view content) {
}

// Remove surrounding quotes
value = trim_quotes(value);
value = TrimQuotes(value);

store_.insert_or_assign(std::string(key), value);
lines = match.suffix();
Expand Down Expand Up @@ -179,7 +180,7 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
}
}

std::string_view Dotenv::trim_quotes(std::string_view str) {
std::string_view Dotenv::TrimQuotes(std::string_view str) {
static const std::unordered_set<char> quotes = {'"', '\'', '`'};
if (str.size() >= 2 && quotes.count(str.front()) &&
quotes.count(str.back())) {
Expand All @@ -188,4 +189,22 @@ std::string_view Dotenv::trim_quotes(std::string_view str) {
return str;
}

std::string Dotenv::RemoveComments(const std::string& content) {
std::istringstream iss(content);
std::string result;
std::string line;

while (std::getline(iss, line)) {
if (line.empty()) {
continue;
}
// Trim leading whitespace and check if the line starts with '#'
std::string trimmed = line.substr(line.find_first_not_of(" \t"));
if (trimmed.front() != '#') {
result.append(line + "\n");
}
}
return result;
}

} // namespace node
3 changes: 2 additions & 1 deletion src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Dotenv {

private:
std::map<std::string, std::string> store_;
std::string_view trim_quotes(std::string_view str);
std::string_view TrimQuotes(std::string_view str);
std::string RemoveComments(const std::string& str);
};

} // namespace node
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ EXPAND_NEWLINES="expand\nnew\nlines"
DONT_EXPAND_UNQUOTED=dontexpand\nnewlines
DONT_EXPAND_SQUOTED='dontexpand\nnewlines'
# COMMENTS=work
#BASIC=basic2
#BASIC=basic3
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
Expand All @@ -37,7 +39,7 @@ TRIM_SPACE_FROM_UNQUOTED= some spaced out string
EMAIL=[email protected]
SPACED_KEY = parsed
EDGE_CASE_INLINE_COMMENTS="VALUE1" # or "VALUE2" or "VALUE3"

export EXAMPLE = ignore export
MULTI_DOUBLE_QUOTED="THIS
IS
A
Expand All @@ -58,4 +60,3 @@ STRING`
MULTI_NOT_VALID_QUOTE="
MULTI_NOT_VALID=THIS
IS NOT MULTILINE
export EXAMPLE = ignore export

0 comments on commit 4d101fc

Please sign in to comment.