@babel/plugin-transform-react-constant-elements
Advanced tools
Comparing version 7.18.12 to 7.20.2
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
var _core = require("@babel/core"); | ||
var _default = (0, _helperPluginUtils.declare)((api, options) => { | ||
@@ -18,3 +15,2 @@ api.assertVersion(7); | ||
} = options; | ||
if (allowMutablePropsOnTags != null && !Array.isArray(allowMutablePropsOnTags)) { | ||
@@ -25,3 +21,2 @@ throw new Error(".allowMutablePropsOnTags must be an array, null, or undefined."); | ||
const HOISTED = new WeakMap(); | ||
function declares(node, scope) { | ||
@@ -42,6 +37,4 @@ if (_core.types.isJSXIdentifier(node, { | ||
} | ||
return scope.hasOwnBinding(node.name); | ||
} | ||
function isHoistingScope({ | ||
@@ -52,9 +45,27 @@ path | ||
} | ||
function getHoistingScope(scope) { | ||
while (!isHoistingScope(scope)) scope = scope.parent; | ||
return scope; | ||
} | ||
const targetScopeVisitor = { | ||
ReferencedIdentifier(path, state) { | ||
const { | ||
node | ||
} = path; | ||
let { | ||
scope | ||
} = path; | ||
while (scope !== state.jsxScope) { | ||
if (declares(node, scope)) return; | ||
scope = scope.parent; | ||
} | ||
while (scope) { | ||
if (scope === state.targetScope) return; | ||
if (declares(node, scope)) break; | ||
scope = scope.parent; | ||
} | ||
state.targetScope = getHoistingScope(scope); | ||
} | ||
}; | ||
const immutabilityVisitor = { | ||
@@ -66,7 +77,5 @@ enter(path, state) { | ||
}; | ||
const skip = () => { | ||
path.skip(); | ||
}; | ||
if (path.isJSXClosingElement()) return skip(); | ||
@@ -94,3 +103,2 @@ | ||
} = state; | ||
if (mutablePropsAllowed && path.isFunction()) { | ||
@@ -100,6 +108,5 @@ path.traverse(targetScopeVisitor, state); | ||
} | ||
if (!path.isPure()) return stop(); | ||
if (!path.isPure()) return stop(); | ||
const expressionResult = path.evaluate(); | ||
if (expressionResult.confident) { | ||
@@ -109,3 +116,2 @@ const { | ||
} = expressionResult; | ||
if (mutablePropsAllowed || value === null || typeof value !== "object" && typeof value !== "function") { | ||
@@ -117,31 +123,6 @@ return skip(); | ||
} | ||
stop(); | ||
} | ||
}; | ||
const targetScopeVisitor = { | ||
ReferencedIdentifier(path, state) { | ||
const { | ||
node | ||
} = path; | ||
let { | ||
scope | ||
} = path; | ||
while (scope !== state.jsxScope) { | ||
if (declares(node, scope)) return; | ||
scope = scope.parent; | ||
} | ||
while (scope) { | ||
if (scope === state.targetScope) return; | ||
if (declares(node, scope)) break; | ||
scope = scope.parent; | ||
} | ||
state.targetScope = getHoistingScope(scope); | ||
} | ||
}; | ||
const hoistingVisitor = Object.assign({}, immutabilityVisitor, targetScopeVisitor); | ||
@@ -153,14 +134,11 @@ return { | ||
var _jsxScope; | ||
if (HOISTED.has(path.node)) return; | ||
const name = path.node.openingElement.name; | ||
let mutablePropsAllowed = false; | ||
if (allowMutablePropsOnTags != null) { | ||
let lastSegment = name; | ||
while (_core.types.isJSXMemberExpression(lastSegment)) { | ||
lastSegment = lastSegment.property; | ||
} | ||
const elementName = lastSegment.name; | ||
@@ -172,3 +150,2 @@ mutablePropsAllowed = allowMutablePropsOnTags.includes(elementName); | ||
let current = path; | ||
while (!jsxScope && current.parentPath.isJSX()) { | ||
@@ -178,3 +155,2 @@ current = current.parentPath; | ||
} | ||
(_jsxScope = jsxScope) != null ? _jsxScope : jsxScope = path.scope; | ||
@@ -193,3 +169,2 @@ HOISTED.set(path.node, jsxScope); | ||
} = visitorState; | ||
for (let currentScope = jsxScope;;) { | ||
@@ -199,3 +174,2 @@ if (targetScope === currentScope) return; | ||
currentScope = currentScope.parent; | ||
if (!currentScope) { | ||
@@ -205,3 +179,2 @@ throw new Error("Internal @babel/plugin-transform-react-constant-elements error: " + "targetScope must be an ancestor of jsxScope. " + "This is a Babel bug, please report it."); | ||
} | ||
const id = path.scope.generateUidBasedOnNode(name); | ||
@@ -215,14 +188,12 @@ targetScope.push({ | ||
`; | ||
if (path.parentPath.isJSXElement() || path.parentPath.isJSXAttribute()) { | ||
replacement = _core.types.jsxExpressionContainer(replacement); | ||
} | ||
path.replaceWith(replacement); | ||
} | ||
} | ||
}; | ||
}); | ||
exports.default = _default; | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/plugin-transform-react-constant-elements", | ||
"version": "7.18.12", | ||
"version": "7.20.2", | ||
"description": "Treat React JSX elements as value types and hoist them to the highest scope", | ||
@@ -20,3 +20,3 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.18.9" | ||
"@babel/helper-plugin-utils": "^7.20.2" | ||
}, | ||
@@ -27,3 +27,3 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.18.10", | ||
"@babel/core": "^7.20.2", | ||
"@babel/helper-plugin-test-runner": "^7.18.6" | ||
@@ -30,0 +30,0 @@ }, |
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
22850
5
170