Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: tests for nested destructured objects and arrays
  • Loading branch information
kaltoft committed Jul 11, 2022
commit 279552da2391dbfbdf088ae24291ea63e29da1a3
27 changes: 26 additions & 1 deletion tests/lib/rules/no-underscore-dangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ ruleTester.run("no-underscore-dangle", rule, {
"function foo(_bar) {}",
"function foo(bar_) {}",
"(function _foo() {})",
"var [foo, [bar, baz]] = [1, ['a', 'b']]",
{ code: "function foo(_bar) {}", options: [{}] },
{ code: "function foo( _bar = 0) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(_bar) { } }", parserOptions: { ecmaVersion: 6 } },
Expand Down Expand Up @@ -71,6 +70,11 @@ ruleTester.run("no-underscore-dangle", rule, {
{ code: "function foo( { _bar }) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo( { _bar = 0 } = {}) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo(...[_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 2016 } },
{ code: "const [foo, ...rest] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const [foo, _bar] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { foo, bar: _bar } = { foo: 1, bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { foo, _bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { foo, _bar: bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { _field; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { _field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { #_field; }", parserOptions: { ecmaVersion: 2022 } },
Expand Down Expand Up @@ -133,6 +137,27 @@ ruleTester.run("no-underscore-dangle", rule, {
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]
}, {
code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }
]
}, {
code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
options: [{ allowInArrayDestructuring: true, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }]
}, {
code: "const [{ foo: [_bar, _, { bar: _baz }] }] = [{ foo: [1, 2, { bar: 'a' }] }]",
options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
{ messageId: "unexpectedUnderscore", data: { identifier: "_baz" } }
]
}, {
code: "const { foo, bar: { baz, _qux } } = { foo: 1, bar: { baz: 3, _qux: 4 } }",
options: [{ allowInObjectDestructuring: false }],
Expand Down