From 81a1c463116f9dc8ec5e8059d70a9254675847e6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 27 May 2021 18:00:31 +0800 Subject: [PATCH] Minor tweak to js printer (#10954) --- jest.config.js | 2 +- .../no-identifier-n.js | 2 +- src/language-js/print/angular.js | 11 ++++ src/language-js/print/function.js | 56 ++++++++++++++++--- src/language-js/print/typescript.js | 4 +- src/language-js/printer-estree.js | 19 ++----- src/language-js/utils.js | 25 +-------- 7 files changed, 69 insertions(+), 50 deletions(-) diff --git a/jest.config.js b/jest.config.js index 940a0aa6f..684a50a5e 100644 --- a/jest.config.js +++ b/jest.config.js @@ -71,7 +71,7 @@ module.exports = { "prettier-local": "/tests/config/require-prettier.js", "prettier-standalone": "/tests/config/require-standalone.js", }, - modulePathIgnorePatterns: ["/dist", "/website/static/lib"], + modulePathIgnorePatterns: ["/dist", "/website"], transform, watchPlugins: [ "jest-watch-typeahead/filename", diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/no-identifier-n.js b/scripts/tools/eslint-plugin-prettier-internal-rules/no-identifier-n.js index 556639ad3..85946d845 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/no-identifier-n.js +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/no-identifier-n.js @@ -34,7 +34,7 @@ module.exports = { /* istanbul ignore next */ if (!variable) { - throw new Error("Unexpected case."); + return; } if (!variables.has(variable)) { diff --git a/src/language-js/print/angular.js b/src/language-js/print/angular.js index aac03f3ec..8f30adf77 100644 --- a/src/language-js/print/angular.js +++ b/src/language-js/print/angular.js @@ -10,6 +10,12 @@ const { printBinaryishExpression } = require("./binaryish"); function printAngular(path, options, print) { const node = path.getValue(); + + // Angular nodes always starts with `NG` + if (!node.type.startsWith("NG")) { + return; + } + switch (node.type) { case "NGRoot": return [ @@ -83,6 +89,11 @@ function printAngular(path, options, print) { ]; case "NGMicrosyntaxAs": return [print("key"), " as ", print("alias")]; + default: + /* istanbul ignore next */ + throw new Error( + `Unknown Angular node type: ${JSON.stringify(node.type)}.` + ); } } diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index b0b235e03..16d650d41 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -34,13 +34,16 @@ const { isTemplateOnItsOwnLine, shouldPrintComma, startsWithNoLookaheadToken, - returnArgumentHasLeadingComment, isBinaryish, isLineComment, hasComment, getComments, CommentCheckFlags, isCallLikeExpression, + isCallExpression, + getCallArguments, + hasNakedLeftSide, + getLeftSide, } = require("../utils"); const { locEnd } = require("../loc"); const { @@ -50,8 +53,22 @@ const { const { printPropertyKey } = require("./property"); const { printFunctionTypeParameters } = require("./misc"); -function printFunctionDeclaration(path, print, options, expandArg) { +function printFunction(path, print, options, args) { const node = path.getValue(); + + let expandArg = false; + if ( + (node.type === "FunctionDeclaration" || + node.type === "FunctionExpression") && + args && + args.expandLastArg + ) { + const parent = path.getParentNode(); + if (isCallExpression(parent) && getCallArguments(parent).length > 1) { + expandArg = true; + } + } + const parts = []; // For TypeScript the TSDeclareFunction node shares the AST @@ -260,7 +277,7 @@ function printArrowChain( ]); } -function printArrowFunctionExpression(path, options, print, args) { +function printArrowFunction(path, options, print, args) { let node = path.getValue(); /** @type {Doc[]} */ const signatures = []; @@ -427,7 +444,7 @@ function printReturnType(path, print, options) { } // `ReturnStatement` and `ThrowStatement` -function printReturnAndThrowArgument(path, options, print) { +function printReturnOrThrowArgument(path, options, print) { const node = path.getValue(); const semi = options.semi ? ";" : ""; const parts = []; @@ -475,16 +492,39 @@ function printReturnAndThrowArgument(path, options, print) { } function printReturnStatement(path, options, print) { - return ["return", printReturnAndThrowArgument(path, options, print)]; + return ["return", printReturnOrThrowArgument(path, options, print)]; } function printThrowStatement(path, options, print) { - return ["throw", printReturnAndThrowArgument(path, options, print)]; + return ["throw", printReturnOrThrowArgument(path, options, print)]; +} + +// This recurses the return argument, looking for the first token +// (the leftmost leaf node) and, if it (or its parents) has any +// leadingComments, returns true (so it can be wrapped in parens). +function returnArgumentHasLeadingComment(options, argument) { + if (hasLeadingOwnLineComment(options.originalText, argument)) { + return true; + } + + if (hasNakedLeftSide(argument)) { + let leftMost = argument; + let newLeftMost; + while ((newLeftMost = getLeftSide(leftMost))) { + leftMost = newLeftMost; + + if (hasLeadingOwnLineComment(options.originalText, leftMost)) { + return true; + } + } + } + + return false; } module.exports = { - printFunctionDeclaration, - printArrowFunctionExpression, + printFunction, + printArrowFunction, printMethod, printReturnStatement, printThrowStatement, diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index fa1619a5f..7ef36edb3 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -38,7 +38,7 @@ const { printTypeParameters, } = require("./type-parameters"); const { printPropertyKey } = require("./property"); -const { printFunctionDeclaration, printMethodInternal } = require("./function"); +const { printFunction, printMethodInternal } = require("./function"); const { printInterface } = require("./interface"); const { printBlock } = require("./block"); const { @@ -98,7 +98,7 @@ function printTypescript(path, options, print) { return group([castGroup, print("expression")]); } case "TSDeclareFunction": - return printFunctionDeclaration(path, print, options); + return printFunction(path, print, options); case "TSExportAssignment": return ["export = ", print("expression"), semi]; case "TSModuleBlock": diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index abd146f3e..75f881a87 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -16,7 +16,6 @@ const handleComments = require("./comments"); const pathNeedsParens = require("./needs-parens"); const preprocess = require("./print-preprocess"); const { - getCallArguments, hasFlowShorthandAnnotationComment, hasComment, CommentCheckFlags, @@ -64,8 +63,8 @@ const { } = require("./print/class"); const { printProperty } = require("./print/property"); const { - printFunctionDeclaration, - printArrowFunctionExpression, + printFunction, + printArrowFunction, printMethod, printReturnStatement, printThrowStatement, @@ -265,18 +264,10 @@ function printPathNoParens(path, options, print, args) { case "RestElement": return printRestSpread(path, options, print); case "FunctionDeclaration": - case "FunctionExpression": { - let expandArg = false; - if (args && args.expandLastArg) { - const parent = path.getParentNode(); - if (isCallExpression(parent) && getCallArguments(parent).length > 1) { - expandArg = true; - } - } - return printFunctionDeclaration(path, print, options, expandArg); - } + case "FunctionExpression": + return printFunction(path, print, options, args); case "ArrowFunctionExpression": - return printArrowFunctionExpression(path, options, print, args); + return printArrowFunction(path, options, print, args); case "YieldExpression": parts.push("yield"); diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 1ff8b7f45..9aefc897b 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -668,29 +668,6 @@ function hasLeadingOwnLineComment(text, node) { ); } -// This recurses the return argument, looking for the first token -// (the leftmost leaf node) and, if it (or its parents) has any -// leadingComments, returns true (so it can be wrapped in parens). -function returnArgumentHasLeadingComment(options, argument) { - if (hasLeadingOwnLineComment(options.originalText, argument)) { - return true; - } - - if (hasNakedLeftSide(argument)) { - let leftMost = argument; - let newLeftMost; - while ((newLeftMost = getLeftSide(leftMost))) { - leftMost = newLeftMost; - - if (hasLeadingOwnLineComment(options.originalText, leftMost)) { - return true; - } - } - } - - return false; -} - // Note: Quoting/unquoting numbers in TypeScript is not safe. // // let a = { 1: 1, 2: 2 } @@ -1334,6 +1311,7 @@ module.exports = { getCallArguments, iterateCallArgumentsPath, hasRestParameter, + getLeftSide, getLeftSidePathName, getParentExportDeclaration, getTypeScriptMappedTypeModifier, @@ -1382,7 +1360,6 @@ module.exports = { isNextLineEmpty, needsHardlineAfterDanglingComment, rawText, - returnArgumentHasLeadingComment, shouldPrintComma, isBitwiseOperator, shouldFlatten,