-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge rule.meta.defaultOptions before validation (#166)
* feat: merge rule.meta.defaultOptions before validation * Switch from ?. to && * Apply suggestions from code review Co-authored-by: Francesco Trotta <[email protected]> * switch from undefined to void 0 in deep-merge-arrays --------- Co-authored-by: Francesco Trotta <[email protected]>
- Loading branch information
1 parent
2b0aa3a
commit d02f914
Showing
4 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @fileoverview Applies default rule options | ||
* @author JoshuaKGoldberg | ||
*/ | ||
|
||
/** | ||
* Check if the variable contains an object strictly rejecting arrays | ||
* @param {unknown} value an object | ||
* @returns {boolean} Whether value is an object | ||
*/ | ||
function isObjectNotArray(value) { | ||
return typeof value === "object" && value !== null && !Array.isArray(value); | ||
} | ||
|
||
/** | ||
* Deeply merges second on top of first, creating a new {} object if needed. | ||
* @param {T} first Base, default value. | ||
* @param {U} second User-specified value. | ||
* @returns {T | U | (T & U)} Merged equivalent of second on top of first. | ||
*/ | ||
function deepMergeObjects(first, second) { | ||
if (second === void 0) { | ||
return first; | ||
} | ||
|
||
if (!isObjectNotArray(first) || !isObjectNotArray(second)) { | ||
return second; | ||
} | ||
|
||
const result = { ...first, ...second }; | ||
|
||
for (const key of Object.keys(second)) { | ||
if (Object.prototype.propertyIsEnumerable.call(first, key)) { | ||
result[key] = deepMergeObjects(first[key], second[key]); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Deeply merges second on top of first, creating a new [] array if needed. | ||
* @param {T[] | undefined} first Base, default values. | ||
* @param {U[] | undefined} second User-specified values. | ||
* @returns {(T | U | (T & U))[]} Merged equivalent of second on top of first. | ||
*/ | ||
function deepMergeArrays(first, second) { | ||
if (!first || !second) { | ||
return second || first || []; | ||
} | ||
|
||
return [ | ||
...first.map((value, i) => deepMergeObjects(value, second[i])), | ||
...second.slice(first.length) | ||
]; | ||
} | ||
|
||
export { deepMergeArrays }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
import assert from "node:assert"; | ||
|
||
import { deepMergeArrays } from "../../../lib/shared/deep-merge-arrays.js"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Turns a value into its string equivalent for a test name. | ||
* @param {unknown} value Value to be stringified. | ||
* @returns {string} String equivalent of the value. | ||
*/ | ||
function toTestCaseName(value) { | ||
return typeof value === "object" ? JSON.stringify(value) : `${value}`; | ||
} | ||
|
||
describe("deepMerge", () => { | ||
for (const [first, second, result] of [ | ||
[void 0, void 0, []], | ||
[[], void 0, []], | ||
[["abc"], void 0, ["abc"]], | ||
[void 0, ["abc"], ["abc"]], | ||
[[], ["abc"], ["abc"]], | ||
[[void 0], ["abc"], ["abc"]], | ||
[[void 0, void 0], ["abc"], ["abc", void 0]], | ||
[[void 0, void 0], ["abc", "def"], ["abc", "def"]], | ||
[[void 0, null], ["abc"], ["abc", null]], | ||
[[void 0, null], ["abc", "def"], ["abc", "def"]], | ||
[[null], ["abc"], ["abc"]], | ||
[[123], [void 0], [123]], | ||
[[123], [null], [null]], | ||
[[123], [{ a: 0 }], [{ a: 0 }]], | ||
[["abc"], [void 0], ["abc"]], | ||
[["abc"], [null], [null]], | ||
[["abc"], ["def"], ["def"]], | ||
[["abc"], [{ a: 0 }], [{ a: 0 }]], | ||
[[["abc"]], [null], [null]], | ||
[[["abc"]], ["def"], ["def"]], | ||
[[["abc"]], [{ a: 0 }], [{ a: 0 }]], | ||
[[{ abc: true }], ["def"], ["def"]], | ||
[[{ abc: true }], [["def"]], [["def"]]], | ||
[[null], [{ abc: true }], [{ abc: true }]], | ||
[[{ a: void 0 }], [{ a: 0 }], [{ a: 0 }]], | ||
[[{ a: null }], [{ a: 0 }], [{ a: 0 }]], | ||
[[{ a: null }], [{ a: { b: 0 } }], [{ a: { b: 0 } }]], | ||
[[{ a: 0 }], [{ a: 1 }], [{ a: 1 }]], | ||
[[{ a: 0 }], [{ a: null }], [{ a: null }]], | ||
[[{ a: 0 }], [{ a: void 0 }], [{ a: 0 }]], | ||
[[{ a: 0 }], ["abc"], ["abc"]], | ||
[[{ a: 0 }], [123], [123]], | ||
[[[{ a: 0 }]], [123], [123]], | ||
[ | ||
[{ a: ["b"] }], | ||
[{ a: ["c"] }], | ||
[{ a: ["c"] }] | ||
], | ||
[ | ||
[{ a: [{ b: "c" }] }], | ||
[{ a: [{ d: "e" }] }], | ||
[{ a: [{ d: "e" }] }] | ||
], | ||
[ | ||
[{ a: { b: "c" }, d: true }], | ||
[{ a: { e: "f" } }], | ||
[{ a: { b: "c", e: "f" }, d: true }] | ||
], | ||
[ | ||
[{ a: { b: "c" } }], | ||
[{ a: { e: "f" }, d: true }], | ||
[{ a: { b: "c", e: "f" }, d: true }] | ||
], | ||
[ | ||
[{ a: { b: "c" } }, { d: true }], | ||
[{ a: { e: "f" } }, { f: 123 }], | ||
[{ a: { b: "c", e: "f" } }, { d: true, f: 123 }] | ||
], | ||
[ | ||
[{ hasOwnProperty: true }], | ||
[{}], | ||
[{ hasOwnProperty: true }] | ||
], | ||
[ | ||
[{ hasOwnProperty: false }], | ||
[{}], | ||
[{ hasOwnProperty: false }] | ||
], | ||
[ | ||
[{ hasOwnProperty: null }], | ||
[{}], | ||
[{ hasOwnProperty: null }] | ||
], | ||
[ | ||
[{ hasOwnProperty: void 0 }], | ||
[{}], | ||
[{ hasOwnProperty: void 0 }] | ||
], | ||
[ | ||
[{}], | ||
[{ hasOwnProperty: null }], | ||
[{ hasOwnProperty: null }] | ||
], | ||
[ | ||
[{}], | ||
[{ hasOwnProperty: void 0 }], | ||
[{ hasOwnProperty: void 0 }] | ||
], | ||
[ | ||
[{ | ||
allow: [], | ||
ignoreDestructuring: false, | ||
ignoreGlobals: false, | ||
ignoreImports: false, | ||
properties: "always" | ||
}], | ||
[], | ||
[{ | ||
allow: [], | ||
ignoreDestructuring: false, | ||
ignoreGlobals: false, | ||
ignoreImports: false, | ||
properties: "always" | ||
}] | ||
] | ||
]) { | ||
it(`${toTestCaseName(first)}, ${toTestCaseName(second)}`, () => { | ||
assert.deepStrictEqual(deepMergeArrays(first, second), result); | ||
}); | ||
} | ||
}); |