From 97db2e39261af857d22ba435b2714b9493e5d70d Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 12 Apr 2021 05:20:27 +0300 Subject: [PATCH] Minor refactoring of printObject (#10696) --- src/language-js/print/object.js | 41 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/language-js/print/object.js b/src/language-js/print/object.js index 475955062..00afcfc6e 100644 --- a/src/language-js/print/object.js +++ b/src/language-js/print/object.js @@ -41,11 +41,10 @@ function printObject(path, options, print) { } const isTypeAnnotation = node.type === "ObjectTypeAnnotation"; - const fields = []; + const fields = [propertiesField]; if (isTypeAnnotation) { fields.push("indexers", "callProperties", "internalSlots"); } - fields.push(propertiesField); const firstProperty = fields .map((field) => node[field][0]) @@ -109,26 +108,28 @@ function printObject(path, options, print) { }, field); } + if (fields.length > 1) { + propsAndLoc.sort((a, b) => a.loc - b.loc); + } + /** @type {Doc[]} */ let separatorParts = []; - const props = propsAndLoc - .sort((a, b) => a.loc - b.loc) - .map((prop) => { - const result = [...separatorParts, group(prop.printed)]; - separatorParts = [separator, line]; - if ( - (prop.node.type === "TSPropertySignature" || - prop.node.type === "TSMethodSignature" || - prop.node.type === "TSConstructSignatureDeclaration") && - hasComment(prop.node, CommentCheckFlags.PrettierIgnore) - ) { - separatorParts.shift(); - } - if (isNextLineEmpty(prop.node, options)) { - separatorParts.push(hardline); - } - return result; - }); + const props = propsAndLoc.map((prop) => { + const result = [...separatorParts, group(prop.printed)]; + separatorParts = [separator, line]; + if ( + (prop.node.type === "TSPropertySignature" || + prop.node.type === "TSMethodSignature" || + prop.node.type === "TSConstructSignatureDeclaration") && + hasComment(prop.node, CommentCheckFlags.PrettierIgnore) + ) { + separatorParts.shift(); + } + if (isNextLineEmpty(prop.node, options)) { + separatorParts.push(hardline); + } + return result; + }); if (node.inexact) { let printed;