• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, b, c } = { a: 100, b: 200, c: 'bar' }; // NOT OK
18let { a2, ...rest2 } = { a2: 1, b2: 2 }; // NOT OK
19let { a3, b3: { c3, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK
20let { a4, b4: { ...rest4 } } = { a4: 1, b4: { c4: 3, d4: 'bah' }}; // NOT OK
21
22let { a, b, c } = {}; // NOT OK, not fixable
23
24function getObject() {
25  return { a5: 1, b5: 2 };
26}
27let { a5, b5 } = getObject(); // NOT OK
28
29// Array destructuring
30const [a6, b6, c6] = [10, 20, 30];
31const [a7, b7, ...rest7] = [10, 20, 30, 40, 50]; // NOT OK
32const [a8, b8, [c8, e8]] = [10, 20, [300, 400], 50];
33const [a9, b9, [c9, ...rest9]] = [10, 20, [30, 40, 50]]; // NOT OK
34
35const [[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]];
36const [[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]] = [[1, 2, 3], [3, 4, 5], [5, 6, 7]];
37
38let tuple: [number, string, number] = [1, '2', 3];
39let [a10, , b10] = tuple;
40let [a11, ...rest11] = tuple; // NOT OK
41
42const getArray = (): number[] => [1, 2, 3];
43let [a12, b12] = getArray();
44
45const set: Set<number> = new Set([1, 2, 3, 4]);
46let [a13, b13, c13] = set; // NOT OK
47
48const map: Map<number, number> = new Map();
49let [[a14, b14], [c14, d14]] = map; // NOT OK
50
51// Mixed destructuring
52let [a15, b15, [x15, { f15 }]] = [1, 2, [{ e15: 20 }, { f15: 5 }]]; // NOT OK
53let [a16, b16, {e16, e16: f16, ...g16}] = [1, 2, {e16: 10}]; // NOT OK
54{ let [a17, b17, ...{length}] = [1, 2, 3, 4]; } // NOT OK
55
56let { a18, b18: [c18, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK
57let {
58  a19,
59  b19: {
60    c19,
61    d19: [e19, f19],
62  },
63} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK
64
65// test for default value
66let { a, b, c = 9 } = { a: 100, b: 200, c: 'bar' }; // NOT OK
67let { a3, b3: { c3 = 10, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK
68let { a5, b5 = 7 } = getObject(); // NOT OK
69let { a18, b18: [c18 = 66, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK
70let {
71  a19,
72  b19: {
73    c19,
74    d19: [e19, f19 = 9],
75  },
76} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK
77
78// test for new Expression
79class C {
80  public a: number = 10;
81  public b: number = 10;
82  public s: string = "0000";
83}
84
85let { a, b, s } = new C(); // NOT OK