Skip to content

Commit

Permalink
Fix TS errors from new babel types version
Browse files Browse the repository at this point in the history
  • Loading branch information
calebeby committed Jun 1, 2023
1 parent bdc489f commit 5f61d8b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"rules": {
"caleb/unicorn/filename-case": "off",
"complexity": "off",
"array-callback-return": "off"
"array-callback-return": "off",
"caleb/@typescript-eslint/no-non-null-assertion": "off"
}
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"rollup": "2.9.1",
"rollup-plugin-dts": "1.4.2",
"source-map": "0.7.3",
"typescript": "3.9.2"
"typescript": "5.1.3"
},
"prettier": {
"semi": false,
Expand Down
7 changes: 3 additions & 4 deletions src/exports/handleAssignmentExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getNamedExportsFromReferencedAssignments = (
// idk if this will ever be false... but type narrowing
if (!refPath.isIdentifier()) return
const memExp = refPath.parentPath
const assignment = memExp.parentPath
const assignment = memExp.parentPath!
// find direct member assignments in the top level
// obj.foo = bar
if (
Expand Down Expand Up @@ -158,9 +158,8 @@ export const handleAssignmentExpression = (
if (
left.isMemberExpression() &&
right.isBooleanLiteral() &&
t.isIdentifier(left.node.object) &&
left.node.object.name === 'exports' &&
left.node.property.name === '__esModule' &&
t.isIdentifier(left.node.object, { name: 'exports' }) &&
t.isIdentifier(left.node.property, { name: '__esModule' }) &&
right.node.value === true
)
return path.remove()
Expand Down
10 changes: 3 additions & 7 deletions src/exports/writeExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ export const writeExports = (
// This is not actually an expressionStatement anymore, now it is a variableDeclaration
newDeclarationPath = expressionStatement
} else {
newDeclarationPath = programBody[0].insertBefore(
newDeclaration,
)[0] as NodePath<typeof newDeclaration>
newDeclarationPath = programBody[0].insertBefore(newDeclaration)[0]
}
// If it was module.exports = foo, it will be handled within the loop through namedExports
// If it was module.exports = () => {} or another non-identifier, it will be handled here.
Expand Down Expand Up @@ -185,9 +183,7 @@ export const writeExports = (
[],
)

const newPath = programChild.insertBefore(
exportDeclaration,
)[0] as NodePath<typeof exportDeclaration>
const newPath = programChild.insertBefore(exportDeclaration)[0]
newPath.scope.registerDeclaration(newPath)
value.replaceWith(newId)
} else {
Expand All @@ -203,7 +199,7 @@ export const writeExports = (
const newPath = programChild.insertBefore([
varDeclaration,
exportDeclaration,
])[1] as NodePath<typeof exportDeclaration>
])[1]
newPath.scope.registerDeclaration(newPath)
value.replaceWith(newId)
}
Expand Down
9 changes: 3 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export const assignMaps = <Key, Val>(
* From a descendent, go up until you hit a direct child of the program path
*/
export const findParentProgramChild = (path: NodePath) =>
path.find((p) => p.parentPath.isProgram())
path.find((p) => p.parentPath!.isProgram())!

export const everyParent = (
path: NodePath,
condition: (p: NodePath) => boolean,
): boolean => {
if (condition(path)) {
if (path.isProgram()) return true
return everyParent(path.parentPath, condition)
return everyParent(path.parentPath!, condition)
}
return false
}
Expand Down Expand Up @@ -116,10 +116,7 @@ export const updateReferencesTo = (
// isReferencedIdentifier will be true for both Identifiers and JSXIdentifiers
if (!p.isReferencedIdentifier()) return
p.node.name = newId.name
p.scope
.getBinding(newId.name)
// @ts-expect-error
?.reference(p)
p.scope.getBinding(newId.name)?.reference(p)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/imports/handleDefaultImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const handleDefaultImport = (path: NodePath<t.CallExpression>) => {
// Replace foo.default with foo
// Replace foo with { default: foo }
references.forEach((ref) => {
if (ref.parentPath.isMemberExpression()) {
if (ref.parentPath!.isMemberExpression()) {
const memberExpression = ref.parentPath
if (
!t.isIdentifier(memberExpression.node.property) ||
Expand Down
5 changes: 2 additions & 3 deletions src/imports/handlePotentialLazyImportFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const handlePotentialLazyImportFunction = (
if (!binding) return
// If the function is referenced in a way that is different from `foo().bar`, then we bail
const hasInvalidReference = binding.referencePaths.some((ref) => {
const parent = ref.parentPath
const parent = ref.parentPath!
if (!parent.isCallExpression()) return true
const grandparent = parent.parentPath
if (!grandparent.isMemberExpression()) return true
Expand Down Expand Up @@ -95,10 +95,9 @@ export const handlePotentialLazyImportFunction = (

// Replace all instances of _parser().foo with newId.foo
binding.referencePaths.forEach((ref) => {
const parent = ref.parentPath
const parent = ref.parentPath!
if (!parent.isCallExpression()) return
parent.replaceWith(newId)
// @ts-expect-error
newBinding.reference(parent)
})
}
28 changes: 20 additions & 8 deletions src/imports/handleRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
isModuleExports,
isExports,
isStillInTree,
toString,
} from '../helpers'
import { NamedExportsMap } from '..'
import { isObjectDefinePropertyExport } from '../handlePotentialObjectDefineProperty'
Expand Down Expand Up @@ -172,7 +171,7 @@ export const handleRequire = (
// Special case: check for if the only reference is module.exports = imported
if (references.length === 1) {
const ref = references[0]
const assignment = ref.parentPath
const assignment = ref.parentPath!
if (
assignment.isAssignmentExpression() &&
isModuleExports(assignment.node.left)
Expand Down Expand Up @@ -216,7 +215,7 @@ export const handleRequire = (
injectImportIntoBody(program, newImport)
if (usesDefaultPropertyOnly) {
references.forEach((ref) => {
const memberExp = ref.parentPath
const memberExp = ref.parentPath!
memberExp.replaceWith(localId)
})
} else {
Expand All @@ -231,7 +230,7 @@ export const handleRequire = (
if (parentCallExp && isObjectDefinePropertyExport(parentCallExp))
return true
// foo.bar
const referenceMemberExp = ref.parentPath
const referenceMemberExp = ref.parentPath!
if (!referenceMemberExp.isMemberExpression()) return false
// exports.asdf = foo.bar
const assignmentExp = referenceMemberExp.parentPath
Expand Down Expand Up @@ -261,20 +260,33 @@ export const handleRequire = (
// Object.defineProperty(exports, 'asdf', { ... })

// in the case of Object.defineProperty, this is not an assignment expression
const assignmentExp = ref.parentPath.parentPath
const assignmentExp = ref.parentPath!.parentPath!

const programPath = getProgramPath(assignmentExp)
if (
!t.isMemberExpression(ref.parent) ||
!t.isIdentifier(ref.parent.property)
)
return

// bar
const importedId: t.Identifier = (ref.parent as t.MemberExpression)
.property
const importedId: t.Identifier = ref.parent.property
// asdf
let exportedId: t.Identifier

if (assignmentExp.isAssignmentExpression()) {
if (
!t.isMemberExpression(assignmentExp.node.left) ||
!t.isIdentifier(assignmentExp.node.left.property)
) {
// Ex: private field? exports.#asdf = foo.bar
// We can ignore that. (it doesn't make sense anyways)
return
}

// case: exports.asdf = foo.bar
// asdf
exportedId = (assignmentExp.node.left as t.MemberExpression).property
exportedId = assignmentExp.node.left.property
assignmentExp.parentPath.remove()
} else {
const parentCallExp = ref.findParent((p) =>
Expand Down

0 comments on commit 5f61d8b

Please sign in to comment.