diff --git a/src/utility/check-func.js b/src/utility/check-func.js new file mode 100644 index 0000000..eb8b0b0 --- /dev/null +++ b/src/utility/check-func.js @@ -0,0 +1,15 @@ +function checkPattern(code, pattern) { + let i = 0 + let j = 0 + while (i < code.length && j < pattern.length) { + if (code[i] == pattern[j]) { + ++j + } + ++i + } + return j == pattern.length +} + +export default { + checkPattern, +} diff --git a/src/utility/safe-func.js b/src/utility/safe-func.js new file mode 100644 index 0000000..e6d3c07 --- /dev/null +++ b/src/utility/safe-func.js @@ -0,0 +1,72 @@ +import * as t from '@babel/types' + +function safeDeleteNode(name, path) { + let binding + if (path.isFunctionDeclaration()) { + binding = path.parentPath.scope.getBinding(name) + } else { + binding = path.scope.getBinding(name) + } + if (!binding) { + return false + } + binding.scope.crawl() + binding = binding.scope.getBinding(name) + if (binding.references) { + return false + } + for (const item of binding.constantViolations) { + item.remove() + } + const decl = binding.path + if (decl.removed) { + return true + } + if (!decl.isVariableDeclarator() && !decl.isFunctionDeclaration()) { + return true + } + binding.path.remove() + return true +} + +function safeGetLiteral(path) { + if (path.isUnaryExpression()) { + if (path.node.operator === '-' && path.get('argument').isNumericLiteral()) { + return -1 * path.get('argument').node.value + } + return null + } + if (path.isLiteral()) { + return path.node.value + } + return null +} + +function safeGetName(path) { + if (path.isIdentifier()) { + return path.node.name + } + if (path.isLiteral()) { + return path.node.value + } + return null +} + +function safeReplace(path, value) { + if (typeof value === 'string') { + path.replaceWith(t.stringLiteral(value)) + return + } + if (typeof value === 'number') { + path.replaceWith(t.numericLiteral(value)) + return + } + path.replaceWithSourceString(value) +} + +export default { + safeDeleteNode, + safeGetLiteral, + safeGetName, + safeReplace, +}