1 line
7.4 KiB
Plaintext
1 line
7.4 KiB
Plaintext
|
{"version":3,"names":["whitespace","require","parens","_t","FLIPPED_ALIAS_KEYS","isCallExpression","isDecorator","isExpressionStatement","isMemberExpression","isNewExpression","isParenthesizedExpression","TokenContext","exports","expressionStatement","arrowBody","exportDefault","forHead","forInHead","forOfHead","arrowFlowReturnType","expandAliases","obj","map","Map","add","type","func","fn","get","set","node","parent","stack","inForInit","_fn","Object","keys","aliases","alias","expandedParens","expandedWhitespaceNodes","nodes","isOrHasCallExpression","object","needsWhitespace","_expandedWhitespaceNo","expression","flag","needsWhitespaceBefore","needsWhitespaceAfter","needsParens","tokenContext","_expandedParens$get","callee","isDecoratorMemberExpression","computed","property"],"sources":["../../src/node/index.ts"],"sourcesContent":["import * as whitespace from \"./whitespace.ts\";\nimport * as parens from \"./parentheses.ts\";\nimport {\n FLIPPED_ALIAS_KEYS,\n isCallExpression,\n isDecorator,\n isExpressionStatement,\n isMemberExpression,\n isNewExpression,\n isParenthesizedExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nimport type { WhitespaceFlag } from \"./whitespace.ts\";\n\nexport const enum TokenContext {\n expressionStatement = 1 << 0,\n arrowBody = 1 << 1,\n exportDefault = 1 << 2,\n forHead = 1 << 3,\n forInHead = 1 << 4,\n forOfHead = 1 << 5,\n arrowFlowReturnType = 1 << 6,\n}\n\ntype NodeHandler<R> = (\n node: t.Node,\n // todo:\n // node: K extends keyof typeof t\n // ? Extract<typeof t[K], { type: \"string\" }>\n // : t.Node,\n parent: t.Node,\n tokenContext?: number,\n inForStatementInit?: boolean,\n) => R;\n\nexport type NodeHandlers<R> = {\n [K in string]?: NodeHandler<R>;\n};\n\nfunction expandAliases<R>(obj: NodeHandlers<R>) {\n const map = new Map<string, NodeHandler<R>>();\n\n function add(type: string, func: NodeHandler<R>) {\n const fn = map.get(type);\n map.set(\n type,\n fn\n ? function (node, parent, stack, inForInit) {\n return (\n fn(node, parent, stack, inForInit) ??\n func(node, parent, stack, inForInit)\n );\n }\n : func,\n );\n }\n\n for (const type of Object.keys(obj)) {\n const aliases = FLIPPED_ALIAS_KEYS[type];\n if (aliases) {\n for (const alias of aliases) {\n add(alias, obj[type]);\n }\n } else {\n add(type, obj[type]);\n }\n }\n\n return map;\n}\n\n// Rather than using `t.is` on each object property, we pre-expand any type aliases\n// into concrete types so that the 'find' call below can be as fast as possible.\nconst expandedParens = expandAliases(parens);\nconst expandedWhitespaceNodes = expandAliases(whitespace.nodes);\n\nfunction isOrHasCallExpression(node: t.Node): boolean {\n if (isCallExpression(node)) {\n return true;\n }\n\n return isMemberExpression(node) && isOrHasCallExpression(node.object);\n}\n\nexport function needsWhitespace(\n node: t.Node,\n parent: t.Node,\n type: WhitespaceFlag,\n): boolean {\n if (!node) return false;\n\n if (isExpressionStatement(node)) {\n node = node.expression;\n }\n\n const flag = expandedWhitespaceNodes.get(node.type)?.(node, parent);\n\n if (typeof flag === \"number\") {\n return (flag & type) !== 0;\n }\n\n return false;\n}\n\nexport function needsWhitespaceBefore(node: t.Node, parent: t.Node) {\n return needsWhitespace(node, parent, 1);\n}\n\nexport function needsWhitespaceAfter(node: t.Node, parent: t.Node) {\n return needsWhitespace(node, parent, 2);\n}\n\nexport function needsParens(\n node: t.Node,\n parent: t.Node,\n tokenContext?: number,\n inForInit?: boolean,\n) {\n if (!parent) return false;\n\n if (isNewExpression(parent) && parent.callee === node) {\n if (isOrHasCallExpression(node)) return true;\n }\n\n if (isDecorator(parent)) {\n return (\n !isDecoratorMemberExpression(node) &&\n !(isCallExpression(node) && isDecoratorMemberExpression(node.callee)) &&\n !isParenthesizedExpression
|