patcher.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. var assert = require("assert");
  2. var linesModule = require("./lines");
  3. var types = require("./types");
  4. var getFieldValue = types.getFieldValue;
  5. var Printable = types.namedTypes.Printable;
  6. var Expression = types.namedTypes.Expression;
  7. var ReturnStatement = types.namedTypes.ReturnStatement;
  8. var SourceLocation = types.namedTypes.SourceLocation;
  9. var util = require("./util");
  10. var comparePos = util.comparePos;
  11. var FastPath = require("./fast-path");
  12. var isObject = types.builtInTypes.object;
  13. var isArray = types.builtInTypes.array;
  14. var isString = types.builtInTypes.string;
  15. var riskyAdjoiningCharExp = /[0-9a-z_$]/i;
  16. function Patcher(lines) {
  17. assert.ok(this instanceof Patcher);
  18. assert.ok(lines instanceof linesModule.Lines);
  19. var self = this,
  20. replacements = [];
  21. self.replace = function(loc, lines) {
  22. if (isString.check(lines))
  23. lines = linesModule.fromString(lines);
  24. replacements.push({
  25. lines: lines,
  26. start: loc.start,
  27. end: loc.end
  28. });
  29. };
  30. self.get = function(loc) {
  31. // If no location is provided, return the complete Lines object.
  32. loc = loc || {
  33. start: { line: 1, column: 0 },
  34. end: { line: lines.length,
  35. column: lines.getLineLength(lines.length) }
  36. };
  37. var sliceFrom = loc.start,
  38. toConcat = [];
  39. function pushSlice(from, to) {
  40. assert.ok(comparePos(from, to) <= 0);
  41. toConcat.push(lines.slice(from, to));
  42. }
  43. replacements.sort(function(a, b) {
  44. return comparePos(a.start, b.start);
  45. }).forEach(function(rep) {
  46. if (comparePos(sliceFrom, rep.start) > 0) {
  47. // Ignore nested replacement ranges.
  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 &&
  63. !oldNode.comments) {
  64. // We were (vacuously) able to reprint all the comments!
  65. return true;
  66. }
  67. var newPath = FastPath.from(newNode);
  68. var oldPath = FastPath.from(oldNode);
  69. newPath.stack.push("comments", getSurroundingComments(newNode));
  70. oldPath.stack.push("comments", getSurroundingComments(oldNode));
  71. var reprints = [];
  72. var ableToReprintComments =
  73. findArrayReprints(newPath, oldPath, reprints);
  74. // No need to pop anything from newPath.stack or oldPath.stack, since
  75. // newPath and oldPath are fresh local variables.
  76. if (ableToReprintComments && reprints.length > 0) {
  77. reprints.forEach(function(reprint) {
  78. var oldComment = reprint.oldPath.getValue();
  79. assert.ok(oldComment.leading || oldComment.trailing);
  80. patcher.replace(
  81. oldComment.loc,
  82. // Comments can't have .comments, so it doesn't matter
  83. // whether we print with comments or without.
  84. print(reprint.newPath).indentTail(oldComment.loc.indent)
  85. );
  86. });
  87. }
  88. return ableToReprintComments;
  89. };
  90. // Get all comments that are either leading or trailing, ignoring any
  91. // comments that occur inside node.loc. Returns an empty array for nodes
  92. // with no leading or trailing comments.
  93. function getSurroundingComments(node) {
  94. var result = [];
  95. if (node.comments &&
  96. node.comments.length > 0) {
  97. node.comments.forEach(function(comment) {
  98. if (comment.leading || comment.trailing) {
  99. result.push(comment);
  100. }
  101. });
  102. }
  103. return result;
  104. }
  105. Pp.deleteComments = function(node) {
  106. if (!node.comments) {
  107. return;
  108. }
  109. var patcher = this;
  110. node.comments.forEach(function(comment) {
  111. if (comment.leading) {
  112. // Delete leading comments along with any trailing whitespace
  113. // they might have.
  114. patcher.replace({
  115. start: comment.loc.start,
  116. end: node.loc.lines.skipSpaces(
  117. comment.loc.end, false, false)
  118. }, "");
  119. } else if (comment.trailing) {
  120. // Delete trailing comments along with any leading whitespace
  121. // they might have.
  122. patcher.replace({
  123. start: node.loc.lines.skipSpaces(
  124. comment.loc.start, true, false),
  125. end: comment.loc.end
  126. }, "");
  127. }
  128. });
  129. };
  130. exports.getReprinter = function(path) {
  131. assert.ok(path instanceof FastPath);
  132. // Make sure that this path refers specifically to a Node, rather than
  133. // some non-Node subproperty of a Node.
  134. var node = path.getValue();
  135. if (!Printable.check(node))
  136. return;
  137. var orig = node.original;
  138. var origLoc = orig && orig.loc;
  139. var lines = origLoc && origLoc.lines;
  140. var reprints = [];
  141. if (!lines || !findReprints(path, reprints))
  142. return;
  143. return function(print) {
  144. var patcher = new Patcher(lines);
  145. reprints.forEach(function(reprint) {
  146. var newNode = reprint.newPath.getValue();
  147. var oldNode = reprint.oldPath.getValue();
  148. SourceLocation.assert(oldNode.loc, true);
  149. var needToPrintNewPathWithComments =
  150. !patcher.tryToReprintComments(newNode, oldNode, print)
  151. if (needToPrintNewPathWithComments) {
  152. // Since we were not able to preserve all leading/trailing
  153. // comments, we delete oldNode's comments, print newPath
  154. // with comments, and then patch the resulting lines where
  155. // oldNode used to be.
  156. patcher.deleteComments(oldNode);
  157. }
  158. var newLines = print(
  159. reprint.newPath,
  160. needToPrintNewPathWithComments
  161. ).indentTail(oldNode.loc.indent);
  162. var nls = needsLeadingSpace(lines, oldNode.loc, newLines);
  163. var nts = needsTrailingSpace(lines, oldNode.loc, newLines);
  164. // If we try to replace the argument of a ReturnStatement like
  165. // return"asdf" with e.g. a literal null expression, we run
  166. // the risk of ending up with returnnull, so we need to add an
  167. // extra leading space in situations where that might
  168. // happen. Likewise for "asdf"in obj. See #170.
  169. if (nls || nts) {
  170. var newParts = [];
  171. nls && newParts.push(" ");
  172. newParts.push(newLines);
  173. nts && newParts.push(" ");
  174. newLines = linesModule.concat(newParts);
  175. }
  176. patcher.replace(oldNode.loc, newLines);
  177. });
  178. // Recall that origLoc is the .loc of an ancestor node that is
  179. // guaranteed to contain all the reprinted nodes and comments.
  180. return patcher.get(origLoc).indentTail(-orig.loc.indent);
  181. };
  182. };
  183. // If the last character before oldLoc and the first character of newLines
  184. // are both identifier characters, they must be separated by a space,
  185. // otherwise they will most likely get fused together into a single token.
  186. function needsLeadingSpace(oldLines, oldLoc, newLines) {
  187. var posBeforeOldLoc = util.copyPos(oldLoc.start);
  188. // The character just before the location occupied by oldNode.
  189. var charBeforeOldLoc =
  190. oldLines.prevPos(posBeforeOldLoc) &&
  191. oldLines.charAt(posBeforeOldLoc);
  192. // First character of the reprinted node.
  193. var newFirstChar = newLines.charAt(newLines.firstPos());
  194. return charBeforeOldLoc &&
  195. riskyAdjoiningCharExp.test(charBeforeOldLoc) &&
  196. newFirstChar &&
  197. riskyAdjoiningCharExp.test(newFirstChar);
  198. }
  199. // If the last character of newLines and the first character after oldLoc
  200. // are both identifier characters, they must be separated by a space,
  201. // otherwise they will most likely get fused together into a single token.
  202. function needsTrailingSpace(oldLines, oldLoc, newLines) {
  203. // The character just after the location occupied by oldNode.
  204. var charAfterOldLoc = oldLines.charAt(oldLoc.end);
  205. var newLastPos = newLines.lastPos();
  206. // Last character of the reprinted node.
  207. var newLastChar = newLines.prevPos(newLastPos) &&
  208. 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.deepEqual(reprints, []);
  220. if (newNode.type !== oldNode.type) {
  221. return false;
  222. }
  223. var oldPath = new FastPath(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. isArray.assert(newNode);
  247. var len = newNode.length;
  248. if (!(isArray.check(oldNode) &&
  249. oldNode.length === len))
  250. return false;
  251. for (var i = 0; i < len; ++i) {
  252. newPath.stack.push(i, newNode[i]);
  253. oldPath.stack.push(i, oldNode[i]);
  254. var canReprint = findAnyReprints(newPath, oldPath, reprints);
  255. newPath.stack.length -= 2;
  256. oldPath.stack.length -= 2;
  257. if (!canReprint) {
  258. return false;
  259. }
  260. }
  261. return true;
  262. }
  263. function findObjectReprints(newPath, oldPath, reprints) {
  264. var newNode = newPath.getValue();
  265. isObject.assert(newNode);
  266. if (newNode.original === null) {
  267. // If newNode.original node was set to null, reprint the node.
  268. return false;
  269. }
  270. var oldNode = oldPath.getValue();
  271. if (!isObject.check(oldNode))
  272. return false;
  273. if (Printable.check(newNode)) {
  274. if (!Printable.check(oldNode)) {
  275. return false;
  276. }
  277. // Here we need to decide whether the reprinted code for newNode
  278. // is appropriate for patching into the location of oldNode.
  279. if (newNode.type === oldNode.type) {
  280. var childReprints = [];
  281. if (findChildReprints(newPath, oldPath, childReprints)) {
  282. reprints.push.apply(reprints, childReprints);
  283. } else if (oldNode.loc) {
  284. // If we have no .loc information for oldNode, then we
  285. // won't be able to reprint it.
  286. reprints.push({
  287. oldPath: oldPath.copy(),
  288. newPath: newPath.copy()
  289. });
  290. } else {
  291. return false;
  292. }
  293. return true;
  294. }
  295. if (Expression.check(newNode) &&
  296. Expression.check(oldNode) &&
  297. // If we have no .loc information for oldNode, then we won't
  298. // be able to reprint it.
  299. oldNode.loc) {
  300. // If both nodes are subtypes of Expression, then we should be
  301. // able to fill the location occupied by the old node with
  302. // code printed for the new node with no ill consequences.
  303. reprints.push({
  304. oldPath: oldPath.copy(),
  305. newPath: newPath.copy()
  306. });
  307. return true;
  308. }
  309. // The nodes have different types, and at least one of the types
  310. // is not a subtype of the Expression type, so we cannot safely
  311. // assume the nodes are syntactically interchangeable.
  312. return false;
  313. }
  314. return findChildReprints(newPath, oldPath, reprints);
  315. }
  316. // This object is reused in hasOpeningParen and hasClosingParen to avoid
  317. // having to allocate a temporary object.
  318. var reusablePos = { line: 1, column: 0 };
  319. var nonSpaceExp = /\S/;
  320. function hasOpeningParen(oldPath) {
  321. var oldNode = oldPath.getValue();
  322. var loc = oldNode.loc;
  323. var lines = loc && loc.lines;
  324. if (lines) {
  325. var pos = reusablePos;
  326. pos.line = loc.start.line;
  327. pos.column = loc.start.column;
  328. while (lines.prevPos(pos)) {
  329. var ch = lines.charAt(pos);
  330. if (ch === "(") {
  331. // If we found an opening parenthesis but it occurred before
  332. // the start of the original subtree for this reprinting, then
  333. // we must not return true for hasOpeningParen(oldPath).
  334. return comparePos(oldPath.getRootValue().loc.start, pos) <= 0;
  335. }
  336. if (nonSpaceExp.test(ch)) {
  337. return false;
  338. }
  339. }
  340. }
  341. return false;
  342. }
  343. function hasClosingParen(oldPath) {
  344. var oldNode = oldPath.getValue();
  345. var loc = oldNode.loc;
  346. var lines = loc && loc.lines;
  347. if (lines) {
  348. var pos = reusablePos;
  349. pos.line = loc.end.line;
  350. pos.column = loc.end.column;
  351. do {
  352. var ch = lines.charAt(pos);
  353. if (ch === ")") {
  354. // If we found a closing parenthesis but it occurred after the
  355. // end of the original subtree for this reprinting, then we
  356. // must not return true for hasClosingParen(oldPath).
  357. return comparePos(pos, oldPath.getRootValue().loc.end) <= 0;
  358. }
  359. if (nonSpaceExp.test(ch)) {
  360. return false;
  361. }
  362. } while (lines.nextPos(pos));
  363. }
  364. return false;
  365. }
  366. function hasParens(oldPath) {
  367. // This logic can technically be fooled if the node has parentheses
  368. // but there are comments intervening between the parentheses and the
  369. // node. In such cases the node will be harmlessly wrapped in an
  370. // additional layer of parentheses.
  371. return hasOpeningParen(oldPath) && hasClosingParen(oldPath);
  372. }
  373. function findChildReprints(newPath, oldPath, reprints) {
  374. var newNode = newPath.getValue();
  375. var oldNode = oldPath.getValue();
  376. isObject.assert(newNode);
  377. isObject.assert(oldNode);
  378. if (newNode.original === null) {
  379. // If newNode.original node was set to null, reprint the node.
  380. return false;
  381. }
  382. // If this type of node cannot come lexically first in its enclosing
  383. // statement (e.g. a function expression or object literal), and it
  384. // seems to be doing so, then the only way we can ignore this problem
  385. // and save ourselves from falling back to the pretty printer is if an
  386. // opening parenthesis happens to precede the node. For example,
  387. // (function(){ ... }()); does not need to be reprinted, even though
  388. // the FunctionExpression comes lexically first in the enclosing
  389. // ExpressionStatement and fails the hasParens test, because the
  390. // parent CallExpression passes the hasParens test. If we relied on
  391. // the path.needsParens() && !hasParens(oldNode) check below, the
  392. // absence of a closing parenthesis after the FunctionExpression would
  393. // trigger pretty-printing unnecessarily.
  394. if (!newPath.canBeFirstInStatement() &&
  395. newPath.firstInStatement() &&
  396. !hasOpeningParen(oldPath))
  397. return false;
  398. // If this node needs parentheses and will not be wrapped with
  399. // parentheses when reprinted, then return false to skip reprinting
  400. // and let it be printed generically.
  401. if (newPath.needsParens(true) && !hasParens(oldPath)) {
  402. return false;
  403. }
  404. var keys = util.getUnionOfKeys(oldNode, newNode);
  405. if (oldNode.type === "File" ||
  406. newNode.type === "File") {
  407. // Don't bother traversing file.tokens, an often very large array
  408. // returned by Babylon, and useless for our purposes.
  409. delete keys.tokens;
  410. }
  411. // Don't bother traversing .loc objects looking for reprintable nodes.
  412. delete keys.loc;
  413. var originalReprintCount = reprints.length;
  414. for (var k in keys) {
  415. newPath.stack.push(k, types.getFieldValue(newNode, k));
  416. oldPath.stack.push(k, types.getFieldValue(oldNode, k));
  417. var canReprint = findAnyReprints(newPath, oldPath, reprints);
  418. newPath.stack.length -= 2;
  419. oldPath.stack.length -= 2;
  420. if (!canReprint) {
  421. return false;
  422. }
  423. }
  424. // Return statements might end up running into ASI issues due to comments
  425. // inserted deep within the tree, so reprint them if anything changed
  426. // within them.
  427. if (ReturnStatement.check(newPath.getNode()) &&
  428. reprints.length > originalReprintCount) {
  429. return false;
  430. }
  431. return true;
  432. }