-
Notifications
You must be signed in to change notification settings - Fork 109
/
trailing-commas.js
43 lines (38 loc) · 1.28 KB
/
trailing-commas.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
/**
* Adds trailing commas to every object literal and array.
*/
module.exports = function(file, api, options) {
const j = api.jscodeshift;
const objectHasNoTrailingComma = ({node}) => {
// Only transform objects that are on multiple lines.
if (node.properties.length === 0 || (node.loc && node.loc.start.line === node.loc.end.line)) {
return false;
}
const lastProp = node.properties[node.properties.length - 1];
return file.source.charAt(lastProp.end) !== ',';
};
const arrayHasNoTrailingComma = ({node}) => {
// Only transform arrays that are on multiple lines.
if (node.elements.length === 0 || node.loc.start.line === node.loc.end.line) {
return false;
}
const lastEle = node.elements[node.elements.length - 1];
return file.source.charAt(lastEle.end) !== ',';
};
const forceReprint = ({node}) => {
node.original = null;
};
const root = j(file.source);
root
.find(j.ObjectExpression)
.filter(objectHasNoTrailingComma)
.forEach(forceReprint);
root
.find(j.ArrayExpression)
.filter(arrayHasNoTrailingComma)
.forEach(forceReprint);
return root.toSource(Object.assign(options.printOptions || {}, {
trailingComma: true,
wrapColumn: 1, // Makes sure we write each values on a separate line.
}));
}