patcher.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getReprinter = exports.Patcher = void 0;
  4. var tslib_1 = require("tslib");
  5. var assert_1 = tslib_1.__importDefault(require("assert"));
  6. var linesModule = tslib_1.__importStar(require("./lines"));
  7. var types = tslib_1.__importStar(require("ast-types"));
  8. var Printable = types.namedTypes.Printable;
  9. var Expression = types.namedTypes.Expression;
  10. var ReturnStatement = types.namedTypes.ReturnStatement;
  11. var SourceLocation = types.namedTypes.SourceLocation;
  12. var util_1 = require("./util");
  13. var fast_path_1 = tslib_1.__importDefault(require("./fast-path"));
  14. var isObject = types.builtInTypes.object;
  15. var isArray = types.builtInTypes.array;
  16. var isString = types.builtInTypes.string;
  17. var riskyAdjoiningCharExp = /[0-9a-z_$]/i;
  18. var Patcher = function Patcher(lines) {
  19. assert_1.default.ok(this instanceof Patcher);
  20. assert_1.default.ok(lines instanceof linesModule.Lines);
  21. var self = this, replacements = [];
  22. self.replace = function (loc, lines) {
  23. if (isString.check(lines))
  24. lines = linesModule.fromString(lines);
  25. replacements.push({
  26. lines: lines,
  27. start: loc.start,
  28. end: loc.end,
  29. });
  30. };
  31. self.get = function (loc) {
  32. // If no location is provided, return the complete Lines object.
  33. loc = loc || {
  34. start: { line: 1, column: 0 },
  35. end: { line: lines.length, column: lines.getLineLength(lines.length) },
  36. };
  37. var sliceFrom = loc.start, toConcat = [];
  38. function pushSlice(from, to) {
  39. assert_1.default.ok((0, util_1.comparePos)(from, to) <= 0);
  40. toConcat.push(lines.slice(from, to));
  41. }
  42. replacements
  43. .sort(function (a, b) { return (0, util_1.comparePos)(a.start, b.start); })
  44. .forEach(function (rep) {
  45. if ((0, util_1.comparePos)(sliceFrom, rep.start) > 0) {
  46. // Ignore nested replacement ranges.
  47. }
  48. else {
  49. pushSlice(sliceFrom, rep.start);
  50. toConcat.push(rep.lines);
  51. sliceFrom = rep.end;
  52. }
  53. });
  54. pushSlice(sliceFrom, loc.end);
  55. return linesModule.concat(toConcat);
  56. };
  57. };
  58. exports.Patcher = Patcher;
  59. var Pp = Patcher.prototype;
  60. Pp.tryToReprintComments = function (newNode, oldNode, print) {
  61. var patcher = this;
  62. if (!newNode.comments && !oldNode.comments) {
  63. // We were (vacuously) able to reprint all the comments!
  64. return true;
  65. }
  66. var newPath = fast_path_1.default.from(newNode);
  67. var oldPath = fast_path_1.default.from(oldNode);
  68. newPath.stack.push("comments", getSurroundingComments(newNode));
  69. oldPath.stack.push("comments", getSurroundingComments(oldNode));
  70. var reprints = [];
  71. var ableToReprintComments = findArrayReprints(newPath, oldPath, reprints);
  72. // No need to pop anything from newPath.stack or oldPath.stack, since
  73. // newPath and oldPath are fresh local variables.
  74. if (ableToReprintComments && reprints.length > 0) {
  75. reprints.forEach(function (reprint) {
  76. var oldComment = reprint.oldPath.getValue();
  77. assert_1.default.ok(oldComment.leading || oldComment.trailing);
  78. patcher.replace(oldComment.loc,
  79. // Comments can't have .comments, so it doesn't matter whether we
  80. // print with comments or without.
  81. print(reprint.newPath).indentTail(oldComment.loc.indent));
  82. });
  83. }
  84. return ableToReprintComments;
  85. };
  86. // Get all comments that are either leading or trailing, ignoring any
  87. // comments that occur inside node.loc. Returns an empty array for nodes
  88. // with no leading or trailing comments.
  89. function getSurroundingComments(node) {
  90. var result = [];
  91. if (node.comments && node.comments.length > 0) {
  92. node.comments.forEach(function (comment) {
  93. if (comment.leading || comment.trailing) {
  94. result.push(comment);
  95. }
  96. });
  97. }
  98. return result;
  99. }
  100. Pp.deleteComments = function (node) {
  101. if (!node.comments) {
  102. return;
  103. }
  104. var patcher = this;
  105. node.comments.forEach(function (comment) {
  106. if (comment.leading) {
  107. // Delete leading comments along with any trailing whitespace they
  108. // might have.
  109. patcher.replace({
  110. start: comment.loc.start,
  111. end: node.loc.lines.skipSpaces(comment.loc.end, false, false),
  112. }, "");
  113. }
  114. else if (comment.trailing) {
  115. // Delete trailing comments along with any leading whitespace they
  116. // might have.
  117. patcher.replace({
  118. start: node.loc.lines.skipSpaces(comment.loc.start, true, false),
  119. end: comment.loc.end,
  120. }, "");
  121. }
  122. });
  123. };
  124. function getReprinter(path) {
  125. assert_1.default.ok(path instanceof fast_path_1.default);
  126. // Make sure that this path refers specifically to a Node, rather than
  127. // some non-Node subproperty of a Node.
  128. var node = path.getValue();
  129. if (!Printable.check(node))
  130. return;
  131. var orig = node.original;
  132. var origLoc = orig && orig.loc;
  133. var lines = origLoc && origLoc.lines;
  134. var reprints = [];
  135. if (!lines || !findReprints(path, reprints))
  136. return;
  137. return function (print) {
  138. var patcher = new Patcher(lines);
  139. reprints.forEach(function (reprint) {
  140. var newNode = reprint.newPath.getValue();
  141. var oldNode = reprint.oldPath.getValue();
  142. SourceLocation.assert(oldNode.loc, true);
  143. var needToPrintNewPathWithComments = !patcher.tryToReprintComments(newNode, oldNode, print);
  144. if (needToPrintNewPathWithComments) {
  145. // Since we were not able to preserve all leading/trailing
  146. // comments, we delete oldNode's comments, print newPath with
  147. // comments, and then patch the resulting lines where oldNode used
  148. // to be.
  149. patcher.deleteComments(oldNode);
  150. }
  151. var newLines = print(reprint.newPath, {
  152. includeComments: needToPrintNewPathWithComments,
  153. // If the oldNode we're replacing already had parentheses, we may
  154. // not need to print the new node with any extra parentheses,
  155. // because the existing parentheses will suffice. However, if the
  156. // newNode has a different type than the oldNode, let the printer
  157. // decide if reprint.newPath needs parentheses, as usual.
  158. avoidRootParens: oldNode.type === newNode.type && reprint.oldPath.hasParens(),
  159. }).indentTail(oldNode.loc.indent);
  160. var nls = needsLeadingSpace(lines, oldNode.loc, newLines);
  161. var nts = needsTrailingSpace(lines, oldNode.loc, newLines);
  162. // If we try to replace the argument of a ReturnStatement like
  163. // return"asdf" with e.g. a literal null expression, we run the risk
  164. // of ending up with returnnull, so we need to add an extra leading
  165. // space in situations where that might happen. Likewise for
  166. // "asdf"in obj. See #170.
  167. if (nls || nts) {
  168. var newParts = [];
  169. nls && newParts.push(" ");
  170. newParts.push(newLines);
  171. nts && newParts.push(" ");
  172. newLines = linesModule.concat(newParts);
  173. }
  174. patcher.replace(oldNode.loc, newLines);
  175. });
  176. // Recall that origLoc is the .loc of an ancestor node that is
  177. // guaranteed to contain all the reprinted nodes and comments.
  178. var patchedLines = patcher.get(origLoc).indentTail(-orig.loc.indent);
  179. if (path.needsParens()) {
  180. return linesModule.concat(["(", patchedLines, ")"]);
  181. }
  182. return patchedLines;
  183. };
  184. }
  185. exports.getReprinter = getReprinter;
  186. // If the last character before oldLoc and the first character of newLines
  187. // are both identifier characters, they must be separated by a space,
  188. // otherwise they will most likely get fused together into a single token.
  189. function needsLeadingSpace(oldLines, oldLoc, newLines) {
  190. var posBeforeOldLoc = (0, util_1.copyPos)(oldLoc.start);
  191. // The character just before the location occupied by oldNode.
  192. var charBeforeOldLoc = oldLines.prevPos(posBeforeOldLoc) && oldLines.charAt(posBeforeOldLoc);
  193. // First character of the reprinted node.
  194. var newFirstChar = newLines.charAt(newLines.firstPos());
  195. return (charBeforeOldLoc &&
  196. riskyAdjoiningCharExp.test(charBeforeOldLoc) &&
  197. newFirstChar &&
  198. riskyAdjoiningCharExp.test(newFirstChar));
  199. }
  200. // If the last character of newLines and the first character after oldLoc
  201. // are both identifier characters, they must be separated by a space,
  202. // otherwise they will most likely get fused together into a single token.
  203. function needsTrailingSpace(oldLines, oldLoc, newLines) {
  204. // The character just after the location occupied by oldNode.
  205. var charAfterOldLoc = oldLines.charAt(oldLoc.end);
  206. var newLastPos = newLines.lastPos();
  207. // Last character of the reprinted node.
  208. var newLastChar = newLines.prevPos(newLastPos) && newLines.charAt(newLastPos);
  209. return (newLastChar &&
  210. riskyAdjoiningCharExp.test(newLastChar) &&
  211. charAfterOldLoc &&
  212. riskyAdjoiningCharExp.test(charAfterOldLoc));
  213. }
  214. function findReprints(newPath, reprints) {
  215. var newNode = newPath.getValue();
  216. Printable.assert(newNode);
  217. var oldNode = newNode.original;
  218. Printable.assert(oldNode);
  219. assert_1.default.deepEqual(reprints, []);
  220. if (newNode.type !== oldNode.type) {
  221. return false;
  222. }
  223. var oldPath = new fast_path_1.default(oldNode);
  224. var canReprint = findChildReprints(newPath, oldPath, reprints);
  225. if (!canReprint) {
  226. // Make absolutely sure the calling code does not attempt to reprint
  227. // any nodes.
  228. reprints.length = 0;
  229. }
  230. return canReprint;
  231. }
  232. function findAnyReprints(newPath, oldPath, reprints) {
  233. var newNode = newPath.getValue();
  234. var oldNode = oldPath.getValue();
  235. if (newNode === oldNode)
  236. return true;
  237. if (isArray.check(newNode))
  238. return findArrayReprints(newPath, oldPath, reprints);
  239. if (isObject.check(newNode))
  240. return findObjectReprints(newPath, oldPath, reprints);
  241. return false;
  242. }
  243. function findArrayReprints(newPath, oldPath, reprints) {
  244. var newNode = newPath.getValue();
  245. var oldNode = oldPath.getValue();
  246. if (newNode === oldNode ||
  247. newPath.valueIsDuplicate() ||
  248. oldPath.valueIsDuplicate()) {
  249. return true;
  250. }
  251. isArray.assert(newNode);
  252. var len = newNode.length;
  253. if (!(isArray.check(oldNode) && oldNode.length === len))
  254. return false;
  255. for (var i = 0; i < len; ++i) {
  256. newPath.stack.push(i, newNode[i]);
  257. oldPath.stack.push(i, oldNode[i]);
  258. var canReprint = findAnyReprints(newPath, oldPath, reprints);
  259. newPath.stack.length -= 2;
  260. oldPath.stack.length -= 2;
  261. if (!canReprint) {
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. function findObjectReprints(newPath, oldPath, reprints) {
  268. var newNode = newPath.getValue();
  269. isObject.assert(newNode);
  270. if (newNode.original === null) {
  271. // If newNode.original node was set to null, reprint the node.
  272. return false;
  273. }
  274. var oldNode = oldPath.getValue();
  275. if (!isObject.check(oldNode))
  276. return false;
  277. if (newNode === oldNode ||
  278. newPath.valueIsDuplicate() ||
  279. oldPath.valueIsDuplicate()) {
  280. return true;
  281. }
  282. if (Printable.check(newNode)) {
  283. if (!Printable.check(oldNode)) {
  284. return false;
  285. }
  286. var newParentNode = newPath.getParentNode();
  287. var oldParentNode = oldPath.getParentNode();
  288. if (oldParentNode !== null &&
  289. oldParentNode.type === "FunctionTypeAnnotation" &&
  290. newParentNode !== null &&
  291. newParentNode.type === "FunctionTypeAnnotation") {
  292. var oldNeedsParens = oldParentNode.params.length !== 1 || !!oldParentNode.params[0].name;
  293. var newNeedParens = newParentNode.params.length !== 1 || !!newParentNode.params[0].name;
  294. if (!oldNeedsParens && newNeedParens) {
  295. return false;
  296. }
  297. }
  298. // Here we need to decide whether the reprinted code for newNode is
  299. // appropriate for patching into the location of oldNode.
  300. if (newNode.type === oldNode.type) {
  301. var childReprints = [];
  302. if (findChildReprints(newPath, oldPath, childReprints)) {
  303. reprints.push.apply(reprints, childReprints);
  304. }
  305. else if (oldNode.loc) {
  306. // If we have no .loc information for oldNode, then we won't be
  307. // able to reprint it.
  308. reprints.push({
  309. oldPath: oldPath.copy(),
  310. newPath: newPath.copy(),
  311. });
  312. }
  313. else {
  314. return false;
  315. }
  316. return true;
  317. }
  318. if (Expression.check(newNode) &&
  319. Expression.check(oldNode) &&
  320. // If we have no .loc information for oldNode, then we won't be
  321. // able to reprint it.
  322. oldNode.loc) {
  323. // If both nodes are subtypes of Expression, then we should be able
  324. // to fill the location occupied by the old node with code printed
  325. // for the new node with no ill consequences.
  326. reprints.push({
  327. oldPath: oldPath.copy(),
  328. newPath: newPath.copy(),
  329. });
  330. return true;
  331. }
  332. // The nodes have different types, and at least one of the types is
  333. // not a subtype of the Expression type, so we cannot safely assume
  334. // the nodes are syntactically interchangeable.
  335. return false;
  336. }
  337. return findChildReprints(newPath, oldPath, reprints);
  338. }
  339. function findChildReprints(newPath, oldPath, reprints) {
  340. var newNode = newPath.getValue();
  341. var oldNode = oldPath.getValue();
  342. isObject.assert(newNode);
  343. isObject.assert(oldNode);
  344. if (newNode.original === null) {
  345. // If newNode.original node was set to null, reprint the node.
  346. return false;
  347. }
  348. // If this node needs parentheses and will not be wrapped with
  349. // parentheses when reprinted, then return false to skip reprinting and
  350. // let it be printed generically.
  351. if (newPath.needsParens() && !oldPath.hasParens()) {
  352. return false;
  353. }
  354. var keys = (0, util_1.getUnionOfKeys)(oldNode, newNode);
  355. if (oldNode.type === "File" || newNode.type === "File") {
  356. // Don't bother traversing file.tokens, an often very large array
  357. // returned by Babylon, and useless for our purposes.
  358. delete keys.tokens;
  359. }
  360. // Don't bother traversing .loc objects looking for reprintable nodes.
  361. delete keys.loc;
  362. var originalReprintCount = reprints.length;
  363. for (var k in keys) {
  364. if (k.charAt(0) === "_") {
  365. // Ignore "private" AST properties added by e.g. Babel plugins and
  366. // parsers like Babylon.
  367. continue;
  368. }
  369. newPath.stack.push(k, types.getFieldValue(newNode, k));
  370. oldPath.stack.push(k, types.getFieldValue(oldNode, k));
  371. var canReprint = findAnyReprints(newPath, oldPath, reprints);
  372. newPath.stack.length -= 2;
  373. oldPath.stack.length -= 2;
  374. if (!canReprint) {
  375. return false;
  376. }
  377. }
  378. // Return statements might end up running into ASI issues due to
  379. // comments inserted deep within the tree, so reprint them if anything
  380. // changed within them.
  381. if (ReturnStatement.check(newPath.getNode()) &&
  382. reprints.length > originalReprintCount) {
  383. return false;
  384. }
  385. return true;
  386. }