forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
require-array-join-separator.js
55 lines (51 loc) · 1.43 KB
/
require-array-join-separator.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
52
53
54
55
'use strict';
const {matches, methodCallSelector, arrayPrototypeMethodSelector} = require('./selectors/index.js');
const {appendArgument} = require('./fix/index.js');
const MESSAGE_ID = 'require-array-join-separator';
const messages = {
[MESSAGE_ID]: 'Missing the separator argument.',
};
const selector = matches([
// `foo.join()`
methodCallSelector({
method: 'join',
argumentsLength: 0,
includeOptionalCall: true,
includeOptionalMember: true,
}),
// `[].join.call(foo)` and `Array.prototype.join.call(foo)`
[
methodCallSelector({method: 'call', argumentsLength: 1}),
arrayPrototypeMethodSelector({path: 'callee.object', method: 'join'}),
].join(''),
]);
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const sourceCode = context.getSourceCode();
return {
[selector](node) {
const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2);
const isPrototypeMethod = node.arguments.length === 1;
return {
loc: {
start: penultimateToken.loc[isPrototypeMethod ? 'end' : 'start'],
end: lastToken.loc.end,
},
messageId: MESSAGE_ID,
/** @param {import('eslint').Rule.RuleFixer} fixer */
fix: fixer => appendArgument(fixer, node, '\',\'', sourceCode),
};
},
};
};
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce using the separator argument with `Array#join()`.',
},
fixable: 'code',
messages,
},
};