Skip to content

Commit 3d27d22

Browse files
authored
chore(codemods): Fix TS errors (#11999)
We're having CI errors on the codemod package. Trying to limit the scope of what I need to look into I'm fixing all TS issues first. They're distracting.
1 parent 9c549ab commit 3d27d22

File tree

8 files changed

+70
-41
lines changed

8 files changed

+70
-41
lines changed

packages/codemods/src/codemods/v4.2.x/updateClerkGetCurrentUser/__testfixtures__/default.input.js renamed to packages/codemods/src/codemods/v4.2.x/updateClerkGetCurrentUser/__testfixtures__/default.input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ts-nocheck
12
import { parseJWT } from '@redwoodjs/api'
23
import { AuthenticationError, ForbiddenError } from '@redwoodjs/graphql-server'
34

packages/codemods/src/codemods/v4.2.x/updateClerkGetCurrentUser/__testfixtures__/default.output.js renamed to packages/codemods/src/codemods/v4.2.x/updateClerkGetCurrentUser/__testfixtures__/default.output.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ts-nocheck
12
import { parseJWT } from '@redwoodjs/api'
23
import { AuthenticationError, ForbiddenError } from '@redwoodjs/graphql-server'
34

packages/codemods/src/codemods/v4.2.x/updateClerkGetCurrentUser/updateClerkGetCurrentUser.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FileInfo, API, ObjectExpression } from 'jscodeshift'
1+
import type { FileInfo, API } from 'jscodeshift'
22

33
const newReturn = `userWithoutPrivateMetadata`
44
const destructureStatement = `const { privateMetadata, ...${newReturn} } = decoded`
@@ -54,14 +54,22 @@ export default function transform(file: FileInfo, api: API) {
5454
},
5555
})
5656
.replaceWith((path) => {
57-
const properties = (
58-
path.value.argument as ObjectExpression
59-
).properties.filter(
60-
(property) =>
61-
property.type !== 'SpreadElement' && property.name !== 'decoded',
62-
)
57+
if (path.value.argument?.type !== 'ObjectExpression') {
58+
return path.value
59+
}
6360

64-
properties.push(j.spreadElement(j.identifier(newReturn)))
61+
// Filter out the spread element with 'decoded'
62+
const properties = path.value.argument.properties.filter((property) => {
63+
return !(
64+
property.type === 'SpreadElement' &&
65+
property.argument.type === 'Identifier' &&
66+
property.argument.name === 'decoded'
67+
)
68+
})
69+
70+
// Create a new spread element with the new name
71+
const spreadElement = j.spreadProperty(j.identifier(newReturn))
72+
properties.push(spreadElement)
6573

6674
return j.returnStatement(j.objectExpression(properties))
6775
})

packages/codemods/src/codemods/v5.x.x/cellQueryResult/cellQueryResult.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FileInfo, API, Property } from 'jscodeshift'
1+
import type { FileInfo, API } from 'jscodeshift'
22

33
// We need to check all of the cell functions
44
const cellFunctionsToCheck = ['Success', 'Failure', 'Loading', 'Empty']
@@ -26,12 +26,12 @@ export default function transform(file: FileInfo, api: API) {
2626
cellFunctionsToCheck.forEach((variableName) => {
2727
const foundCellFunctions = ast.findVariableDeclarators(variableName)
2828
if (foundCellFunctions.size() === 1) {
29-
const foundFunction = foundCellFunctions.get()
29+
const foundFunction = foundCellFunctions.paths()[0]
3030

3131
// We expect the variable to be a function (standard or arrow)
3232
if (
33-
foundFunction.value.init.type === 'ArrowFunctionExpression' ||
34-
foundFunction.value.init.type === 'FunctionExpression'
33+
foundFunction.value.init?.type === 'ArrowFunctionExpression' ||
34+
foundFunction.value.init?.type === 'FunctionExpression'
3535
) {
3636
const firstParameter = foundFunction.value.init.params.at(0)
3737

@@ -42,14 +42,20 @@ export default function transform(file: FileInfo, api: API) {
4242
// We expect the function to be destructuring the properties the cell is passed
4343
if (firstParameter.type === 'ObjectPattern') {
4444
const previouslySpreadPropertiesInUse =
45-
firstParameter.properties.filter((property: Property) => {
45+
firstParameter.properties.filter((property) => {
4646
// skip rest params
47-
if (property.type === 'RestElement') {
47+
if (
48+
property.type === 'SpreadProperty' ||
49+
property.type === 'SpreadPropertyPattern' ||
50+
property.type === 'RestProperty' ||
51+
property.key.type !== 'Identifier'
52+
) {
4853
return false
4954
}
5055

5156
return nonSpreadVariables.includes(property.key.name)
5257
})
58+
5359
if (previouslySpreadPropertiesInUse.length > 0) {
5460
// Add the newly destructured properties as function parameters
5561
firstParameter.properties.push(
@@ -58,33 +64,37 @@ export default function transform(file: FileInfo, api: API) {
5864
j.identifier('queryResult'), // Previously spead properties are now found within 'queryResult'
5965
j.objectPattern(
6066
// For every previously spead property in use add a destructuring
61-
previouslySpreadPropertiesInUse.map(
62-
(usedProperty: Property) => {
63-
if (
64-
usedProperty.key.type !== 'Identifier' ||
65-
usedProperty.value.type !== 'Identifier'
66-
) {
67-
throw new Error(
68-
'Unable to process a parameter within the cell function',
69-
)
70-
}
71-
const prop = j.property(
72-
'init',
73-
j.identifier(usedProperty.key.name),
74-
j.identifier(usedProperty.value.name),
67+
previouslySpreadPropertiesInUse.map((usedProperty) => {
68+
if (
69+
!('key' in usedProperty) ||
70+
!('value' in usedProperty) ||
71+
usedProperty.key.type !== 'Identifier' ||
72+
usedProperty.value.type !== 'Identifier'
73+
) {
74+
throw new Error(
75+
'Unable to process a parameter within the cell function',
7576
)
76-
// Use an alias if one was previously defined by the user
77-
prop.shorthand = usedProperty.shorthand
78-
return prop
79-
},
80-
),
77+
}
78+
79+
const prop = j.property(
80+
'init',
81+
j.identifier(usedProperty.key.name),
82+
j.identifier(usedProperty.value.name),
83+
)
84+
// Use an alias if one was previously defined by the user
85+
prop.shorthand = usedProperty.shorthand
86+
return prop
87+
}),
8188
),
8289
),
8390
)
8491
// Remove the existing function parameters corresponding to previously spread variables
8592
firstParameter.properties = firstParameter.properties.filter(
86-
(property: Property) => {
87-
if (property.key.type !== 'Identifier') {
93+
(property) => {
94+
if (
95+
!('key' in property) ||
96+
property.key.type !== 'Identifier'
97+
) {
8898
throw new Error('Unable to process a parameter')
8999
}
90100
return !nonSpreadVariables.includes(property.key.name)

packages/codemods/src/codemods/v5.x.x/updateAuth0ToV2/updateAuth0ToV2.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ export default function transform(file: FileInfo, api: API) {
55
const ast = j(file.source)
66

77
const paths = ast.find(j.ObjectProperty, (node) => {
8-
return ['redirect_uri', 'audience'].includes(node.key.name)
8+
return (
9+
'name' in node.key &&
10+
(node.key.name === 'redirect_uri' || node.key.name === 'audience')
11+
)
912
})
1013

1114
let nodes = paths.nodes()

packages/codemods/src/codemods/v5.x.x/upgradeToReact18/upgradeToReact18.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import type { TaskInnerAPI } from 'tasuku'
77

88
import { getPaths } from '@redwoodjs/project-config'
99

10-
function checkAndTransformReactRoot(taskContext: TaskInnerAPI) {
10+
function checkAndTransformReactRoot(
11+
taskContext: Pick<TaskInnerAPI, 'setWarning'>,
12+
) {
1113
const indexHTMLFilepath = path.join(getPaths().web.src, 'index.html')
1214

1315
const indexHTML = load(fs.readFileSync(indexHTMLFilepath, 'utf-8'))

packages/codemods/src/lib/cells.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export const getNamedExports = (ast: types.Node): NamedExports[] => {
123123
})
124124
} else if (declaration.type === 'ClassDeclaration') {
125125
namedExports.push({
126-
name: declaration?.id?.name,
126+
name: declaration?.id?.name || '',
127127
type: 'class',
128128
})
129129
}

packages/codemods/tsconfig.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
22
"extends": "../../tsconfig.compilerOption.json",
33
"compilerOptions": {
4-
"rootDir": "src",
54
"emitDeclarationOnly": false,
65
"noEmit": true,
76
"module": "Node16",
8-
"moduleResolution": "Node16"
7+
"moduleResolution": "Node16",
8+
"rootDir": "."
99
},
1010
"include": ["src", "./testUtils.d.ts"],
11-
"exclude": ["**/__testfixtures__"]
11+
"exclude": ["**/__testfixtures__", "**/__tests__"],
12+
"references": [
13+
{ "path": "../framework-tools" },
14+
{ "path": "../project-config" }
15+
]
1216
}

0 commit comments

Comments
 (0)