Skip to content

Commit 2c68f65

Browse files
Merge pull request #1012 from Automattic/sort-config
feat(core): make `LintConfig` sorted by key
2 parents aa517cc + 723fc6e commit 2c68f65

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

harper-core/src/linting/lint_group.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ use crate::{Dictionary, MutableDictionary};
7373
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
7474
#[serde(transparent)]
7575
pub struct LintGroupConfig {
76-
inner: HashMap<String, Option<bool>>,
76+
/// A `BTreeMap` so that the config has a stable ordering when written to disk.
77+
inner: BTreeMap<String, Option<bool>>,
7778
}
7879

7980
#[cached]
@@ -95,7 +96,7 @@ impl LintGroupConfig {
9596
}
9697

9798
pub fn set_rule_enabled_if_unset(&mut self, key: impl AsRef<str>, val: bool) {
98-
if self.inner.get(key.as_ref()).is_none() {
99+
if !self.inner.contains_key(key.as_ref()) {
99100
self.set_rule_enabled(key.as_ref().to_string(), val);
100101
}
101102
}
@@ -117,13 +118,15 @@ impl LintGroupConfig {
117118
///
118119
/// Conflicting keys will be overridden by the value in the other group.
119120
pub fn merge_from(&mut self, other: &mut LintGroupConfig) {
120-
for (key, val) in other.inner.drain() {
121+
for (key, val) in other.inner.iter() {
121122
if val.is_none() {
122123
continue;
123124
}
124125

125-
self.inner.insert(key, val);
126+
self.inner.insert(key.to_string(), *val);
126127
}
128+
129+
other.clear();
127130
}
128131

129132
/// Fill the group with the values for the curated lint group.

packages/harper.js/src/Linter.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ test('Linters have the same JSON config format', async () => {
210210
for (const Linter of Object.values(linters)) {
211211
const linter = new Linter({ binary });
212212

213-
configs.push(await linter.getLintConfig());
213+
configs.push(await linter.getLintConfigAsJSON());
214214
}
215215

216216
for (const config of configs) {
217-
// The keys of stringified configs would be unstable, so we'll just check the object.
218217
expect(config).toEqual(configs[0]);
219-
expect(config).toBeTypeOf('object');
218+
expect(config).toBeTypeOf('string');
220219
}
221220
});

0 commit comments

Comments
 (0)