1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
{"version":3,"names":["_t","require","isIdentifier","_params","node","idNode","parentNode","print","typeParameters","nameInfo","_getFuncIdName","call","sourceIdentifierName","name","pos","token","_parameters","params","noLineTerminator","type","returnType","_noLineTerminator","parameters","parent","paramLength","length","i","_param","space","parameter","printJoin","decorators","optional","typeAnnotation","_methodHead","kind","key","word","async","generator","computed","undefined","_predicate","noLineTerminatorAfter","predicate","_functionHead","_endsWithInnerRaw","id","FunctionExpression","body","ArrowFunctionExpression","firstParam","format","retainLines","hasTypesOrComments","printInnerComments","param","_param$leadingComment","_param$trailingCommen","leadingComments","trailingComments","parentType","left","_id$loc","_id$loc2","loc","start","identifierName","_id$loc3","_id$loc4","value"],"sources":["../../src/generators/methods.ts"],"sourcesContent":["import type Printer from \"../printer.ts\";\nimport type * as t from \"@babel/types\";\nimport { isIdentifier } from \"@babel/types\";\nimport type { NodePath } from \"@babel/traverse\";\n\nexport function _params(\n this: Printer,\n node: t.Function | t.TSDeclareMethod | t.TSDeclareFunction,\n idNode: t.Expression | t.PrivateName,\n parentNode: NodePath<\n t.Function | t.TSDeclareMethod | t.TSDeclareFunction\n >[\"parent\"],\n) {\n this.print(node.typeParameters, node);\n\n const nameInfo = _getFuncIdName.call(this, idNode, parentNode);\n if (nameInfo) {\n this.sourceIdentifierName(nameInfo.name, nameInfo.pos);\n }\n\n this.token(\"(\");\n this._parameters(node.params, node);\n this.token(\")\");\n\n const noLineTerminator = node.type === \"ArrowFunctionExpression\";\n this.print(node.returnType, node, noLineTerminator);\n\n this._noLineTerminator = noLineTerminator;\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(\n node,\n node.computed && node.key.type !== \"StringLiteral\" ? undefined : node.key,\n undefined,\n );\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 parent: NodePath<\n t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction\n >[\"parent\"],\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, node.id, parent);\n if (node.type !== \"TSDeclareFunction\") {\n this._predicate(node);\n }\n}\n\nexport function FunctionExpression(\n this: Printer,\n node: t.FunctionExpression,\n parent: NodePath<t.FunctionExpression>[\"parent\"],\n) {\n this._functionHead(node, parent);\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 parent: NodePath<t.ArrowFunctionExpression>[\"parent\"],\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, undefined, parent);\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\nfunction _getFuncIdName(\n this: Printer,\n idNode: t.Expression | t.PrivateName,\n parent: NodePath<\n t.Function | t.TSDeclareMethod | t.TSDeclareFunction\n >[\"parent\"],\n) {\n let id: t.Expression | t.PrivateName | t.LVal = idNode;\n\n if (!id && parent) {\n const parentType = parent.type;\n\n if (parentType === \"VariableDeclarator\") {\n id = parent.id;\n } else if (\n parentType === \"AssignmentExpression\" ||\n parentType === \"AssignmentPattern\"\n ) {\n id = parent.left;\n } else if (\n parentType === \"ObjectProperty\" ||\n parentType === \"ClassProperty\"\n ) {\n if (!parent.computed || parent.key.type === \"StringLiteral\") {\n id = parent.key;\n }\n } else if (\n parentType === \"ClassPrivateProperty\" ||\n parentType === \"ClassAccessorProperty\"\n ) {\n id = parent.key;\n }\n }\n\n if (!id) return;\n\n let nameInfo;\n\n if (id.type === \"Identifier\") {\n nameInfo = {\n pos: id.loc?.start,\n name: id.loc?.identifierName || id.name,\n };\n } else if (id.type === \"PrivateName\") {\n nameInfo = {\n pos: id.loc?.start,\n name: \"#\" + id.id.name,\n };\n } else if (id.type === \"StringLiteral\") {\n nameInfo = {\n pos: id.loc?.start,\n name: id.value,\n };\n }\n\n return nameInfo;\n}\n"],"mappings":";;;;;;;;;;;;;AAEA,IAAAA,EAAA,GAAAC,OAAA;AAA4C;EAAnCC;AAAY,IAAAF,EAAA;AAGd,SAASG,OAAOA,CAErBC,IAA0D,EAC1DC,MAAoC,EACpCC,UAEW,EACX;EACA,IAAI,CAACC,KAAK,CAACH,IAAI,CAACI,cAAc,EAAEJ,IAAI,CAAC;EAErC,MAAMK,QAAQ,GAAGC,cAAc,CAACC,IAAI,CAAC,IAAI,EAAEN,MAAM,EAAEC,UAAU,CAAC;EAC9D,IAAIG,QAAQ,EAAE;IACZ,IAAI,CAACG,oBAAoB,CAACH,QAAQ,CAACI,IAAI,EAAEJ,QAAQ,CAACK,GAAG,CAAC;EACxD;EAEA,IAAI,CAACC,SAAK,GAAI,CAAC;EACf,IAAI,CAACC,WAAW,CAACZ,IAAI,CAACa,MAAM,EAAEb,IAAI,CAAC;EACnC,IAAI,CAACW,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