Skip to content

Commit 3fcc4bb

Browse files
authored
no-object-as-default-parameter: Forbid destructuring (#1433)
1 parent 2d05252 commit 3fcc4bb

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

docs/rules/no-object-as-default-parameter.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
Default parameters should not be passed to a function through an object literal. The `foo = {a: false}` parameter works fine if only used with one option. As soon as additional options are added, you risk replacing the whole `foo = {a: false, b: true}` object when passing only one option: `{a: true}`. For this reason, object destructuring should be used instead.
44

5-
65
## Fail
76

87
```js
98
const abc = (foo = {a: false}) => {};
109
```
1110

11+
```js
12+
function foo({a} = {a: false}) {}
13+
```
14+
1215
```js
1316
const abc = (foo = {a: false, b: 123}) => {};
1417
```
@@ -20,6 +23,12 @@ const abc = (foo = {a: false, b: 123}) => {};
2023
const abc = (foo = {}) => {};
2124
```
2225

26+
```js
27+
function foo(options) {
28+
const {a} = {a: false, ...options};
29+
}
30+
```
31+
2332
```js
2433
const abc = (foo = false) => {};
2534
```

rules/no-object-as-default-parameter.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
'use strict';
22

3-
const MESSAGE_ID = 'noObjectAsDefaultParameter';
3+
const MESSAGE_ID_IDENTIFIER = 'identifier';
4+
const MESSAGE_ID_NON_IDENTIFIER = 'non-identifier';
45
const messages = {
5-
[MESSAGE_ID]: 'Do not use an object literal as default for parameter `{{parameter}}`.',
6+
[MESSAGE_ID_IDENTIFIER]: 'Do not use an object literal as default for parameter `{{parameter}}`.',
7+
[MESSAGE_ID_NON_IDENTIFIER]: 'Do not use an object literal as default.',
68
};
79

810
const objectParameterSelector = [
911
':function > AssignmentPattern.params',
10-
'[left.type="Identifier"]',
1112
'[right.type="ObjectExpression"]',
1213
'[right.properties.length>0]',
1314
].join('');
1415

1516
const create = () => {
1617
return {
1718
[objectParameterSelector]: node => {
19+
const {left, right} = node;
20+
21+
if (left.type === 'Identifier') {
22+
return {
23+
node: left,
24+
messageId: MESSAGE_ID_IDENTIFIER,
25+
data: {parameter: left.name},
26+
};
27+
}
28+
1829
return {
19-
node: node.left,
20-
messageId: MESSAGE_ID,
21-
data: {parameter: node.left.name},
30+
node: right,
31+
messageId: MESSAGE_ID_NON_IDENTIFIER,
2232
};
2333
},
2434
};

test/no-object-as-default-parameter.mjs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import {getTester} from './utils/test.mjs';
44
const {test} = getTester(import.meta);
55

66
const error = {
7-
messageId: 'noObjectAsDefaultParameter',
7+
messageId: 'identifier',
88
data: {parameter: 'foo'},
99
};
1010

11+
const errorNonIdentifier = {
12+
messageId: 'non-identifier',
13+
};
14+
1115
test({
1216
valid: [
1317
'const abc = {};',
@@ -160,6 +164,14 @@ test({
160164
`,
161165
errors: [error],
162166
},
167+
{
168+
code: outdent`
169+
const A = class {
170+
abc({a} = {a: 123}) {}
171+
}
172+
`,
173+
errors: [errorNonIdentifier],
174+
},
163175
],
164176
});
165177

@@ -168,5 +180,7 @@ test.snapshot({
168180
invalid: [
169181
'function abc(foo = {a: 123}) {}',
170182
'const abc = (foo = {a: false}) => {};',
183+
'function abc({a} = {a: 123}) {}',
184+
'function abc([a] = {a: 123}) {}',
171185
],
172186
});

test/snapshots/no-object-as-default-parameter.mjs.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,23 @@ Generated by [AVA](https://avajs.dev).
2323
> 1 | const abc = (foo = {a: false}) => {};␊
2424
| ^^^ Do not use an object literal as default for parameter \`foo\`.␊
2525
`
26+
27+
## Invalid #3
28+
1 | function abc({a} = {a: 123}) {}
29+
30+
> Error 1/1
31+
32+
`␊
33+
> 1 | function abc({a} = {a: 123}) {}␊
34+
| ^^^^^^^^ Do not use an object literal as default.␊
35+
`
36+
37+
## Invalid #4
38+
1 | function abc([a] = {a: 123}) {}
39+
40+
> Error 1/1
41+
42+
`␊
43+
> 1 | function abc([a] = {a: 123}) {}␊
44+
| ^^^^^^^^ Do not use an object literal as default.␊
45+
`
Binary file not shown.

0 commit comments

Comments
 (0)