1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// Object destructuring 17let a = 5, b = 10, c = 15, d = 20, s = 'foo'; 18let rest, rest2; 19({ a, b, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK 20({ a, b, s } = {}); // NOT OK, not fixable 21({ a, ...rest } = { a: 1, b: 2 }); // NOT OK 22({ a, b: { c, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK 23({ a, b: { ...rest } } = { a: 1, b: { c: 3, d: 'bah' }}); // NOT OK 24 25function getObject() { 26 return { a: 1, b: 2 }; 27} 28({ a, b } = getObject()); // NOT OK 29 30// Array destructuring 31[a, b, c] = [10, 20, 30]; 32[a, b, c] = []; 33[[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]]; 34[a, b] = [b, a]; 35[a, b, ...rest] = [10, 20, 30, 40, 50]; // NOT OK 36[a, b, [c, d]] = [10, 20, [300, 400], 50]; 37[a, b, [c, ...rest]] = [10, 20, [30, 40, 50]]; // NOT OK 38 39let tuple: [number, string, number] = [1, '2', 3]; 40[a, , b] = tuple; 41[a, ...rest] = tuple; // NOT OK 42 43const getArray = (): number[] => [1, 2, 3]; 44[a, b] = getArray(); 45 46const set: Set<number> = new Set([1, 2, 3, 4]); 47[a, b, c] = set; // NOT OK 48 49const map: Map<number, number> = new Map(); 50[[a, b], [c, d]] = map; // NOT OK 51 52// Mixed destructuring 53let e: number, f: number, x: { e: number }, g; 54[a, b, [x, { f }]] = [1, 2, [{ e: 20 }, { f: 5 }]]; // NOT OK 55[a, b, {e, e: f, ...g}] = [1, 2, {e: 10}]; // NOT OK 56[a, b, ...{length}] = [1, 2, {e: 10}]; // NOT OK 57 58({ a, b : [c, d] } = { a: 1, b: [2, 3] }); // NOT OK 59({ 60 a, 61 b: { 62 s, 63 c: [d, f], 64 }, 65} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK 66 67// test for default value 68({ a, b = 7, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK 69({ a, b: { c = 6, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK 70({ a, b : [c, d = 8] } = { a: 1, b: [2, 3] }); // NOT OK 71({ 72 a, 73 b: { 74 s, 75 c: [d = 9, f], 76 }, 77} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK 78 79 80// test for new Expression 81class C { 82 public a: number = 10; 83 public b: number = 10; 84 public s: string = "0000"; 85} 86 87({ a, b, s } = new C()); // NOT OK 88 89