| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822 |
- var assert = require("assert");
- var sourceMap = require("source-map");
- var printComments = require("./comments").printComments;
- var linesModule = require("./lines");
- var fromString = linesModule.fromString;
- var concat = linesModule.concat;
- var normalizeOptions = require("./options").normalize;
- var getReprinter = require("./patcher").getReprinter;
- var types = require("./types");
- var namedTypes = types.namedTypes;
- var isString = types.builtInTypes.string;
- var isObject = types.builtInTypes.object;
- var FastPath = require("./fast-path");
- var util = require("./util");
-
- function PrintResult(code, sourceMap) {
- assert.ok(this instanceof PrintResult);
-
- isString.assert(code);
- this.code = code;
-
- if (sourceMap) {
- isObject.assert(sourceMap);
- this.map = sourceMap;
- }
- }
-
- var PRp = PrintResult.prototype;
- var warnedAboutToString = false;
-
- PRp.toString = function() {
- if (!warnedAboutToString) {
- console.warn(
- "Deprecation warning: recast.print now returns an object with " +
- "a .code property. You appear to be treating the object as a " +
- "string, which might still work but is strongly discouraged."
- );
-
- warnedAboutToString = true;
- }
-
- return this.code;
- };
-
- var emptyPrintResult = new PrintResult("");
-
- function Printer(originalOptions) {
- assert.ok(this instanceof Printer);
-
- var explicitTabWidth = originalOptions && originalOptions.tabWidth;
- var options = normalizeOptions(originalOptions);
- assert.notStrictEqual(options, originalOptions);
-
- // It's common for client code to pass the same options into both
- // recast.parse and recast.print, but the Printer doesn't need (and
- // can be confused by) options.sourceFileName, so we null it out.
- options.sourceFileName = null;
-
- function printWithComments(path) {
- assert.ok(path instanceof FastPath);
- return printComments(path, print);
- }
-
- function print(path, includeComments) {
- if (includeComments)
- return printWithComments(path);
-
- assert.ok(path instanceof FastPath);
-
- if (!explicitTabWidth) {
- var oldTabWidth = options.tabWidth;
- var loc = path.getNode().loc;
- if (loc && loc.lines && loc.lines.guessTabWidth) {
- options.tabWidth = loc.lines.guessTabWidth();
- var lines = maybeReprint(path);
- options.tabWidth = oldTabWidth;
- return lines;
- }
- }
-
- return maybeReprint(path);
- }
-
- function maybeReprint(path) {
- var reprinter = getReprinter(path);
- if (reprinter) {
- // Since the print function that we pass to the reprinter will
- // be used to print "new" nodes, it's tempting to think we
- // should pass printRootGenerically instead of print, to avoid
- // calling maybeReprint again, but that would be a mistake
- // because the new nodes might not be entirely new, but merely
- // moved from elsewhere in the AST. The print function is the
- // right choice because it gives us the opportunity to reprint
- // such nodes using their original source.
- return maybeAddParens(path, reprinter(print));
- }
- return printRootGenerically(path);
- }
-
- // Print the root node generically, but then resume reprinting its
- // children non-generically.
- function printRootGenerically(path, includeComments) {
- return includeComments
- ? printComments(path, printRootGenerically)
- : genericPrint(path, options, printWithComments);
- }
-
- // Print the entire AST generically.
- function printGenerically(path) {
- return genericPrint(path, options, printGenerically);
- }
-
- this.print = function(ast) {
- if (!ast) {
- return emptyPrintResult;
- }
-
- var lines = print(FastPath.from(ast), true);
-
- return new PrintResult(
- lines.toString(options),
- util.composeSourceMaps(
- options.inputSourceMap,
- lines.getSourceMap(
- options.sourceMapName,
- options.sourceRoot
- )
- )
- );
- };
-
- this.printGenerically = function(ast) {
- if (!ast) {
- return emptyPrintResult;
- }
-
- var path = FastPath.from(ast);
- var oldReuseWhitespace = options.reuseWhitespace;
-
- // Do not reuse whitespace (or anything else, for that matter)
- // when printing generically.
- options.reuseWhitespace = false;
-
- // TODO Allow printing of comments?
- var pr = new PrintResult(printGenerically(path).toString(options));
- options.reuseWhitespace = oldReuseWhitespace;
- return pr;
- };
- }
-
- exports.Printer = Printer;
-
- function maybeAddParens(path, lines) {
- return path.needsParens() ? concat(["(", lines, ")"]) : lines;
- }
-
- function genericPrint(path, options, printPath) {
- assert.ok(path instanceof FastPath);
-
- var node = path.getValue();
- var parts = [];
- var needsParens = false;
- var linesWithoutParens =
- genericPrintNoParens(path, options, printPath);
-
- if (! node || linesWithoutParens.isEmpty()) {
- return linesWithoutParens;
- }
-
- if (node.decorators &&
- node.decorators.length > 0 &&
- // If the parent node is an export declaration, it will be
- // responsible for printing node.decorators.
- ! util.getParentExportDeclaration(path)) {
-
- path.each(function(decoratorPath) {
- parts.push(printPath(decoratorPath), "\n");
- }, "decorators");
-
- } else if (util.isExportDeclaration(node) &&
- node.declaration &&
- node.declaration.decorators) {
- // Export declarations are responsible for printing any decorators
- // that logically apply to node.declaration.
- path.each(function(decoratorPath) {
- parts.push(printPath(decoratorPath), "\n");
- }, "declaration", "decorators");
-
- } else {
- // Nodes with decorators can't have parentheses, so we can avoid
- // computing path.needsParens() except in this case.
- needsParens = path.needsParens();
- }
-
- if (needsParens) {
- parts.unshift("(");
- }
-
- parts.push(linesWithoutParens);
-
- if (needsParens) {
- parts.push(")");
- }
-
- return concat(parts);
- }
-
- function genericPrintNoParens(path, options, print) {
- var n = path.getValue();
-
- if (!n) {
- return fromString("");
- }
-
- if (typeof n === "string") {
- return fromString(n, options);
- }
-
- namedTypes.Printable.assert(n);
-
- switch (n.type) {
- case "File":
- return path.call(print, "program");
-
- case "Program":
- return path.call(function(bodyPath) {
- return printStatementSequence(bodyPath, options, print);
- }, "body");
-
- case "Noop": // Babel extension.
- case "EmptyStatement":
- return fromString("");
-
- case "ExpressionStatement":
- return concat([path.call(print, "expression"), ";"]);
-
- case "ParenthesizedExpression": // Babel extension.
- return concat(["(", path.call(print, "expression"), ")"]);
-
- case "BinaryExpression":
- case "LogicalExpression":
- case "AssignmentExpression":
- return fromString(" ").join([
- path.call(print, "left"),
- n.operator,
- path.call(print, "right")
- ]);
-
- case "AssignmentPattern":
- return concat([
- path.call(print, "left"),
- "=",
- path.call(print, "right")
- ]);
-
- case "MemberExpression":
- var parts = [path.call(print, "object")];
-
- var property = path.call(print, "property");
- if (n.computed) {
- parts.push("[", property, "]");
- } else {
- parts.push(".", property);
- }
-
- return concat(parts);
-
- case "MetaProperty":
- return concat([
- path.call(print, "meta"),
- ".",
- path.call(print, "property")
- ]);
-
- case "BindExpression":
- var parts = [];
-
- if (n.object) {
- parts.push(path.call(print, "object"));
- }
-
- parts.push("::", path.call(print, "callee"));
-
- return concat(parts);
-
- case "Path":
- return fromString(".").join(n.body);
-
- case "Identifier":
- return concat([
- fromString(n.name, options),
- path.call(print, "typeAnnotation")
- ]);
-
- case "SpreadElement":
- case "SpreadElementPattern":
- case "SpreadProperty":
- case "SpreadPropertyPattern":
- case "RestElement":
- return concat(["...", path.call(print, "argument")]);
-
- case "FunctionDeclaration":
- case "FunctionExpression":
- var parts = [];
-
- if (n.async)
- parts.push("async ");
-
- parts.push("function");
-
- if (n.generator)
- parts.push("*");
-
- if (n.id) {
- parts.push(
- " ",
- path.call(print, "id"),
- path.call(print, "typeParameters")
- );
- }
-
- parts.push(
- "(",
- printFunctionParams(path, options, print),
- ")",
- path.call(print, "returnType"),
- " ",
- path.call(print, "body")
- );
-
- return concat(parts);
-
- case "ArrowFunctionExpression":
- var parts = [];
-
- if (n.async)
- parts.push("async ");
-
- if (
- n.params.length === 1 &&
- !n.rest &&
- n.params[0].type === 'Identifier'
- ) {
- parts.push(path.call(print, "params", 0));
- } else {
- parts.push(
- "(",
- printFunctionParams(path, options, print),
- ")"
- );
- }
-
- parts.push(" => ", path.call(print, "body"));
-
- return concat(parts);
-
- case "MethodDefinition":
- var parts = [];
-
- if (n.static) {
- parts.push("static ");
- }
-
- parts.push(printMethod(path, options, print));
-
- return concat(parts);
-
- case "YieldExpression":
- var parts = ["yield"];
-
- if (n.delegate)
- parts.push("*");
-
- if (n.argument)
- parts.push(" ", path.call(print, "argument"));
-
- return concat(parts);
-
- case "AwaitExpression":
- var parts = ["await"];
-
- if (n.all)
- parts.push("*");
-
- if (n.argument)
- parts.push(" ", path.call(print, "argument"));
-
- return concat(parts);
-
- case "ModuleDeclaration":
- var parts = ["module", path.call(print, "id")];
-
- if (n.source) {
- assert.ok(!n.body);
- parts.push("from", path.call(print, "source"));
- } else {
- parts.push(path.call(print, "body"));
- }
-
- return fromString(" ").join(parts);
-
- case "ImportSpecifier":
- var parts = [];
-
- if (n.imported) {
- parts.push(path.call(print, "imported"));
- if (n.local &&
- n.local.name !== n.imported.name) {
- parts.push(" as ", path.call(print, "local"));
- }
- } else if (n.id) {
- parts.push(path.call(print, "id"));
- if (n.name) {
- parts.push(" as ", path.call(print, "name"));
- }
- }
-
- return concat(parts);
-
- case "ExportSpecifier":
- var parts = [];
-
- if (n.local) {
- parts.push(path.call(print, "local"));
- if (n.exported &&
- n.exported.name !== n.local.name) {
- parts.push(" as ", path.call(print, "exported"));
- }
- } else if (n.id) {
- parts.push(path.call(print, "id"));
- if (n.name) {
- parts.push(" as ", path.call(print, "name"));
- }
- }
-
- return concat(parts);
-
- case "ExportBatchSpecifier":
- return fromString("*");
-
- case "ImportNamespaceSpecifier":
- var parts = ["* as "];
- if (n.local) {
- parts.push(path.call(print, "local"));
- } else if (n.id) {
- parts.push(path.call(print, "id"));
- }
- return concat(parts);
-
- case "ImportDefaultSpecifier":
- if (n.local) {
- return path.call(print, "local");
- }
- return path.call(print, "id");
-
- case "ExportDeclaration":
- case "ExportDefaultDeclaration":
- case "ExportNamedDeclaration":
- return printExportDeclaration(path, options, print);
-
- case "ExportAllDeclaration":
- var parts = ["export *"];
-
- if (n.exported) {
- parts.push(" as ", path.call(print, "exported"));
- }
-
- parts.push(
- " from ",
- path.call(print, "source")
- );
-
- return concat(parts);
-
- case "ExportNamespaceSpecifier":
- return concat(["* as ", path.call(print, "exported")]);
-
- case "ExportDefaultSpecifier":
- return path.call(print, "exported");
-
- case "ImportDeclaration":
- var parts = ["import "];
-
- if (n.importKind && n.importKind !== "value") {
- parts.push(n.importKind + " ");
- }
-
- if (n.specifiers &&
- n.specifiers.length > 0) {
-
- var foundImportSpecifier = false;
-
- path.each(function(specifierPath) {
- var i = specifierPath.getName();
- if (i > 0) {
- parts.push(", ");
- }
-
- var value = specifierPath.getValue();
-
- if (namedTypes.ImportDefaultSpecifier.check(value) ||
- namedTypes.ImportNamespaceSpecifier.check(value)) {
- assert.strictEqual(foundImportSpecifier, false);
- } else {
- namedTypes.ImportSpecifier.assert(value);
- if (!foundImportSpecifier) {
- foundImportSpecifier = true;
- parts.push("{");
- }
- }
-
- parts.push(print(specifierPath));
- }, "specifiers");
-
- if (foundImportSpecifier) {
- parts.push("}");
- }
-
- parts.push(" from ");
- }
-
- parts.push(path.call(print, "source"), ";");
-
- return concat(parts);
-
- case "BlockStatement":
- var naked = path.call(function(bodyPath) {
- return printStatementSequence(bodyPath, options, print);
- }, "body");
-
- if (naked.isEmpty()) {
- return fromString("{}");
- }
-
- return concat([
- "{\n",
- naked.indent(options.tabWidth),
- "\n}"
- ]);
-
- case "ReturnStatement":
- var parts = ["return"];
-
- if (n.argument) {
- var argLines = path.call(print, "argument");
- if (argLines.length > 1 &&
- (namedTypes.XJSElement &&
- namedTypes.XJSElement.check(n.argument) ||
- namedTypes.JSXElement &&
- namedTypes.JSXElement.check(n.argument))) {
- parts.push(
- " (\n",
- argLines.indent(options.tabWidth),
- "\n)"
- );
- } else {
- parts.push(" ", argLines);
- }
- }
-
- parts.push(";");
-
- return concat(parts);
-
- case "CallExpression":
- return concat([
- path.call(print, "callee"),
- printArgumentsList(path, options, print)
- ]);
-
- case "ObjectExpression":
- case "ObjectPattern":
- case "ObjectTypeAnnotation":
- var allowBreak = false;
- var isTypeAnnotation = n.type === "ObjectTypeAnnotation";
- var separator = isTypeAnnotation ? ';' : ',';
- var fields = [];
-
- if (isTypeAnnotation) {
- fields.push("indexers", "callProperties");
- }
-
- fields.push("properties");
-
- var len = 0;
- fields.forEach(function(field) {
- len += n[field].length;
- });
-
- var oneLine = (isTypeAnnotation && len === 1) || len === 0;
- var parts = [oneLine ? "{" : "{\n"];
-
- var i = 0;
- fields.forEach(function(field) {
- path.each(function(childPath) {
- var lines = print(childPath);
-
- if (!oneLine) {
- lines = lines.indent(options.tabWidth);
- }
-
- var multiLine = !isTypeAnnotation && lines.length > 1;
- if (multiLine && allowBreak) {
- // Similar to the logic for BlockStatement.
- parts.push("\n");
- }
-
- parts.push(lines);
-
- if (i < len - 1) {
- // Add an extra line break if the previous object property
- // had a multi-line value.
- parts.push(separator + (multiLine ? "\n\n" : "\n"));
- allowBreak = !multiLine;
- } else if (len !== 1 && isTypeAnnotation) {
- parts.push(separator);
- } else if (options.trailingComma) {
- parts.push(separator);
- }
- i++;
- }, field);
- });
-
- parts.push(oneLine ? "}" : "\n}");
-
- return concat(parts);
-
- case "PropertyPattern":
- return concat([
- path.call(print, "key"),
- ": ",
- path.call(print, "pattern")
- ]);
-
- case "Property": // Non-standard AST node type.
- if (n.method || n.kind === "get" || n.kind === "set") {
- return printMethod(path, options, print);
- }
-
- var parts = [];
-
- var key = path.call(print, "key");
- if (n.computed) {
- parts.push("[", key, "]");
- } else {
- parts.push(key);
- }
-
- if (! n.shorthand) {
- parts.push(": ", path.call(print, "value"));
- }
-
- return concat(parts);
-
- case "Decorator":
- return concat(["@", path.call(print, "expression")]);
-
- case "ArrayExpression":
- case "ArrayPattern":
- var elems = n.elements,
- len = elems.length;
-
- var printed = path.map(print, "elements");
- var joined = fromString(", ").join(printed);
- var oneLine = joined.getLineLength(1) <= options.wrapColumn;
- var parts = [oneLine ? "[" : "[\n"];
-
- path.each(function(elemPath) {
- var i = elemPath.getName();
- var elem = elemPath.getValue();
- if (!elem) {
- // If the array expression ends with a hole, that hole
- // will be ignored by the interpreter, but if it ends with
- // two (or more) holes, we need to write out two (or more)
- // commas so that the resulting code is interpreted with
- // both (all) of the holes.
- parts.push(",");
- } else {
- var lines = printed[i];
- if (oneLine) {
- if (i > 0)
- parts.push(" ");
- } else {
- lines = lines.indent(options.tabWidth);
- }
- parts.push(lines);
- if (i < len - 1 || (!oneLine && options.trailingComma))
- parts.push(",");
- if (!oneLine)
- parts.push("\n");
- }
- }, "elements");
-
- parts.push("]");
-
- return concat(parts);
-
- case "SequenceExpression":
- return fromString(", ").join(path.map(print, "expressions"));
-
- case "ThisExpression":
- return fromString("this");
-
- case "Super":
- return fromString("super");
-
- case "Literal":
- if (typeof n.value !== "string")
- return fromString(n.value, options);
-
- return fromString(nodeStr(n.value, options), options);
-
- case "ModuleSpecifier":
- if (n.local) {
- throw new Error(
- "The ESTree ModuleSpecifier type should be abstract"
- );
- }
-
- // The Esprima ModuleSpecifier type is just a string-valued
- // Literal identifying the imported-from module.
- return fromString(nodeStr(n.value, options), options);
-
- case "UnaryExpression":
- var parts = [n.operator];
- if (/[a-z]$/.test(n.operator))
- parts.push(" ");
- parts.push(path.call(print, "argument"));
- return concat(parts);
-
- case "UpdateExpression":
- var parts = [path.call(print, "argument"), n.operator];
-
- if (n.prefix)
- parts.reverse();
-
- return concat(parts);
-
- case "ConditionalExpression":
- return concat([
- "(", path.call(print, "test"),
- " ? ", path.call(print, "consequent"),
- " : ", path.call(print, "alternate"), ")"
- ]);
-
- case "NewExpression":
- var parts = ["new ", path.call(print, "callee")];
- var args = n.arguments;
- if (args) {
- parts.push(printArgumentsList(path, options, print));
- }
-
- return concat(parts);
-
- case "VariableDeclaration":
- var parts = [n.kind, " "];
- var maxLen = 0;
- var printed = path.map(function(childPath) {
- var lines = print(childPath);
- maxLen = Math.max(lines.length, maxLen);
- return lines;
- }, "declarations");
-
- if (maxLen === 1) {
- parts.push(fromString(", ").join(printed));
- } else if (printed.length > 1 ) {
- parts.push(
- fromString(",\n").join(printed)
- .indentTail(n.kind.length + 1)
- );
- } else {
- parts.push(printed[0]);
- }
-
- // We generally want to terminate all variable declarations with a
- // semicolon, except when they are children of for loops.
- var parentNode = path.getParentNode();
- if (!namedTypes.ForStatement.check(parentNode) &&
- !namedTypes.ForInStatement.check(parentNode) &&
- !(namedTypes.ForOfStatement &&
- namedTypes.ForOfStatement.check(parentNode))) {
- parts.push(";");
- }
-
- return concat(parts);
-
- case "VariableDeclarator":
- return n.init ? fromString(" = ").join([
- path.call(print, "id"),
- path.call(print, "init")
- ]) : path.call(print, "id");
-
- case "WithStatement":
- return concat([
- "with (",
- path.call(print, "object"),
- ") ",
- path.call(print, "body")
- ]);
-
- case "IfStatement":
- var con = adjustClause(path.call(print, "consequent"), options),
- parts = ["if (", path.call(print, "test"), ")", con];
-
- if (n.alternate)
- parts.push(
- endsWithBrace(con) ? " else" : "\nelse",
- adjustClause(path.call(print, "alternate"), options));
-
- return concat(parts);
-
- case "ForStatement":
- // TODO Get the for (;;) case right.
- var init = path.call(print, "init"),
- sep = init.length > 1 ? ";\n" : "; ",
- forParen = "for (",
- indented = fromString(sep).join([
- init,
- path.call(print, "test"),
- path.call(print, "update")
- ]).indentTail(forParen.length),
- head = concat([forParen, indented, ")"]),
- clause = adjustClause(path.call(print, "body"), options),
- parts = [head];
-
- if (head.length > 1) {
- parts.push("\n");
- clause = clause.trimLeft();
- }
-
- parts.push(clause);
-
- return concat(parts);
-
- case "WhileStatement":
- return concat([
- "while (",
- path.call(print, "test"),
- ")",
- adjustClause(path.call(print, "body"), options)
- ]);
-
- case "ForInStatement":
- // Note: esprima can't actually parse "for each (".
- return concat([
- n.each ? "for each (" : "for (",
- path.call(print, "left"),
- " in ",
- path.call(print, "right"),
- ")",
- adjustClause(path.call(print, "body"), options)
- ]);
-
- case "ForOfStatement":
- return concat([
- "for (",
- path.call(print, "left"),
- " of ",
- path.call(print, "right"),
- ")",
- adjustClause(path.call(print, "body"), options)
- ]);
-
- case "DoWhileStatement":
- var doBody = concat([
- "do",
- adjustClause(path.call(print, "body"), options)
- ]), parts = [doBody];
-
- if (endsWithBrace(doBody))
- parts.push(" while");
- else
- parts.push("\nwhile");
-
- parts.push(" (", path.call(print, "test"), ");");
-
- return concat(parts);
-
- case "DoExpression":
- var statements = path.call(function(bodyPath) {
- return printStatementSequence(bodyPath, options, print);
- }, "body");
-
- return concat([
- "do {\n",
- statements.indent(options.tabWidth),
- "\n}"
- ]);
-
- case "BreakStatement":
- var parts = ["break"];
- if (n.label)
- parts.push(" ", path.call(print, "label"));
- parts.push(";");
- return concat(parts);
-
- case "ContinueStatement":
- var parts = ["continue"];
- if (n.label)
- parts.push(" ", path.call(print, "label"));
- parts.push(";");
- return concat(parts);
-
- case "LabeledStatement":
- return concat([
- path.call(print, "label"),
- ":\n",
- path.call(print, "body")
- ]);
-
- case "TryStatement":
- var parts = [
- "try ",
- path.call(print, "block")
- ];
-
- if (n.handler) {
- parts.push(" ", path.call(print, "handler"));
- } else if (n.handlers) {
- path.each(function(handlerPath) {
- parts.push(" ", print(handlerPath));
- }, "handlers");
- }
-
- if (n.finalizer) {
- parts.push(" finally ", path.call(print, "finalizer"));
- }
-
- return concat(parts);
-
- case "CatchClause":
- var parts = ["catch (", path.call(print, "param")];
-
- if (n.guard)
- // Note: esprima does not recognize conditional catch clauses.
- parts.push(" if ", path.call(print, "guard"));
-
- parts.push(") ", path.call(print, "body"));
-
- return concat(parts);
-
- case "ThrowStatement":
- return concat(["throw ", path.call(print, "argument"), ";"]);
-
- case "SwitchStatement":
- return concat([
- "switch (",
- path.call(print, "discriminant"),
- ") {\n",
- fromString("\n").join(path.map(print, "cases")),
- "\n}"
- ]);
-
- // Note: ignoring n.lexical because it has no printing consequences.
-
- case "SwitchCase":
- var parts = [];
-
- if (n.test)
- parts.push("case ", path.call(print, "test"), ":");
- else
- parts.push("default:");
-
- if (n.consequent.length > 0) {
- parts.push("\n", path.call(function(consequentPath) {
- return printStatementSequence(consequentPath, options, print);
- }, "consequent").indent(options.tabWidth));
- }
-
- return concat(parts);
-
- case "DebuggerStatement":
- return fromString("debugger;");
-
- // JSX extensions below.
-
- case "XJSAttribute":
- case "JSXAttribute":
- var parts = [path.call(print, "name")];
- if (n.value)
- parts.push("=", path.call(print, "value"));
- return concat(parts);
-
- case "XJSIdentifier":
- case "JSXIdentifier":
- return fromString(n.name, options);
-
- case "XJSNamespacedName":
- case "JSXNamespacedName":
- return fromString(":").join([
- path.call(print, "namespace"),
- path.call(print, "name")
- ]);
-
- case "XJSMemberExpression":
- case "JSXMemberExpression":
- return fromString(".").join([
- path.call(print, "object"),
- path.call(print, "property")
- ]);
-
- case "XJSSpreadAttribute":
- case "JSXSpreadAttribute":
- return concat(["{...", path.call(print, "argument"), "}"]);
-
- case "XJSExpressionContainer":
- case "JSXExpressionContainer":
- return concat(["{", path.call(print, "expression"), "}"]);
-
- case "XJSElement":
- case "JSXElement":
- var openingLines = path.call(print, "openingElement");
-
- if (n.openingElement.selfClosing) {
- assert.ok(!n.closingElement);
- return openingLines;
- }
-
- var childLines = concat(
- path.map(function(childPath) {
- var child = childPath.getValue();
-
- if (namedTypes.Literal.check(child) &&
- typeof child.value === "string") {
- if (/\S/.test(child.value)) {
- return child.value.replace(/^\s+|\s+$/g, "");
- } else if (/\n/.test(child.value)) {
- return "\n";
- }
- }
-
- return print(childPath);
- }, "children")
- ).indentTail(options.tabWidth);
-
- var closingLines = path.call(print, "closingElement");
-
- return concat([
- openingLines,
- childLines,
- closingLines
- ]);
-
- case "XJSOpeningElement":
- case "JSXOpeningElement":
- var parts = ["<", path.call(print, "name")];
- var attrParts = [];
-
- path.each(function(attrPath) {
- attrParts.push(" ", print(attrPath));
- }, "attributes");
-
- var attrLines = concat(attrParts);
-
- var needLineWrap = (
- attrLines.length > 1 ||
- attrLines.getLineLength(1) > options.wrapColumn
- );
-
- if (needLineWrap) {
- attrParts.forEach(function(part, i) {
- if (part === " ") {
- assert.strictEqual(i % 2, 0);
- attrParts[i] = "\n";
- }
- });
-
- attrLines = concat(attrParts).indentTail(options.tabWidth);
- }
-
- parts.push(attrLines, n.selfClosing ? " />" : ">");
-
- return concat(parts);
-
- case "XJSClosingElement":
- case "JSXClosingElement":
- return concat(["</", path.call(print, "name"), ">"]);
-
- case "XJSText":
- case "JSXText":
- return fromString(n.value, options);
-
- case "XJSEmptyExpression":
- case "JSXEmptyExpression":
- return fromString("");
-
- case "TypeAnnotatedIdentifier":
- return concat([
- path.call(print, "annotation"),
- " ",
- path.call(print, "identifier")
- ]);
-
- case "ClassBody":
- if (n.body.length === 0) {
- return fromString("{}");
- }
-
- return concat([
- "{\n",
- path.call(function(bodyPath) {
- return printStatementSequence(bodyPath, options, print);
- }, "body").indent(options.tabWidth),
- "\n}"
- ]);
-
- case "ClassPropertyDefinition":
- var parts = ["static ", path.call(print, "definition")];
- if (!namedTypes.MethodDefinition.check(n.definition))
- parts.push(";");
- return concat(parts);
-
- case "ClassProperty":
- var parts = [];
- if (n.static)
- parts.push("static ");
-
- parts.push(path.call(print, "key"));
- if (n.typeAnnotation)
- parts.push(path.call(print, "typeAnnotation"));
-
- if (n.value)
- parts.push(" = ", path.call(print, "value"));
-
- parts.push(";");
- return concat(parts);
-
- case "ClassDeclaration":
- case "ClassExpression":
- var parts = [];
-
- parts.push("class");
-
- if (n.id) {
- parts.push(
- " ",
- path.call(print, "id"),
- path.call(print, "typeParameters")
- );
- }
-
- if (n.superClass) {
- parts.push(
- " extends ",
- path.call(print, "superClass"),
- path.call(print, "superTypeParameters")
- );
- }
-
- if (n["implements"] && n['implements'].length > 0) {
- parts.push(
- " implements ",
- fromString(", ").join(path.map(print, "implements"))
- );
- }
-
- parts.push(" ", path.call(print, "body"));
-
- return concat(parts);
-
- case "TemplateElement":
- return fromString(n.value.raw, options).lockIndentTail();
-
- case "TemplateLiteral":
- var expressions = path.map(print, "expressions");
- var parts = ["`"];
-
- path.each(function(childPath) {
- var i = childPath.getName();
- parts.push(print(childPath));
- if (i < expressions.length) {
- parts.push("${", expressions[i], "}");
- }
- }, "quasis");
-
- parts.push("`");
-
- return concat(parts).lockIndentTail();
-
- case "TaggedTemplateExpression":
- return concat([
- path.call(print, "tag"),
- path.call(print, "quasi")
- ]);
-
- // These types are unprintable because they serve as abstract
- // supertypes for other (printable) types.
- case "Node":
- case "Printable":
- case "SourceLocation":
- case "Position":
- case "Statement":
- case "Function":
- case "Pattern":
- case "Expression":
- case "Declaration":
- case "Specifier":
- case "NamedSpecifier":
- case "Comment": // Supertype of Block and Line.
- case "MemberTypeAnnotation": // Flow
- case "TupleTypeAnnotation": // Flow
- case "Type": // Flow
- throw new Error("unprintable type: " + JSON.stringify(n.type));
-
- case "CommentBlock": // Babel block comment.
- case "Block": // Esprima block comment.
- return concat(["/*", fromString(n.value, options), "*/"]);
-
- case "CommentLine": // Babel line comment.
- case "Line": // Esprima line comment.
- return concat(["//", fromString(n.value, options)]);
-
- // Type Annotations for Facebook Flow, typically stripped out or
- // transformed away before printing.
- case "TypeAnnotation":
- var parts = [];
-
- if (n.typeAnnotation) {
- if (n.typeAnnotation.type !== "FunctionTypeAnnotation") {
- parts.push(": ");
- }
- parts.push(path.call(print, "typeAnnotation"));
- return concat(parts);
- }
-
- return fromString("");
-
- case "AnyTypeAnnotation":
- return fromString("any", options);
-
- case "MixedTypeAnnotation":
- return fromString("mixed", options);
-
- case "ArrayTypeAnnotation":
- return concat([
- path.call(print, "elementType"),
- "[]"
- ]);
-
- case "BooleanTypeAnnotation":
- return fromString("boolean", options);
-
- case "BooleanLiteralTypeAnnotation":
- assert.strictEqual(typeof n.value, "boolean");
- return fromString("" + n.value, options);
-
- case "DeclareClass":
- return printFlowDeclaration(path, [
- "class ",
- path.call(print, "id"),
- " ",
- path.call(print, "body"),
- ]);
-
- case "DeclareFunction":
- return printFlowDeclaration(path, [
- "function ",
- path.call(print, "id"),
- ";"
- ]);
-
- case "DeclareModule":
- return printFlowDeclaration(path, [
- "module ",
- path.call(print, "id"),
- " ",
- path.call(print, "body"),
- ]);
-
- case "DeclareVariable":
- return printFlowDeclaration(path, [
- "var ",
- path.call(print, "id"),
- ";"
- ]);
-
- case "DeclareExportDeclaration":
- return concat([
- "declare ",
- printExportDeclaration(path, options, print)
- ]);
-
- case "FunctionTypeAnnotation":
- // FunctionTypeAnnotation is ambiguous:
- // declare function(a: B): void; OR
- // var A: (a: B) => void;
- var parts = [];
- var parent = path.getParentNode(0);
- var isArrowFunctionTypeAnnotation = !(
- namedTypes.ObjectTypeCallProperty.check(parent) ||
- namedTypes.DeclareFunction.check(path.getParentNode(2))
- );
-
- var needsColon =
- isArrowFunctionTypeAnnotation &&
- !namedTypes.FunctionTypeParam.check(parent);
-
- if (needsColon) {
- parts.push(": ");
- }
-
- parts.push(
- "(",
- fromString(", ").join(path.map(print, "params")),
- ")"
- );
-
- // The returnType is not wrapped in a TypeAnnotation, so the colon
- // needs to be added separately.
- if (n.returnType) {
- parts.push(
- isArrowFunctionTypeAnnotation ? " => " : ": ",
- path.call(print, "returnType")
- );
- }
-
- return concat(parts);
-
- case "FunctionTypeParam":
- return concat([
- path.call(print, "name"),
- ": ",
- path.call(print, "typeAnnotation"),
- ]);
-
- case "GenericTypeAnnotation":
- return concat([
- path.call(print, "id"),
- path.call(print, "typeParameters")
- ]);
-
- case "InterfaceDeclaration":
- var parts = [
- fromString("interface ", options),
- path.call(print, "id"),
- path.call(print, "typeParameters"),
- " "
- ];
-
- if (n["extends"]) {
- parts.push(
- "extends ",
- fromString(", ").join(path.map(print, "extends"))
- );
- }
-
- parts.push(" ", path.call(print, "body"));
-
- return concat(parts);
-
- case "ClassImplements":
- case "InterfaceExtends":
- return concat([
- path.call(print, "id"),
- path.call(print, "typeParameters")
- ]);
-
- case "IntersectionTypeAnnotation":
- return fromString(" & ").join(path.map(print, "types"));
-
- case "NullableTypeAnnotation":
- return concat([
- "?",
- path.call(print, "typeAnnotation")
- ]);
-
- case "NumberTypeAnnotation":
- return fromString("number", options);
-
- case "ObjectTypeCallProperty":
- return path.call(print, "value");
-
- case "ObjectTypeIndexer":
- return concat([
- "[",
- path.call(print, "id"),
- ": ",
- path.call(print, "key"),
- "]: ",
- path.call(print, "value")
- ]);
-
- case "ObjectTypeProperty":
- return concat([
- path.call(print, "key"),
- ": ",
- path.call(print, "value")
- ]);
-
- case "QualifiedTypeIdentifier":
- return concat([
- path.call(print, "qualification"),
- ".",
- path.call(print, "id")
- ]);
-
- case "StringLiteralTypeAnnotation":
- return fromString(nodeStr(n.value, options), options);
-
- case "NumberLiteralTypeAnnotation":
- assert.strictEqual(typeof n.value, "number");
- return fromString("" + n.value, options);
-
- case "StringTypeAnnotation":
- return fromString("string", options);
-
- case "TypeAlias":
- return concat([
- "type ",
- path.call(print, "id"),
- " = ",
- path.call(print, "right"),
- ";"
- ]);
-
- case "TypeCastExpression":
- return concat([
- "(",
- path.call(print, "expression"),
- path.call(print, "typeAnnotation"),
- ")"
- ]);
-
- case "TypeParameterDeclaration":
- case "TypeParameterInstantiation":
- return concat([
- "<",
- fromString(", ").join(path.map(print, "params")),
- ">"
- ]);
-
- case "TypeofTypeAnnotation":
- return concat([
- fromString("typeof ", options),
- path.call(print, "argument")
- ]);
-
- case "UnionTypeAnnotation":
- return fromString(" | ").join(path.map(print, "types"));
-
- case "VoidTypeAnnotation":
- return fromString("void", options);
-
- // Unhandled types below. If encountered, nodes of these types should
- // be either left alone or desugared into AST types that are fully
- // supported by the pretty-printer.
- case "ClassHeritage": // TODO
- case "ComprehensionBlock": // TODO
- case "ComprehensionExpression": // TODO
- case "Glob": // TODO
- case "GeneratorExpression": // TODO
- case "LetStatement": // TODO
- case "LetExpression": // TODO
- case "GraphExpression": // TODO
- case "GraphIndexExpression": // TODO
-
- // XML types that nobody cares about or needs to print.
- case "XMLDefaultDeclaration":
- case "XMLAnyName":
- case "XMLQualifiedIdentifier":
- case "XMLFunctionQualifiedIdentifier":
- case "XMLAttributeSelector":
- case "XMLFilterExpression":
- case "XML":
- case "XMLElement":
- case "XMLList":
- case "XMLEscape":
- case "XMLText":
- case "XMLStartTag":
- case "XMLEndTag":
- case "XMLPointTag":
- case "XMLName":
- case "XMLAttribute":
- case "XMLCdata":
- case "XMLComment":
- case "XMLProcessingInstruction":
- default:
- debugger;
- throw new Error("unknown type: " + JSON.stringify(n.type));
- }
-
- return p;
- }
-
- function printStatementSequence(path, options, print) {
- var inClassBody =
- namedTypes.ClassBody &&
- namedTypes.ClassBody.check(path.getParentNode());
-
- var filtered = [];
- var sawComment = false;
- var sawStatement = false;
-
- path.each(function(stmtPath) {
- var i = stmtPath.getName();
- var stmt = stmtPath.getValue();
-
- // Just in case the AST has been modified to contain falsy
- // "statements," it's safer simply to skip them.
- if (!stmt) {
- return;
- }
-
- // Skip printing EmptyStatement nodes to avoid leaving stray
- // semicolons lying around.
- if (stmt.type === "EmptyStatement") {
- return;
- }
-
- if (namedTypes.Comment.check(stmt)) {
- // The pretty printer allows a dangling Comment node to act as
- // a Statement when the Comment can't be attached to any other
- // non-Comment node in the tree.
- sawComment = true;
- } else if (namedTypes.Statement.check(stmt)) {
- sawStatement = true;
- } else {
- // When the pretty printer encounters a string instead of an
- // AST node, it just prints the string. This behavior can be
- // useful for fine-grained formatting decisions like inserting
- // blank lines.
- isString.assert(stmt);
- }
-
- // We can't hang onto stmtPath outside of this function, because
- // it's just a reference to a mutable FastPath object, so we have
- // to go ahead and print it here.
- filtered.push({
- node: stmt,
- printed: print(stmtPath)
- });
- });
-
- if (sawComment) {
- assert.strictEqual(
- sawStatement, false,
- "Comments may appear as statements in otherwise empty statement " +
- "lists, but may not coexist with non-Comment nodes."
- );
- }
-
- var prevTrailingSpace = null;
- var len = filtered.length;
- var parts = [];
-
- filtered.forEach(function(info, i) {
- var printed = info.printed;
- var stmt = info.node;
- var multiLine = printed.length > 1;
- var notFirst = i > 0;
- var notLast = i < len - 1;
- var leadingSpace;
- var trailingSpace;
- var lines = stmt && stmt.loc && stmt.loc.lines;
- var trueLoc = lines && options.reuseWhitespace &&
- util.getTrueLoc(stmt, lines);
-
- if (notFirst) {
- if (trueLoc) {
- var beforeStart = lines.skipSpaces(trueLoc.start, true);
- var beforeStartLine = beforeStart ? beforeStart.line : 1;
- var leadingGap = trueLoc.start.line - beforeStartLine;
- leadingSpace = Array(leadingGap + 1).join("\n");
- } else {
- leadingSpace = multiLine ? "\n\n" : "\n";
- }
- } else {
- leadingSpace = "";
- }
-
- if (notLast) {
- if (trueLoc) {
- var afterEnd = lines.skipSpaces(trueLoc.end);
- var afterEndLine = afterEnd ? afterEnd.line : lines.length;
- var trailingGap = afterEndLine - trueLoc.end.line;
- trailingSpace = Array(trailingGap + 1).join("\n");
- } else {
- trailingSpace = multiLine ? "\n\n" : "\n";
- }
- } else {
- trailingSpace = "";
- }
-
- parts.push(
- maxSpace(prevTrailingSpace, leadingSpace),
- printed
- );
-
- if (notLast) {
- prevTrailingSpace = trailingSpace;
- } else if (trailingSpace) {
- parts.push(trailingSpace);
- }
- });
-
- return concat(parts);
- }
-
- function maxSpace(s1, s2) {
- if (!s1 && !s2) {
- return fromString("");
- }
-
- if (!s1) {
- return fromString(s2);
- }
-
- if (!s2) {
- return fromString(s1);
- }
-
- var spaceLines1 = fromString(s1);
- var spaceLines2 = fromString(s2);
-
- if (spaceLines2.length > spaceLines1.length) {
- return spaceLines2;
- }
-
- return spaceLines1;
- }
-
- function printMethod(path, options, print) {
- var node = path.getNode();
- var kind = node.kind;
- var parts = [];
-
- namedTypes.FunctionExpression.assert(node.value);
-
- if (node.value.async) {
- parts.push("async ");
- }
-
- if (!kind || kind === "init" || kind === "method" || kind === "constructor") {
- if (node.value.generator) {
- parts.push("*");
- }
- } else {
- assert.ok(kind === "get" || kind === "set");
- parts.push(kind, " ");
- }
-
- var key = path.call(print, "key");
- if (node.computed) {
- key = concat(["[", key, "]"]);
- }
-
- parts.push(
- key,
- path.call(print, "value", "typeParameters"),
- "(",
- path.call(function(valuePath) {
- return printFunctionParams(valuePath, options, print);
- }, "value"),
- ")",
- path.call(print, "value", "returnType"),
- " ",
- path.call(print, "value", "body")
- );
-
- return concat(parts);
- }
-
- function printArgumentsList(path, options, print) {
- var printed = path.map(print, "arguments");
-
- var joined = fromString(", ").join(printed);
- if (joined.getLineLength(1) > options.wrapColumn) {
- joined = fromString(",\n").join(printed);
- return concat([
- "(\n",
- joined.indent(options.tabWidth),
- options.trailingComma ? ",\n)" : "\n)"
- ]);
- }
-
- return concat(["(", joined, ")"]);
- }
-
- function printFunctionParams(path, options, print) {
- var fun = path.getValue();
- namedTypes.Function.assert(fun);
-
- var printed = path.map(print, "params");
-
- if (fun.defaults) {
- path.each(function(defExprPath) {
- var i = defExprPath.getName();
- var p = printed[i];
- if (p && defExprPath.getValue()) {
- printed[i] = concat([p, "=", print(defExprPath)]);
- }
- }, "defaults");
- }
-
- if (fun.rest) {
- printed.push(concat(["...", path.call(print, "rest")]));
- }
-
- var joined = fromString(", ").join(printed);
- if (joined.length > 1 ||
- joined.getLineLength(1) > options.wrapColumn) {
- joined = fromString(",\n").join(printed);
- if (options.trailingComma && !fun.rest) {
- joined = concat([joined, ",\n"]);
- }
- return concat(["\n", joined.indent(options.tabWidth)]);
- }
-
- return joined;
- }
-
- function printExportDeclaration(path, options, print) {
- var decl = path.getValue();
- var parts = ["export "];
-
- namedTypes.Declaration.assert(decl);
-
- if (decl["default"] ||
- decl.type === "ExportDefaultDeclaration") {
- parts.push("default ");
- }
-
- if (decl.declaration) {
- parts.push(path.call(print, "declaration"));
-
- } else if (decl.specifiers &&
- decl.specifiers.length > 0) {
-
- if (decl.specifiers.length === 1 &&
- decl.specifiers[0].type === "ExportBatchSpecifier") {
- parts.push("*");
- } else {
- parts.push(
- "{",
- fromString(", ").join(path.map(print, "specifiers")),
- "}"
- );
- }
-
- if (decl.source) {
- parts.push(" from ", path.call(print, "source"));
- }
- }
-
- var lines = concat(parts);
-
- if (lastNonSpaceCharacter(lines) !== ";") {
- lines = concat([lines, ";"]);
- }
-
- return lines;
- }
-
- function printFlowDeclaration(path, parts) {
- var parentExportDecl = util.getParentExportDeclaration(path);
-
- if (parentExportDecl) {
- assert.strictEqual(
- parentExportDecl.type,
- "DeclareExportDeclaration"
- );
- } else {
- // If the parent node has type DeclareExportDeclaration, then it
- // will be responsible for printing the "declare" token. Otherwise
- // it needs to be printed with this non-exported declaration node.
- parts.unshift("declare ");
- }
-
- return concat(parts);
- }
-
- function adjustClause(clause, options) {
- if (clause.length > 1)
- return concat([" ", clause]);
-
- return concat([
- "\n",
- maybeAddSemicolon(clause).indent(options.tabWidth)
- ]);
- }
-
- function lastNonSpaceCharacter(lines) {
- var pos = lines.lastPos();
- do {
- var ch = lines.charAt(pos);
- if (/\S/.test(ch))
- return ch;
- } while (lines.prevPos(pos));
- }
-
- function endsWithBrace(lines) {
- return lastNonSpaceCharacter(lines) === "}";
- }
-
- function swapQuotes(str) {
- return str.replace(/['"]/g, function(m) {
- return m === '"' ? '\'' : '"';
- });
- }
-
- function nodeStr(str, options) {
- isString.assert(str);
- switch (options.quote) {
- case "auto":
- var double = JSON.stringify(str);
- var single = swapQuotes(JSON.stringify(swapQuotes(str)));
- return double.length > single.length ? single : double;
- case "single":
- return swapQuotes(JSON.stringify(swapQuotes(str)));
- case "double":
- default:
- return JSON.stringify(str);
- }
- }
-
- function maybeAddSemicolon(lines) {
- var eoc = lastNonSpaceCharacter(lines);
- if (!eoc || "\n};".indexOf(eoc) < 0)
- return concat([lines, ";"]);
- return lines;
- }
|