From 08e4e2c04c1b57ace85110c7e5c285848b62df9a Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 18 Apr 2017 07:51:50 -0700 Subject: [PATCH] Do not print the sub-tree when using prettier-ignore (#1286) In #1250, @jeresig reported that adding // prettier-ignore on a file that has a ton of conditional group still took 1.7s. The issue is that we're printing before checking for the comment. Swaping the two had the unfortunate side effect of not marking the comments as being printed, so I had to skip that safety check if there's a prettier-ignore. With this change, adding prettier-ignore makes the file be printed instantly! --- index.js | 8 ++++++++ src/printer.js | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 9874a2473..b455bba18 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,14 @@ function attachComments(text, ast, opts) { } function ensureAllCommentsPrinted(astComments) { + for (let i = 0; i < astComments.length; ++i) { + if (astComments[i].value.trim() === "prettier-ignore") { + // If there's a prettier-ignore, we're not printing that sub-tree so we + // don't know if the comments was printed or not. + return; + } + } + astComments.forEach(comment => { if (!comment.printed) { throw new Error( diff --git a/src/printer.js b/src/printer.js index 9199efe36..991c1862b 100644 --- a/src/printer.js +++ b/src/printer.js @@ -53,6 +53,17 @@ function genericPrint(path, options, printPath, args) { assert.ok(path instanceof FastPath); var node = path.getValue(); + + // Escape hatch + if ( + node && + node.comments && + node.comments.length > 0 && + node.comments.some(comment => comment.value.trim() === "prettier-ignore") + ) { + return options.originalText.slice(util.locStart(node), util.locEnd(node)); + } + var parts = []; var needsParens = false; var linesWithoutParens = genericPrintNoParens(path, options, printPath, args); @@ -61,15 +72,6 @@ function genericPrint(path, options, printPath, args) { return linesWithoutParens; } - // Escape hatch - if ( - node.comments && - node.comments.length > 0 && - node.comments.some(comment => comment.value.trim() === "prettier-ignore") - ) { - return options.originalText.slice(util.locStart(node), util.locEnd(node)); - } - if ( node.decorators && node.decorators.length > 0 &&