Add option for trailing commas in objects
parent
9596695a12
commit
fc186c8df8
|
@ -5,7 +5,7 @@ const minimist = require("minimist");
|
|||
const jscodefmt = require("../index");
|
||||
|
||||
const argv = minimist(process.argv.slice(2), {
|
||||
boolean: ["write", "useFlowParser", "bracket-spacing", "single-quote"]
|
||||
boolean: ["write", "useFlowParser", "bracket-spacing", "single-quote", "trailing-comma"]
|
||||
});
|
||||
|
||||
const filename = argv["_"][0];
|
||||
|
@ -16,7 +16,8 @@ const output = jscodefmt.format(fs.readFileSync(filename, "utf8"), {
|
|||
tabWidth: argv['tab-width'],
|
||||
bracketSpacing: argv['bracket-spacing'],
|
||||
useFlowParser: argv['flow-parser'],
|
||||
singleQuote: argv["single-quote"]
|
||||
singleQuote: argv["single-quote"],
|
||||
trailingComma: argv["trailing-comma"]
|
||||
});
|
||||
|
||||
if(write) {
|
||||
|
|
16
src/pp.js
16
src/pp.js
|
@ -46,6 +46,10 @@ function conditionalGroup(states, opts) {
|
|||
}));
|
||||
}
|
||||
|
||||
function ifBreak(contents) {
|
||||
return { type: 'if-break', contents };
|
||||
}
|
||||
|
||||
function iterDoc(topDoc, func) {
|
||||
const docs = [topDoc];
|
||||
|
||||
|
@ -163,6 +167,11 @@ function fits(next, restCommands, width) {
|
|||
case "group":
|
||||
cmds.push([ind, doc.break ? MODE_BREAK : mode, doc.contents]);
|
||||
break;
|
||||
case "if-break":
|
||||
if(mode === MODE_BREAK) {
|
||||
cmds.push([ind, mode, doc.contents]);
|
||||
}
|
||||
break;
|
||||
case "line":
|
||||
switch(mode) {
|
||||
case MODE_FLAT:
|
||||
|
@ -264,6 +273,11 @@ function print(w, doc) {
|
|||
break;
|
||||
}
|
||||
break;
|
||||
case "if-break":
|
||||
if(mode === MODE_BREAK) {
|
||||
cmds.push([ind, MODE_BREAK, doc.contents]);
|
||||
}
|
||||
break;
|
||||
case "line":
|
||||
switch(mode) {
|
||||
case MODE_FLAT:
|
||||
|
@ -314,5 +328,5 @@ function print(w, doc) {
|
|||
module.exports = {
|
||||
fromString, concat, isEmpty, join,
|
||||
line, softline, hardline, literalline, group, multilineGroup,
|
||||
conditionalGroup, hasHardLine, indent, print, getFirstString
|
||||
conditionalGroup, ifBreak, hasHardLine, indent, print, getFirstString
|
||||
};
|
||||
|
|
|
@ -16,6 +16,7 @@ var indent = pp.indent;
|
|||
var getFirstString = pp.getFirstString;
|
||||
var hasHardLine = pp.hasHardLine;
|
||||
var conditionalGroup = pp.conditionalGroup;
|
||||
var ifBreak = pp.ifBreak;
|
||||
var normalizeOptions = require("./options").normalize;
|
||||
var types = require("ast-types");
|
||||
var namedTypes = types.namedTypes;
|
||||
|
@ -629,6 +630,7 @@ function genericPrintNoParens(path, options, print) {
|
|||
case "ObjectTypeAnnotation":
|
||||
var allowBreak = false;
|
||||
var isTypeAnnotation = n.type === "ObjectTypeAnnotation";
|
||||
var isObjectExpression = n.type === "ObjectExpression";
|
||||
// Leave this here because we *might* want to make this
|
||||
// configurable later -- flow accepts ";" for type separators
|
||||
var separator = (isTypeAnnotation ? "," : ",");
|
||||
|
@ -663,6 +665,7 @@ function genericPrintNoParens(path, options, print) {
|
|||
(options.bracketSpacing ? line : softline),
|
||||
join(concat([ separator, line ]), props)
|
||||
])),
|
||||
ifBreak((isObjectExpression && options.trailingComma) ? "," : ""),
|
||||
(options.bracketSpacing ? line : softline),
|
||||
rightBrace,
|
||||
path.call(print, "typeAnnotation")
|
||||
|
@ -1671,7 +1674,6 @@ function printMethod(path, options, print) {
|
|||
|
||||
function printArgumentsList(path, options, print) {
|
||||
var printed = path.map(print, "arguments");
|
||||
var trailingComma = util.isTrailingCommaEnabled(options, "parameters");
|
||||
var args;
|
||||
|
||||
if (printed.length === 0) {
|
||||
|
|
Loading…
Reference in New Issue