| 1 |
import { declare } from "@babel/helper-plugin-utils"; |
| 2 |
import syntaxDestructuringPrivate from "@babel/plugin-syntax-destructuring-private"; |
| 3 |
import { |
| 4 |
hasPrivateKeys, |
| 5 |
hasPrivateClassElement, |
| 6 |
transformPrivateKeyDestructuring, |
| 7 |
buildVariableDeclarationFromParams, |
| 8 |
} from "./util"; |
| 9 |
import { convertFunctionParams } from "@babel/plugin-transform-parameters"; |
| 10 |
import { unshiftForXStatementBody } from "@babel/plugin-transform-destructuring"; |
| 11 |
|
| 12 |
import type { PluginPass } from "@babel/core"; |
| 13 |
import type { NodePath, Visitor } from "@babel/traverse"; |
| 14 |
import type * as t from "@babel/types"; |
| 15 |
|
| 16 |
export default declare(function ({ assertVersion, assumption, types: t }) { |
| 17 |
assertVersion("^7.17.0"); |
| 18 |
const { |
| 19 |
assignmentExpression, |
| 20 |
assignmentPattern, |
| 21 |
cloneNode, |
| 22 |
expressionStatement, |
| 23 |
isExpressionStatement, |
| 24 |
isIdentifier, |
| 25 |
isSequenceExpression, |
| 26 |
sequenceExpression, |
| 27 |
variableDeclaration, |
| 28 |
variableDeclarator, |
| 29 |
} = t; |
| 30 |
|
| 31 |
const ignoreFunctionLength = assumption("ignoreFunctionLength"); |
| 32 |
const objectRestNoSymbols = assumption("objectRestNoSymbols"); |
| 33 |
|
| 34 |
const privateKeyDestructuringVisitor: Visitor<PluginPass> = { |
| 35 |
Function(path) { |
| 36 |
// (b, { #x: x } = I) => body |
| 37 |
// transforms to: |
| 38 |
// (b, p1) => { var { #x: x } = p1 === undefined ? I : p1; body; } |
| 39 |
const firstPrivateIndex = path.node.params.findIndex(param => |
| 40 |
hasPrivateKeys(param), |
| 41 |
); |
| 42 |
if (firstPrivateIndex === -1) return; |
| 43 |
// wrap function body within IIFE if any param is shadowed |
| 44 |
convertFunctionParams(path, ignoreFunctionLength, () => false); |
| 45 |
// invariant: path.body is always a BlockStatement after `convertFunctionParams` |
| 46 |
const { node, scope } = path; |
| 47 |
const { params } = node; |
| 48 |
const firstAssignmentPatternIndex = ignoreFunctionLength |
| 49 |
? -1 |
| 50 |
: params.findIndex(param => param.type === "AssignmentPattern"); |
| 51 |
const paramsAfterIndex = params.splice(firstPrivateIndex); |
| 52 |
const { params: transformedParams, variableDeclaration } = |
| 53 |
buildVariableDeclarationFromParams(paramsAfterIndex, scope); |
| 54 |
|
| 55 |
(path.get("body") as NodePath<t.BlockStatement>).unshiftContainer( |
| 56 |
"body", |
| 57 |
variableDeclaration, |
| 58 |
); |
| 59 |
params.push(...transformedParams); |
| 60 |
// preserve function.length |
| 61 |
// (b, p1) => {} |
| 62 |
// transforms to |
| 63 |
// (b, p1 = void 0) => {} |
| 64 |
if (firstAssignmentPatternIndex >= firstPrivateIndex) { |
| 65 |
params[firstAssignmentPatternIndex] = assignmentPattern( |
| 66 |
// @ts-expect-error The transformed assignment pattern must not be a RestElement |
| 67 |
params[firstAssignmentPatternIndex], |
| 68 |
scope.buildUndefinedNode(), |
| 69 |
); |
| 70 |
} |
| 71 |
scope.crawl(); |
| 72 |
// the pattern will be handled by VariableDeclaration visitor. |
| 73 |
}, |
| 74 |
CatchClause(path) { |
| 75 |
// catch({ #x: x }) { body } |
| 76 |
// transforms to: |
| 77 |
// catch(_e) { var {#x: x } = _e; body } |
| 78 |
const { node, scope } = path; |
| 79 |
if (!hasPrivateKeys(node.param)) return; |
| 80 |
// todo: handle shadowed param as we did in convertFunctionParams |
| 81 |
const ref = scope.generateUidIdentifier("e"); |
| 82 |
path |
| 83 |
.get("body") |
| 84 |
.unshiftContainer( |
| 85 |
"body", |
| 86 |
variableDeclaration("let", [variableDeclarator(node.param, ref)]), |
| 87 |
); |
| 88 |
node.param = cloneNode(ref); |
| 89 |
scope.crawl(); |
| 90 |
// the pattern will be handled by VariableDeclaration visitor. |
| 91 |
}, |
| 92 |
ForXStatement(path) { |
| 93 |
const { node, scope } = path; |
| 94 |
const leftPath = path.get("left"); |
| 95 |
if (leftPath.isVariableDeclaration()) { |
| 96 |
const left = leftPath.node; |
| 97 |
if (!hasPrivateKeys(left.declarations[0].id)) return; |
| 98 |
// for (const { #x: x } of cls) body; |
| 99 |
// transforms to: |
| 100 |
// for (const ref of cls) { const { #x: x } = ref; body; } |
| 101 |
// todo: the transform here assumes that any expression within |
| 102 |
// the destructuring pattern (`{ #x: x }`), when evluated, do not interfere |
| 103 |
// with the iterator of cls. Otherwise we have to pause the iterator and |
| 104 |
// interleave the expressions. |
| 105 |
// See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 |
| 106 |
const temp = scope.generateUidIdentifier("ref"); |
| 107 |
node.left = variableDeclaration(left.kind, [ |
| 108 |
variableDeclarator(temp, null), |
| 109 |
]); |
| 110 |
left.declarations[0].init = cloneNode(temp); |
| 111 |
unshiftForXStatementBody(path, [left]); |
| 112 |
scope.crawl(); |
| 113 |
// the pattern will be handled by VariableDeclaration visitor. |
| 114 |
} else if (leftPath.isPattern()) { |
| 115 |
if (!hasPrivateKeys(leftPath.node)) return; |
| 116 |
// for ({ #x: x } of cls); |
| 117 |
// transforms to: |
| 118 |
// for (const ref of cls) { ({ #x: x } = ref); body; } |
| 119 |
// This transform assumes that any expression within the pattern |
| 120 |
// does not interfere with the iterable `cls`. |
| 121 |
const temp = scope.generateUidIdentifier("ref"); |
| 122 |
node.left = variableDeclaration("const", [ |
| 123 |
variableDeclarator(temp, null), |
| 124 |
]); |
| 125 |
const assignExpr = expressionStatement( |
| 126 |
assignmentExpression("=", leftPath.node, cloneNode(temp)), |
| 127 |
); |
| 128 |
unshiftForXStatementBody(path, [assignExpr]); |
| 129 |
scope.crawl(); |
| 130 |
} |
| 131 |
}, |
| 132 |
VariableDeclaration(path, state) { |
| 133 |
const { scope, node } = path; |
| 134 |
const { declarations } = node; |
| 135 |
if (!declarations.some(declarator => hasPrivateKeys(declarator.id))) { |
| 136 |
return; |
| 137 |
} |
| 138 |
const newDeclarations = []; |
| 139 |
for (const declarator of declarations) { |
| 140 |
for (const { left, right } of transformPrivateKeyDestructuring( |
| 141 |
// @ts-expect-error The id of a variable declarator must not be a RestElement |
| 142 |
declarator.id, |
| 143 |
declarator.init, |
| 144 |
scope, |
| 145 |
/* isAssignment */ false, |
| 146 |
/* shouldPreserveCompletion */ false, |
| 147 |
name => state.addHelper(name), |
| 148 |
objectRestNoSymbols, |
| 149 |
/* useBuiltIns */ true, |
| 150 |
)) { |
| 151 |
newDeclarations.push(variableDeclarator(left, right)); |
| 152 |
} |
| 153 |
} |
| 154 |
node.declarations = newDeclarations; |
| 155 |
scope.crawl(); |
| 156 |
}, |
| 157 |
|
| 158 |
AssignmentExpression(path, state) { |
| 159 |
const { node, scope, parent } = path; |
| 160 |
if (!hasPrivateKeys(node.left)) return; |
| 161 |
const assignments = []; |
| 162 |
const shouldPreserveCompletion = |
| 163 |
(!isExpressionStatement(parent) && !isSequenceExpression(parent)) || |
| 164 |
path.isCompletionRecord(); |
| 165 |
for (const { left, right } of transformPrivateKeyDestructuring( |
| 166 |
// @ts-expect-error The left of an assignment expression must not be a RestElement |
| 167 |
node.left, |
| 168 |
node.right, |
| 169 |
scope, |
| 170 |
/* isAssignment */ true, |
| 171 |
shouldPreserveCompletion, |
| 172 |
name => state.addHelper(name), |
| 173 |
objectRestNoSymbols, |
| 174 |
/* useBuiltIns */ true, |
| 175 |
)) { |
| 176 |
assignments.push(assignmentExpression("=", left, right)); |
| 177 |
} |
| 178 |
// preserve completion record |
| 179 |
if (shouldPreserveCompletion) { |
| 180 |
const { left, right } = assignments[0]; |
| 181 |
// If node.right is right and left is an identifier, then the left is an effectively-constant memoised id |
| 182 |
if (isIdentifier(left) && right === node.right) { |
| 183 |
if ( |
| 184 |
!isIdentifier(assignments[assignments.length - 1].right, { |
| 185 |
name: left.name, |
| 186 |
}) |
| 187 |
) { |
| 188 |
// If the last assignment does not end with left, then we push `left` as the completion value |
| 189 |
assignments.push(cloneNode(left)); |
| 190 |
} |
| 191 |
// do nothing as `left` is already at the end of assignments |
| 192 |
} else { |
| 193 |
const tempId = scope.generateDeclaredUidIdentifier("m"); |
| 194 |
assignments.unshift( |
| 195 |
assignmentExpression("=", tempId, cloneNode(node.right)), |
| 196 |
); |
| 197 |
assignments.push(cloneNode(tempId)); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
path.replaceWith(sequenceExpression(assignments)); |
| 202 |
scope.crawl(); |
| 203 |
}, |
| 204 |
}; |
| 205 |
|
| 206 |
const visitor: Visitor<PluginPass> = { |
| 207 |
Class(path, state) { |
| 208 |
if (!hasPrivateClassElement(path.node.body)) return; |
| 209 |
path.traverse(privateKeyDestructuringVisitor, state); |
| 210 |
}, |
| 211 |
}; |
| 212 |
|
| 213 |
return { |
| 214 |
name: "proposal-destructuring-private", |
| 215 |
inherits: syntaxDestructuringPrivate, |
| 216 |
visitor: visitor, |
| 217 |
}; |
| 218 |
}); |