-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C3] fix: make sure that C3 doesn't delete pre-existing options in So…
…lid's `defineConfig` call (#5954) * [C3] fix: make sure that C3 doesn't delete pre-existing options in Solid's `defineConfig` call * C3: reuse the new `mergeObjectProperties` function in nuxt as well
- Loading branch information
1 parent
38b5c16
commit 3b99e63
Showing
5 changed files
with
201 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-cloudflare": patch | ||
--- | ||
|
||
fix: make sure that C3 doesn't delete pre-existing options in the `defineConfig` call |
134 changes: 134 additions & 0 deletions
134
packages/create-cloudflare/src/helpers/__tests__/codemod.test.ts
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 @@ | ||
import { mergeObjectProperties } from "helpers/codemod"; | ||
import * as recast from "recast"; | ||
import parser from "recast/parsers/babel"; | ||
import { describe, expect, test } from "vitest"; | ||
|
||
describe("mergeObjectProperties", () => { | ||
const tests = [ | ||
{ | ||
testName: "merges simple objects", | ||
sourcePropertiesObject: { | ||
propA: "_A_", | ||
}, | ||
newPropertiesObject: { | ||
propB: "_B_", | ||
}, | ||
expectedPropertiesObject: { | ||
propA: "_A_", | ||
propB: "_B_", | ||
}, | ||
}, | ||
{ | ||
testName: "overrides existing non-object properties", | ||
sourcePropertiesObject: { | ||
__Prop0: true, | ||
propA: "_A_", | ||
propB: false, | ||
propC: 123, | ||
}, | ||
newPropertiesObject: { | ||
propA: "_a_", | ||
propB: true, | ||
propC: 456, | ||
}, | ||
expectedPropertiesObject: { | ||
__Prop0: true, | ||
propA: "_a_", | ||
propB: true, | ||
propC: 456, | ||
}, | ||
}, | ||
{ | ||
testName: "deep merges object properties", | ||
sourcePropertiesObject: { | ||
propA: { | ||
propAA: "a", | ||
propAB: "b", | ||
propAC: { | ||
propAA: { | ||
propAAA: "this is quite nested 1", | ||
propAAC: { | ||
propAAAA: "this is even more nested", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
newPropertiesObject: { | ||
propA: { | ||
propAB: "B", | ||
propAC: { | ||
propAA: { | ||
propAAB: "this is quite nested 2", | ||
propAAC: { | ||
propAAAA: "this is even more nested 1", | ||
propAAAB: "this is even more nested 2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedPropertiesObject: { | ||
propA: { | ||
propAA: "a", | ||
propAB: "B", | ||
propAC: { | ||
propAA: { | ||
propAAA: "this is quite nested 1", | ||
propAAC: { | ||
propAAAA: "this is even more nested 1", | ||
propAAAB: "this is even more nested 2", | ||
}, | ||
propAAB: "this is quite nested 2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
] satisfies { | ||
testName: string; | ||
sourcePropertiesObject: Record<string, unknown>; | ||
newPropertiesObject: Record<string, unknown>; | ||
expectedPropertiesObject: Record<string, unknown>; | ||
}[]; | ||
|
||
tests.forEach(({ testName, ...testObjects }) => | ||
test(testName, () => testMergeObjectProperties(testObjects)), | ||
); | ||
}); | ||
|
||
const testMergeObjectProperties = ({ | ||
sourcePropertiesObject, | ||
newPropertiesObject, | ||
expectedPropertiesObject, | ||
}: { | ||
sourcePropertiesObject: Record<string, unknown>; | ||
newPropertiesObject: Record<string, unknown>; | ||
expectedPropertiesObject: Record<string, unknown>; | ||
}) => { | ||
const sourceObj = createObjectExpression(sourcePropertiesObject); | ||
const newProperties = createObjectExpression(newPropertiesObject) | ||
.properties as recast.types.namedTypes.ObjectProperty[]; | ||
const expectedObj = createObjectExpression(expectedPropertiesObject); | ||
|
||
mergeObjectProperties(sourceObj, newProperties); | ||
|
||
expect(recast.prettyPrint(sourceObj, { parser }).code).toEqual( | ||
recast.prettyPrint(expectedObj, { parser }).code, | ||
); | ||
}; | ||
|
||
const createObjectExpression = ( | ||
sourceObj: Record<string, unknown>, | ||
): recast.types.namedTypes.ObjectExpression => { | ||
return ( | ||
( | ||
recast.parse( | ||
`const obj = {${Object.entries(sourceObj) | ||
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`) | ||
.join(",\n")}}`, | ||
{ parser }, | ||
).program.body[0] as recast.types.namedTypes.VariableDeclaration | ||
).declarations[0] as recast.types.namedTypes.VariableDeclarator | ||
).init as recast.types.namedTypes.ObjectExpression; | ||
}; |
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
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