metro-transform-plugins
Advanced tools
Comparing version 0.80.10 to 0.80.11
{ | ||
"name": "metro-transform-plugins", | ||
"version": "0.80.10", | ||
"version": "0.80.11", | ||
"description": "🚇 Transform plugins for Metro.", | ||
@@ -30,3 +30,3 @@ "main": "src/index.js", | ||
"babel-plugin-tester": "^6.0.1", | ||
"metro": "0.80.10" | ||
"metro": "0.80.11" | ||
}, | ||
@@ -33,0 +33,0 @@ "engines": { |
"use strict"; | ||
module.exports = (babel) => ({ | ||
module.exports = ({ types: t, traverse }) => ({ | ||
name: "inline-requires", | ||
visitor: { | ||
Program: { | ||
enter() {}, | ||
exit(path, state) { | ||
const t = babel.types; | ||
const ignoredRequires = new Set(); | ||
const inlineableCalls = new Set(["require"]); | ||
if (state.opts != null) { | ||
if (state.opts.ignoredRequires != null) { | ||
for (const name of state.opts.ignoredRequires) { | ||
const opts = state.opts; | ||
if (opts != null) { | ||
if (opts.ignoredRequires != null) { | ||
for (const name of opts.ignoredRequires) { | ||
ignoredRequires.add(name); | ||
} | ||
} | ||
if (state.opts.inlineableCalls != null) { | ||
for (const name of state.opts.inlineableCalls) { | ||
if (opts.inlineableCalls != null) { | ||
for (const name of opts.inlineableCalls) { | ||
inlineableCalls.add(name); | ||
@@ -39,11 +40,14 @@ } | ||
: null; | ||
const binding = declarationPath.scope.getBinding(name); | ||
if (binding.constantViolations.length > 0) { | ||
const binding = | ||
name == null ? null : declarationPath.scope.getBinding(name); | ||
if (binding == null || binding.constantViolations.length > 0) { | ||
return; | ||
} | ||
const initLoc = getNearestLocFromPath( | ||
declarationPath.get("init") | ||
); | ||
const initPath = declarationPath.get("init"); | ||
if (init == null || Array.isArray(initPath)) { | ||
return; | ||
} | ||
const initLoc = getNearestLocFromPath(initPath); | ||
deleteLocation(init); | ||
babel.traverse(init, { | ||
traverse(init, { | ||
noScope: true, | ||
@@ -80,11 +84,11 @@ enter: (path) => deleteLocation(path.node), | ||
function excludeMemberAssignment(moduleName, referencePath, state) { | ||
const assignment = referencePath.parentPath.parent; | ||
const isValid = | ||
assignment.type === "AssignmentExpression" && | ||
assignment.left.type === "MemberExpression" && | ||
assignment.left.object === referencePath.node; | ||
if (!isValid) { | ||
const assignment = referencePath.parentPath?.parent; | ||
if (assignment?.type !== "AssignmentExpression") { | ||
return; | ||
} | ||
const memberPropertyName = getMemberPropertyName(assignment.left); | ||
const left = assignment.left; | ||
if (left.type !== "MemberExpression" || left.object !== referencePath.node) { | ||
return; | ||
} | ||
const memberPropertyName = getMemberPropertyName(left); | ||
if (memberPropertyName == null) { | ||
@@ -105,5 +109,2 @@ return; | ||
function getMemberPropertyName(node) { | ||
if (node.type !== "MemberExpression") { | ||
return null; | ||
} | ||
if (node.property.type === "Identifier") { | ||
@@ -128,11 +129,19 @@ return node.property.name; | ||
const { moduleName, requireFnName } = module; | ||
const parentPath = path.parentPath; | ||
if (parentPath == null) { | ||
return null; | ||
} | ||
const grandParentPath = parentPath.parentPath; | ||
if (grandParentPath == null) { | ||
return null; | ||
} | ||
const isValid = | ||
path.parent.type === "VariableDeclarator" && | ||
path.parent.id.type === "Identifier" && | ||
path.parentPath.parent.type === "VariableDeclaration" && | ||
path.parentPath.parentPath.parent.type === "Program"; | ||
return !isValid || path.parentPath.node == null | ||
parentPath.parent.type === "VariableDeclaration" && | ||
grandParentPath.parent.type === "Program"; | ||
return !isValid || parentPath.node == null | ||
? null | ||
: { | ||
declarationPath: path.parentPath, | ||
declarationPath: parentPath, | ||
moduleName, | ||
@@ -148,15 +157,35 @@ requireFnName, | ||
const { moduleName, requireFnName } = module; | ||
const isValid = | ||
path.parent.type === "MemberExpression" && | ||
path.parentPath.parent.type === "VariableDeclarator" && | ||
path.parentPath.parent.id.type === "Identifier" && | ||
path.parentPath.parentPath.parent.type === "VariableDeclaration" && | ||
path.parentPath.parentPath.parentPath.parent.type === "Program"; | ||
const memberPropertyName = getMemberPropertyName(path.parent); | ||
return !isValid || | ||
path.parentPath.parentPath.node == null || | ||
const parent = path.parent; | ||
const parentPath = path.parentPath; | ||
if (parentPath == null) { | ||
return null; | ||
} | ||
const grandParentPath = parentPath.parentPath; | ||
if (grandParentPath == null) { | ||
return null; | ||
} | ||
if (parent.type !== "MemberExpression") { | ||
return null; | ||
} | ||
const memberExpression = parent; | ||
if (parentPath.parent.type !== "VariableDeclarator") { | ||
return null; | ||
} | ||
const variableDeclarator = parentPath.parent; | ||
if (variableDeclarator.id.type !== "Identifier") { | ||
return null; | ||
} | ||
if ( | ||
grandParentPath.parent.type !== "VariableDeclaration" || | ||
grandParentPath.parentPath?.parent.type !== "Program" || | ||
grandParentPath.node == null | ||
) { | ||
return null; | ||
} | ||
const memberPropertyName = getMemberPropertyName(memberExpression); | ||
return memberPropertyName == null || | ||
isExcludedMemberAssignment(moduleName, memberPropertyName, state) | ||
? null | ||
: { | ||
declarationPath: path.parentPath.parentPath, | ||
declarationPath: grandParentPath, | ||
moduleName, | ||
@@ -181,15 +210,24 @@ requireFnName, | ||
if (moduleName == null) { | ||
moduleName = | ||
node["arguments"][0].type === "CallExpression" && | ||
node["arguments"][0].callee.type === "MemberExpression" && | ||
node["arguments"][0].callee.object.type === "Identifier" && | ||
state.inlineableCalls.has(node["arguments"][0].callee.object.name) && | ||
node["arguments"][0].callee.property.type === "Identifier" && | ||
node["arguments"][0].callee.property.name === "resolve" && | ||
node["arguments"][0]["arguments"].length >= 1 && | ||
node["arguments"][0]["arguments"][0].type === "StringLiteral" | ||
? node["arguments"][0]["arguments"][0].value | ||
: null; | ||
const callNode = node["arguments"][0]; | ||
if ( | ||
callNode.type === "CallExpression" && | ||
callNode.callee.type === "MemberExpression" && | ||
callNode.callee.object.type === "Identifier" | ||
) { | ||
const callee = callNode.callee; | ||
moduleName = | ||
callee.object.type === "Identifier" && | ||
state.inlineableCalls.has(callee.object.name) && | ||
callee.property.type === "Identifier" && | ||
callee.property.name === "resolve" && | ||
callNode["arguments"].length >= 1 && | ||
callNode["arguments"][0].type === "StringLiteral" | ||
? callNode["arguments"][0].value | ||
: null; | ||
} | ||
} | ||
const fnName = node.callee.name; | ||
if (fnName == null) { | ||
return null; | ||
} | ||
const isRequireInScope = path.scope.getBinding(fnName) != null; | ||
@@ -196,0 +234,0 @@ return moduleName == null || |
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
86507
17
1179