Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-object-as-default-parameter: Forbid destructuring #1433

Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion docs/rules/no-object-as-default-parameter.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.