inline-loops.macro
Advanced tools
Comparing version 2.0.0-beta.1 to 2.0.0-beta.2
@@ -12,3 +12,2 @@ "use strict"; | ||
exports.handleInvalidUsage = handleInvalidUsage; | ||
exports.isConditionalUsage = isConditionalUsage; | ||
exports.isMacroHandlerName = isMacroHandlerName; | ||
@@ -18,2 +17,3 @@ exports.processNestedInlineLoopMacros = processNestedInlineLoopMacros; | ||
exports.replaceOrRemove = replaceOrRemove; | ||
exports.shouldWrapInClosure = shouldWrapInClosure; | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
@@ -98,6 +98,2 @@ var _babelPluginMacros = require("babel-plugin-macros"); | ||
} | ||
function isConditionalUsage(path) { | ||
var parentPath = path.parentPath; | ||
return parentPath.isConditionalExpression() || parentPath.isLogicalExpression(); | ||
} | ||
function isMacroHandlerName(handlers, name) { | ||
@@ -123,8 +119,4 @@ return !!(name && handlers[name]); | ||
function replaceOrRemove(_ref, path, local, templates, replacement) { | ||
var _functionParent$get; | ||
var t = _ref.types; | ||
var functionParent = path.getFunctionParent(); | ||
var contents = functionParent === null || functionParent === void 0 || (_functionParent$get = functionParent.get('body')) === null || _functionParent$get === void 0 ? void 0 : _functionParent$get.get('body'); | ||
var shouldWrapInIife = functionParent && (Array.isArray(contents) && contents.length > 1 || local.contents.length > 1 || isConditionalUsage(path)); | ||
if (shouldWrapInIife) { | ||
if (shouldWrapInClosure(path, local)) { | ||
if (!t.isIdentifier(replacement, { | ||
@@ -154,2 +146,34 @@ name: 'undefined' | ||
} | ||
} | ||
function shouldWrapInClosure(path, local) { | ||
var _functionParent$get; | ||
var parentPath = path.parentPath; | ||
if (!parentPath) { | ||
return false; | ||
} | ||
var functionParent = path.getFunctionParent(); | ||
var grandparentPath = parentPath.parentPath; | ||
if (!functionParent || !grandparentPath) { | ||
return false; | ||
} | ||
if (parentPath.isPattern() || local.contents.length > 1) { | ||
return true; | ||
} | ||
var contents = functionParent === null || functionParent === void 0 || (_functionParent$get = functionParent.get('body')) === null || _functionParent$get === void 0 ? void 0 : _functionParent$get.get('body'); | ||
if (Array.isArray(contents) && contents.length > 1) { | ||
return contents.flat().some(function (content) { | ||
return content.isVariableDeclaration(); | ||
}); | ||
} | ||
if (!parentPath.isExpression()) { | ||
return false; | ||
} | ||
var maybeNestedConditional = grandparentPath.isPattern() || grandparentPath.isExpression() && !grandparentPath.isBinaryExpression(); | ||
if (parentPath.isLogicalExpression()) { | ||
return !maybeNestedConditional && parentPath.get('right').node === path.node; | ||
} | ||
if (parentPath.isConditionalExpression()) { | ||
return !maybeNestedConditional && parentPath.get('test').node !== path.node; | ||
} | ||
return maybeNestedConditional; | ||
} |
@@ -61,3 +61,3 @@ { | ||
"typings": "./index.d.ts", | ||
"version": "2.0.0-beta.1" | ||
"version": "2.0.0-beta.2" | ||
} |
66170
1263