feat: add options to check destructuring in no-underscore-dangle (#16… · eslint/eslint@b401cde (original) (raw)

`@@ -70,6 +70,11 @@ ruleTester.run("no-underscore-dangle", rule, {

`

70

70

`{ code: "function foo( { _bar }) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },

`

71

71

`{ code: "function foo( { _bar = 0 } = {}) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },

`

72

72

`{ code: "function foo(...[_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 2016 } },

`

``

73

`+

{ code: "const [foo, ...rest] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },

`

``

74

`+

{ code: "const [foo, _bar] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },

`

``

75

`+

{ code: "const { foo, bar: _bar } = { foo: 1, bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },

`

``

76

`+

{ code: "const { foo, _bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },

`

``

77

`+

{ code: "const { foo, _bar: bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },

`

73

78

`{ code: "class foo { _field; }", parserOptions: { ecmaVersion: 2022 } },

`

74

79

`{ code: "class foo { _field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },

`

75

80

`{ code: "class foo { #_field; }", parserOptions: { ecmaVersion: 2022 } },

`

`@@ -103,6 +108,62 @@ ruleTester.run("no-underscore-dangle", rule, {

`

103

108

`{ code: "const foo = { onClick(..._bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },

`

104

109

`{ code: "const foo = (..._bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },

`

105

110

`{

`

``

111

`+

code: "const [foo, _bar] = [1, 2]",

`

``

112

`+

options: [{ allowInArrayDestructuring: false }],

`

``

113

`+

parserOptions: { ecmaVersion: 2022 },

`

``

114

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } }]

`

``

115

`+

}, {

`

``

116

`+

code: "const [foo, ..._rest] = [1, 2, 3]",

`

``

117

`+

options: [{ allowInArrayDestructuring: false }],

`

``

118

`+

parserOptions: { ecmaVersion: 2022 },

`

``

119

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]

`

``

120

`+

}, {

`

``

121

`+

code: "const [foo, [bar_, baz]] = [1, [2, 3]]",

`

``

122

`+

options: [{ allowInArrayDestructuring: false }],

`

``

123

`+

parserOptions: { ecmaVersion: 2022 },

`

``

124

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "bar_" } }]

`

``

125

`+

}, {

`

``

126

`+

code: "const { _foo, bar } = { _foo: 1, bar: 2 }",

`

``

127

`+

options: [{ allowInObjectDestructuring: false }],

`

``

128

`+

parserOptions: { ecmaVersion: 2022 },

`

``

129

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]

`

``

130

`+

}, {

`

``

131

`+

code: "const { foo: _foo, bar } = { foo: 1, bar: 2 }",

`

``

132

`+

options: [{ allowInObjectDestructuring: false }],

`

``

133

`+

parserOptions: { ecmaVersion: 2022 },

`

``

134

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]

`

``

135

`+

}, {

`

``

136

`+

code: "const { foo, ..._rest} = { foo: 1, bar: 2, baz: 3 }",

`

``

137

`+

options: [{ allowInObjectDestructuring: false }],

`

``

138

`+

parserOptions: { ecmaVersion: 2022 },

`

``

139

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]

`

``

140

`+

}, {

`

``

141

`+

code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",

`

``

142

`+

options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],

`

``

143

`+

parserOptions: { ecmaVersion: 2022 },

`

``

144

`+

errors: [

`

``

145

`+

{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },

`

``

146

`+

{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }

`

``

147

`+

]

`

``

148

`+

}, {

`

``

149

`+

code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",

`

``

150

`+

options: [{ allowInArrayDestructuring: true, allowInObjectDestructuring: false }],

`

``

151

`+

parserOptions: { ecmaVersion: 2022 },

`

``

152

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }]

`

``

153

`+

}, {

`

``

154

`+

code: "const [{ foo: [_bar, _, { bar: _baz }] }] = [{ foo: [1, 2, { bar: 'a' }] }]",

`

``

155

`+

options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],

`

``

156

`+

parserOptions: { ecmaVersion: 2022 },

`

``

157

`+

errors: [

`

``

158

`+

{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },

`

``

159

`+

{ messageId: "unexpectedUnderscore", data: { identifier: "_baz" } }

`

``

160

`+

]

`

``

161

`+

}, {

`

``

162

`+

code: "const { foo, bar: { baz, _qux } } = { foo: 1, bar: { baz: 3, _qux: 4 } }",

`

``

163

`+

options: [{ allowInObjectDestructuring: false }],

`

``

164

`+

parserOptions: { ecmaVersion: 2022 },

`

``

165

`+

errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_qux" } }]

`

``

166

`+

}, {

`

106

167

`code: "class foo { #_bar() {} }",

`

107

168

`options: [{ enforceInMethodNames: true }],

`

108

169

`parserOptions: { ecmaVersion: 2022 },

`