Skip to content

Commit de85e21

Browse files
committed
Reduce canonical_path->std::string conversions
1 parent 6f3032c commit de85e21

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/configuration-loader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ configuration* configuration_loader::load_config_file(const char* config_path) {
4646
std::forward_as_tuple());
4747
QLJS_ASSERT(inserted);
4848
configuration* config = &config_it->second;
49-
config->set_config_file_path(canonical_config_path.c_str());
49+
config->set_config_file_path(std::move(canonical_config_path).canonical());
5050
config->load_from_json(&config_json.content);
5151
return config;
5252
}
@@ -93,7 +93,7 @@ configuration* configuration_loader::find_and_load_config_file(
9393
std::forward_as_tuple());
9494
QLJS_ASSERT(inserted);
9595
configuration* config = &config_it->second;
96-
config->set_config_file_path(std::move(config_path).path());
96+
config->set_config_file_path(std::move(config_path));
9797
config->load_from_json(&config_json.content);
9898
return config;
9999
}

src/configuration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const global_declared_variable_set& configuration::globals() noexcept {
136136
return this->globals_;
137137
}
138138

139-
const std::string& configuration::config_file_path() const noexcept {
139+
std::optional<canonical_path> configuration::config_file_path() const {
140140
return this->config_file_path_;
141141
}
142142

@@ -208,7 +208,7 @@ void configuration::load_from_json(padded_string_view json) {
208208
}
209209
}
210210

211-
void configuration::set_config_file_path(std::string&& path) {
211+
void configuration::set_config_file_path(canonical_path&& path) {
212212
this->config_file_path_ = std::move(path);
213213
}
214214

src/quick-lint-js/configuration.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#ifndef QUICK_LINT_JS_CONFIGURATION_H
55
#define QUICK_LINT_JS_CONFIGURATION_H
66

7+
#include <optional>
78
#include <quick-lint-js/char8.h>
9+
#include <quick-lint-js/file-canonical.h>
810
#include <quick-lint-js/lint.h>
911
#include <quick-lint-js/monotonic-allocator.h>
1012
#include <quick-lint-js/padded-string.h>
@@ -16,8 +18,7 @@ class configuration {
1618
public:
1719
const global_declared_variable_set& globals() noexcept;
1820

19-
// If a config file path was not set, this function returns an empty string.
20-
const std::string& config_file_path() const noexcept;
21+
std::optional<canonical_path> config_file_path() const;
2122

2223
void reset_global_groups();
2324
bool add_global_group(string8_view group_name);
@@ -26,7 +27,7 @@ class configuration {
2627
void remove_global_variable(string8_view name);
2728

2829
void load_from_json(padded_string_view);
29-
void set_config_file_path(std::string&&);
30+
void set_config_file_path(canonical_path&&);
3031

3132
private:
3233
void load_global_groups_from_json(simdjson::ondemand::value&);
@@ -36,7 +37,7 @@ class configuration {
3637

3738
global_declared_variable_set globals_;
3839
std::vector<string8> globals_to_remove_;
39-
std::string config_file_path_;
40+
std::optional<canonical_path> config_file_path_;
4041
bool add_global_group_node_js_ = true;
4142
bool add_global_group_ecmascript_ = true;
4243
monotonic_allocator string_allocator_;

test/quick-lint-js/file-matcher.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <cerrno>
88
#include <cstring>
99
#include <gtest/gtest.h>
10+
#include <optional>
11+
#include <quick-lint-js/file-canonical.h>
1012
#include <quick-lint-js/file.h>
1113
#include <quick-lint-js/have.h>
1214

@@ -69,6 +71,15 @@ inline ::testing::AssertionResult assert_same_file(const char* lhs_expr,
6971
return assert_same_file(lhs_expr, rhs_expr, std::string(lhs_path).c_str(),
7072
std::string(rhs_path).c_str());
7173
}
74+
75+
inline ::testing::AssertionResult assert_same_file(
76+
const char* lhs_expr, const char* rhs_expr,
77+
const std::optional<canonical_path>& lhs_path,
78+
const std::string& rhs_path) {
79+
return assert_same_file(lhs_expr, rhs_expr,
80+
lhs_path.has_value() ? lhs_path->c_str() : "",
81+
rhs_path);
82+
}
7283
}
7384

7485
#endif

0 commit comments

Comments
 (0)