Son CV dans un terminal web en Javascript! https://terminal-cv.gregandev.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
34 KiB

1 year ago
{"version":3,"file":"index.js","sources":["../src/util.ts","../src/index.ts"],"sourcesContent":["import type { NodePath } from \"@babel/traverse\";\n\n/**\n * Test if a NodePath will be cast to boolean when evaluated.\n *\n * @example\n * // returns true\n * const nodePathAQDotB = NodePath(\"if (a?.#b) {}\").get(\"test\"); // a?.#b\n * willPathCastToBoolean(nodePathAQDotB)\n * @example\n * // returns false\n * willPathCastToBoolean(NodePath(\"a?.#b\"))\n * @todo Respect transparent expression wrappers\n * @see {@link packages/babel-plugin-proposal-optional-chaining/src/util.js}\n * @param {NodePath} path\n * @returns {boolean}\n */\nexport function willPathCastToBoolean(path: NodePath): boolean {\n const maybeWrapped = path;\n const { node, parentPath } = maybeWrapped;\n if (parentPath.isLogicalExpression()) {\n const { operator, right } = parentPath.node;\n if (\n operator === \"&&\" ||\n operator === \"||\" ||\n (operator === \"??\" && node === right)\n ) {\n return willPathCastToBoolean(parentPath);\n }\n }\n if (parentPath.isSequenceExpression()) {\n const { expressions } = parentPath.node;\n if (expressions[expressions.length - 1] === node) {\n return willPathCastToBoolean(parentPath);\n } else {\n // if it is in the middle of a sequence expression, we don't\n // care the return value so just cast to boolean for smaller\n // output\n return true;\n }\n }\n return (\n parentPath.isConditional({ test: node }) ||\n parentPath.isUnaryExpression({ operator: \"!\" }) ||\n parentPath.isLoop({ test: node })\n );\n}\n","import type { NodePath, Visitor } from \"@babel/traverse\";\nimport {\n LOGICAL_OPERATORS,\n arrowFunctionExpression,\n assignmentExpression,\n binaryExpression,\n booleanLiteral,\n callExpression,\n cloneNode,\n conditionalExpression,\n identifier,\n isMemberExpression,\n isOptionalCallExpression,\n isOptionalMemberExpression,\n isUpdateExpression,\n logicalExpression,\n memberExpression,\n nullLiteral,\n numericLiteral,\n optionalCallExpression,\n optionalMemberExpression,\n sequenceExpression,\n unaryExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport { willPathCastToBoolean } from \"./util\";\n\nclass AssignmentMemoiser {\n private _map: WeakMap<t.Expression, { count: number; value: t.LVal }>;\n constructor() {\n this._map = new WeakMap();\n }\n\n has(key: t.Expression) {\n return this._map.has(key);\n }\n\n get(key: t.Expression) {\n if (!this.has(key)) return;\n\n const record = this._map.get(key);\n const { value } = record;\n\n record.count--;\n if (record.count === 0) {\n // The `count` access is the outermost function call (hopefully), so it\n // does the assignment.\n return assignmentExpression(\"=\", value, key);\n }\n return value;\n }\n\n set(key: t.Expression, value: t.LVal, count: number) {\n return this._map.set(key, { count, value });\n }\n}\n\nfunction toNonOptional(\n path: NodePath<t.Expression>,\n base: t.Expression,\n): t.Expression {\n const { node } = path;\n if (isOptionalMemberExpression(node)) {\n return memberExpression(base, node.property, node.computed);\n }\n\n if (path.isOptionalCallExpression()) {\n const callee = path.get(\"callee\");\n if (path.node.optional && callee.isOptionalMemberExpression()) {\n const { object } = callee.node;\n const context = path.scope.maybeGenerateMemoised(object) || object;\n callee\n .get(\"object\")\n .replaceWith(assignmentExpression(\"=\", context as t.LVal, object));\n\n return callExpression(memberExpression(base, identifier(\"call\")), [\n context,\n ...path.node.arguments,\n ]);\n }\n\n return callExpression(base, path.node.arguments);\n }\n\n return path.node;\n}\n\n// Determines if the current path is in a detached tree. This can happen when\n// we are iterating on a path, and replace an ancestor with a new node. Babel\n// doesn't always stop traversing the old node