1/* 2 * Copyright (c) 2024 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, ...rest } = { a: 1, b: 2 }); // NOT OK 21({ a, b: { c, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK 22({ a, b: { ...rest } } = { a: 1, b: { c: 3, d: 'bah' }}); // NOT OK 23 24function getObject() { 25 return { a: 1, b: 2 }; 26} 27({ a, b } = getObject()); // NOT OK 28 29// Array destructuring 30[a, b, c] = [10, 20, 30]; 31[a, b] = [b, a]; 32[a, b, ...rest] = [10, 20, 30, 40, 50]; // NOT OK 33[a, b, [c, d]] = [10, 20, [300, 400], 50]; 34[a, b, [c, ...rest]] = [10, 20, [30, 40, 50]]; // NOT OK 35 36let tuple: [number, string, number] = [1, '2', 3]; 37[a, , b] = tuple; 38[a, ...rest] = tuple; // NOT OK 39 40const getArray = (): number[] => [1, 2, 3]; 41[a, b] = getArray(); 42 43const set: Set<number> = new Set([1, 2, 3, 4]); 44[a, b, c] = set; // NOT OK 45 46const map: Map<number, number> = new Map(); 47[[a, b], [c, d]] = map; // NOT OK 48 49// Mixed destructuring 50let e: number, f: number, x: { e: number }, g; 51[a, b, [x, { f }]] = [1, 2, [{ e: 20 }, { f: 5 }]]; // NOT OK 52[a, b, {e, e: f, ...g}] = [1, 2, {e: 10}]; // NOT OK 53[a, b, ...{length}] = [1, 2, {e: 10}]; // NOT OK 54 55({ a, b : [c, d] } = { a: 1, b: [2, 3] }); // NOT OK 56({ 57 a, 58 b: { 59 s, 60 c: [d, f], 61 }, 62} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK 63