patcher.js 16 KB

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