-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprefer-array-from.js
39 lines (37 loc) · 1.13 KB
/
prefer-array-from.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
/**
* @license MIT
* @author Martin Giger
*/
const firstElement = (array) => {
const [ element ] = array;
return element;
};
export default {
meta: {
docs: {
description: "Prefer using Array.from over spreading an iterable in an array literal. Using Array.from also preserves the original type of TypedArrays while mapping.",
recommended: true,
},
schema: [],
fixable: "code",
type: "problem",
messages: {
preferArrayFrom: "Use Array.from to convert from iterable to array",
},
},
create(context) {
return {
"ArrayExpression > SpreadElement:first-child:last-child"(node) {
node = node.parent;
context.report({
node,
messageId: 'preferArrayFrom',
fix(fixer) {
const { sourceCode } = context;
return fixer.replaceText(node, `Array.from(${sourceCode.getText(firstElement(node.elements).argument)})`);
},
});
},
};
},
};