@babel/plugin-transform-react-constant-elements
Advanced tools
Comparing version 7.12.13 to 7.13.10
110
lib/index.js
@@ -22,4 +22,36 @@ "use strict"; | ||
const HOISTED = new WeakSet(); | ||
const immutabilityVisitor = { | ||
const HOISTED = new WeakMap(); | ||
function declares(node, scope) { | ||
if (_core.types.isJSXIdentifier(node, { | ||
name: "this" | ||
}) || _core.types.isJSXIdentifier(node, { | ||
name: "arguments" | ||
}) || _core.types.isJSXIdentifier(node, { | ||
name: "super" | ||
}) || _core.types.isJSXIdentifier(node, { | ||
name: "new" | ||
})) { | ||
const { | ||
path | ||
} = scope; | ||
return path.isFunctionParent() && !path.isArrowFunctionExpression(); | ||
} | ||
return scope.hasOwnBinding(node.name); | ||
} | ||
function isHoistingScope({ | ||
path | ||
}) { | ||
return path.isFunctionParent() || path.isLoop() || path.isProgram(); | ||
} | ||
function getHoistingScope(scope) { | ||
while (!isHoistingScope(scope)) scope = scope.parent; | ||
return scope; | ||
} | ||
const analyzer = { | ||
enter(path, state) { | ||
@@ -44,3 +76,3 @@ const stop = () => { | ||
if (path.isJSXIdentifier() || path.isIdentifier() || path.isJSXMemberExpression()) { | ||
if (path.isJSXIdentifier() || path.isIdentifier() || path.isJSXMemberExpression() || path.isJSXNamespacedName()) { | ||
return; | ||
@@ -70,2 +102,19 @@ } | ||
} | ||
}, | ||
ReferencedIdentifier(path, state) { | ||
const { | ||
node | ||
} = path; | ||
let { | ||
scope | ||
} = path; | ||
while (scope) { | ||
if (scope === state.targetScope) return; | ||
if (declares(node, scope)) break; | ||
scope = scope.parent; | ||
} | ||
state.targetScope = getHoistingScope(scope); | ||
} | ||
@@ -78,21 +127,54 @@ | ||
JSXElement(path) { | ||
var _jsxScope; | ||
if (HOISTED.has(path.node)) return; | ||
HOISTED.add(path.node); | ||
const state = { | ||
isImmutable: true | ||
}; | ||
HOISTED.set(path.node, path.scope); | ||
const name = path.node.openingElement.name; | ||
let mutablePropsAllowed = false; | ||
if (allowMutablePropsOnTags != null) { | ||
let namePath = path.get("openingElement.name"); | ||
let lastSegment = name; | ||
while (namePath.isJSXMemberExpression()) { | ||
namePath = namePath.get("property"); | ||
while (_core.types.isJSXMemberExpression(lastSegment)) { | ||
lastSegment = lastSegment.property; | ||
} | ||
const elementName = namePath.node.name; | ||
state.mutablePropsAllowed = allowMutablePropsOnTags.indexOf(elementName) > -1; | ||
const elementName = lastSegment.name; | ||
mutablePropsAllowed = allowMutablePropsOnTags.includes(elementName); | ||
} | ||
path.traverse(immutabilityVisitor, state); | ||
if (state.isImmutable) path.hoist(); | ||
const state = { | ||
isImmutable: true, | ||
mutablePropsAllowed, | ||
targetScope: path.scope.getProgramParent() | ||
}; | ||
path.traverse(analyzer, state); | ||
if (!state.isImmutable) return; | ||
const { | ||
targetScope | ||
} = state; | ||
HOISTED.set(path.node, targetScope); | ||
let jsxScope; | ||
let current = path; | ||
while (!jsxScope && current.parentPath.isJSX()) { | ||
current = current.parentPath; | ||
jsxScope = HOISTED.get(current.node); | ||
} | ||
(_jsxScope = jsxScope) != null ? _jsxScope : jsxScope = getHoistingScope(path.scope); | ||
if (targetScope === jsxScope) return; | ||
const id = path.scope.generateUidBasedOnNode(name); | ||
targetScope.push({ | ||
id: _core.types.identifier(id) | ||
}); | ||
let replacement = _core.template.expression.ast` | ||
${_core.types.identifier(id)} || (${_core.types.identifier(id)} = ${path.node}) | ||
`; | ||
if (path.parentPath.isJSXElement() || path.parentPath.isJSXAttribute()) { | ||
replacement = _core.types.jsxExpressionContainer(replacement); | ||
} | ||
path.replaceWith(replacement); | ||
} | ||
@@ -99,0 +181,0 @@ |
{ | ||
"name": "@babel/plugin-transform-react-constant-elements", | ||
"version": "7.12.13", | ||
"version": "7.13.10", | ||
"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.12.13" | ||
"@babel/helper-plugin-utils": "^7.13.0" | ||
}, | ||
@@ -27,5 +27,5 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "7.12.13", | ||
"@babel/helper-plugin-test-runner": "7.12.13" | ||
"@babel/core": "7.13.10", | ||
"@babel/helper-plugin-test-runner": "7.13.10" | ||
} | ||
} |
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
7064
147