UNPKG

astx

Version:

super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring

182 lines (145 loc) 6.99 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.capturesAreEquivalent = capturesAreEquivalent; exports.compileArrayPlaceholderMatcher = compileArrayPlaceholderMatcher; exports.compileRestPlaceholderMatcher = compileRestPlaceholderMatcher; exports.compileStringPlaceholderMatcher = compileStringPlaceholderMatcher; exports["default"] = compilePlaceholderMatcher; exports.getArrayPlaceholder = getArrayPlaceholder; exports.getPlaceholder = getPlaceholder; exports.getRestPlaceholder = getRestPlaceholder; exports.isCapturePlaceholder = isCapturePlaceholder; exports.isPlaceholder = isPlaceholder; exports.unescapeIdentifier = unescapeIdentifier; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _ = require("."); var _convertToJSXIdentifierName = _interopRequireDefault(require("../convertReplacement/convertToJSXIdentifierName")); function unescapeIdentifier(identifier) { return identifier.replace(/^\$_/, '$'); } function getPlaceholder(identifier) { var _exec; return (_exec = /^\$([a-z0-9]+.*)?$/i.exec(identifier)) === null || _exec === void 0 ? void 0 : _exec[0]; } function getArrayPlaceholder(identifier) { var _exec2; return (_exec2 = /^\${2}([a-z0-9]+.*)?$/i.exec(identifier)) === null || _exec2 === void 0 ? void 0 : _exec2[0]; } function getRestPlaceholder(identifier) { var _exec3; return (_exec3 = /^\${3}([a-z0-9]+.*)?$/i.exec(identifier)) === null || _exec3 === void 0 ? void 0 : _exec3[0]; } function isPlaceholder(identifier) { var _exec4; return (_exec4 = /^\${1,3}([a-z0-9]+.*)?$/i.exec(identifier)) === null || _exec4 === void 0 ? void 0 : _exec4[0]; } function isCapturePlaceholder(identifier) { return /[^$]/.test(identifier); } function compileArrayPlaceholderMatcher(pattern, identifier, // eslint-disable-next-line @typescript-eslint/no-unused-vars compileOptions, otherOptions) { var arrayPlaceholder = getArrayPlaceholder(identifier); if (arrayPlaceholder) { return { pattern: pattern, arrayPlaceholder: arrayPlaceholder, nodeType: otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.nodeType, match: function match() { throw new Error("array capture placeholder ".concat(arrayPlaceholder, " is in an invalid position")); } }; } } function compileRestPlaceholderMatcher(pattern, identifier, // eslint-disable-next-line @typescript-eslint/no-unused-vars compileOptions, otherOptions) { var restPlaceholder = getRestPlaceholder(identifier); if (restPlaceholder) { return { pattern: pattern, restPlaceholder: restPlaceholder, nodeType: otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.nodeType, match: function match() { throw new Error("rest capture placeholder ".concat(restPlaceholder, " is in an invalid position")); } }; } } function capturesAreEquivalent(backend, a, b) { if (backend.t.astNodesAreEquivalent(a, b)) return true; var aIdent = (0, _convertToJSXIdentifierName["default"])(a); var bIdent = (0, _convertToJSXIdentifierName["default"])(b); return aIdent ? aIdent === bIdent : false; } function compilePlaceholderMatcher(pattern, identifier, compileOptions, otherOptions) { var debug = compileOptions.debug; var placeholder = getPlaceholder(identifier); if (placeholder) { var _compileOptions$where, _otherOptions$getCond; var whereCondition = placeholder === '$' ? undefined : compileOptions === null || compileOptions === void 0 ? void 0 : (_compileOptions$where = compileOptions.where) === null || _compileOptions$where === void 0 ? void 0 : _compileOptions$where[placeholder]; var condition = (otherOptions === null || otherOptions === void 0 ? void 0 : (_otherOptions$getCond = otherOptions.getCondition) === null || _otherOptions$getCond === void 0 ? void 0 : _otherOptions$getCond.call(otherOptions)) || function () { return true; }; var nodeType = otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.nodeType; return { pattern: pattern, placeholder: placeholder, nodeType: nodeType, match: function match(path, matchSoFar) { var _matchSoFar$captures; if (path.value == null) return null; debug('Placeholder', placeholder); if (!condition(path)) { debug(' condition returned false'); return null; } if (placeholder === '$') return matchSoFar || {}; var existingCapture = matchSoFar === null || matchSoFar === void 0 ? void 0 : (_matchSoFar$captures = matchSoFar.captures) === null || _matchSoFar$captures === void 0 ? void 0 : _matchSoFar$captures[placeholder]; if (existingCapture) { return capturesAreEquivalent(compileOptions.backend, existingCapture.node, path.value) ? matchSoFar : null; } if (whereCondition && !whereCondition(path)) { debug(' where condition returned false'); return null; } debug(' captured as %s', placeholder); return (0, _.mergeCaptures)(matchSoFar, { captures: (0, _defineProperty2["default"])({}, placeholder, path) }); } }; } return compileArrayPlaceholderMatcher(pattern, identifier, compileOptions, otherOptions) || compileRestPlaceholderMatcher(pattern, identifier, compileOptions, otherOptions); } function compileStringPlaceholderMatcher(pattern, getString, compileOptions, otherOptions) { var debug = compileOptions.debug; var string = getString(pattern.value); if (!string) return; var placeholder = getPlaceholder(string); var nodeType = otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.nodeType; if (placeholder) { return { pattern: pattern, placeholder: placeholder, nodeType: nodeType, match: function match(path, matchSoFar) { var _path$value, _matchSoFar$stringCap; if (((_path$value = path.value) === null || _path$value === void 0 ? void 0 : _path$value.type) !== pattern.value.type) return null; debug('String Placeholder', placeholder); var string = getString(path.value); if (!string) return null; var existingCapture = matchSoFar === null || matchSoFar === void 0 ? void 0 : (_matchSoFar$stringCap = matchSoFar.stringCaptures) === null || _matchSoFar$stringCap === void 0 ? void 0 : _matchSoFar$stringCap[placeholder]; if (existingCapture) { return string === existingCapture ? matchSoFar : null; } debug(' captured as %s', placeholder); return (0, _.mergeCaptures)(matchSoFar, { captures: (0, _defineProperty2["default"])({}, placeholder, path), stringCaptures: (0, _defineProperty2["default"])({}, placeholder, string) }); } }; } }