123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getReprinter = exports.Patcher = void 0;
- var tslib_1 = require("tslib");
- var assert_1 = tslib_1.__importDefault(require("assert"));
- var linesModule = tslib_1.__importStar(require("./lines"));
- var types = tslib_1.__importStar(require("ast-types"));
- var Printable = types.namedTypes.Printable;
- var Expression = types.namedTypes.Expression;
- var ReturnStatement = types.namedTypes.ReturnStatement;
- var SourceLocation = types.namedTypes.SourceLocation;
- var util_1 = require("./util");
- var fast_path_1 = tslib_1.__importDefault(require("./fast-path"));
- var isObject = types.builtInTypes.object;
- var isArray = types.builtInTypes.array;
- var isString = types.builtInTypes.string;
- var riskyAdjoiningCharExp = /[0-9a-z_$]/i;
- var Patcher = function Patcher(lines) {
- assert_1.default.ok(this instanceof Patcher);
- assert_1.default.ok(lines instanceof linesModule.Lines);
- var self = this, replacements = [];
- self.replace = function (loc, lines) {
- if (isString.check(lines))
- lines = linesModule.fromString(lines);
- replacements.push({
- lines: lines,
- start: loc.start,
- end: loc.end,
- });
- };
- self.get = function (loc) {
-
- loc = loc || {
- start: { line: 1, column: 0 },
- end: { line: lines.length, column: lines.getLineLength(lines.length) },
- };
- var sliceFrom = loc.start, toConcat = [];
- function pushSlice(from, to) {
- assert_1.default.ok((0, util_1.comparePos)(from, to) <= 0);
- toConcat.push(lines.slice(from, to));
- }
- replacements
- .sort(function (a, b) { return (0, util_1.comparePos)(a.start, b.start); })
- .forEach(function (rep) {
- if ((0, util_1.comparePos)(sliceFrom, rep.start) > 0) {
-
- }
- else {
- pushSlice(sliceFrom, rep.start);
- toConcat.push(rep.lines);
- sliceFrom = rep.end;
- }
- });
- pushSlice(sliceFrom, loc.end);
- return linesModule.concat(toConcat);
- };
- };
- exports.Patcher = Patcher;
- var Pp = Patcher.prototype;
- Pp.tryToReprintComments = function (newNode, oldNode, print) {
- var patcher = this;
- if (!newNode.comments && !oldNode.comments) {
-
- return true;
- }
- var newPath = fast_path_1.default.from(newNode);
- var oldPath = fast_path_1.default.from(oldNode);
- newPath.stack.push("comments", getSurroundingComments(newNode));
- oldPath.stack.push("comments", getSurroundingComments(oldNode));
- var reprints = [];
- var ableToReprintComments = findArrayReprints(newPath, oldPath, reprints);
-
-
- if (ableToReprintComments && reprints.length > 0) {
- reprints.forEach(function (reprint) {
- var oldComment = reprint.oldPath.getValue();
- assert_1.default.ok(oldComment.leading || oldComment.trailing);
- patcher.replace(oldComment.loc,
-
-
- print(reprint.newPath).indentTail(oldComment.loc.indent));
- });
- }
- return ableToReprintComments;
- };
- function getSurroundingComments(node) {
- var result = [];
- if (node.comments && node.comments.length > 0) {
- node.comments.forEach(function (comment) {
- if (comment.leading || comment.trailing) {
- result.push(comment);
- }
- });
- }
- return result;
- }
- Pp.deleteComments = function (node) {
- if (!node.comments) {
- return;
- }
- var patcher = this;
- node.comments.forEach(function (comment) {
- if (comment.leading) {
-
-
- patcher.replace({
- start: comment.loc.start,
- end: node.loc.lines.skipSpaces(comment.loc.end, false, false),
- }, "");
- }
- else if (comment.trailing) {
-
-
- patcher.replace({
- start: node.loc.lines.skipSpaces(comment.loc.start, true, false),
- end: comment.loc.end,
- }, "");
- }
- });
- };
- function getReprinter(path) {
- assert_1.default.ok(path instanceof fast_path_1.default);
-
-
- var node = path.getValue();
- if (!Printable.check(node))
- return;
- var orig = node.original;
- var origLoc = orig && orig.loc;
- var lines = origLoc && origLoc.lines;
- var reprints = [];
- if (!lines || !findReprints(path, reprints))
- return;
- return function (print) {
- var patcher = new Patcher(lines);
- reprints.forEach(function (reprint) {
- var newNode = reprint.newPath.getValue();
- var oldNode = reprint.oldPath.getValue();
- SourceLocation.assert(oldNode.loc, true);
- var needToPrintNewPathWithComments = !patcher.tryToReprintComments(newNode, oldNode, print);
- if (needToPrintNewPathWithComments) {
-
-
-
-
- patcher.deleteComments(oldNode);
- }
- var newLines = print(reprint.newPath, {
- includeComments: needToPrintNewPathWithComments,
-
-
-
-
-
- avoidRootParens: oldNode.type === newNode.type && reprint.oldPath.hasParens(),
- }).indentTail(oldNode.loc.indent);
- var nls = needsLeadingSpace(lines, oldNode.loc, newLines);
- var nts = needsTrailingSpace(lines, oldNode.loc, newLines);
-
-
-
-
-
- if (nls || nts) {
- var newParts = [];
- nls && newParts.push(" ");
- newParts.push(newLines);
- nts && newParts.push(" ");
- newLines = linesModule.concat(newParts);
- }
- patcher.replace(oldNode.loc, newLines);
- });
-
-
- var patchedLines = patcher.get(origLoc).indentTail(-orig.loc.indent);
- if (path.needsParens()) {
- return linesModule.concat(["(", patchedLines, ")"]);
- }
- return patchedLines;
- };
- }
- exports.getReprinter = getReprinter;
- function needsLeadingSpace(oldLines, oldLoc, newLines) {
- var posBeforeOldLoc = (0, util_1.copyPos)(oldLoc.start);
-
- var charBeforeOldLoc = oldLines.prevPos(posBeforeOldLoc) && oldLines.charAt(posBeforeOldLoc);
-
- var newFirstChar = newLines.charAt(newLines.firstPos());
- return (charBeforeOldLoc &&
- riskyAdjoiningCharExp.test(charBeforeOldLoc) &&
- newFirstChar &&
- riskyAdjoiningCharExp.test(newFirstChar));
- }
- function needsTrailingSpace(oldLines, oldLoc, newLines) {
-
- var charAfterOldLoc = oldLines.charAt(oldLoc.end);
- var newLastPos = newLines.lastPos();
-
- var newLastChar = newLines.prevPos(newLastPos) && newLines.charAt(newLastPos);
- return (newLastChar &&
- riskyAdjoiningCharExp.test(newLastChar) &&
- charAfterOldLoc &&
- riskyAdjoiningCharExp.test(charAfterOldLoc));
- }
- function findReprints(newPath, reprints) {
- var newNode = newPath.getValue();
- Printable.assert(newNode);
- var oldNode = newNode.original;
- Printable.assert(oldNode);
- assert_1.default.deepEqual(reprints, []);
- if (newNode.type !== oldNode.type) {
- return false;
- }
- var oldPath = new fast_path_1.default(oldNode);
- var canReprint = findChildReprints(newPath, oldPath, reprints);
- if (!canReprint) {
-
-
- reprints.length = 0;
- }
- return canReprint;
- }
- function findAnyReprints(newPath, oldPath, reprints) {
- var newNode = newPath.getValue();
- var oldNode = oldPath.getValue();
- if (newNode === oldNode)
- return true;
- if (isArray.check(newNode))
- return findArrayReprints(newPath, oldPath, reprints);
- if (isObject.check(newNode))
- return findObjectReprints(newPath, oldPath, reprints);
- return false;
- }
- function findArrayReprints(newPath, oldPath, reprints) {
- var newNode = newPath.getValue();
- var oldNode = oldPath.getValue();
- if (newNode === oldNode ||
- newPath.valueIsDuplicate() ||
- oldPath.valueIsDuplicate()) {
- return true;
- }
- isArray.assert(newNode);
- var len = newNode.length;
- if (!(isArray.check(oldNode) && oldNode.length === len))
- return false;
- for (var i = 0; i < len; ++i) {
- newPath.stack.push(i, newNode[i]);
- oldPath.stack.push(i, oldNode[i]);
- var canReprint = findAnyReprints(newPath, oldPath, reprints);
- newPath.stack.length -= 2;
- oldPath.stack.length -= 2;
- if (!canReprint) {
- return false;
- }
- }
- return true;
- }
- function findObjectReprints(newPath, oldPath, reprints) {
- var newNode = newPath.getValue();
- isObject.assert(newNode);
- if (newNode.original === null) {
-
- return false;
- }
- var oldNode = oldPath.getValue();
- if (!isObject.check(oldNode))
- return false;
- if (newNode === oldNode ||
- newPath.valueIsDuplicate() ||
- oldPath.valueIsDuplicate()) {
- return true;
- }
- if (Printable.check(newNode)) {
- if (!Printable.check(oldNode)) {
- return false;
- }
- var newParentNode = newPath.getParentNode();
- var oldParentNode = oldPath.getParentNode();
- if (oldParentNode !== null &&
- oldParentNode.type === "FunctionTypeAnnotation" &&
- newParentNode !== null &&
- newParentNode.type === "FunctionTypeAnnotation") {
- var oldNeedsParens = oldParentNode.params.length !== 1 || !!oldParentNode.params[0].name;
- var newNeedParens = newParentNode.params.length !== 1 || !!newParentNode.params[0].name;
- if (!oldNeedsParens && newNeedParens) {
- return false;
- }
- }
-
-
- if (newNode.type === oldNode.type) {
- var childReprints = [];
- if (findChildReprints(newPath, oldPath, childReprints)) {
- reprints.push.apply(reprints, childReprints);
- }
- else if (oldNode.loc) {
-
-
- reprints.push({
- oldPath: oldPath.copy(),
- newPath: newPath.copy(),
- });
- }
- else {
- return false;
- }
- return true;
- }
- if (Expression.check(newNode) &&
- Expression.check(oldNode) &&
-
-
- oldNode.loc) {
-
-
-
- reprints.push({
- oldPath: oldPath.copy(),
- newPath: newPath.copy(),
- });
- return true;
- }
-
-
-
- return false;
- }
- return findChildReprints(newPath, oldPath, reprints);
- }
- function findChildReprints(newPath, oldPath, reprints) {
- var newNode = newPath.getValue();
- var oldNode = oldPath.getValue();
- isObject.assert(newNode);
- isObject.assert(oldNode);
- if (newNode.original === null) {
-
- return false;
- }
-
-
-
- if (newPath.needsParens() && !oldPath.hasParens()) {
- return false;
- }
- var keys = (0, util_1.getUnionOfKeys)(oldNode, newNode);
- if (oldNode.type === "File" || newNode.type === "File") {
-
-
- delete keys.tokens;
- }
-
- delete keys.loc;
- var originalReprintCount = reprints.length;
- for (var k in keys) {
- if (k.charAt(0) === "_") {
-
-
- continue;
- }
- newPath.stack.push(k, types.getFieldValue(newNode, k));
- oldPath.stack.push(k, types.getFieldValue(oldNode, k));
- var canReprint = findAnyReprints(newPath, oldPath, reprints);
- newPath.stack.length -= 2;
- oldPath.stack.length -= 2;
- if (!canReprint) {
- return false;
- }
- }
-
-
-
- if (ReturnStatement.check(newPath.getNode()) &&
- reprints.length > originalReprintCount) {
- return false;
- }
- return true;
- }
|