| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.buildCheckInRHS = buildCheckInRHS;
|
|---|
| 7 | exports.buildFieldsInitNodes = buildFieldsInitNodes;
|
|---|
| 8 | exports.buildPrivateNamesMap = buildPrivateNamesMap;
|
|---|
| 9 | exports.buildPrivateNamesNodes = buildPrivateNamesNodes;
|
|---|
| 10 | exports.privateNameVisitorFactory = privateNameVisitorFactory;
|
|---|
| 11 | exports.transformPrivateNamesUsage = transformPrivateNamesUsage;
|
|---|
| 12 | var _core = require("@babel/core");
|
|---|
| 13 | var _traverse = require("@babel/traverse");
|
|---|
| 14 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
|---|
| 15 | var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
|
|---|
| 16 | var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
|
|---|
| 17 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
|---|
| 18 | var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
|---|
| 19 | var ts = require("./typescript.js");
|
|---|
| 20 | var newHelpers = file => {
|
|---|
| 21 | return file.availableHelper("classPrivateFieldGet2");
|
|---|
| 22 | };
|
|---|
| 23 | function buildPrivateNamesMap(className, privateFieldsAsSymbolsOrProperties, props, file) {
|
|---|
| 24 | const privateNamesMap = new Map();
|
|---|
| 25 | let classBrandId;
|
|---|
| 26 | for (const prop of props) {
|
|---|
| 27 | if (prop.isPrivate()) {
|
|---|
| 28 | const {
|
|---|
| 29 | name
|
|---|
| 30 | } = prop.node.key.id;
|
|---|
| 31 | let update = privateNamesMap.get(name);
|
|---|
| 32 | if (!update) {
|
|---|
| 33 | const isMethod = !prop.isProperty();
|
|---|
| 34 | const isStatic = prop.node.static;
|
|---|
| 35 | let initAdded = false;
|
|---|
| 36 | let id;
|
|---|
| 37 | if (!privateFieldsAsSymbolsOrProperties && newHelpers(file) && isMethod && !isStatic) {
|
|---|
| 38 | initAdded = !!classBrandId;
|
|---|
| 39 | classBrandId != null ? classBrandId : classBrandId = prop.scope.generateUidIdentifier(`${className}_brand`);
|
|---|
| 40 | id = classBrandId;
|
|---|
| 41 | } else {
|
|---|
| 42 | id = prop.scope.generateUidIdentifier(name);
|
|---|
| 43 | }
|
|---|
| 44 | update = {
|
|---|
| 45 | id,
|
|---|
| 46 | static: isStatic,
|
|---|
| 47 | method: isMethod,
|
|---|
| 48 | initAdded
|
|---|
| 49 | };
|
|---|
| 50 | privateNamesMap.set(name, update);
|
|---|
| 51 | }
|
|---|
| 52 | if (prop.isClassPrivateMethod()) {
|
|---|
| 53 | if (prop.node.kind === "get") {
|
|---|
| 54 | const {
|
|---|
| 55 | body
|
|---|
| 56 | } = prop.node.body;
|
|---|
| 57 | let $;
|
|---|
| 58 | if (body.length === 1 && _core.types.isReturnStatement($ = body[0]) && _core.types.isCallExpression($ = $.argument) && $.arguments.length === 1 && _core.types.isThisExpression($.arguments[0]) && _core.types.isIdentifier($ = $.callee)) {
|
|---|
| 59 | update.getId = _core.types.cloneNode($);
|
|---|
| 60 | update.getterDeclared = true;
|
|---|
| 61 | } else {
|
|---|
| 62 | update.getId = prop.scope.generateUidIdentifier(`get_${name}`);
|
|---|
| 63 | }
|
|---|
| 64 | } else if (prop.node.kind === "set") {
|
|---|
| 65 | const {
|
|---|
| 66 | params
|
|---|
| 67 | } = prop.node;
|
|---|
| 68 | const {
|
|---|
| 69 | body
|
|---|
| 70 | } = prop.node.body;
|
|---|
| 71 | let $;
|
|---|
| 72 | if (body.length === 1 && _core.types.isExpressionStatement($ = body[0]) && _core.types.isCallExpression($ = $.expression) && $.arguments.length === 2 && _core.types.isThisExpression($.arguments[0]) && _core.types.isIdentifier($.arguments[1], {
|
|---|
| 73 | name: params[0].name
|
|---|
| 74 | }) && _core.types.isIdentifier($ = $.callee)) {
|
|---|
| 75 | update.setId = _core.types.cloneNode($);
|
|---|
| 76 | update.setterDeclared = true;
|
|---|
| 77 | } else {
|
|---|
| 78 | update.setId = prop.scope.generateUidIdentifier(`set_${name}`);
|
|---|
| 79 | }
|
|---|
| 80 | } else if (prop.node.kind === "method") {
|
|---|
| 81 | update.methodId = prop.scope.generateUidIdentifier(name);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | privateNamesMap.set(name, update);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | return privateNamesMap;
|
|---|
| 88 | }
|
|---|
| 89 | function buildPrivateNamesNodes(privateNamesMap, privateFieldsAsProperties, privateFieldsAsSymbols, state) {
|
|---|
| 90 | const initNodes = [];
|
|---|
| 91 | const injectedIds = new Set();
|
|---|
| 92 | for (const [name, value] of privateNamesMap) {
|
|---|
| 93 | const {
|
|---|
| 94 | static: isStatic,
|
|---|
| 95 | method: isMethod,
|
|---|
| 96 | getId,
|
|---|
| 97 | setId
|
|---|
| 98 | } = value;
|
|---|
| 99 | const isGetterOrSetter = getId || setId;
|
|---|
| 100 | const id = _core.types.cloneNode(value.id);
|
|---|
| 101 | let init;
|
|---|
| 102 | if (privateFieldsAsProperties) {
|
|---|
| 103 | init = _core.types.callExpression(state.addHelper("classPrivateFieldLooseKey"), [_core.types.stringLiteral(name)]);
|
|---|
| 104 | } else if (privateFieldsAsSymbols) {
|
|---|
| 105 | init = _core.types.callExpression(_core.types.identifier("Symbol"), [_core.types.stringLiteral(name)]);
|
|---|
| 106 | } else if (!isStatic) {
|
|---|
| 107 | if (injectedIds.has(id.name)) continue;
|
|---|
| 108 | injectedIds.add(id.name);
|
|---|
| 109 | init = _core.types.newExpression(_core.types.identifier(isMethod && (!isGetterOrSetter || newHelpers(state)) ? "WeakSet" : "WeakMap"), []);
|
|---|
| 110 | }
|
|---|
| 111 | if (init) {
|
|---|
| 112 | if (!privateFieldsAsSymbols) {
|
|---|
| 113 | (0, _helperAnnotateAsPure.default)(init);
|
|---|
| 114 | }
|
|---|
| 115 | initNodes.push(_core.template.statement.ast`var ${id} = ${init}`);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | return initNodes;
|
|---|
| 119 | }
|
|---|
| 120 | function privateNameVisitorFactory(visitor) {
|
|---|
| 121 | const nestedVisitor = _traverse.visitors.environmentVisitor(Object.assign({}, visitor));
|
|---|
| 122 | const privateNameVisitor = Object.assign({}, visitor, {
|
|---|
| 123 | Class(path) {
|
|---|
| 124 | const {
|
|---|
| 125 | privateNamesMap
|
|---|
| 126 | } = this;
|
|---|
| 127 | const body = path.get("body.body");
|
|---|
| 128 | const visiblePrivateNames = new Map(privateNamesMap);
|
|---|
| 129 | const redeclared = [];
|
|---|
| 130 | for (const prop of body) {
|
|---|
| 131 | if (!prop.isPrivate()) continue;
|
|---|
| 132 | const {
|
|---|
| 133 | name
|
|---|
| 134 | } = prop.node.key.id;
|
|---|
| 135 | visiblePrivateNames.delete(name);
|
|---|
| 136 | redeclared.push(name);
|
|---|
| 137 | }
|
|---|
| 138 | if (!redeclared.length) {
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 | path.get("body").traverse(nestedVisitor, Object.assign({}, this, {
|
|---|
| 142 | redeclared
|
|---|
| 143 | }));
|
|---|
| 144 | path.traverse(privateNameVisitor, Object.assign({}, this, {
|
|---|
| 145 | privateNamesMap: visiblePrivateNames
|
|---|
| 146 | }));
|
|---|
| 147 | path.skipKey("body");
|
|---|
| 148 | }
|
|---|
| 149 | });
|
|---|
| 150 | return privateNameVisitor;
|
|---|
| 151 | }
|
|---|
| 152 | const privateNameVisitor = privateNameVisitorFactory({
|
|---|
| 153 | PrivateName(path, {
|
|---|
| 154 | noDocumentAll
|
|---|
| 155 | }) {
|
|---|
| 156 | const {
|
|---|
| 157 | privateNamesMap,
|
|---|
| 158 | redeclared
|
|---|
| 159 | } = this;
|
|---|
| 160 | const {
|
|---|
| 161 | node,
|
|---|
| 162 | parentPath
|
|---|
| 163 | } = path;
|
|---|
| 164 | if (!parentPath.isMemberExpression({
|
|---|
| 165 | property: node
|
|---|
| 166 | }) && !parentPath.isOptionalMemberExpression({
|
|---|
| 167 | property: node
|
|---|
| 168 | })) {
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 | const {
|
|---|
| 172 | name
|
|---|
| 173 | } = node.id;
|
|---|
| 174 | if (!privateNamesMap.has(name)) return;
|
|---|
| 175 | if (redeclared != null && redeclared.includes(name)) return;
|
|---|
| 176 | this.handle(parentPath, noDocumentAll);
|
|---|
| 177 | }
|
|---|
| 178 | });
|
|---|
| 179 | function unshadow(name, scope, innerBinding) {
|
|---|
| 180 | while ((_scope = scope) != null && _scope.hasBinding(name) && !scope.bindingIdentifierEquals(name, innerBinding)) {
|
|---|
| 181 | var _scope;
|
|---|
| 182 | scope.rename(name);
|
|---|
| 183 | scope = scope.parent;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | function buildCheckInRHS(rhs, file, inRHSIsObject) {
|
|---|
| 187 | if (inRHSIsObject || !(file.availableHelper != null && file.availableHelper("checkInRHS"))) return rhs;
|
|---|
| 188 | return _core.types.callExpression(file.addHelper("checkInRHS"), [rhs]);
|
|---|
| 189 | }
|
|---|
| 190 | const privateInVisitor = privateNameVisitorFactory({
|
|---|
| 191 | BinaryExpression(path, {
|
|---|
| 192 | file
|
|---|
| 193 | }) {
|
|---|
| 194 | const {
|
|---|
| 195 | operator,
|
|---|
| 196 | left,
|
|---|
| 197 | right
|
|---|
| 198 | } = path.node;
|
|---|
| 199 | if (operator !== "in") return;
|
|---|
| 200 | if (!_core.types.isPrivateName(left)) return;
|
|---|
| 201 | const {
|
|---|
| 202 | privateFieldsAsProperties,
|
|---|
| 203 | privateNamesMap,
|
|---|
| 204 | redeclared
|
|---|
| 205 | } = this;
|
|---|
| 206 | const {
|
|---|
| 207 | name
|
|---|
| 208 | } = left.id;
|
|---|
| 209 | if (!privateNamesMap.has(name)) return;
|
|---|
| 210 | if (redeclared != null && redeclared.includes(name)) return;
|
|---|
| 211 | unshadow(this.classRef.name, path.scope, this.innerBinding);
|
|---|
| 212 | if (privateFieldsAsProperties) {
|
|---|
| 213 | const {
|
|---|
| 214 | id
|
|---|
| 215 | } = privateNamesMap.get(name);
|
|---|
| 216 | path.replaceWith(_core.template.expression.ast`
|
|---|
| 217 | Object.prototype.hasOwnProperty.call(${buildCheckInRHS(right, file)}, ${_core.types.cloneNode(id)})
|
|---|
| 218 | `);
|
|---|
| 219 | return;
|
|---|
| 220 | }
|
|---|
| 221 | const {
|
|---|
| 222 | id,
|
|---|
| 223 | static: isStatic
|
|---|
| 224 | } = privateNamesMap.get(name);
|
|---|
| 225 | if (isStatic) {
|
|---|
| 226 | path.replaceWith(_core.template.expression.ast`${buildCheckInRHS(right, file)} === ${_core.types.cloneNode(this.classRef)}`);
|
|---|
| 227 | return;
|
|---|
| 228 | }
|
|---|
| 229 | path.replaceWith(_core.template.expression.ast`${_core.types.cloneNode(id)}.has(${buildCheckInRHS(right, file)})`);
|
|---|
| 230 | }
|
|---|
| 231 | });
|
|---|
| 232 | function readOnlyError(file, name) {
|
|---|
| 233 | return _core.types.callExpression(file.addHelper("readOnlyError"), [_core.types.stringLiteral(`#${name}`)]);
|
|---|
| 234 | }
|
|---|
| 235 | function writeOnlyError(file, name) {
|
|---|
| 236 | if (!file.availableHelper("writeOnlyError")) {
|
|---|
| 237 | console.warn(`@babel/helpers is outdated, update it to silence this warning.`);
|
|---|
| 238 | return _core.types.buildUndefinedNode();
|
|---|
| 239 | }
|
|---|
| 240 | return _core.types.callExpression(file.addHelper("writeOnlyError"), [_core.types.stringLiteral(`#${name}`)]);
|
|---|
| 241 | }
|
|---|
| 242 | function buildStaticPrivateFieldAccess(expr, noUninitializedPrivateFieldAccess) {
|
|---|
| 243 | if (noUninitializedPrivateFieldAccess) return expr;
|
|---|
| 244 | return _core.types.memberExpression(expr, _core.types.identifier("_"));
|
|---|
| 245 | }
|
|---|
| 246 | function autoInherits(fn) {
|
|---|
| 247 | return function (member) {
|
|---|
| 248 | return _core.types.inherits(fn.apply(this, arguments), member.node);
|
|---|
| 249 | };
|
|---|
| 250 | }
|
|---|
| 251 | const privateNameHandlerSpec = {
|
|---|
| 252 | memoise(member, count) {
|
|---|
| 253 | const {
|
|---|
| 254 | scope
|
|---|
| 255 | } = member;
|
|---|
| 256 | const {
|
|---|
| 257 | object
|
|---|
| 258 | } = member.node;
|
|---|
| 259 | const memo = scope.maybeGenerateMemoised(object);
|
|---|
| 260 | if (!memo) {
|
|---|
| 261 | return;
|
|---|
| 262 | }
|
|---|
| 263 | this.memoiser.set(object, memo, count);
|
|---|
| 264 | },
|
|---|
| 265 | receiver(member) {
|
|---|
| 266 | const {
|
|---|
| 267 | object
|
|---|
| 268 | } = member.node;
|
|---|
| 269 | if (this.memoiser.has(object)) {
|
|---|
| 270 | return _core.types.cloneNode(this.memoiser.get(object));
|
|---|
| 271 | }
|
|---|
| 272 | return _core.types.cloneNode(object);
|
|---|
| 273 | },
|
|---|
| 274 | get: autoInherits(function (member) {
|
|---|
| 275 | const {
|
|---|
| 276 | classRef,
|
|---|
| 277 | privateNamesMap,
|
|---|
| 278 | file,
|
|---|
| 279 | innerBinding,
|
|---|
| 280 | noUninitializedPrivateFieldAccess
|
|---|
| 281 | } = this;
|
|---|
| 282 | const privateName = member.node.property;
|
|---|
| 283 | const {
|
|---|
| 284 | name
|
|---|
| 285 | } = privateName.id;
|
|---|
| 286 | const {
|
|---|
| 287 | id,
|
|---|
| 288 | static: isStatic,
|
|---|
| 289 | method: isMethod,
|
|---|
| 290 | methodId,
|
|---|
| 291 | getId,
|
|---|
| 292 | setId
|
|---|
| 293 | } = privateNamesMap.get(name);
|
|---|
| 294 | const isGetterOrSetter = getId || setId;
|
|---|
| 295 | const cloneId = id => _core.types.inherits(_core.types.cloneNode(id), privateName);
|
|---|
| 296 | if (isStatic) {
|
|---|
| 297 | unshadow(classRef.name, member.scope, innerBinding);
|
|---|
| 298 | if (!newHelpers(file)) {
|
|---|
| 299 | const helperName = isMethod && !isGetterOrSetter ? "classStaticPrivateMethodGet" : "classStaticPrivateFieldSpecGet";
|
|---|
| 300 | return _core.types.callExpression(file.addHelper(helperName), [this.receiver(member), _core.types.cloneNode(classRef), cloneId(id)]);
|
|---|
| 301 | }
|
|---|
| 302 | const receiver = this.receiver(member);
|
|---|
| 303 | const skipCheck = _core.types.isIdentifier(receiver) && receiver.name === classRef.name;
|
|---|
| 304 | if (!isMethod) {
|
|---|
| 305 | if (skipCheck) {
|
|---|
| 306 | return buildStaticPrivateFieldAccess(cloneId(id), noUninitializedPrivateFieldAccess);
|
|---|
| 307 | }
|
|---|
| 308 | return buildStaticPrivateFieldAccess(_core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(classRef), receiver, cloneId(id)]), noUninitializedPrivateFieldAccess);
|
|---|
| 309 | }
|
|---|
| 310 | if (getId) {
|
|---|
| 311 | if (skipCheck) {
|
|---|
| 312 | return _core.types.callExpression(cloneId(getId), [receiver]);
|
|---|
| 313 | }
|
|---|
| 314 | return _core.types.callExpression(file.addHelper("classPrivateGetter"), [_core.types.cloneNode(classRef), receiver, cloneId(getId)]);
|
|---|
| 315 | }
|
|---|
| 316 | if (setId) {
|
|---|
| 317 | const err = _core.types.buildUndefinedNode();
|
|---|
| 318 | if (skipCheck) return err;
|
|---|
| 319 | return _core.types.sequenceExpression([_core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(classRef), receiver]), err]);
|
|---|
| 320 | }
|
|---|
| 321 | if (skipCheck) return cloneId(id);
|
|---|
| 322 | return _core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(classRef), receiver, cloneId(id)]);
|
|---|
| 323 | }
|
|---|
| 324 | if (isMethod) {
|
|---|
| 325 | if (isGetterOrSetter) {
|
|---|
| 326 | if (!getId) {
|
|---|
| 327 | return _core.types.sequenceExpression([this.receiver(member), writeOnlyError(file, name)]);
|
|---|
| 328 | }
|
|---|
| 329 | if (!newHelpers(file)) {
|
|---|
| 330 | return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), cloneId(id)]);
|
|---|
| 331 | }
|
|---|
| 332 | return _core.types.callExpression(file.addHelper("classPrivateGetter"), [_core.types.cloneNode(id), this.receiver(member), cloneId(getId)]);
|
|---|
| 333 | }
|
|---|
| 334 | if (!newHelpers(file)) {
|
|---|
| 335 | return _core.types.callExpression(file.addHelper("classPrivateMethodGet"), [this.receiver(member), _core.types.cloneNode(id), cloneId(methodId)]);
|
|---|
| 336 | }
|
|---|
| 337 | return _core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(id), this.receiver(member), cloneId(methodId)]);
|
|---|
| 338 | }
|
|---|
| 339 | if (newHelpers(file)) {
|
|---|
| 340 | return _core.types.callExpression(file.addHelper("classPrivateFieldGet2"), [cloneId(id), this.receiver(member)]);
|
|---|
| 341 | }
|
|---|
| 342 | return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), cloneId(id)]);
|
|---|
| 343 | }),
|
|---|
| 344 | boundGet(member) {
|
|---|
| 345 | this.memoise(member, 1);
|
|---|
| 346 | return _core.types.callExpression(_core.types.memberExpression(this.get(member), _core.types.identifier("bind")), [this.receiver(member)]);
|
|---|
| 347 | },
|
|---|
| 348 | set: autoInherits(function (member, value) {
|
|---|
| 349 | const {
|
|---|
| 350 | classRef,
|
|---|
| 351 | privateNamesMap,
|
|---|
| 352 | file,
|
|---|
| 353 | noUninitializedPrivateFieldAccess
|
|---|
| 354 | } = this;
|
|---|
| 355 | const privateName = member.node.property;
|
|---|
| 356 | const {
|
|---|
| 357 | name
|
|---|
| 358 | } = privateName.id;
|
|---|
| 359 | const {
|
|---|
| 360 | id,
|
|---|
| 361 | static: isStatic,
|
|---|
| 362 | method: isMethod,
|
|---|
| 363 | setId,
|
|---|
| 364 | getId
|
|---|
| 365 | } = privateNamesMap.get(name);
|
|---|
| 366 | const isGetterOrSetter = getId || setId;
|
|---|
| 367 | const cloneId = id => _core.types.inherits(_core.types.cloneNode(id), privateName);
|
|---|
| 368 | if (isStatic) {
|
|---|
| 369 | if (!newHelpers(file)) {
|
|---|
| 370 | const helperName = isMethod && !isGetterOrSetter ? "classStaticPrivateMethodSet" : "classStaticPrivateFieldSpecSet";
|
|---|
| 371 | return _core.types.callExpression(file.addHelper(helperName), [this.receiver(member), _core.types.cloneNode(classRef), cloneId(id), value]);
|
|---|
| 372 | }
|
|---|
| 373 | const receiver = this.receiver(member);
|
|---|
| 374 | const skipCheck = _core.types.isIdentifier(receiver) && receiver.name === classRef.name;
|
|---|
| 375 | if (isMethod && !setId) {
|
|---|
| 376 | const err = readOnlyError(file, name);
|
|---|
| 377 | if (skipCheck) return _core.types.sequenceExpression([value, err]);
|
|---|
| 378 | return _core.types.sequenceExpression([value, _core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(classRef), receiver]), readOnlyError(file, name)]);
|
|---|
| 379 | }
|
|---|
| 380 | if (setId) {
|
|---|
| 381 | if (skipCheck) {
|
|---|
| 382 | return _core.types.callExpression(_core.types.cloneNode(setId), [receiver, value]);
|
|---|
| 383 | }
|
|---|
| 384 | return _core.types.callExpression(file.addHelper("classPrivateSetter"), [_core.types.cloneNode(classRef), cloneId(setId), receiver, value]);
|
|---|
| 385 | }
|
|---|
| 386 | return _core.types.assignmentExpression("=", buildStaticPrivateFieldAccess(cloneId(id), noUninitializedPrivateFieldAccess), skipCheck ? value : _core.types.callExpression(file.addHelper("assertClassBrand"), [_core.types.cloneNode(classRef), receiver, value]));
|
|---|
| 387 | }
|
|---|
| 388 | if (isMethod) {
|
|---|
| 389 | if (setId) {
|
|---|
| 390 | if (!newHelpers(file)) {
|
|---|
| 391 | return _core.types.callExpression(file.addHelper("classPrivateFieldSet"), [this.receiver(member), cloneId(id), value]);
|
|---|
| 392 | }
|
|---|
| 393 | return _core.types.callExpression(file.addHelper("classPrivateSetter"), [_core.types.cloneNode(id), cloneId(setId), this.receiver(member), value]);
|
|---|
| 394 | }
|
|---|
| 395 | return _core.types.sequenceExpression([this.receiver(member), value, readOnlyError(file, name)]);
|
|---|
| 396 | }
|
|---|
| 397 | if (newHelpers(file)) {
|
|---|
| 398 | return _core.types.callExpression(file.addHelper("classPrivateFieldSet2"), [cloneId(id), this.receiver(member), value]);
|
|---|
| 399 | }
|
|---|
| 400 | return _core.types.callExpression(file.addHelper("classPrivateFieldSet"), [this.receiver(member), cloneId(id), value]);
|
|---|
| 401 | }),
|
|---|
| 402 | destructureSet(member) {
|
|---|
| 403 | const {
|
|---|
| 404 | classRef,
|
|---|
| 405 | privateNamesMap,
|
|---|
| 406 | file,
|
|---|
| 407 | noUninitializedPrivateFieldAccess
|
|---|
| 408 | } = this;
|
|---|
| 409 | const privateName = member.node.property;
|
|---|
| 410 | const {
|
|---|
| 411 | name
|
|---|
| 412 | } = privateName.id;
|
|---|
| 413 | const {
|
|---|
| 414 | id,
|
|---|
| 415 | static: isStatic,
|
|---|
| 416 | method: isMethod,
|
|---|
| 417 | setId
|
|---|
| 418 | } = privateNamesMap.get(name);
|
|---|
| 419 | const cloneId = id => _core.types.inherits(_core.types.cloneNode(id), privateName);
|
|---|
| 420 | if (!newHelpers(file)) {
|
|---|
| 421 | if (isStatic) {
|
|---|
| 422 | try {
|
|---|
| 423 | var helper = file.addHelper("classStaticPrivateFieldDestructureSet");
|
|---|
| 424 | } catch (_unused) {
|
|---|
| 425 | throw new Error("Babel can not transpile `[C.#p] = [0]` with @babel/helpers < 7.13.10, \n" + "please update @babel/helpers to the latest version.");
|
|---|
| 426 | }
|
|---|
| 427 | return _core.types.memberExpression(_core.types.callExpression(helper, [this.receiver(member), _core.types.cloneNode(classRef), cloneId(id)]), _core.types.identifier("value"));
|
|---|
| 428 | }
|
|---|
| 429 | return _core.types.memberExpression(_core.types.callExpression(file.addHelper("classPrivateFieldDestructureSet"), [this.receiver(member), cloneId(id)]), _core.types.identifier("value"));
|
|---|
| 430 | }
|
|---|
| 431 | if (isMethod && !setId) {
|
|---|
| 432 | return _core.types.memberExpression(_core.types.sequenceExpression([member.node.object, readOnlyError(file, name)]), _core.types.identifier("_"));
|
|---|
| 433 | }
|
|---|
| 434 | if (isStatic && !isMethod) {
|
|---|
| 435 | const getCall = this.get(member);
|
|---|
| 436 | if (!noUninitializedPrivateFieldAccess || !_core.types.isCallExpression(getCall)) {
|
|---|
| 437 | return getCall;
|
|---|
| 438 | }
|
|---|
| 439 | const ref = getCall.arguments.pop();
|
|---|
| 440 | getCall.arguments.push(_core.template.expression.ast`(_) => ${ref} = _`);
|
|---|
| 441 | return _core.types.memberExpression(_core.types.callExpression(file.addHelper("toSetter"), [getCall]), _core.types.identifier("_"));
|
|---|
| 442 | }
|
|---|
| 443 | const setCall = this.set(member, _core.types.identifier("_"));
|
|---|
| 444 | if (!_core.types.isCallExpression(setCall) || !_core.types.isIdentifier(setCall.arguments[setCall.arguments.length - 1], {
|
|---|
| 445 | name: "_"
|
|---|
| 446 | })) {
|
|---|
| 447 | throw member.buildCodeFrameError("Internal Babel error while compiling this code. This is a Babel bug. " + "Please report it at https://github.com/babel/babel/issues.");
|
|---|
| 448 | }
|
|---|
| 449 | let args;
|
|---|
| 450 | if (_core.types.isMemberExpression(setCall.callee, {
|
|---|
| 451 | computed: false
|
|---|
| 452 | }) && _core.types.isIdentifier(setCall.callee.property) && setCall.callee.property.name === "call") {
|
|---|
| 453 | args = [setCall.callee.object, _core.types.arrayExpression(setCall.arguments.slice(1, -1)), setCall.arguments[0]];
|
|---|
| 454 | } else {
|
|---|
| 455 | args = [setCall.callee, _core.types.arrayExpression(setCall.arguments.slice(0, -1))];
|
|---|
| 456 | }
|
|---|
| 457 | return _core.types.memberExpression(_core.types.callExpression(file.addHelper("toSetter"), args), _core.types.identifier("_"));
|
|---|
| 458 | },
|
|---|
| 459 | call(member, args) {
|
|---|
| 460 | this.memoise(member, 1);
|
|---|
| 461 | return (0, _helperOptimiseCallExpression.default)(this.get(member), this.receiver(member), args, false);
|
|---|
| 462 | },
|
|---|
| 463 | optionalCall(member, args) {
|
|---|
| 464 | this.memoise(member, 1);
|
|---|
| 465 | return (0, _helperOptimiseCallExpression.default)(this.get(member), this.receiver(member), args, true);
|
|---|
| 466 | },
|
|---|
| 467 | delete() {
|
|---|
| 468 | throw new Error("Internal Babel error: deleting private elements is a parsing error.");
|
|---|
| 469 | }
|
|---|
| 470 | };
|
|---|
| 471 | const privateNameHandlerLoose = {
|
|---|
| 472 | get(member) {
|
|---|
| 473 | const {
|
|---|
| 474 | privateNamesMap,
|
|---|
| 475 | file
|
|---|
| 476 | } = this;
|
|---|
| 477 | const {
|
|---|
| 478 | object
|
|---|
| 479 | } = member.node;
|
|---|
| 480 | const {
|
|---|
| 481 | name
|
|---|
| 482 | } = member.node.property.id;
|
|---|
| 483 | return _core.template.expression`BASE(REF, PROP)[PROP]`({
|
|---|
| 484 | BASE: file.addHelper("classPrivateFieldLooseBase"),
|
|---|
| 485 | REF: _core.types.cloneNode(object),
|
|---|
| 486 | PROP: _core.types.cloneNode(privateNamesMap.get(name).id)
|
|---|
| 487 | });
|
|---|
| 488 | },
|
|---|
| 489 | set() {
|
|---|
| 490 | throw new Error("private name handler with loose = true don't need set()");
|
|---|
| 491 | },
|
|---|
| 492 | boundGet(member) {
|
|---|
| 493 | return _core.types.callExpression(_core.types.memberExpression(this.get(member), _core.types.identifier("bind")), [_core.types.cloneNode(member.node.object)]);
|
|---|
| 494 | },
|
|---|
| 495 | simpleSet(member) {
|
|---|
| 496 | return this.get(member);
|
|---|
| 497 | },
|
|---|
| 498 | destructureSet(member) {
|
|---|
| 499 | return this.get(member);
|
|---|
| 500 | },
|
|---|
| 501 | call(member, args) {
|
|---|
| 502 | return _core.types.callExpression(this.get(member), args);
|
|---|
| 503 | },
|
|---|
| 504 | optionalCall(member, args) {
|
|---|
| 505 | return _core.types.optionalCallExpression(this.get(member), args, true);
|
|---|
| 506 | },
|
|---|
| 507 | delete() {
|
|---|
| 508 | throw new Error("Internal Babel error: deleting private elements is a parsing error.");
|
|---|
| 509 | }
|
|---|
| 510 | };
|
|---|
| 511 | function transformPrivateNamesUsage(ref, path, privateNamesMap, {
|
|---|
| 512 | privateFieldsAsProperties,
|
|---|
| 513 | noUninitializedPrivateFieldAccess,
|
|---|
| 514 | noDocumentAll,
|
|---|
| 515 | innerBinding
|
|---|
| 516 | }, state) {
|
|---|
| 517 | if (!privateNamesMap.size) return;
|
|---|
| 518 | const body = path.get("body");
|
|---|
| 519 | const handler = privateFieldsAsProperties ? privateNameHandlerLoose : privateNameHandlerSpec;
|
|---|
| 520 | (0, _helperMemberExpressionToFunctions.default)(body, privateNameVisitor, Object.assign({
|
|---|
| 521 | privateNamesMap,
|
|---|
| 522 | classRef: ref,
|
|---|
| 523 | file: state
|
|---|
| 524 | }, handler, {
|
|---|
| 525 | noDocumentAll,
|
|---|
| 526 | noUninitializedPrivateFieldAccess,
|
|---|
| 527 | innerBinding
|
|---|
| 528 | }));
|
|---|
| 529 | body.traverse(privateInVisitor, {
|
|---|
| 530 | privateNamesMap,
|
|---|
| 531 | classRef: ref,
|
|---|
| 532 | file: state,
|
|---|
| 533 | privateFieldsAsProperties,
|
|---|
| 534 | innerBinding
|
|---|
| 535 | });
|
|---|
| 536 | }
|
|---|
| 537 | function buildPrivateFieldInitLoose(ref, prop, privateNamesMap) {
|
|---|
| 538 | const {
|
|---|
| 539 | id
|
|---|
| 540 | } = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 541 | const value = prop.node.value || prop.scope.buildUndefinedNode();
|
|---|
| 542 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 543 | Object.defineProperty(${ref}, ${_core.types.cloneNode(id)}, {
|
|---|
| 544 | // configurable is false by default
|
|---|
| 545 | // enumerable is false by default
|
|---|
| 546 | writable: true,
|
|---|
| 547 | value: ${value}
|
|---|
| 548 | });
|
|---|
| 549 | `, prop);
|
|---|
| 550 | }
|
|---|
| 551 | function buildPrivateInstanceFieldInitSpec(ref, prop, privateNamesMap, state) {
|
|---|
| 552 | const {
|
|---|
| 553 | id
|
|---|
| 554 | } = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 555 | const value = prop.node.value || prop.scope.buildUndefinedNode();
|
|---|
| 556 | if (!state.availableHelper("classPrivateFieldInitSpec")) {
|
|---|
| 557 | return inheritPropComments(_core.template.statement.ast`${_core.types.cloneNode(id)}.set(${ref}, {
|
|---|
| 558 | // configurable is always false for private elements
|
|---|
| 559 | // enumerable is always false for private elements
|
|---|
| 560 | writable: true,
|
|---|
| 561 | value: ${value},
|
|---|
| 562 | })`, prop);
|
|---|
| 563 | }
|
|---|
| 564 | const helper = state.addHelper("classPrivateFieldInitSpec");
|
|---|
| 565 | return inheritLoc(inheritPropComments(_core.types.expressionStatement(_core.types.callExpression(helper, [_core.types.thisExpression(), inheritLoc(_core.types.cloneNode(id), prop.node.key), newHelpers(state) ? value : _core.template.expression.ast`{ writable: true, value: ${value} }`])), prop), prop.node);
|
|---|
| 566 | }
|
|---|
| 567 | function buildPrivateStaticFieldInitSpec(prop, privateNamesMap, noUninitializedPrivateFieldAccess) {
|
|---|
| 568 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 569 | const value = noUninitializedPrivateFieldAccess ? prop.node.value : _core.template.expression.ast`{
|
|---|
| 570 | _: ${prop.node.value || _core.types.buildUndefinedNode()}
|
|---|
| 571 | }`;
|
|---|
| 572 | return inheritPropComments(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.cloneNode(privateName.id), value)]), prop);
|
|---|
| 573 | }
|
|---|
| 574 | var buildPrivateStaticFieldInitSpecOld = function (prop, privateNamesMap) {
|
|---|
| 575 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 576 | const {
|
|---|
| 577 | id,
|
|---|
| 578 | getId,
|
|---|
| 579 | setId,
|
|---|
| 580 | initAdded
|
|---|
| 581 | } = privateName;
|
|---|
| 582 | const isGetterOrSetter = getId || setId;
|
|---|
| 583 | if (!prop.isProperty() && (initAdded || !isGetterOrSetter)) return;
|
|---|
| 584 | if (isGetterOrSetter) {
|
|---|
| 585 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 586 | initAdded: true
|
|---|
| 587 | }));
|
|---|
| 588 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 589 | var ${_core.types.cloneNode(id)} = {
|
|---|
| 590 | // configurable is false by default
|
|---|
| 591 | // enumerable is false by default
|
|---|
| 592 | // writable is false by default
|
|---|
| 593 | get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
|
|---|
| 594 | set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
|
|---|
| 595 | }
|
|---|
| 596 | `, prop);
|
|---|
| 597 | }
|
|---|
| 598 | const value = prop.node.value || prop.scope.buildUndefinedNode();
|
|---|
| 599 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 600 | var ${_core.types.cloneNode(id)} = {
|
|---|
| 601 | // configurable is false by default
|
|---|
| 602 | // enumerable is false by default
|
|---|
| 603 | writable: true,
|
|---|
| 604 | value: ${value}
|
|---|
| 605 | };
|
|---|
| 606 | `, prop);
|
|---|
| 607 | };
|
|---|
| 608 | function buildPrivateMethodInitLoose(ref, prop, privateNamesMap) {
|
|---|
| 609 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 610 | const {
|
|---|
| 611 | methodId,
|
|---|
| 612 | id,
|
|---|
| 613 | getId,
|
|---|
| 614 | setId,
|
|---|
| 615 | initAdded
|
|---|
| 616 | } = privateName;
|
|---|
| 617 | if (initAdded) return;
|
|---|
| 618 | if (methodId) {
|
|---|
| 619 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 620 | Object.defineProperty(${ref}, ${id}, {
|
|---|
| 621 | // configurable is false by default
|
|---|
| 622 | // enumerable is false by default
|
|---|
| 623 | // writable is false by default
|
|---|
| 624 | value: ${methodId.name}
|
|---|
| 625 | });
|
|---|
| 626 | `, prop);
|
|---|
| 627 | }
|
|---|
| 628 | const isGetterOrSetter = getId || setId;
|
|---|
| 629 | if (isGetterOrSetter) {
|
|---|
| 630 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 631 | initAdded: true
|
|---|
| 632 | }));
|
|---|
| 633 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 634 | Object.defineProperty(${ref}, ${id}, {
|
|---|
| 635 | // configurable is false by default
|
|---|
| 636 | // enumerable is false by default
|
|---|
| 637 | // writable is false by default
|
|---|
| 638 | get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
|
|---|
| 639 | set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
|
|---|
| 640 | });
|
|---|
| 641 | `, prop);
|
|---|
| 642 | }
|
|---|
| 643 | }
|
|---|
| 644 | function buildPrivateInstanceMethodInitSpec(ref, prop, privateNamesMap, state) {
|
|---|
| 645 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 646 | if (privateName.initAdded) return;
|
|---|
| 647 | if (!newHelpers(state)) {
|
|---|
| 648 | const isGetterOrSetter = privateName.getId || privateName.setId;
|
|---|
| 649 | if (isGetterOrSetter) {
|
|---|
| 650 | return buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state);
|
|---|
| 651 | }
|
|---|
| 652 | }
|
|---|
| 653 | return buildPrivateInstanceMethodInitialization(ref, prop, privateNamesMap, state);
|
|---|
| 654 | }
|
|---|
| 655 | function buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state) {
|
|---|
| 656 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 657 | const {
|
|---|
| 658 | id,
|
|---|
| 659 | getId,
|
|---|
| 660 | setId
|
|---|
| 661 | } = privateName;
|
|---|
| 662 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 663 | initAdded: true
|
|---|
| 664 | }));
|
|---|
| 665 | if (!state.availableHelper("classPrivateFieldInitSpec")) {
|
|---|
| 666 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 667 | ${id}.set(${ref}, {
|
|---|
| 668 | get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
|
|---|
| 669 | set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
|
|---|
| 670 | });
|
|---|
| 671 | `, prop);
|
|---|
| 672 | }
|
|---|
| 673 | const helper = state.addHelper("classPrivateFieldInitSpec");
|
|---|
| 674 | return inheritLoc(inheritPropComments(_core.template.statement.ast`${helper}(
|
|---|
| 675 | ${_core.types.thisExpression()},
|
|---|
| 676 | ${_core.types.cloneNode(id)},
|
|---|
| 677 | {
|
|---|
| 678 | get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
|
|---|
| 679 | set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
|
|---|
| 680 | },
|
|---|
| 681 | )`, prop), prop.node);
|
|---|
| 682 | }
|
|---|
| 683 | function buildPrivateInstanceMethodInitialization(ref, prop, privateNamesMap, state) {
|
|---|
| 684 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 685 | const {
|
|---|
| 686 | id
|
|---|
| 687 | } = privateName;
|
|---|
| 688 | if (!state.availableHelper("classPrivateMethodInitSpec")) {
|
|---|
| 689 | return inheritPropComments(_core.template.statement.ast`${id}.add(${ref})`, prop);
|
|---|
| 690 | }
|
|---|
| 691 | const helper = state.addHelper("classPrivateMethodInitSpec");
|
|---|
| 692 | return inheritPropComments(_core.template.statement.ast`${helper}(
|
|---|
| 693 | ${_core.types.thisExpression()},
|
|---|
| 694 | ${_core.types.cloneNode(id)}
|
|---|
| 695 | )`, prop);
|
|---|
| 696 | }
|
|---|
| 697 | function buildPublicFieldInitLoose(ref, prop) {
|
|---|
| 698 | const {
|
|---|
| 699 | key,
|
|---|
| 700 | computed
|
|---|
| 701 | } = prop.node;
|
|---|
| 702 | const value = prop.node.value || prop.scope.buildUndefinedNode();
|
|---|
| 703 | return inheritPropComments(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(ref, key, computed || _core.types.isLiteral(key)), value)), prop);
|
|---|
| 704 | }
|
|---|
| 705 | function buildPublicFieldInitSpec(ref, prop, state) {
|
|---|
| 706 | const {
|
|---|
| 707 | key,
|
|---|
| 708 | computed
|
|---|
| 709 | } = prop.node;
|
|---|
| 710 | const value = prop.node.value || prop.scope.buildUndefinedNode();
|
|---|
| 711 | return inheritPropComments(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineProperty"), [ref, computed || _core.types.isLiteral(key) ? key : _core.types.stringLiteral(key.name), value])), prop);
|
|---|
| 712 | }
|
|---|
| 713 | function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) {
|
|---|
| 714 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 715 | const {
|
|---|
| 716 | id,
|
|---|
| 717 | methodId,
|
|---|
| 718 | getId,
|
|---|
| 719 | setId,
|
|---|
| 720 | initAdded
|
|---|
| 721 | } = privateName;
|
|---|
| 722 | if (initAdded) return;
|
|---|
| 723 | const isGetterOrSetter = getId || setId;
|
|---|
| 724 | if (isGetterOrSetter) {
|
|---|
| 725 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 726 | initAdded: true
|
|---|
| 727 | }));
|
|---|
| 728 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 729 | Object.defineProperty(${ref}, ${id}, {
|
|---|
| 730 | // configurable is false by default
|
|---|
| 731 | // enumerable is false by default
|
|---|
| 732 | // writable is false by default
|
|---|
| 733 | get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
|
|---|
| 734 | set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
|
|---|
| 735 | })
|
|---|
| 736 | `, prop);
|
|---|
| 737 | }
|
|---|
| 738 | return inheritPropComments(_core.template.statement.ast`
|
|---|
| 739 | Object.defineProperty(${ref}, ${id}, {
|
|---|
| 740 | // configurable is false by default
|
|---|
| 741 | // enumerable is false by default
|
|---|
| 742 | // writable is false by default
|
|---|
| 743 | value: ${methodId.name}
|
|---|
| 744 | });
|
|---|
| 745 | `, prop);
|
|---|
| 746 | }
|
|---|
| 747 | function buildPrivateMethodDeclaration(file, prop, privateNamesMap, privateFieldsAsSymbolsOrProperties = false) {
|
|---|
| 748 | const privateName = privateNamesMap.get(prop.node.key.id.name);
|
|---|
| 749 | const {
|
|---|
| 750 | id,
|
|---|
| 751 | methodId,
|
|---|
| 752 | getId,
|
|---|
| 753 | setId,
|
|---|
| 754 | getterDeclared,
|
|---|
| 755 | setterDeclared,
|
|---|
| 756 | static: isStatic
|
|---|
| 757 | } = privateName;
|
|---|
| 758 | const {
|
|---|
| 759 | params,
|
|---|
| 760 | body,
|
|---|
| 761 | generator,
|
|---|
| 762 | async
|
|---|
| 763 | } = prop.node;
|
|---|
| 764 | const isGetter = getId && params.length === 0;
|
|---|
| 765 | const isSetter = setId && params.length > 0;
|
|---|
| 766 | if (isGetter && getterDeclared || isSetter && setterDeclared) {
|
|---|
| 767 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 768 | initAdded: true
|
|---|
| 769 | }));
|
|---|
| 770 | return null;
|
|---|
| 771 | }
|
|---|
| 772 | if (newHelpers(file) && (isGetter || isSetter) && !privateFieldsAsSymbolsOrProperties) {
|
|---|
| 773 | const scope = prop.get("body").scope;
|
|---|
| 774 | const thisArg = scope.generateUidIdentifier("this");
|
|---|
| 775 | const state = {
|
|---|
| 776 | thisRef: thisArg,
|
|---|
| 777 | argumentsPath: []
|
|---|
| 778 | };
|
|---|
| 779 | prop.traverse(thisContextVisitor, state);
|
|---|
| 780 | if (state.argumentsPath.length) {
|
|---|
| 781 | const argumentsId = scope.generateUidIdentifier("arguments");
|
|---|
| 782 | scope.push({
|
|---|
| 783 | id: argumentsId,
|
|---|
| 784 | init: _core.template.expression.ast`[].slice.call(arguments, 1)`
|
|---|
| 785 | });
|
|---|
| 786 | for (const path of state.argumentsPath) {
|
|---|
| 787 | path.replaceWith(_core.types.cloneNode(argumentsId));
|
|---|
| 788 | }
|
|---|
| 789 | }
|
|---|
| 790 | params.unshift(_core.types.cloneNode(thisArg));
|
|---|
| 791 | }
|
|---|
| 792 | let declId = methodId;
|
|---|
| 793 | if (isGetter) {
|
|---|
| 794 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 795 | getterDeclared: true,
|
|---|
| 796 | initAdded: true
|
|---|
| 797 | }));
|
|---|
| 798 | declId = getId;
|
|---|
| 799 | } else if (isSetter) {
|
|---|
| 800 | privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
|
|---|
| 801 | setterDeclared: true,
|
|---|
| 802 | initAdded: true
|
|---|
| 803 | }));
|
|---|
| 804 | declId = setId;
|
|---|
| 805 | } else if (isStatic && !privateFieldsAsSymbolsOrProperties) {
|
|---|
| 806 | declId = id;
|
|---|
| 807 | }
|
|---|
| 808 | return inheritPropComments(_core.types.functionDeclaration(_core.types.cloneNode(declId), params, body, generator, async), prop);
|
|---|
| 809 | }
|
|---|
| 810 | const thisContextVisitor = _traverse.visitors.environmentVisitor({
|
|---|
| 811 | Identifier(path, state) {
|
|---|
| 812 | if (state.argumentsPath && path.node.name === "arguments") {
|
|---|
| 813 | state.argumentsPath.push(path);
|
|---|
| 814 | }
|
|---|
| 815 | },
|
|---|
| 816 | UnaryExpression(path) {
|
|---|
| 817 | const {
|
|---|
| 818 | node
|
|---|
| 819 | } = path;
|
|---|
| 820 | if (node.operator === "delete") {
|
|---|
| 821 | const argument = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(node.argument);
|
|---|
| 822 | if (_core.types.isThisExpression(argument)) {
|
|---|
| 823 | path.replaceWith(_core.types.booleanLiteral(true));
|
|---|
| 824 | }
|
|---|
| 825 | }
|
|---|
| 826 | },
|
|---|
| 827 | ThisExpression(path, state) {
|
|---|
| 828 | state.needsClassRef = true;
|
|---|
| 829 | path.replaceWith(_core.types.cloneNode(state.thisRef));
|
|---|
| 830 | },
|
|---|
| 831 | MetaProperty(path) {
|
|---|
| 832 | const {
|
|---|
| 833 | node,
|
|---|
| 834 | scope
|
|---|
| 835 | } = path;
|
|---|
| 836 | if (node.meta.name === "new" && node.property.name === "target") {
|
|---|
| 837 | path.replaceWith(scope.buildUndefinedNode());
|
|---|
| 838 | }
|
|---|
| 839 | }
|
|---|
| 840 | });
|
|---|
| 841 | const innerReferencesVisitor = {
|
|---|
| 842 | ReferencedIdentifier(path, state) {
|
|---|
| 843 | if (path.scope.bindingIdentifierEquals(path.node.name, state.innerBinding)) {
|
|---|
| 844 | state.needsClassRef = true;
|
|---|
| 845 | path.node.name = state.thisRef.name;
|
|---|
| 846 | }
|
|---|
| 847 | }
|
|---|
| 848 | };
|
|---|
| 849 | function replaceThisContext(path, ref, innerBindingRef) {
|
|---|
| 850 | var _state$thisRef;
|
|---|
| 851 | const state = {
|
|---|
| 852 | thisRef: ref,
|
|---|
| 853 | needsClassRef: false,
|
|---|
| 854 | innerBinding: innerBindingRef
|
|---|
| 855 | };
|
|---|
| 856 | if (!path.isMethod()) {
|
|---|
| 857 | path.traverse(thisContextVisitor, state);
|
|---|
| 858 | }
|
|---|
| 859 | if (innerBindingRef != null && (_state$thisRef = state.thisRef) != null && _state$thisRef.name && state.thisRef.name !== innerBindingRef.name) {
|
|---|
| 860 | path.traverse(innerReferencesVisitor, state);
|
|---|
| 861 | }
|
|---|
| 862 | return state.needsClassRef;
|
|---|
| 863 | }
|
|---|
| 864 | function isNameOrLength({
|
|---|
| 865 | key,
|
|---|
| 866 | computed
|
|---|
| 867 | }) {
|
|---|
| 868 | if (key.type === "Identifier") {
|
|---|
| 869 | return !computed && (key.name === "name" || key.name === "length");
|
|---|
| 870 | }
|
|---|
| 871 | if (key.type === "StringLiteral") {
|
|---|
| 872 | return key.value === "name" || key.value === "length";
|
|---|
| 873 | }
|
|---|
| 874 | return false;
|
|---|
| 875 | }
|
|---|
| 876 | function inheritPropComments(node, prop) {
|
|---|
| 877 | _core.types.inheritLeadingComments(node, prop.node);
|
|---|
| 878 | _core.types.inheritInnerComments(node, prop.node);
|
|---|
| 879 | return node;
|
|---|
| 880 | }
|
|---|
| 881 | function inheritLoc(node, original) {
|
|---|
| 882 | node.start = original.start;
|
|---|
| 883 | node.end = original.end;
|
|---|
| 884 | node.loc = original.loc;
|
|---|
| 885 | return node;
|
|---|
| 886 | }
|
|---|
| 887 | function buildFieldsInitNodes(ref, superRef, props, privateNamesMap, file, setPublicClassFields, privateFieldsAsSymbolsOrProperties, noUninitializedPrivateFieldAccess, constantSuper, innerBindingRef) {
|
|---|
| 888 | let classRefFlags = 0;
|
|---|
| 889 | let injectSuperRef;
|
|---|
| 890 | const staticNodes = [];
|
|---|
| 891 | const instanceNodes = [];
|
|---|
| 892 | let lastInstanceNodeReturnsThis = false;
|
|---|
| 893 | const pureStaticNodes = [];
|
|---|
| 894 | let classBindingNode = null;
|
|---|
| 895 | const getSuperRef = _core.types.isIdentifier(superRef) ? () => superRef : () => {
|
|---|
| 896 | injectSuperRef != null ? injectSuperRef : injectSuperRef = props[0].scope.generateUidIdentifierBasedOnNode(superRef);
|
|---|
| 897 | return injectSuperRef;
|
|---|
| 898 | };
|
|---|
| 899 | const classRefForInnerBinding = ref != null ? ref : props[0].scope.generateUidIdentifier((innerBindingRef == null ? void 0 : innerBindingRef.name) || "Class");
|
|---|
| 900 | ref != null ? ref : ref = _core.types.cloneNode(innerBindingRef);
|
|---|
| 901 | for (const prop of props) {
|
|---|
| 902 | if (prop.isClassProperty()) {
|
|---|
| 903 | ts.assertFieldTransformed(prop);
|
|---|
| 904 | }
|
|---|
| 905 | const isStatic = !(_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node)) && prop.node.static;
|
|---|
| 906 | const isInstance = !isStatic;
|
|---|
| 907 | const isPrivate = prop.isPrivate();
|
|---|
| 908 | const isPublic = !isPrivate;
|
|---|
| 909 | const isField = prop.isProperty();
|
|---|
| 910 | const isMethod = !isField;
|
|---|
| 911 | const isStaticBlock = prop.isStaticBlock == null ? void 0 : prop.isStaticBlock();
|
|---|
| 912 | if (isStatic) classRefFlags |= 1;
|
|---|
| 913 | if (isStatic || isMethod && isPrivate || isStaticBlock) {
|
|---|
| 914 | new _helperReplaceSupers.default({
|
|---|
| 915 | methodPath: prop,
|
|---|
| 916 | constantSuper,
|
|---|
| 917 | file: file,
|
|---|
| 918 | refToPreserve: innerBindingRef,
|
|---|
| 919 | getSuperRef,
|
|---|
| 920 | getObjectRef() {
|
|---|
| 921 | classRefFlags |= 2;
|
|---|
| 922 | if (isStatic || isStaticBlock) {
|
|---|
| 923 | return classRefForInnerBinding;
|
|---|
| 924 | } else {
|
|---|
| 925 | return _core.types.memberExpression(classRefForInnerBinding, _core.types.identifier("prototype"));
|
|---|
| 926 | }
|
|---|
| 927 | }
|
|---|
| 928 | }).replace();
|
|---|
| 929 | const replaced = replaceThisContext(prop, classRefForInnerBinding, innerBindingRef);
|
|---|
| 930 | if (replaced) {
|
|---|
| 931 | classRefFlags |= 2;
|
|---|
| 932 | }
|
|---|
| 933 | }
|
|---|
| 934 | lastInstanceNodeReturnsThis = false;
|
|---|
| 935 | switch (true) {
|
|---|
| 936 | case isStaticBlock:
|
|---|
| 937 | {
|
|---|
| 938 | const blockBody = prop.node.body;
|
|---|
| 939 | if (blockBody.length === 1 && _core.types.isExpressionStatement(blockBody[0])) {
|
|---|
| 940 | staticNodes.push(inheritPropComments(blockBody[0], prop));
|
|---|
| 941 | } else {
|
|---|
| 942 | staticNodes.push(_core.types.inheritsComments(_core.template.statement.ast`(() => { ${blockBody} })()`, prop.node));
|
|---|
| 943 | }
|
|---|
| 944 | break;
|
|---|
| 945 | }
|
|---|
| 946 | case isStatic && isPrivate && isField && privateFieldsAsSymbolsOrProperties:
|
|---|
| 947 | staticNodes.push(buildPrivateFieldInitLoose(_core.types.cloneNode(ref), prop, privateNamesMap));
|
|---|
| 948 | break;
|
|---|
| 949 | case isStatic && isPrivate && isField && !privateFieldsAsSymbolsOrProperties:
|
|---|
| 950 | if (!newHelpers(file)) {
|
|---|
| 951 | staticNodes.push(buildPrivateStaticFieldInitSpecOld(prop, privateNamesMap));
|
|---|
| 952 | } else {
|
|---|
| 953 | staticNodes.push(buildPrivateStaticFieldInitSpec(prop, privateNamesMap, noUninitializedPrivateFieldAccess));
|
|---|
| 954 | }
|
|---|
| 955 | break;
|
|---|
| 956 | case isStatic && isPublic && isField && setPublicClassFields:
|
|---|
| 957 | if (!isNameOrLength(prop.node)) {
|
|---|
| 958 | staticNodes.push(buildPublicFieldInitLoose(_core.types.cloneNode(ref), prop));
|
|---|
| 959 | break;
|
|---|
| 960 | }
|
|---|
| 961 | case isStatic && isPublic && isField && !setPublicClassFields:
|
|---|
| 962 | staticNodes.push(buildPublicFieldInitSpec(_core.types.cloneNode(ref), prop, file));
|
|---|
| 963 | break;
|
|---|
| 964 | case isInstance && isPrivate && isField && privateFieldsAsSymbolsOrProperties:
|
|---|
| 965 | instanceNodes.push(buildPrivateFieldInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
|
|---|
| 966 | break;
|
|---|
| 967 | case isInstance && isPrivate && isField && !privateFieldsAsSymbolsOrProperties:
|
|---|
| 968 | instanceNodes.push(buildPrivateInstanceFieldInitSpec(_core.types.thisExpression(), prop, privateNamesMap, file));
|
|---|
| 969 | break;
|
|---|
| 970 | case isInstance && isPrivate && isMethod && privateFieldsAsSymbolsOrProperties:
|
|---|
| 971 | instanceNodes.unshift(buildPrivateMethodInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
|
|---|
| 972 | pureStaticNodes.push(buildPrivateMethodDeclaration(file, prop, privateNamesMap, privateFieldsAsSymbolsOrProperties));
|
|---|
| 973 | break;
|
|---|
| 974 | case isInstance && isPrivate && isMethod && !privateFieldsAsSymbolsOrProperties:
|
|---|
| 975 | instanceNodes.unshift(buildPrivateInstanceMethodInitSpec(_core.types.thisExpression(), prop, privateNamesMap, file));
|
|---|
| 976 | pureStaticNodes.push(buildPrivateMethodDeclaration(file, prop, privateNamesMap, privateFieldsAsSymbolsOrProperties));
|
|---|
| 977 | break;
|
|---|
| 978 | case isStatic && isPrivate && isMethod && !privateFieldsAsSymbolsOrProperties:
|
|---|
| 979 | if (!newHelpers(file)) {
|
|---|
| 980 | staticNodes.unshift(buildPrivateStaticFieldInitSpecOld(prop, privateNamesMap));
|
|---|
| 981 | }
|
|---|
| 982 | pureStaticNodes.push(buildPrivateMethodDeclaration(file, prop, privateNamesMap, privateFieldsAsSymbolsOrProperties));
|
|---|
| 983 | break;
|
|---|
| 984 | case isStatic && isPrivate && isMethod && privateFieldsAsSymbolsOrProperties:
|
|---|
| 985 | staticNodes.unshift(buildPrivateStaticMethodInitLoose(_core.types.cloneNode(ref), prop, file, privateNamesMap));
|
|---|
| 986 | pureStaticNodes.push(buildPrivateMethodDeclaration(file, prop, privateNamesMap, privateFieldsAsSymbolsOrProperties));
|
|---|
| 987 | break;
|
|---|
| 988 | case isInstance && isPublic && isField && setPublicClassFields:
|
|---|
| 989 | instanceNodes.push(buildPublicFieldInitLoose(_core.types.thisExpression(), prop));
|
|---|
| 990 | break;
|
|---|
| 991 | case isInstance && isPublic && isField && !setPublicClassFields:
|
|---|
| 992 | lastInstanceNodeReturnsThis = true;
|
|---|
| 993 | instanceNodes.push(buildPublicFieldInitSpec(_core.types.thisExpression(), prop, file));
|
|---|
| 994 | break;
|
|---|
| 995 | default:
|
|---|
| 996 | throw new Error("Unreachable.");
|
|---|
| 997 | }
|
|---|
| 998 | }
|
|---|
| 999 | if (classRefFlags & 2 && innerBindingRef != null) {
|
|---|
| 1000 | classBindingNode = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classRefForInnerBinding), _core.types.cloneNode(innerBindingRef)));
|
|---|
| 1001 | }
|
|---|
| 1002 | return {
|
|---|
| 1003 | staticNodes: staticNodes.filter(Boolean),
|
|---|
| 1004 | instanceNodes: instanceNodes.filter(Boolean),
|
|---|
| 1005 | lastInstanceNodeReturnsThis,
|
|---|
| 1006 | pureStaticNodes: pureStaticNodes.filter(Boolean),
|
|---|
| 1007 | classBindingNode,
|
|---|
| 1008 | wrapClass(path) {
|
|---|
| 1009 | for (const prop of props) {
|
|---|
| 1010 | prop.node.leadingComments = null;
|
|---|
| 1011 | prop.remove();
|
|---|
| 1012 | }
|
|---|
| 1013 | if (injectSuperRef) {
|
|---|
| 1014 | path.scope.push({
|
|---|
| 1015 | id: _core.types.cloneNode(injectSuperRef)
|
|---|
| 1016 | });
|
|---|
| 1017 | path.set("superClass", _core.types.assignmentExpression("=", injectSuperRef, path.node.superClass));
|
|---|
| 1018 | }
|
|---|
| 1019 | if (classRefFlags !== 0) {
|
|---|
| 1020 | if (path.isClassExpression()) {
|
|---|
| 1021 | path.scope.push({
|
|---|
| 1022 | id: ref
|
|---|
| 1023 | });
|
|---|
| 1024 | path.replaceWith(_core.types.assignmentExpression("=", _core.types.cloneNode(ref), path.node));
|
|---|
| 1025 | } else {
|
|---|
| 1026 | if (innerBindingRef == null) {
|
|---|
| 1027 | path.node.id = ref;
|
|---|
| 1028 | }
|
|---|
| 1029 | if (classBindingNode != null) {
|
|---|
| 1030 | path.scope.push({
|
|---|
| 1031 | id: classRefForInnerBinding
|
|---|
| 1032 | });
|
|---|
| 1033 | }
|
|---|
| 1034 | }
|
|---|
| 1035 | }
|
|---|
| 1036 | return path;
|
|---|
| 1037 | }
|
|---|
| 1038 | };
|
|---|
| 1039 | }
|
|---|
| 1040 |
|
|---|
| 1041 | //# sourceMappingURL=fields.js.map
|
|---|