|
| 1 | +'use strict' |
| 2 | + |
| 3 | +Object.defineProperty(exports, '__esModule', { |
| 4 | + value: true, |
| 5 | +}) |
| 6 | +exports.default = void 0 |
| 7 | +const PURE_ANNOTATION = '#__PURE__' |
| 8 | +const isPureAnnotated = node => { |
| 9 | + const leadingComments = node.leadingComments |
| 10 | + if (!leadingComments) { |
| 11 | + return false |
| 12 | + } |
| 13 | + return leadingComments.some(comment => /[@#]__PURE__/.test(comment.value)) |
| 14 | +} |
| 15 | +const annotateAsPure = path => { |
| 16 | + if (isPureAnnotated(path.node)) { |
| 17 | + return |
| 18 | + } |
| 19 | + path.addComment('leading', PURE_ANNOTATION) |
| 20 | +} |
| 21 | +const hasCallableParent = ({ parentPath }) => |
| 22 | + parentPath.isCallExpression() || parentPath.isNewExpression() |
| 23 | +const isUsedAsCallee = path => { |
| 24 | + if (!hasCallableParent(path)) { |
| 25 | + return false |
| 26 | + } |
| 27 | + return path.parentPath.get('callee') === path |
| 28 | +} |
| 29 | +const isInCallee = path => { |
| 30 | + do { |
| 31 | + path = path.parentPath |
| 32 | + if (isUsedAsCallee(path)) { |
| 33 | + return true |
| 34 | + } |
| 35 | + } while (!path.isStatement() && !path.isFunction()) |
| 36 | + return false |
| 37 | +} |
| 38 | +const isExecutedDuringInitialization = path => { |
| 39 | + let functionParent = path.getFunctionParent() |
| 40 | + while (functionParent) { |
| 41 | + if (!isUsedAsCallee(functionParent)) { |
| 42 | + return false |
| 43 | + } |
| 44 | + functionParent = functionParent.getFunctionParent() |
| 45 | + } |
| 46 | + return true |
| 47 | +} |
| 48 | +const isInAssignmentContext = path => { |
| 49 | + const statement = path.getStatementParent() |
| 50 | + let parentPath |
| 51 | + do { |
| 52 | + var _ref = parentPath || path |
| 53 | + parentPath = _ref.parentPath |
| 54 | + if ( |
| 55 | + parentPath.isVariableDeclaration() || |
| 56 | + parentPath.isAssignmentExpression() || |
| 57 | + parentPath.isClass() |
| 58 | + ) { |
| 59 | + return true |
| 60 | + } |
| 61 | + } while (parentPath !== statement) |
| 62 | + return false |
| 63 | +} |
| 64 | +const callableExpressionVisitor = path => { |
| 65 | + if (isUsedAsCallee(path) || isInCallee(path)) { |
| 66 | + return |
| 67 | + } |
| 68 | + if (!isExecutedDuringInitialization(path)) { |
| 69 | + return |
| 70 | + } |
| 71 | + if ( |
| 72 | + !isInAssignmentContext(path) && |
| 73 | + !path.getStatementParent().isExportDefaultDeclaration() |
| 74 | + ) { |
| 75 | + return |
| 76 | + } |
| 77 | + annotateAsPure(path) |
| 78 | +} |
| 79 | +var _default = () => ({ |
| 80 | + name: 'annotate-pure-calls', |
| 81 | + visitor: { |
| 82 | + 'CallExpression|NewExpression': callableExpressionVisitor, |
| 83 | + }, |
| 84 | +}) |
| 85 | +exports.default = _default |
0 commit comments