{"version":3,"names":["isIdentifier","_params","node","print","typeParameters","token","_parameters","params","_noLineTerminator","returnType","type","parameters","parent","paramLength","length","i","_param","space","parameter","printJoin","decorators","optional","typeAnnotation","_methodHead","kind","key","word","async","generator","computed","_predicate","noLineTerminatorAfter","predicate","_functionHead","_endsWithInnerRaw","id","FunctionExpression","body","ArrowFunctionExpression","firstParam","format","retainLines","hasTypesOrComments","printInnerComments","param","leadingComments","trailingComments"],"sources":["../../src/generators/methods.ts"],"sourcesContent":["import type Printer from \"../printer\";\nimport { isIdentifier } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport function _params(\n this: Printer,\n node: t.Function | t.TSDeclareMethod | t.TSDeclareFunction,\n) {\n this.print(node.typeParameters, node);\n this.token(\"(\");\n this._parameters(node.params, node);\n this.token(\")\");\n this._noLineTerminator = true;\n\n this.print(node.returnType, node, node.type === \"ArrowFunctionExpression\");\n}\n\nexport function _parameters(\n this: Printer,\n parameters: t.Function[\"params\"],\n parent:\n | t.Function\n | t.TSIndexSignature\n | t.TSDeclareMethod\n | t.TSDeclareFunction\n | t.TSFunctionType\n | t.TSConstructorType,\n) {\n const paramLength = parameters.length;\n for (let i = 0; i < paramLength; i++) {\n this._param(parameters[i], parent);\n\n if (i < parameters.length - 1) {\n this.token(\",\");\n this.space();\n }\n }\n}\n\nexport function _param(\n this: Printer,\n parameter: t.Identifier | t.RestElement | t.Pattern | t.TSParameterProperty,\n parent?:\n | t.Function\n | t.TSIndexSignature\n | t.TSDeclareMethod\n | t.TSDeclareFunction\n | t.TSFunctionType\n | t.TSConstructorType,\n) {\n this.printJoin(parameter.decorators, parameter);\n this.print(parameter, parent);\n if (\n // @ts-expect-error optional is not in TSParameterProperty\n parameter.optional\n ) {\n this.token(\"?\"); // TS / flow\n }\n\n this.print(\n // @ts-expect-error typeAnnotation is not in TSParameterProperty\n parameter.typeAnnotation,\n parameter,\n ); // TS / flow\n}\n\nexport function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {\n const kind = node.kind;\n const key = node.key;\n\n if (kind === \"get\" || kind === \"set\") {\n this.word(kind);\n this.space();\n }\n\n if (node.async) {\n this.word(\"async\", true);\n this.space();\n }\n\n if (\n kind === \"method\" ||\n // @ts-expect-error Fixme: kind: \"init\" is not defined\n kind === \"init\"\n ) {\n if (node.generator) {\n this.token(\"*\");\n }\n }\n\n if (node.computed) {\n this.token(\"[\");\n this.print(key, node);\n this.token(\"]\");\n } else {\n this.print(key, node);\n }\n\n if (\n // @ts-expect-error optional is not in ObjectMethod\n node.optional\n ) {\n // TS\n this.token(\"?\");\n }\n\n this._params(node);\n}\n\nexport function _predicate(\n this: Printer,\n node:\n | t.FunctionDeclaration\n | t.FunctionExpression\n | t.ArrowFunctionExpression,\n noLineTerminatorAfter?: boolean,\n) {\n if (node.predicate) {\n if (!node.returnType) {\n this.token(\":\");\n }\n this.space();\n this.print(node.predicate, node, noLineTerminatorAfter);\n }\n}\n\nexport function _functionHead(\n this: Printer,\n node: t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction,\n) {\n if (node.async) {\n this.word(\"async\");\n // We prevent inner comments from being printed here,\n // so that they are always consistently printed in the\n // same place regardless of the function type.\n this._endsWithInnerRaw = false;\n this.space();\n }\n this.word(\"function\");\n if (node.generator) {\n // We prevent inner comments from being printed here,\n // so that they are always consistently printed in the\n // same place regardless of the function type.\n this._endsWithInnerRaw = false;\n this.token(\"*\");\n }\n\n this.space();\n if (node.id) {\n this.print(node.id, node);\n }\n\n this._params(node);\n if (node.type !== \"TSDeclareFunction\") {\n this._predicate(node);\n }\n}\n\nexport function FunctionExpression(this: Printer, node: t.FunctionExpression) {\n this._functionHead(node);\n this.space();\n this.print(node.body, node);\n}\n\nexport { FunctionExpression as FunctionDeclaration };\n\nexport function ArrowFunctionExpression(\n this: Printer,\n node: t.ArrowFunctionExpression,\n) {\n if (node.async) {\n this.word(\"async\", true);\n this.space();\n }\n\n // Try to avoid printing parens in simple cases, but only if we're pretty\n // sure that they aren't needed by type annotations or potential newlines.\n let firstParam;\n if (\n !this.format.retainLines &&\n node.params.length === 1 &&\n isIdentifier((firstParam = node.params[0])) &&\n !hasTypesOrComments(node, firstParam)\n ) {\n this.print(firstParam, node, true);\n } else {\n this._params(node);\n }\n\n this._predicate(node, true);\n this.space();\n // When printing (x)/*1*/=>{}, we remove the parentheses\n // and thus there aren't two contiguous inner tokens.\n // We forcefully print inner comments here.\n this.printInnerComments();\n this.token(\"=>\");\n\n this.space();\n\n this.print(node.body, node);\n}\n\nfunction hasTypesOrComments(\n node: t.ArrowFunctionExpression,\n param: t.Identifier,\n): boolean {\n return !!(\n node.typeParameters ||\n node.returnType ||\n node.predicate ||\n param.typeAnnotation ||\n param.optional ||\n // Flow does not support `foo /*: string*/ => {};`\n param.leadingComments?.length ||\n param.trailingComments?.length\n );\n}\n"],"mappings":";;;;;;;;;;;;;AACA;AAA4C;EAAnCA;AAAY;AAGd,SAASC,OAAO,CAErBC,IAA0D,EAC1D;EACA,IAAI,CAACC,KAAK,CAACD,IAAI,CAACE,cAAc,EAAEF,IAAI,CAAC;EACrC,IAAI,CAACG,SAAK,IAAK;EACf,IAAI,CAACC,WAAW,CAACJ,IAAI,CAACK,MAAM,EAAEL,IAAI,CAAC;EACnC,IAAI,CAACG,SAAK,IAAK;EACf,IAAI,CAACG,iBAAiB,GAAG,IAAI;EAE7B,IAAI,CAACL,KAAK,CAACD,IAAI,CAACO,UAAU,EAAEP,IAAI,EAAEA,IAAI,CAACQ,IAAI,KAAK,yBAAyB,CAAC;AAC5E;AAEO,SAASJ,WAAW,CAEzBK,UAAgC,EAChCC,MAMuB,EACvB;EACA,MAAMC,WAAW,GAAGF,UAAU,CAACG,MAAM;EACrC,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,WAAW,EAAEE,CAAC,EAAE,EAAE;IACpC,IAAI,CAACC,MAAM,CAACL,UAAU,CAACI,CAAC,CAAC,EAAEH,MAAM,CAAC;IAElC,IAAIG,CAAC,GA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