diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 261f2d316175..08d12c2b8fff 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -462,6 +462,7 @@ function copyDts(packages) { const libBundles = [ "packages/babel-parser", + "packages/babel-plugin-proposal-destructuring-private", "packages/babel-plugin-proposal-object-rest-spread", "packages/babel-plugin-proposal-optional-chaining", "packages/babel-preset-react", diff --git a/packages/babel-plugin-proposal-destructuring-private/.npmignore b/packages/babel-plugin-proposal-destructuring-private/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-proposal-destructuring-private/README.md b/packages/babel-plugin-proposal-destructuring-private/README.md new file mode 100644 index 000000000000..eeca2d9af0b2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-proposal-destructuring-private + +> Transform destructuring private proposal + +See our website [@babel/plugin-proposal-destructuring-private](https://babeljs.io/docs/en/babel-plugin-proposal-destructuring-private) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-proposal-destructuring-private +``` + +or using yarn: + +```sh +yarn add @babel/plugin-proposal-destructuring-private --dev +``` diff --git a/packages/babel-plugin-proposal-destructuring-private/package.json b/packages/babel-plugin-proposal-destructuring-private/package.json new file mode 100644 index 000000000000..a914fb6682d9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/package.json @@ -0,0 +1,50 @@ +{ + "name": "@babel/plugin-proposal-destructuring-private", + "version": "0.0.0", + "description": "Transform destructuring private proposal", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-proposal-destructuring-private" + }, + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "./lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "workspace:^", + "@babel/plugin-syntax-destructuring-private": "workspace:^", + "@babel/plugin-transform-destructuring": "workspace:^", + "@babel/plugin-transform-parameters": "workspace:^" + }, + "peerDependencies": { + "@babel/core": "^7.17.0" + }, + "devDependencies": { + "@babel/core": "workspace:^", + "@babel/helper-plugin-test-runner": "workspace:^", + "@babel/traverse": "workspace:^", + "@babel/types": "workspace:^" + }, + "homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-destructuring-private", + "engines": { + "node": ">=6.9.0" + }, + "author": "The Babel Team (https://babel.dev/team)", + "conditions": { + "BABEL_8_BREAKING": [ + null, + { + "exports": null + } + ] + }, + "exports": { + ".": "./lib/index.js", + "./package.json": "./package.json" + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts new file mode 100644 index 000000000000..bb33c86057e1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -0,0 +1,205 @@ +import { declare } from "@babel/helper-plugin-utils"; +import syntaxDestructuringPrivate from "@babel/plugin-syntax-destructuring-private"; +import { + hasPrivateKeys, + hasPrivateClassElement, + transformPrivateKeyDestructuring, + buildVariableDeclarationFromParams, +} from "./util"; +import { convertFunctionParams } from "@babel/plugin-transform-parameters"; + +import type { PluginPass } from "@babel/core"; +import type { Visitor } from "@babel/traverse"; +import { types as t } from "@babel/core"; + +const { + assignmentExpression, + cloneNode, + expressionStatement, + isExpressionStatement, + isIdentifier, + isSequenceExpression, + sequenceExpression, + variableDeclaration, + variableDeclarator, +} = t; + +export default declare(function ({ + assertVersion, + assumption, +}: { + assumption: (string) => boolean; + assertVersion: (string) => void; +}) { + assertVersion("^7.17.0"); + + const ignoreFunctionLength = assumption("ignoreFunctionLength"); + const objectRestNoSymbols = assumption("objectRestNoSymbols"); + + const privateKeyDestructuringVisitor: Visitor = { + Function(path) { + // (b, { #x: x } = I) => body + // transforms to: + // (p1, p2) => { var b = p1, { #x: x } = p2 === undefined ? I : p2; body; } + if (!path.node.params.some(param => hasPrivateKeys(param))) return; + // wrap function body within IIFE if any param is shadowed + convertFunctionParams(path, ignoreFunctionLength, () => false, false); + const { node, scope } = path; + const { params, variableDeclaration } = + buildVariableDeclarationFromParams(node.params, scope); + + path + .get("body") // invariant: path.body is always a BlockStatement + .unshiftContainer("body", variableDeclaration); + node.params = params; + scope.crawl(); + // the pattern will be handled by VariableDeclaration visitor. + }, + CatchClause(path) { + // catch({ #x: x }) { body } + // transforms to: + // catch(_e) { var {#x: x } = _e; body } + const { node, scope } = path; + if (!hasPrivateKeys(node.param)) return; + // todo: handle shadowed param as we did in convertFunctionParams + const ref = scope.generateUidIdentifier("e"); + path + .get("body") + .unshiftContainer( + "body", + variableDeclaration("var", [variableDeclarator(node.param, ref)]), + ); + node.param = cloneNode(ref); + // the pattern will be handled by VariableDeclaration visitor. + }, + ForXStatement(path) { + const { node, scope } = path; + const leftPath = path.get("left"); + if (leftPath.isVariableDeclaration()) { + const left = leftPath.node; + if (!hasPrivateKeys(left.declarations[0].id)) return; + // for (const { #x: x } of cls) body; + // transforms to: + // for (const ref of cls) { const { #x: x } = ref; body; } + const declarator = scope.generateUidIdentifier("ref"); + node.left = variableDeclaration(left.kind, [ + variableDeclarator(declarator, null), + ]); + left.declarations[0].init = cloneNode(declarator); + path.ensureBlock(); + // todo: handle shadowed variables referenced in computed keys: + // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } + const blockBody = (node.body as t.BlockStatement).body; + blockBody.unshift(left); + scope.crawl(); + // the pattern will be handled by VariableDeclaration visitor. + } else if (leftPath.isPattern()) { + if (!hasPrivateKeys(leftPath.node)) return; + // for ({ #x: x } of cls); + // transforms to: + // for (const ref of cls) { ({ #x: x } = ref); body; } + // This transform assumes that any expression within the pattern + // does not interfere with the iterable `cls`. + const declarator = scope.generateUidIdentifier("ref"); + node.left = variableDeclaration("const", [ + variableDeclarator(declarator, null), + ]); + path.ensureBlock(); + + const blockBody = (node.body as t.BlockStatement).body; + + // preserve completion record: + // for ({ #x: x } of []); + if (blockBody.length === 0 && path.isCompletionRecord()) { + blockBody.unshift(expressionStatement(scope.buildUndefinedNode())); + } + + // todo: handle shadowed variables referenced in computed keys: + // var a = 0, x;for ({ #x: x, [a++]: y } of z) { const a = 1; } + blockBody.unshift( + expressionStatement( + assignmentExpression("=", leftPath.node, cloneNode(declarator)), + ), + ); + } + }, + VariableDeclaration(path, state) { + const { scope, node } = path; + const { declarations } = node; + if (!declarations.some(declarator => hasPrivateKeys(declarator.id))) { + return; + } + const newDeclarations = []; + for (const declarator of declarations) { + for (const { left, right } of transformPrivateKeyDestructuring( + declarator.id, + declarator.init, + scope, + /* isAssignment */ false, + name => state.addHelper(name), + objectRestNoSymbols, + /* useBuiltIns */ true, + )) { + newDeclarations.push(variableDeclarator(left, right)); + } + } + node.declarations = newDeclarations; + scope.crawl(); + }, + + AssignmentExpression(path, state) { + const { node, scope, parent } = path; + if (!hasPrivateKeys(node.left)) return; + const assignments = []; + for (const { left, right } of transformPrivateKeyDestructuring( + node.left, + node.right, + scope, + /* isAssignment */ true, + name => state.addHelper(name), + objectRestNoSymbols, + /* useBuiltIns */ true, + )) { + assignments.push(assignmentExpression("=", left, right)); + } + // preserve completion record + if ( + (!isExpressionStatement(parent) && !isSequenceExpression(parent)) || + path.isCompletionRecord() + ) { + const { left } = assignments[0]; + if (scope.isStatic(node.right)) { + const tempId = scope.generateDeclaredUidIdentifier("m"); + assignments.unshift( + assignmentExpression("=", tempId, cloneNode(node.right)), + ); + assignments.push(cloneNode(tempId)); + } else if ( + !isIdentifier(assignments[assignments.length - 1].right, { + name: left.name, + }) + ) { + // If node.right is non-static and then the left is an effectively-constant memoised id + // If the last assignment does not end with left, that we can safely reuse `left` as the completion value + assignments.push(cloneNode(left)); + } + } + + path.replaceWith(sequenceExpression(assignments)); + scope.crawl(); + }, + }; + + const visitor: Visitor = { + Class(path, state) { + if (!hasPrivateClassElement(path.node.body)) return; + path.traverse(privateKeyDestructuringVisitor, state); + }, + }; + + return { + name: "proposal-destructuring-private", + inherits: syntaxDestructuringPrivate, + visitor: visitor, + }; +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts new file mode 100644 index 000000000000..757c75784059 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -0,0 +1,428 @@ +import type * as t from "@babel/types"; +import type { Scope } from "@babel/traverse"; +import { types } from "@babel/core"; +import type { File } from "@babel/core"; +import { buildObjectExcludingKeys } from "@babel/plugin-transform-destructuring"; +import { assignmentExpression, ObjectProperty } from "@babel/types"; +const { + binaryExpression, + conditionalExpression, + cloneNode, + isObjectProperty, + isPrivateName, + memberExpression, + numericLiteral, + objectPattern, + restElement, + variableDeclarator, + variableDeclaration, + unaryExpression, +} = types; + +function buildUndefinedNode() { + return unaryExpression("void", numericLiteral(0)); +} + +function transformAssignmentPattern( + initializer: t.Expression, + tempId: t.Identifier, +) { + return conditionalExpression( + binaryExpression("===", cloneNode(tempId), buildUndefinedNode()), + initializer, + cloneNode(tempId), + ); +} + +function initRestExcludingKeys(pattern: t.LVal) { + if (pattern.type === "ObjectPattern") { + const { properties } = pattern; + if (properties[properties.length - 1].type === "RestElement") { + return []; + } + } + return null; +} + +/** + * grow restExcludingKeys on given properties. This routine mutates properties by + * memoising the computed non-static keys. + * + * @param {ObjectProperty[]} properties An array of object properties that should be excluded by rest element transform + * @param {Scope} scope Where should we register the memoised id + */ +function* growRestExcludingKeys(properties: ObjectProperty[], scope: Scope) { + for (const property of properties) { + if (property.computed && !scope.isStatic(property.key)) { + const tempId = scope.generateDeclaredUidIdentifier("m"); + // @ts-expect-error A computed property key must not be a private name + property.key = assignmentExpression("=", tempId, property.key); + yield { key: tempId, computed: true }; + } else { + yield property; + } + } +} + +/** + * Prepare var declarations for params. Only param initializers + * will be transformed to undefined coalescing, other features are preserved. + * This function does NOT mutate given AST structures. + * + * @export + * @param {Function["params"]} params An array of function params + * @param {Scope} scope A scope used to generate uid for function params + * @returns {{ params: Identifier[]; variableDeclaration: VariableDeclaration }} An array of new id for params + * and variable declaration to be prepended to the function body + */ +export function buildVariableDeclarationFromParams( + params: t.Function["params"], + scope: Scope, +): { + params: (t.Identifier | t.RestElement)[]; + variableDeclaration: t.VariableDeclaration; +} { + const { elements, transformed } = buildAssignmentsFromPatternList( + params, + scope, + /* isAssignment */ false, + ); + return { + params: elements, + variableDeclaration: variableDeclaration( + "var", + transformed.map(({ left, right }) => variableDeclarator(left, right)), + ), + }; +} + +interface Transformed { + left: t.Identifier | t.Pattern | t.MemberExpression; + right: t.Expression; +} + +function buildAssignmentsFromPatternList( + elements: (t.LVal | null)[], + scope: Scope, + isAssignment: boolean, +): { + elements: (t.Identifier | t.RestElement | null)[]; + transformed: Transformed[]; +} { + const newElements: (t.Identifier | t.RestElement)[] = [], + transformed = []; + for (let element of elements) { + if (element === null) { + newElements.push(null); + transformed.push(null); + continue; + } + const tempId = scope.generateUidIdentifier("p"); + if (isAssignment) { + scope.push({ id: cloneNode(tempId) }); + } + if (element.type === "RestElement") { + newElements.push(restElement(tempId)); + // The argument of a RestElement within a BindingPattern must be either Identifier or BindingPattern + element = element.argument as t.Identifier | t.Pattern; + } else { + newElements.push(tempId); + } + if (element.type === "AssignmentPattern") { + transformed.push({ + left: element.left, + right: transformAssignmentPattern(element.right, tempId), + }); + } else { + transformed.push({ + left: element, + right: cloneNode(tempId), + }); + } + } + return { elements: newElements, transformed }; +} + +/** + * A DFS simplified pattern traverser. It skips computed property keys and assignment pattern + * initializers. The following-type path will be delegate to the visitor: + * - ArrayPattern + * - ArrayPattern elements + * - AssignmentPattern + * - ObjectPattern + * - ObjectProperty + * - RestElement + * @param root + * @param visitor + */ +export function* traversePattern( + root: t.LVal, + visitor: ( + node: t.LVal | t.ObjectProperty, + index: number, + depth: number, + ) => Generator, +) { + const stack = []; + stack.push({ node: root, index: 0, depth: 0 }); + let item: { + node: t.LVal | t.ObjectProperty | null; + index: number; + depth: number; + }; + while ((item = stack.pop()) !== undefined) { + const { node, index } = item; + if (node === null) continue; + yield* visitor(node, index, item.depth); + const depth = item.depth + 1; + switch (node.type) { + case "AssignmentPattern": + stack.push({ node: node.left, index: 0, depth }); + break; + case "ObjectProperty": + // inherit the depth and index as an object property can not be an LHS without object pattern + stack.push({ node: node.value, index, depth: item.depth }); + break; + case "RestElement": + stack.push({ node: node.argument, index: 0, depth }); + break; + case "ObjectPattern": + for (let list = node.properties, i = list.length - 1; i >= 0; i--) { + stack.push({ node: list[i], index: i, depth }); + } + break; + case "ArrayPattern": + for (let list = node.elements, i = list.length - 1; i >= 0; i--) { + stack.push({ node: list[i], index: i, depth }); + } + break; + case "TSParameterProperty": + throw new Error( + `TypeScript parameter properties must first be transformed by ` + + `@babel/plugin-transform-typescript.\n` + + `If you have already enabled that plugin (or '@babel/preset-typescript'), make sure ` + + `that it runs before @babel/plugin-proposal-destructuring-private.`, + ); + default: + break; + } + } +} + +export function hasPrivateKeys(pattern: t.LVal) { + return ( + traversePattern(pattern, function* (node) { + if (isObjectProperty(node) && isPrivateName(node.key)) { + yield; + } + }).next().done === false + ); +} + +export function hasPrivateClassElement(node: t.ClassBody): boolean { + return node.body.some(element => + isPrivateName( + // @ts-expect-error: for those class element without `key`, they must + // not be a private element + element.key, + ), + ); +} + +/** + * Traverse the given pattern and report the private key path. + * A private key path is analagous to an array of `key` from the pattern NodePath + * to the private key NodePath. See also test/util.skip-bundled.js for an example output + * + * @export + * @param {t.LVal} pattern + */ +export function* privateKeyPathIterator(pattern: t.LVal) { + const indexPath = []; + yield* traversePattern(pattern, function* (node, index, depth) { + indexPath[depth] = index; + if (isObjectProperty(node)) { + const propertyKey = node.key; + if (isPrivateName(propertyKey)) { + yield indexPath.slice(1, depth + 1); + } + } + }); +} + +type Item = { + left: t.LVal; + right: t.Expression; + restExcludingKeys?: t.ObjectProperty[] | null; +}; + +/** + * Transform private destructuring without object rest element. It returns a generator + * which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or + * AssignmentExpression later. + * + * @export + * @param {t.LVal} left The root pattern + * @param {t.Expression} right The initializer or the RHS of pattern + * @param {Scope} scope The scope where memoized id should be registered + * @param {boolean} isAssignment Whether we are transforming from an AssignmengExpression of VariableDeclaration + * @returns {Generator} + */ +export function* transformPrivateKeyDestructuring( + left: t.LVal, + right: t.Expression, + scope: Scope, + isAssignment: boolean, + addHelper: File["addHelper"], + objectRestNoSymbols: boolean, + useBuiltIns: boolean, +): Generator { + const stack: Item[] = []; + // The stack holds patterns that we don't known whether they contain private key + stack.push({ + left, + right, + restExcludingKeys: initRestExcludingKeys(left), + }); + let item: Item; + while ((item = stack.pop()) !== undefined) { + const { restExcludingKeys } = item; + let { left, right } = item; + const searchPrivateKey = privateKeyPathIterator(left).next(); + if (searchPrivateKey.done) { + if (restExcludingKeys != null && restExcludingKeys.length > 0) { + // optimize out the rest element because `objectWithoutProperties` + // always return a new object + // `{ ...z } = babelHelpers.objectWithoutProperties(m, ["x"])` + // to + // `z = babelHelpers.objectWithoutProperties(m, ["x"])` + const { properties } = left as t.ObjectPattern; + if (properties.length === 1) { + left = (properties[0] as t.RestElement).argument; + } + yield { + left: left as t.ObjectPattern, + right: buildObjectExcludingKeys( + restExcludingKeys, + right, + scope, + addHelper, + objectRestNoSymbols, + useBuiltIns, + ), + }; + } else { + // @ts-expect-error left must not contain RestElement if restExcludingKeys is nullish + yield { left, right }; + } + } else { + // now we need to split according to the indexPath; + const indexPath = searchPrivateKey.value; + let index; + let isFirst = true; + while ( + (index = indexPath.shift()) !== undefined || + left.type === "AssignmentPattern" + ) { + if (!scope.isStatic(right) && left.type !== "Identifier") { + const tempId = scope.generateUidIdentifier("m"); + if (isAssignment) { + scope.push({ id: cloneNode(tempId) }); + } + yield { left: tempId, right }; + right = cloneNode(tempId); + } + // invariant: at this point right must be a static identifier; + switch (left.type) { + case "ObjectPattern": { + const { properties } = left; + // inherit the restExcludingKeys on the stack if we are at + // the first level, otherwise initialize a new restExcludingKeys + let nextRestExcludingKeys = restExcludingKeys; + if (!isFirst) { + nextRestExcludingKeys = initRestExcludingKeys(left); + } + if (index > 0) { + // properties[0, index) must not contain private keys + const propertiesSlice = properties.slice(0, index); + if (nextRestExcludingKeys !== null) { + nextRestExcludingKeys.push( + // @ts-expect-error + ...growRestExcludingKeys(propertiesSlice, scope), + ); + } + yield { + left: objectPattern(propertiesSlice), + right: cloneNode(right), + }; + } + if (index < properties.length - 1) { + // for properties after `index`, push them to stack so we can process them later + stack.push({ + left: objectPattern(properties.slice(index + 1)), + right: cloneNode(right), + restExcludingKeys: nextRestExcludingKeys, + }); + } + // An object rest element must not contain a private key + const property = properties[index] as t.ObjectProperty; + left = property.value as t.LVal; + const { key } = property; + const computed = + property.computed || + // `{ 0: x } = RHS` is transformed to a computed member expression `x = RHS[0]` + (key.type !== "Identifier" && key.type !== "PrivateName"); + right = memberExpression(right, key, computed); + break; + } + case "AssignmentPattern": { + right = transformAssignmentPattern( + left.right, + right as t.Identifier, + ); + left = left.left; + break; + } + case "ArrayPattern": { + // todo: the transform here assumes that any expression within + // the array pattern, when evluated, do not interfere with the iterable + // in RHS. Otherwise we have to pause the iterable and interleave + // the expressions. + // See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 + const { elements, transformed } = buildAssignmentsFromPatternList( + left.elements, + scope, + isAssignment, + ); + left.elements = elements; + yield { left, right: cloneNode(right) }; + // we are sure elements[0, index) does not contain private keys + for (let i = 0; i < index; i++) { + // skipping array holes + if (transformed[i] !== null) { + yield transformed[i]; + } + } + // for elements after `index`, push them to stack so we can process them later + for (let i = transformed.length - 1; i > index; i--) { + // skipping array holes + if (transformed[i] !== null) { + stack.push(transformed[i]); + } + } + ({ left, right } = transformed[index]); + break; + } + default: + break; + } + isFirst = false; + } + stack.push({ + left, + right, + restExcludingKeys: initRestExcludingKeys(left), + }); + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js new file mode 100644 index 000000000000..54c0347d10e9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js @@ -0,0 +1,10 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js new file mode 100644 index 000000000000..fc244ab5af80 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js @@ -0,0 +1,7 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js new file mode 100644 index 000000000000..25816df07323 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js @@ -0,0 +1,14 @@ +let x, z; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _p2, _m2; + + _m = [C], [_p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? 1 : _m2, z = _p2; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js new file mode 100644 index 000000000000..b0c00d030344 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js @@ -0,0 +1,10 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} +expect(a).toBe(1); +expect(x).toBe(2); +expect(b).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js new file mode 100644 index 000000000000..16d994cde245 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js @@ -0,0 +1,7 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js new file mode 100644 index 000000000000..42e29778c281 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js @@ -0,0 +1,18 @@ +let a, x, b; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m; + + ({ + a = 1 + } = C), _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), x = _m === void 0 ? 2 : _m, ({ + b = 3 + } = C); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js new file mode 100644 index 000000000000..f56e05c08f33 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js @@ -0,0 +1,13 @@ +let r1, r2, r3; +class C { + static #x = { b: 2 }; + static { + let a, b; + r1 = ({ a = 1, #x: { b }} = C); + (function f(r = ({ #x: { b }} = C)) { r2 = r })(); + ((g = (r = ({ #x: { a } } = { #x: { b }} = C)) => { r3 = r }) => g())(); + } +} +expect(r1).toBe(C); +expect(r2).toBe(C); +expect(r3).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js new file mode 100644 index 000000000000..3c865df1a3e8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js @@ -0,0 +1,11 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} +expect(x).toBe(0); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js new file mode 100644 index 000000000000..45be876183ce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js @@ -0,0 +1,10 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js new file mode 100644 index 000000000000..c63cef733485 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js @@ -0,0 +1,23 @@ +let x; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; +var _y = { + writable: true, + value: void 0 +}; +var _z = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _p2, _p3; + + let z; + _m = [0, C], [_p, _p2, ..._p3] = _m, babelHelpers.classStaticPrivateFieldSpecSet(C, C, _x, _p), x = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _x), z = _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..686556b8466b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js @@ -0,0 +1,14 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..5ca915829bdb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js @@ -0,0 +1,11 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..1087c312042c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js @@ -0,0 +1,26 @@ +class C {} + +function _self() { + return C; +} + +var _x = { + writable: true, + value: "x" +}; +var _y = { + writable: true, + value: [] +}; +var _z = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "self", C); + +(() => { + var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + + let x, y, z; + _m = [C, C], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js new file mode 100644 index 000000000000..c59766b8f366 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js new file mode 100644 index 000000000000..890f16f203bd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json new file mode 100644 index 000000000000..48eb48aad7d6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ], + "minNodeVersion": "8.0.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js new file mode 100644 index 000000000000..d207099e0f1f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js @@ -0,0 +1,30 @@ +class C {} + +var _y = { + writable: true, + value: "y" +}; +var _z = { + writable: true, + value: "self" +}; +var _x = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "self", C); +var _self = { + writable: true, + value: C +}; + +(() => { + var _m, _m2, _m3, _m4, _m5; + + let cloned, b, y, yy, yy2; + _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2 = _m === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self) : _m, _m3 = _m2[babelHelpers.classStaticPrivateFieldSpecGet(C, C, _z)], ({ + b + } = _m3), _m4 = babelHelpers.classStaticPrivateFieldSpecGet(_m3, C, _x), y = _m4 === void 0 ? (C.b = "bb", babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self), C, _y)) : _m4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), babelHelpers.classStaticPrivateFieldSpecSet(C, C, _y, "yy")) : _m5, yy2 = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..fcfcf3bafcd5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..f29b28b5a652 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..d13cce9b234b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js @@ -0,0 +1,27 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _C; + + let x, y, z; + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), (_C = C, ({} = _C), z = Object.assign({}, _C), _C); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js new file mode 100644 index 000000000000..6dff97a97aff --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js @@ -0,0 +1,13 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js new file mode 100644 index 000000000000..d67ebdde0914 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js new file mode 100644 index 000000000000..3ada6df296df --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js @@ -0,0 +1,28 @@ +const _excluded = ["y"]; +let result; + +class C {} + +babelHelpers.defineProperty(C, "x", "x"); +babelHelpers.defineProperty(C, "y", "y"); +babelHelpers.defineProperty(C, "z", "z"); +var _x = { + writable: true, + value: C +}; + +(() => { + var _babelHelpers$classSt; + + var x, y, z; + ({ + x + } = C), (_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), ({ + y + } = _babelHelpers$classSt), z = babelHelpers.objectWithoutProperties(_babelHelpers$classSt, _excluded), _babelHelpers$classSt); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js new file mode 100644 index 000000000000..f03d0735bead --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js new file mode 100644 index 000000000000..231b2f804345 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js new file mode 100644 index 000000000000..eab489f2dde1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js @@ -0,0 +1,33 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var a, b, x, y, z; + ({ + [_m = C.a]: a + } = C), x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), ({ + [_m2 = C.b]: b + } = C), y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json new file mode 100644 index 000000000000..e1c5858604ed --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js new file mode 100644 index 000000000000..387b1623a8f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js @@ -0,0 +1,11 @@ +let a,b; +class C { + static #x = { a: 1, b: 2 }; + static { + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); +expect(a).toBe(1); +expect(b).toBe(2); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js new file mode 100644 index 000000000000..cd50bcd759bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js @@ -0,0 +1,10 @@ +let a; +class C { + static #x = { a: 1, b: 2 }; + static { + let b; + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js new file mode 100644 index 000000000000..829bd11aa63a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js @@ -0,0 +1,30 @@ +var _m2; + +let a; + +class C { + static m(r = (_m2 = C, ({ + a + } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m2)) {} + +} + +var _x = { + writable: true, + value: { + a: 1, + b: 2 + } +}; + +(() => { + var _m; + + let b; + + (function f(r = (_m = C, ({ + b + } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m)) {})(); +})(); + +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js new file mode 100644 index 000000000000..54c0347d10e9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js @@ -0,0 +1,10 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js new file mode 100644 index 000000000000..fc244ab5af80 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js @@ -0,0 +1,7 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js new file mode 100644 index 000000000000..1ca9c8ead1cf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js @@ -0,0 +1,10 @@ +let x, z; + +class C { + static #x; + static { + var _m, _p, _p2, _m2; + + _m = [C], [_p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? 1 : _m2, z = _p2; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js new file mode 100644 index 000000000000..b0c00d030344 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js @@ -0,0 +1,10 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} +expect(a).toBe(1); +expect(x).toBe(2); +expect(b).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js new file mode 100644 index 000000000000..16d994cde245 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js @@ -0,0 +1,7 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js new file mode 100644 index 000000000000..9e2c02707338 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js @@ -0,0 +1,14 @@ +let a, x, b; + +class C { + static #x; + static { + var _m; + + ({ + a = 1 + } = C), _m = C.#x, x = _m === void 0 ? 2 : _m, ({ + b = 3 + } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js new file mode 100644 index 000000000000..f56e05c08f33 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js @@ -0,0 +1,13 @@ +let r1, r2, r3; +class C { + static #x = { b: 2 }; + static { + let a, b; + r1 = ({ a = 1, #x: { b }} = C); + (function f(r = ({ #x: { b }} = C)) { r2 = r })(); + ((g = (r = ({ #x: { a } } = { #x: { b }} = C)) => { r3 = r }) => g())(); + } +} +expect(r1).toBe(C); +expect(r2).toBe(C); +expect(r3).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js new file mode 100644 index 000000000000..3c865df1a3e8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js @@ -0,0 +1,11 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} +expect(x).toBe(0); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js new file mode 100644 index 000000000000..45be876183ce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js @@ -0,0 +1,10 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js new file mode 100644 index 000000000000..ff4f866363fe --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js @@ -0,0 +1,13 @@ +let x; + +class C { + static #x; + static #y; + static #z; + static { + var _m, _p, _p2, _p3; + + let z; + _m = [0, C], [_p, _p2, ..._p3] = _m, C.#x = _p, x = _p2.#x, z = _p3; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..686556b8466b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js @@ -0,0 +1,14 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..5ca915829bdb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js @@ -0,0 +1,11 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..7cde39fb5f0f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js @@ -0,0 +1,17 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + + static #self() { + return C; + } + + static { + var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + + let x, y, z; + _m = [this, this], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = _m2.#x, _m3 = _p2.#y, [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = _m4.#z, y = _m5 === void 0 ? C.#self() : _m5, z = _p3 === void 0 ? y.#y : _p3; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js new file mode 100644 index 000000000000..c59766b8f366 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js new file mode 100644 index 000000000000..890f16f203bd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js new file mode 100644 index 000000000000..6ddd0084d98b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js @@ -0,0 +1,17 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + var _m, _m2, _m3, _m4, _m5; + + let cloned, b, y, yy, yy2; + _m = C.#x, _m2 = _m === void 0 ? C.#self : _m, _m3 = _m2[C.#z], ({ + b + } = _m3), _m4 = _m3.#x, y = _m4 === void 0 ? (C.b = "bb", C.#self.#y) : _m4, _m5 = _m2.#x, yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), C.#y = "yy") : _m5, yy2 = C.#y; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..fcfcf3bafcd5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..f29b28b5a652 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..9cbe29ab2cce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js @@ -0,0 +1,19 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + x = C.#x, y = C.#y, ({ ...z + } = C); + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js new file mode 100644 index 000000000000..6dff97a97aff --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js @@ -0,0 +1,13 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js new file mode 100644 index 000000000000..d67ebdde0914 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js new file mode 100644 index 000000000000..a31104ad6357 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js @@ -0,0 +1,22 @@ +let result; + +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ + x + } = C), ({ + y, + ...z + } = C.#x); + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js new file mode 100644 index 000000000000..f03d0735bead --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js new file mode 100644 index 000000000000..231b2f804345 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js new file mode 100644 index 000000000000..10ca5e47a965 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js @@ -0,0 +1,26 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var _m, _m2; + + var a, b, x, y, z; + ({ + [_m = C.a]: a + } = C), x = C.#x, ({ + [_m2 = C.b]: b + } = C), y = C.#y, z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js new file mode 100644 index 000000000000..387b1623a8f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js @@ -0,0 +1,11 @@ +let a,b; +class C { + static #x = { a: 1, b: 2 }; + static { + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); +expect(a).toBe(1); +expect(b).toBe(2); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js new file mode 100644 index 000000000000..cd50bcd759bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js @@ -0,0 +1,10 @@ +let a; +class C { + static #x = { a: 1, b: 2 }; + static { + let b; + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js new file mode 100644 index 000000000000..21c71cdb74c7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js @@ -0,0 +1,27 @@ +var _m2; + +let a; + +class C { + static #x = { + a: 1, + b: 2 + }; + static { + var _m; + + let b; + + (function f(r = (_m = C, ({ + b + } = C.#x), _m)) {})(); + + } + + static m(r = (_m2 = C, ({ + a + } = C.#x), _m2)) {} + +} + +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json new file mode 100644 index 000000000000..613db701c964 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json @@ -0,0 +1,12 @@ +{ + "assumptions": { + "objectRestNoSymbols": true + }, + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js new file mode 100644 index 000000000000..f525646493a3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js @@ -0,0 +1,36 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + [_m2 = C.b]: b + } = C, + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = babelHelpers.objectWithoutPropertiesLoose(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js new file mode 100644 index 000000000000..e2ac65996b95 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js @@ -0,0 +1,7 @@ +class C { + #x; + static { + try { throw new C() } catch ({ #x: x }) { + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js new file mode 100644 index 000000000000..23c3b28e7821 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js @@ -0,0 +1,19 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + try { + throw new C(); + } catch (_e) { + var x = babelHelpers.classPrivateFieldGet(_e, _x); + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js new file mode 100644 index 000000000000..e2ac65996b95 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js @@ -0,0 +1,7 @@ +class C { + #x; + static { + try { throw new C() } catch ({ #x: x }) { + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js new file mode 100644 index 000000000000..a6e307b2cae2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js @@ -0,0 +1,11 @@ +class C { + #x; + static { + try { + throw new C(); + } catch (_e) { + var x = _e.#x; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js new file mode 100644 index 000000000000..a5758b3f342f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { + result = { x, y }; + break; + } + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js new file mode 100644 index 000000000000..82b14c5452f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js @@ -0,0 +1,7 @@ +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js new file mode 100644 index 000000000000..a1dbc5da395e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js @@ -0,0 +1,16 @@ +class C {} + +var _x = { + writable: true, + value: 42 +}; + +(() => { + let x, y; + + for (_m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), _m;;) { + var _m, _m2; + + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js new file mode 100644 index 000000000000..435022d57a6d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } = this;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js new file mode 100644 index 000000000000..48d4b0febd9b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js @@ -0,0 +1,19 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (_m = C, x = babelHelpers.classPrivateFieldGet(C, _x), _m;;) { + var _m; + + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js new file mode 100644 index 000000000000..50c718f481c0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + result = { x, y }; + break + }; + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js new file mode 100644 index 000000000000..0b30c4deb3d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js @@ -0,0 +1,9 @@ +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + break + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js new file mode 100644 index 000000000000..bef362866c61 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js @@ -0,0 +1,18 @@ +class C {} + +var _x = { + writable: true, + value: 42 +}; + +(() => { + let y; + + for (let _m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x);;) { + var _m2; + + break; + } + + ; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js new file mode 100644 index 000000000000..d9b6b142e217 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + for (let { #x: x } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js new file mode 100644 index 000000000000..b24ba926176f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js @@ -0,0 +1,12 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + for (let x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x);;) { + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js new file mode 100644 index 000000000000..a5758b3f342f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { + result = { x, y }; + break; + } + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js new file mode 100644 index 000000000000..82b14c5452f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js @@ -0,0 +1,7 @@ +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js new file mode 100644 index 000000000000..e52600816752 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js @@ -0,0 +1,13 @@ +class C { + static #x = 42; + static { + let x, y; + + for (_m = (_m2 = C, y = C.#x, _m2), x = _m.#x, _m;;) { + var _m, _m2; + + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js new file mode 100644 index 000000000000..435022d57a6d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } = this;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js new file mode 100644 index 000000000000..e4f394a6488b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js @@ -0,0 +1,11 @@ +class C { + #x; + static { + for (_m = this, x = this.#x, _m;;) { + var _m; + + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js new file mode 100644 index 000000000000..50c718f481c0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + result = { x, y }; + break + }; + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js new file mode 100644 index 000000000000..0b30c4deb3d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js @@ -0,0 +1,9 @@ +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + break + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js new file mode 100644 index 000000000000..29bb33212367 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js @@ -0,0 +1,14 @@ +class C { + static #x = 42; + static { + let y; + + for (let _m = (_m2 = C, y = C.#x, _m2), x = _m.#x;;) { + var _m2; + + break; + } + + ; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js new file mode 100644 index 000000000000..d9b6b142e217 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + for (let { #x: x } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js new file mode 100644 index 000000000000..99b21d9250da --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js @@ -0,0 +1,9 @@ +class C { + static #x; + static { + for (let x = C.#x;;) { + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js new file mode 100644 index 000000000000..f02105a5d715 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js new file mode 100644 index 000000000000..356869f3e61f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js @@ -0,0 +1,18 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (const _ref of [C]) { + x = babelHelpers.classPrivateFieldGet(_ref, _x); + ; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js new file mode 100644 index 000000000000..96346f23285b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for (const { #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js new file mode 100644 index 000000000000..b44bf3a082ec --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js @@ -0,0 +1,18 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (const _ref of [C]) { + const x = babelHelpers.classPrivateFieldGet(_ref, _x); + ; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js new file mode 100644 index 000000000000..f02105a5d715 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js new file mode 100644 index 000000000000..abd59a8e704d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js @@ -0,0 +1,10 @@ +class C { + #x; + static { + for (const _ref of [this]) { + x = _ref.#x; + ; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js new file mode 100644 index 000000000000..96346f23285b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for (const { #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js new file mode 100644 index 000000000000..236bc19524d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js @@ -0,0 +1,10 @@ +class C { + #x; + static { + for (const _ref of [this]) { + const x = _ref.#x; + ; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js new file mode 100644 index 000000000000..6ad25f261e99 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js @@ -0,0 +1,5 @@ +class C { + #x; + static m(a, { #x: x }, ...b) {} +} +expect(C.m.length).toBe(2) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js new file mode 100644 index 000000000000..90f84a73805f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js new file mode 100644 index 000000000000..2b2fd2073cdf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js @@ -0,0 +1,17 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + + m(_p, _p2, ..._p3) { + var a = _p, + x = babelHelpers.classPrivateFieldGet(_p2, _x), + b = _p3; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js new file mode 100644 index 000000000000..4c877a3d1481 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + var a = 1; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js new file mode 100644 index 000000000000..1ed0c90d22d3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js @@ -0,0 +1,20 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + + m(_p, _p2, ..._p3) { + var a = _p, + x = babelHelpers.classPrivateFieldGet(_p2, _x), + b = _p3; + return function (a) { + var a = 1; + }(a); + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js new file mode 100644 index 000000000000..6ad25f261e99 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js @@ -0,0 +1,5 @@ +class C { + #x; + static m(a, { #x: x }, ...b) {} +} +expect(C.m.length).toBe(2) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js new file mode 100644 index 000000000000..90f84a73805f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js new file mode 100644 index 000000000000..b110d694a6fc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + m(_p, _p2, ..._p3) { + var a = _p, + x = _p2.#x, + b = _p3; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js new file mode 100644 index 000000000000..4c877a3d1481 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + var a = 1; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js new file mode 100644 index 000000000000..ced7c933c0c4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js @@ -0,0 +1,13 @@ +class C { + #x; + + m(_p, _p2, ..._p3) { + var a = _p, + x = _p2.#x, + b = _p3; + return function (a) { + var a = 1; + }(a); + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js new file mode 100644 index 000000000000..42b2f89bd467 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + } +} + +var nums = Array.from({ length: 12 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js new file mode 100644 index 000000000000..280a7a1a424e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + } +} + +var nums = Array.from({ length: 11 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js new file mode 100644 index 000000000000..46e9cfe5b3f6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js @@ -0,0 +1,21 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7), + #x: x = push(8), + [push(9)]: d = push(10), + ...e + } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js new file mode 100644 index 000000000000..bf9aedfe8ca0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) } = C; + } +} + +var nums = Array.from({ length: 10 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json new file mode 100644 index 000000000000..8099cdd9d774 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js new file mode 100644 index 000000000000..42b2f89bd467 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + } +} + +var nums = Array.from({ length: 12 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js new file mode 100644 index 000000000000..280a7a1a424e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + } +} + +var nums = Array.from({ length: 11 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js new file mode 100644 index 000000000000..46e9cfe5b3f6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js @@ -0,0 +1,21 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7), + #x: x = push(8), + [push(9)]: d = push(10), + ...e + } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js new file mode 100644 index 000000000000..bf9aedfe8ca0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) } = C; + } +} + +var nums = Array.from({ length: 10 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json new file mode 100644 index 000000000000..2e503cc0a75f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json @@ -0,0 +1,6 @@ +{ + "plugins": ["proposal-destructuring-private", "transform-typescript"], + "throws": [ + "TypeScript parameter properties must first be transformed by @babel/plugin-transform-typescript." + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json new file mode 100644 index 000000000000..21e1abbf8a7b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-typescript", "proposal-destructuring-private"] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js new file mode 100644 index 000000000000..67bc13190763 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + constructor(_p, _p2) { + var foo = _p, + x = _p2.#x; + this.foo = foo; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/options.json new file mode 100644 index 000000000000..901cfe6b50f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "presets": ["typescript"] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/output.js new file mode 100644 index 000000000000..67bc13190763 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-preset-typescript/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + constructor(_p, _p2) { + var foo = _p, + x = _p2.#x; + this.foo = foo; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js new file mode 100644 index 000000000000..216a27c621bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: [] }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js new file mode 100644 index 000000000000..7f0e8f3446f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js new file mode 100644 index 000000000000..8e4a9f07f94e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js @@ -0,0 +1,14 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m = [C], + [_p, ..._p2] = _m, + _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), + x = _m2 === void 0 ? 1 : _m2, + z = _p2; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js new file mode 100644 index 000000000000..cbee2112f773 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + result = { a, x, b }; + } +} +expect(result).toStrictEqual({ a: 1, x: 2, b: 3 }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js new file mode 100644 index 000000000000..0ed7639bb2c8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js new file mode 100644 index 000000000000..d0b62f6685cb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js @@ -0,0 +1,17 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var { + a = 1 + } = C, + _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + x = _m === void 0 ? 2 : _m, + { + b = 3 + } = C; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..026ddd6c82a8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js @@ -0,0 +1,13 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..7570c494dd96 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js @@ -0,0 +1,10 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..0f7f7a4f8f6b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js @@ -0,0 +1,32 @@ +class C {} + +function _self() { + return C; +} + +var _x = { + writable: true, + value: "x" +}; +var _y = { + writable: true, + value: [] +}; +var _z = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "self", C); + +(() => { + var _m = [C, C], + [_p, _p2,, _p3] = _m, + _m2 = _p === void 0 ? C.self : _p, + x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), + _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), + [, _p4] = _m3, + _m4 = _p4 === void 0 ? C.self : _p4, + _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), + y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, + z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js new file mode 100644 index 000000000000..504cee587e65 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js new file mode 100644 index 000000000000..50fe617b26cc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json new file mode 100644 index 000000000000..48eb48aad7d6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ], + "minNodeVersion": "8.0.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js new file mode 100644 index 000000000000..77a8a9a3bbcc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js @@ -0,0 +1,37 @@ +class C {} + +var _y = { + writable: true, + value: "y" +}; +var _z = { + writable: true, + value: "self" +}; +var _x = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "self", C); +var _self = { + writable: true, + value: C +}; + +(() => { + let cloned; + + var _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + _m2 = _m === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self) : _m, + _m3 = _m2[babelHelpers.classStaticPrivateFieldSpecGet(C, C, _z)], + { + b + } = _m3, + _m4 = babelHelpers.classStaticPrivateFieldSpecGet(_m3, C, _x), + y = _m4 === void 0 ? (C.b = "bb", babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self), C, _y)) : _m4, + _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), + yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), babelHelpers.classStaticPrivateFieldSpecSet(C, C, _y, "yy")) : _m5, + yy2 = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js new file mode 100644 index 000000000000..fc5b7e36b72c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { "0": { #x: w }, 1: { #x: x }, 2n: {#x: y}, 3m: {#x: z} } = [C, C, C, C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json new file mode 100644 index 000000000000..5e85d3896305 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private", "syntax-decimal"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js new file mode 100644 index 000000000000..5c6376193ae1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js @@ -0,0 +1,14 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + _m2 = _m["0"], + w = _m2.#x, + _m3 = _m[1], + x = _m3.#x, + _m4 = _m[2n], + y = _m4.#x, + _m5 = _m[3m], + z = _m5.#x; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..109bcfba54aa --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..30e7783da1b1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..a4cb5e90ca77 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js @@ -0,0 +1,26 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = Object.assign({}, C); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js new file mode 100644 index 000000000000..57bd774a7c43 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js new file mode 100644 index 000000000000..abbe3dd5d8f5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js @@ -0,0 +1,11 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js new file mode 100644 index 000000000000..c0c57671bfbd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js @@ -0,0 +1,29 @@ +const _excluded = ["y"]; +let result; + +class C {} + +babelHelpers.defineProperty(C, "x", "x"); +babelHelpers.defineProperty(C, "y", "y"); +babelHelpers.defineProperty(C, "z", "z"); +var _x = { + writable: true, + value: C +}; + +(() => { + var { + x + } = C, + _babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + y + } = _babelHelpers$classSt, + z = babelHelpers.objectWithoutProperties(_babelHelpers$classSt, _excluded); + + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json new file mode 100644 index 000000000000..82bccc4aeed5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js new file mode 100644 index 000000000000..bcacc784b73f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js @@ -0,0 +1,36 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + [_m2 = C.b]: b + } = C, + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json new file mode 100644 index 000000000000..e1c5858604ed --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js new file mode 100644 index 000000000000..216a27c621bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: [] }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js new file mode 100644 index 000000000000..7f0e8f3446f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js new file mode 100644 index 000000000000..9c642bfc8702 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js @@ -0,0 +1,10 @@ +class C { + static #x; + static { + var _m = [C], + [_p, ..._p2] = _m, + _m2 = _p.#x, + x = _m2 === void 0 ? 1 : _m2, + z = _p2; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js new file mode 100644 index 000000000000..cbee2112f773 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + result = { a, x, b }; + } +} +expect(result).toStrictEqual({ a: 1, x: 2, b: 3 }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js new file mode 100644 index 000000000000..0ed7639bb2c8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js new file mode 100644 index 000000000000..768092799091 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js @@ -0,0 +1,13 @@ +class C { + static #x; + static { + var { + a = 1 + } = C, + _m = C.#x, + x = _m === void 0 ? 2 : _m, + { + b = 3 + } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..026ddd6c82a8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js @@ -0,0 +1,13 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..7570c494dd96 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js @@ -0,0 +1,10 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..f487f9d2b173 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js @@ -0,0 +1,24 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + + static #self() { + return C; + } + + static { + var _m = [this, this], + [_p, _p2,, _p3] = _m, + _m2 = _p === void 0 ? C.self : _p, + x = _m2.#x, + _m3 = _p2.#y, + [, _p4] = _m3, + _m4 = _p4 === void 0 ? C.self : _p4, + _m5 = _m4.#z, + y = _m5 === void 0 ? C.#self() : _m5, + z = _p3 === void 0 ? y.#y : _p3; + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js new file mode 100644 index 000000000000..504cee587e65 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js new file mode 100644 index 000000000000..50fe617b26cc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js new file mode 100644 index 000000000000..77f9a4a16296 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js @@ -0,0 +1,25 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + + var _m = C.#x, + _m2 = _m === void 0 ? C.#self : _m, + _m3 = _m2[C.#z], + { + b + } = _m3, + _m4 = _m3.#x, + y = _m4 === void 0 ? (C.b = "bb", C.#self.#y) : _m4, + _m5 = _m2.#x, + yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), C.#y = "yy") : _m5, + yy2 = C.#y; + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js new file mode 100644 index 000000000000..fc5b7e36b72c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { "0": { #x: w }, 1: { #x: x }, 2n: {#x: y}, 3m: {#x: z} } = [C, C, C, C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json new file mode 100644 index 000000000000..5e85d3896305 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private", "syntax-decimal"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js new file mode 100644 index 000000000000..5c6376193ae1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js @@ -0,0 +1,14 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + _m2 = _m["0"], + w = _m2.#x, + _m3 = _m[1], + x = _m3.#x, + _m4 = _m[2n], + y = _m4.#x, + _m5 = _m[3m], + z = _m5.#x; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..109bcfba54aa --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..30e7783da1b1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..612e5fac9d45 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js @@ -0,0 +1,20 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var x = C.#x, + y = C.#y, + { ...z + } = C; + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js new file mode 100644 index 000000000000..57bd774a7c43 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js new file mode 100644 index 000000000000..abbe3dd5d8f5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js @@ -0,0 +1,11 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js new file mode 100644 index 000000000000..8f3720bacca2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js @@ -0,0 +1,22 @@ +let result; + +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { + x + } = C, + { + y, + ...z + } = C.#x; + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js new file mode 100644 index 000000000000..6a40c624b11c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js @@ -0,0 +1,29 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = C.#x, + { + [_m2 = C.b]: b + } = C, + y = C.#y, + z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/index.js b/packages/babel-plugin-proposal-destructuring-private/test/index.js new file mode 100644 index 000000000000..21a55ce6b5e7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/package.json b/packages/babel-plugin-proposal-destructuring-private/test/package.json new file mode 100644 index 000000000000..5ffd9800b97c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/package.json @@ -0,0 +1 @@ +{ "type": "module" } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js new file mode 100644 index 000000000000..64ace639375e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js @@ -0,0 +1,28 @@ +import babel from "@babel/core"; +import proposalDestructuringPrivate from "../lib/index.js"; + +describe("plugin ordering", () => { + it("should work when @babel/plugin-proposal-destructuring-private is after class features plugin", () => { + const source = `class Foo { + static #x = 1; + static { + const {#x: x } = Foo; + }; + } + `; + expect( + () => + babel.transformSync(source, { + filename: "example.js", + highlightCode: false, + configFile: false, + babelrc: false, + plugins: [ + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-class-static-block", + proposalDestructuringPrivate, + ], + }).code, + ).not.toThrow(); + }); +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js b/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js new file mode 100644 index 000000000000..5e50a88acb64 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js @@ -0,0 +1,77 @@ +import babel from "@babel/core"; +import { traversePattern, privateKeyPathIterator } from "../lib/util.js"; +const { isObjectProperty, isPrivateName } = babel.types; + +const { parseSync, traverse } = babel; + +function wrapSourceInClassEnvironment(input) { + const usedPrivateNames = new Set(); + let matched; + const re = /#[\w_]+/g; + while ((matched = re.exec(input)) !== null) { + usedPrivateNames.add(matched[0]); + } + let result = "(class {"; + for (const name of usedPrivateNames) { + result += name + ";"; + } + result += "m(){ " + input + "}})"; + return result; +} + +function getPath(input, parserOpts = { plugins: ["destructuringPrivate"] }) { + let targetPath; + traverse( + parseSync(wrapSourceInClassEnvironment(input), { + parserOpts, + filename: "example.js", + configFile: false, + }), + { + Pattern(path) { + targetPath = path; + path.stop(); + }, + }, + ); + return targetPath; +} + +describe("traversePattern", () => { + it("should visit property with private keys in depth-first order", () => { + const patternPath = getPath( + "const { #a: { #b: b, c, ...d }, e: [{ #c: [{ #d: { #c: g } }] }, ...{ #b: h }], #a: i } = obj;", + ); + const keys = [ + ...traversePattern(patternPath.node, function* (node) { + if (isObjectProperty(node)) { + const propertyKey = node.key; + if (isPrivateName(propertyKey)) { + yield propertyKey.id.name; + } + } + }), + ]; + + expect(keys).toEqual(["a", "b", "c", "d", "c", "b", "a"]); + }); +}); + +describe("privateKeyPathIterator", () => { + const indexPaths = [ + ...privateKeyPathIterator( + getPath( + "const { #a: { a, #b: b, c, ...d }, e: [{ #c: [{ d: e, #d: { #c: f } }] }, ...{ #b: g }], #a: i } = obj;", + ).node, + ), + ].map(indexPath => indexPath.join(",")); + expect(indexPaths).toEqual([ + "0", + "0,1", + "1,0,0", + "1,0,0,0,1", + "1,0,0,0,1,0", + "1,1,0,0", + "2", + ]); +}); diff --git a/tsconfig.json b/tsconfig.json index 3028bec10b2a..f83edfd17202 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -43,6 +43,7 @@ "./packages/babel-plugin-proposal-class-properties/src/**/*.ts", "./packages/babel-plugin-proposal-class-static-block/src/**/*.ts", "./packages/babel-plugin-proposal-decorators/src/**/*.ts", + "./packages/babel-plugin-proposal-destructuring-private/src/**/*.ts", "./packages/babel-plugin-proposal-do-expressions/src/**/*.ts", "./packages/babel-plugin-proposal-dynamic-import/src/**/*.ts", "./packages/babel-plugin-proposal-export-default-from/src/**/*.ts", @@ -274,6 +275,9 @@ "@babel/plugin-proposal-decorators": [ "./packages/babel-plugin-proposal-decorators/src" ], + "@babel/plugin-proposal-destructuring-private": [ + "./packages/babel-plugin-proposal-destructuring-private/src" + ], "@babel/plugin-proposal-do-expressions": [ "./packages/babel-plugin-proposal-do-expressions/src" ],