Skip to content

Commit

Permalink
refactor!: lodash.mergeWith customizer now allows unsetting options b…
Browse files Browse the repository at this point in the history
…y setting them to `undefined`

BREAKING CHANGE: In previous versions, the lodash.mergeWith customizer skipped source properties
that resolved to `undefined`. With this version, the customizer now unsets these properties
(sets them to `undefined`), allowing the end user to easily unset defaults (e.g. `filename`).
  • Loading branch information
Xunnamius committed Jan 8, 2023
1 parent 9d1b321 commit 74af680
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/plugin-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ export function pluginTester(options: PluginTesterOptions = {}) {
}

function tryInferFilepath() {
// ? Allow the end user to unset filepath by setting it to undefined
if ('filepath' in rawBaseConfig || 'filename' in rawBaseConfig) {
return undefined;
}

const oldStackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = Number.POSITIVE_INFINITY;

Expand Down Expand Up @@ -448,12 +453,12 @@ export function pluginTester(options: PluginTesterOptions = {}) {
{ babelOptions: baseBabelOptions },
{
babelOptions: {
filename: codePath,
filename: codePath || execPath || baseBabelOptions.filename,
// ? If they have a babelrc, then we'll let them use that
babelrc: hasBabelrc
}
},
{ babelOptions },
{ babelOptions: babelOptions || {} },
{
testBlockTitle: `${currentTestNumber++}. ${title || blockTitle}`,
only,
Expand Down Expand Up @@ -555,9 +560,14 @@ export function pluginTester(options: PluginTesterOptions = {}) {
{ [$type]: 'test-object' } as const,
{ babelOptions: baseBabelOptions },
{
babelOptions: { filename: getAbsolutePath(filepath, codeFixture) ?? filepath }
babelOptions: {
filename:
getAbsolutePath(filepath, codeFixture) ||
filepath ||
baseBabelOptions.filename
}
},
{ babelOptions },
{ babelOptions: babelOptions || {} },
{
snapshot: snapshot ?? baseSnapshot,
testBlockTitle: `${currentTestNumber++}. ${title || pluginName || presetName}`,
Expand Down Expand Up @@ -916,8 +926,20 @@ export function pluginTester(options: PluginTesterOptions = {}) {
}
}

function mergeCustomizer(objValue: unknown[], srcValue: unknown) {
return Array.isArray(objValue) ? objValue.concat(srcValue) : undefined;
function mergeCustomizer(
objValue: unknown,
srcValue: unknown,
key: string,
object: Record<string, unknown>,
source: Record<string, unknown>
) {
if (object && srcValue === undefined && key in source) {
delete object[key];
} else if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}

return undefined;
}

function getAbsolutePath(filename?: string, basename?: string) {
Expand Down

0 comments on commit 74af680

Please sign in to comment.