MCPcopy Create free account
hub / github.com/babel/babel / ArrayPattern

Function ArrayPattern

packages/babel-plugin-transform-object-rest-spread/src/index.ts:800–848  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

798
799 // [{a, ...b}] = c;
800 ArrayPattern(path) {
801 type LhsAndRhs = { left: t.ObjectPattern; right: t.Identifier };
802 const objectPatterns: LhsAndRhs[] = [];
803 const { scope } = path;
804 const uidIdentifiers: t.Identifier[] = [];
805
806 visitObjectRestElements(path, path => {
807 const objectPattern = path.parentPath as NodePath<t.ObjectPattern>;
808
809 const uid = scope.generateUidIdentifier("ref");
810 objectPatterns.push({ left: objectPattern.node, right: uid });
811 uidIdentifiers.push(uid);
812
813 objectPattern.replaceWith(t.cloneNode(uid));
814 path.skip();
815 });
816
817 if (objectPatterns.length > 0) {
818 const patternParentPath = path.findParent(
819 path => !(path.isPattern() || path.isObjectProperty()),
820 )!;
821 const patternParent = patternParentPath.node;
822 switch (patternParent.type) {
823 case "VariableDeclarator":
824 patternParentPath.insertAfter(
825 objectPatterns.map(({ left, right }) =>
826 t.variableDeclarator(left, right),
827 ),
828 );
829 break;
830 case "AssignmentExpression":
831 {
832 for (const uidIdentifier of uidIdentifiers) {
833 scope.push({ id: t.cloneNode(uidIdentifier) });
834 }
835 patternParentPath.insertAfter(
836 objectPatterns.map(({ left, right }) =>
837 t.assignmentExpression("=", left, right),
838 ),
839 );
840 }
841 break;
842 default:
843 throw new Error(
844 `Unexpected pattern parent type: ${patternParent.type}`,
845 );
846 }
847 }
848 },
849
850 // var a = { ...b, ...c }
851 ObjectExpression(path, file) {

Callers

nothing calls this directly

Calls 6

visitObjectRestElementsFunction · 0.85
generateUidIdentifierMethod · 0.80
isPatternMethod · 0.80
mapMethod · 0.80
isObjectPropertyMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected