-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathno-object-as-default-parameter.js
51 lines (45 loc) · 1.11 KB
/
no-object-as-default-parameter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const {isFunction} = require('./ast/index.js');
const MESSAGE_ID_IDENTIFIER = 'identifier';
const MESSAGE_ID_NON_IDENTIFIER = 'non-identifier';
const messages = {
[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.',
};
/** @param {import('eslint').Rule.RuleContext} context */
const create = () => ({
AssignmentPattern(node) {
if (!(
node.right.type === 'ObjectExpression'
&& node.right.properties.length > 0
&& isFunction(node.parent)
&& node.parent.params.includes(node)
)) {
return;
}
const {left, right} = node;
if (left.type === 'Identifier') {
return {
node: left,
messageId: MESSAGE_ID_IDENTIFIER,
data: {parameter: left.name},
};
}
return {
node: right,
messageId: MESSAGE_ID_NON_IDENTIFIER,
};
},
});
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'problem',
docs: {
description: 'Disallow the use of objects as default parameters.',
recommended: true,
},
messages,
},
};