UNPKG

astx

Version:

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

349 lines (300 loc) 9 kB
'use strict' var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault') var _typeof = require('@babel/runtime/helpers/typeof') Object.defineProperty(exports, '__esModule', { value: true, }) exports['default'] = replace exports.replaceAll = replaceAll var _toConsumableArray2 = _interopRequireDefault( require('@babel/runtime/helpers/toConsumableArray') ) var _compileReplacement = _interopRequireDefault( require('./compileReplacement') ) var _convertReplacement = _interopRequireWildcard( require('./convertReplacement') ) var _pipeline = _interopRequireDefault(require('./util/pipeline')) var _lodash = require('lodash') function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== 'function') return null var cacheBabelInterop = new WeakMap() var cacheNodeInterop = new WeakMap() return (_getRequireWildcardCache = function _getRequireWildcardCache( nodeInterop ) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop })(nodeInterop) } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj } if ( obj === null || (_typeof(obj) !== 'object' && typeof obj !== 'function') ) { return { default: obj, } } var cache = _getRequireWildcardCache(nodeInterop) if (cache && cache.has(obj)) { return cache.get(obj) } var newObj = {} var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor for (var key in obj) { if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc) } else { newObj[key] = obj[key] } } } newObj['default'] = obj if (cache) { cache.set(obj, newObj) } return newObj } function _createForOfIteratorHelper(o, allowArrayLike) { var it = (typeof Symbol !== 'undefined' && o[Symbol.iterator]) || o['@@iterator'] if (!it) { if ( Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || (allowArrayLike && o && typeof o.length === 'number') ) { if (it) o = it var i = 0 var F = function F() {} return { s: F, n: function n() { if (i >= o.length) return { done: true, } return { done: false, value: o[i++], } }, e: function e(_e) { throw _e }, f: F, } } throw new TypeError( 'Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.' ) } var normalCompletion = true, didErr = false, err return { s: function s() { it = it.call(o) }, n: function n() { var step = it.next() normalCompletion = step.done return step }, e: function e(_e2) { didErr = true err = _e2 }, f: function f() { try { if (!normalCompletion && it['return'] != null) it['return']() } finally { if (didErr) throw err } }, } } function _unsupportedIterableToArray(o, minLen) { if (!o) return if (typeof o === 'string') return _arrayLikeToArray(o, minLen) var n = Object.prototype.toString.call(o).slice(8, -1) if (n === 'Object' && o.constructor) n = o.constructor.name if (n === 'Map' || n === 'Set') return Array.from(o) if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen) } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i] } return arr2 } function replace(match, replace, _ref) { var _match$path$parentPat var backend = _ref.backend var path = ((_match$path$parentPat = match.path.parentPath) === null || _match$path$parentPat === void 0 ? void 0 : _match$path$parentPat.node.type) === 'ExpressionStatement' ? match.path.parentPath : match.path var replacements = (0, _toConsumableArray2['default'])( (0, _convertReplacement.bulkConvert)( (replace instanceof Object && typeof replace.generate === 'function' ? replace : (0, _compileReplacement['default'])( Array.isArray(replace) ? replace.map(function (n) { return new backend.t.NodePath(n) }) : new backend.t.NodePath(replace), { backend: backend, } ) ).generate(match), (0, _convertReplacement['default'])(path) ) ) doReplace(match, replacements) } function replaceAll(matches, replace, _ref2) { var backend = _ref2.backend var _iterator = _createForOfIteratorHelper(matches), _step try { for (_iterator.s(); !(_step = _iterator.n()).done; ) { var _match$path$parentPat2 var _match = _step.value var path = ((_match$path$parentPat2 = _match.path.parentPath) === null || _match$path$parentPat2 === void 0 ? void 0 : _match$path$parentPat2.node.type) === 'ExpressionStatement' ? _match.path.parentPath : _match.path var replacements = (0, _toConsumableArray2['default'])( (0, _convertReplacement.bulkConvert)( (replace instanceof Object && typeof replace.generate === 'function' ? replace : (0, _compileReplacement['default'])( (0, _pipeline['default'])( typeof replace === 'function' ? replace(_match) : replace, function (replacement) { return Array.isArray(replacement) ? replacement.map(function (n) { return new backend.t.NodePath(n) }) : new backend.t.NodePath(replacement) } ), { backend: backend, } ) ).generate(_match), (0, _convertReplacement['default'])(path) ) ) doReplace(_match, replacements) } } catch (err) { _iterator.e(err) } finally { _iterator.f() } } function doReplace(match, replacements) { var _replacedPaths$ var replacedPaths = match.paths.map(function (p) { var _p$parentPath return ((_p$parentPath = p.parentPath) === null || _p$parentPath === void 0 ? void 0 : _p$parentPath.node.type) === 'ExpressionStatement' ? p.parentPath : p }) transferComments(replacedPaths[0], replacements[0], { leading: true, }) transferComments( (0, _lodash.last)(replacedPaths), (0, _lodash.last)(replacements), { trailing: true, } ) ;(_replacedPaths$ = replacedPaths[0]) === null || _replacedPaths$ === void 0 ? void 0 : _replacedPaths$.replace.apply( _replacedPaths$, (0, _toConsumableArray2['default'])(replacements) ) for (var i = 1; i < replacedPaths.length; i++) { replacedPaths[i].prune() } } function transferComments(from, to, options) { var _node$comments, _node$comments2 if (!from || !to) return var node = from.node var leading = options.leading ? ((_node$comments = node.comments) === null || _node$comments === void 0 ? void 0 : _node$comments.filter(function (c) { return c.leading })) || node.leadingComments : undefined if (leading !== null && leading !== void 0 && leading.length) { var dest = node.comments ? to.comments || (to.comments = []) : to.leadingComments || (to.leadingComments = []) var _iterator2 = _createForOfIteratorHelper(leading), _step2 try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done; ) { var c = _step2.value dest.push(c) } } catch (err) { _iterator2.e(err) } finally { _iterator2.f() } } var trailing = options.trailing ? ((_node$comments2 = node.comments) === null || _node$comments2 === void 0 ? void 0 : _node$comments2.filter(function (c) { return c.trailing })) || node.trailingComments : undefined if (trailing !== null && trailing !== void 0 && trailing.length) { var _dest = node.comments ? to.comments || (to.comments = []) : to.trailingComments || (to.trailingComments = []) var _iterator3 = _createForOfIteratorHelper(trailing), _step3 try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done; ) { var _c = _step3.value _dest.push(_c) } } catch (err) { _iterator3.e(err) } finally { _iterator3.f() } } }