metro-transform-plugins
Advanced tools
Comparing version 0.80.12 to 0.81.0-alpha.0
{ | ||
"name": "metro-transform-plugins", | ||
"version": "0.80.12", | ||
"version": "0.81.0-alpha.0", | ||
"description": "🚇 Transform plugins for Metro.", | ||
@@ -16,6 +16,6 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"@babel/core": "^7.20.0", | ||
"@babel/generator": "^7.20.0", | ||
"@babel/template": "^7.0.0", | ||
"@babel/traverse": "^7.20.0", | ||
"@babel/core": "^7.25.2", | ||
"@babel/generator": "^7.25.0", | ||
"@babel/template": "^7.25.0", | ||
"@babel/traverse": "^7.25.3", | ||
"flow-enums-runtime": "^0.0.6", | ||
@@ -25,9 +25,9 @@ "nullthrows": "^1.1.1" | ||
"devDependencies": { | ||
"@babel/code-frame": "^7.0.0", | ||
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.0.0", | ||
"@babel/plugin-transform-flow-strip-types": "^7.20.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.0.0", | ||
"@babel/types": "^7.20.0", | ||
"@babel/code-frame": "^7.24.7", | ||
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3", | ||
"@babel/plugin-transform-flow-strip-types": "^7.25.2", | ||
"@babel/plugin-transform-modules-commonjs": "^7.24.8", | ||
"@babel/types": "^7.25.2", | ||
"babel-plugin-tester": "^6.0.1", | ||
"metro": "0.80.12" | ||
"metro": "0.81.0-alpha.0" | ||
}, | ||
@@ -34,0 +34,0 @@ "engines": { |
@@ -11,2 +11,3 @@ "use strict"; | ||
const inlineableCalls = new Set(["require"]); | ||
let memoizeCalls = false; | ||
const opts = state.opts; | ||
@@ -24,3 +25,8 @@ if (opts != null) { | ||
} | ||
memoizeCalls = opts.memoizeCalls ?? false; | ||
} | ||
const programNode = path.scope.block; | ||
if (programNode.type !== "Program") { | ||
return; | ||
} | ||
path.scope.crawl(); | ||
@@ -38,3 +44,3 @@ path.traverse( | ||
parseResult; | ||
const init = declarationPath.node.init; | ||
const maybeInit = declarationPath.node.init; | ||
const name = declarationPath.node.id | ||
@@ -45,7 +51,13 @@ ? declarationPath.node.id.name | ||
name == null ? null : declarationPath.scope.getBinding(name); | ||
if (binding == null || binding.constantViolations.length > 0) { | ||
if ( | ||
maybeInit == null || | ||
!t.isExpression(maybeInit) || | ||
binding == null || | ||
binding.constantViolations.length > 0 | ||
) { | ||
return; | ||
} | ||
const init = maybeInit; | ||
const initPath = declarationPath.get("init"); | ||
if (init == null || Array.isArray(initPath)) { | ||
if (Array.isArray(initPath)) { | ||
return; | ||
@@ -60,2 +72,25 @@ } | ||
let thrown = false; | ||
const memoVarName = parseResult.identifierName; | ||
let hasMemoVar = false; | ||
if (memoizeCalls && binding.referencePaths.length > 0) { | ||
const varInitStmt = t.variableDeclaration("var", [ | ||
t.variableDeclarator(t.identifier(memoVarName)), | ||
]); | ||
declarationPath.remove(); | ||
hasMemoVar = addStmtToBlock(programNode, varInitStmt, 0); | ||
} | ||
function getMemoOrCallExpr() { | ||
const refExpr = t.cloneDeep(init); | ||
refExpr.METRO_INLINE_REQUIRES_INIT_LOC = initLoc; | ||
return t.logicalExpression( | ||
"||", | ||
t.identifier(memoVarName), | ||
t.assignmentExpression( | ||
"=", | ||
t.identifier(memoVarName), | ||
refExpr | ||
) | ||
); | ||
} | ||
const scopesWithInlinedRequire = new Set(); | ||
for (const referencePath of binding.referencePaths) { | ||
@@ -65,5 +100,19 @@ excludeMemberAssignment(moduleName, referencePath, state); | ||
referencePath.scope.rename(requireFnName); | ||
const refExpr = t.cloneDeep(init); | ||
refExpr.METRO_INLINE_REQUIRES_INIT_LOC = initLoc; | ||
referencePath.replaceWith(refExpr); | ||
if (hasMemoVar) { | ||
referencePath.scope.rename(memoVarName); | ||
if (!isDirectlyEnclosedByBlock(t, referencePath)) { | ||
referencePath.replaceWith(getMemoOrCallExpr()); | ||
continue; | ||
} | ||
if (scopesWithInlinedRequire.has(referencePath.scope)) { | ||
referencePath.replaceWith(t.identifier(memoVarName)); | ||
} else { | ||
referencePath.replaceWith(getMemoOrCallExpr()); | ||
scopesWithInlinedRequire.add(referencePath.scope); | ||
} | ||
} else { | ||
const refExpr = t.cloneDeep(init); | ||
refExpr.METRO_INLINE_REQUIRES_INIT_LOC = initLoc; | ||
referencePath.replaceWith(refExpr); | ||
} | ||
} catch (error) { | ||
@@ -73,3 +122,3 @@ thrown = true; | ||
} | ||
if (!thrown) { | ||
if (!thrown && declarationPath.node != null) { | ||
declarationPath.remove(); | ||
@@ -141,5 +190,11 @@ } | ||
} | ||
if (path.parent.type !== "VariableDeclarator") { | ||
return null; | ||
} | ||
const variableDeclarator = path.parent; | ||
if (variableDeclarator.id.type !== "Identifier") { | ||
return null; | ||
} | ||
const identifier = variableDeclarator.id; | ||
const isValid = | ||
path.parent.type === "VariableDeclarator" && | ||
path.parent.id.type === "Identifier" && | ||
parentPath.parent.type === "VariableDeclaration" && | ||
@@ -153,2 +208,3 @@ grandParentPath.parent.type === "Program"; | ||
requireFnName, | ||
identifierName: identifier.name, | ||
}; | ||
@@ -182,2 +238,3 @@ } | ||
} | ||
const identifier = variableDeclarator.id; | ||
if ( | ||
@@ -198,2 +255,3 @@ grandParentPath.parent.type !== "VariableDeclaration" || | ||
requireFnName, | ||
identifierName: identifier.name, | ||
}; | ||
@@ -256,1 +314,39 @@ } | ||
} | ||
function isBranch(t, node) { | ||
return ( | ||
t.isIfStatement(node) || | ||
t.isLogicalExpression(node) || | ||
t.isConditionalExpression(node) || | ||
t.isSwitchStatement(node) || | ||
t.isSwitchCase(node) || | ||
t.isForStatement(node) || | ||
t.isForInStatement(node) || | ||
t.isForOfStatement(node) || | ||
t.isWhileStatement(node) | ||
); | ||
} | ||
function isDirectlyEnclosedByBlock(t, path) { | ||
let curPath = path; | ||
while (curPath) { | ||
if (isBranch(t, curPath.node)) { | ||
return false; | ||
} | ||
if (t.isBlockStatement(curPath.node)) { | ||
return true; | ||
} | ||
curPath = curPath.parentPath; | ||
} | ||
return true; | ||
} | ||
function addStmtToBlock(block, stmt, idx) { | ||
const scopeBody = block.body; | ||
if (Array.isArray(scopeBody)) { | ||
scopeBody.splice(idx, 0, stmt); | ||
return true; | ||
} else if (scopeBody && Array.isArray(scopeBody.body)) { | ||
scopeBody.body.splice(idx, 0, stmt); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94401
1275
Updated@babel/core@^7.25.2
Updated@babel/generator@^7.25.0
Updated@babel/template@^7.25.0
Updated@babel/traverse@^7.25.3