Skip to content

Commit

Permalink
feat(un-esm): add annotation for requires with missing module
Browse files Browse the repository at this point in the history
closes #77
  • Loading branch information
pionxzh committed Dec 30, 2023
1 parent 1a5fa1e commit 2999909
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ var buz = foo$0.bar("baz");
`,
)

inlineTest.only('add annotations for require with missing module',
`
const foo = require(9527);
`,
`
const foo = require(9527/* wakaru:missing */);
`,
)

inlineTest('should not transform these invalid require',
`
var ei=require("core-js")["__core-js_shared__"]; // invalid identifier
Expand Down
27 changes: 26 additions & 1 deletion packages/unminify/src/transformations/un-esm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isTopLevel } from '@wakaru/ast-utils'
import { mergeComments } from '@wakaru/ast-utils/comments'
import { generateName, isValidIdentifier } from '@wakaru/ast-utils/identifier'
import { ImportManager } from '@wakaru/ast-utils/imports'
import { isExportObject, isStringObjectProperty, isUndefined } from '@wakaru/ast-utils/matchers'
Expand All @@ -11,7 +12,7 @@ import type { ASTTransformation, Context } from '@wakaru/ast-utils/wrapAstTransf
import type { ExpressionKind } from 'ast-types/lib/gen/kinds'
import type { NodePath } from 'ast-types/lib/node-path'
import type { Scope } from 'ast-types/lib/scope'
import type { ASTNode, ASTPath, AssignmentExpression, CallExpression, Identifier, JSCodeshift, MemberExpression, Node, StringLiteral, VariableDeclaration, VariableDeclarator } from 'jscodeshift'
import type { ASTNode, ASTPath, AssignmentExpression, CallExpression, Identifier, JSCodeshift, MemberExpression, Node, NumericLiteral, StringLiteral, VariableDeclaration, VariableDeclarator } from 'jscodeshift'

export const Schema = z.object({
hoist: z.boolean().default(false).describe('Hoist non-top-level require calls to the top of the file'),
Expand Down Expand Up @@ -121,6 +122,8 @@ function transformImport(context: Context, hoist: boolean) {

handleNamespaceImport()

handleMissingModuleRequire()

importManager.applyImportToRoot(j, root)

/**
Expand Down Expand Up @@ -369,6 +372,28 @@ function transformImport(context: Context, hoist: boolean) {
})
}
}

/**
* Add /* wakaru:missing / annotation to require calls that cannot be transformed.
*/
function handleMissingModuleRequire() {
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'require',
},
arguments: (args) => {
if (args.length !== 1) return false
return j.NumericLiteral.check(args[0])
},
})
.forEach((path) => {
const sourcePath = path.get('arguments', 0) as ASTPath<NumericLiteral>
const comment = j.commentBlock(' wakaru:missing ', false, true)
mergeComments(sourcePath.node, [comment])
})
}
}

function checkHoistable(j: JSCodeshift, path: ASTPath, hoist: boolean) {
Expand Down

0 comments on commit 2999909

Please sign in to comment.