Skip to content

Commit

Permalink
no-object-as-default-parameter: Forbid destructuring (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 19, 2021
1 parent 2d05252 commit 3fcc4bb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
11 changes: 10 additions & 1 deletion docs/rules/no-object-as-default-parameter.md
Expand Up @@ -2,13 +2,16 @@

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.


## Fail

```js
const abc = (foo = {a: false}) => {};
```

```js
function foo({a} = {a: false}) {}
```

```js
const abc = (foo = {a: false, b: 123}) => {};
```
Expand All @@ -20,6 +23,12 @@ const abc = (foo = {a: false, b: 123}) => {};
const abc = (foo = {}) => {};
```

```js
function foo(options) {
const {a} = {a: false, ...options};
}
```

```js
const abc = (foo = false) => {};
```
Expand Down
22 changes: 16 additions & 6 deletions rules/no-object-as-default-parameter.js
@@ -1,24 +1,34 @@
'use strict';

const MESSAGE_ID = 'noObjectAsDefaultParameter';
const MESSAGE_ID_IDENTIFIER = 'identifier';
const MESSAGE_ID_NON_IDENTIFIER = 'non-identifier';
const messages = {
[MESSAGE_ID]: 'Do not use an object literal as default for parameter `{{parameter}}`.',
[MESSAGE_ID_IDENTIFIER]: 'Do not use an object literal as default for parameter `{{parameter}}`.',
[MESSAGE_ID_NON_IDENTIFIER]: 'Do not use an object literal as default.',
};

const objectParameterSelector = [
':function > AssignmentPattern.params',
'[left.type="Identifier"]',
'[right.type="ObjectExpression"]',
'[right.properties.length>0]',
].join('');

const create = () => {
return {
[objectParameterSelector]: node => {
const {left, right} = node;

if (left.type === 'Identifier') {
return {
node: left,
messageId: MESSAGE_ID_IDENTIFIER,
data: {parameter: left.name},
};
}

return {
node: node.left,
messageId: MESSAGE_ID,
data: {parameter: node.left.name},
node: right,
messageId: MESSAGE_ID_NON_IDENTIFIER,
};
},
};
Expand Down
16 changes: 15 additions & 1 deletion test/no-object-as-default-parameter.mjs
Expand Up @@ -4,10 +4,14 @@ import {getTester} from './utils/test.mjs';
const {test} = getTester(import.meta);

const error = {
messageId: 'noObjectAsDefaultParameter',
messageId: 'identifier',
data: {parameter: 'foo'},
};

const errorNonIdentifier = {
messageId: 'non-identifier',
};

test({
valid: [
'const abc = {};',
Expand Down Expand Up @@ -160,6 +164,14 @@ test({
`,
errors: [error],
},
{
code: outdent`
const A = class {
abc({a} = {a: 123}) {}
}
`,
errors: [errorNonIdentifier],
},
],
});

Expand All @@ -168,5 +180,7 @@ test.snapshot({
invalid: [
'function abc(foo = {a: 123}) {}',
'const abc = (foo = {a: false}) => {};',
'function abc({a} = {a: 123}) {}',
'function abc([a] = {a: 123}) {}',
],
});
20 changes: 20 additions & 0 deletions test/snapshots/no-object-as-default-parameter.mjs.md
Expand Up @@ -23,3 +23,23 @@ Generated by [AVA](https://avajs.dev).
> 1 | const abc = (foo = {a: false}) => {};␊
| ^^^ Do not use an object literal as default for parameter \`foo\`.␊
`

## Invalid #3
1 | function abc({a} = {a: 123}) {}

> Error 1/1
`␊
> 1 | function abc({a} = {a: 123}) {}␊
| ^^^^^^^^ Do not use an object literal as default.␊
`

## Invalid #4
1 | function abc([a] = {a: 123}) {}

> Error 1/1
`␊
> 1 | function abc([a] = {a: 123}) {}␊
| ^^^^^^^^ Do not use an object literal as default.␊
`
Binary file modified test/snapshots/no-object-as-default-parameter.mjs.snap
Binary file not shown.

0 comments on commit 3fcc4bb

Please sign in to comment.