Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: Remove empty multiline string parts earlier #11120

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/libexpr/parser-state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,23 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
s2 = std::string(s2, 0, p + 1);
}

es2->emplace_back(i->first, new ExprString(std::move(s2)));
// Ignore empty strings for a minor optimisation and AST simplification
if (s2 != "") {
es2->emplace_back(i->first, new ExprString(std::move(s2)));
}
};
for (; i != es.end(); ++i, --n) {
std::visit(overloaded { trimExpr, trimString }, i->second);
}

// If there is nothing at all, return the empty string directly.
// This also ensures that equivalent empty strings result in the same ast, which is helpful when testing formatters.
if (es2->size() == 0) {
auto *const result = new ExprString("");
delete es2;
return result;
}

/* If this is a single string, then don't do a concatenation. */
if (es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0].second)) {
auto *const result = (*es2)[0].second;
Expand Down
1 change: 1 addition & 0 deletions tests/functional/lang/parse-okay-ind-string.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(let string = "str"; in [ (/some/path) ((/some/path)) ((/some/path)) ((/some/path + "\n end")) (string) ((string)) ((string)) ((string + "\n end")) ("") ("") ("end") ])
31 changes: 31 additions & 0 deletions tests/functional/lang/parse-okay-ind-string.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let
string = "str";
in [
/some/path

''${/some/path}''

''
${/some/path}''

''${/some/path}
end''

string

''${string}''

''
${string}''

''${string}
end''

''''

''
''

''
end''
]
Loading