options.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var defaults = {
  2. // If you want to use a different branch of esprima, or any other
  3. // module that supports a .parse function, pass that module object to
  4. // recast.parse as options.parser (legacy synonym: options.esprima).
  5. parser: require("../parsers/esprima"),
  6. // Number of spaces the pretty-printer should use per tab for
  7. // indentation. If you do not pass this option explicitly, it will be
  8. // (quite reliably!) inferred from the original code.
  9. tabWidth: 4,
  10. // If you really want the pretty-printer to use tabs instead of
  11. // spaces, make this option true.
  12. useTabs: false,
  13. // The reprinting code leaves leading whitespace untouched unless it
  14. // has to reindent a line, or you pass false for this option.
  15. reuseWhitespace: true,
  16. // Override this option to use a different line terminator, e.g. \r\n.
  17. lineTerminator: require("os").EOL,
  18. // Some of the pretty-printer code (such as that for printing function
  19. // parameter lists) makes a valiant attempt to prevent really long
  20. // lines. You can adjust the limit by changing this option; however,
  21. // there is no guarantee that line length will fit inside this limit.
  22. wrapColumn: 74, // Aspirational for now.
  23. // Pass a string as options.sourceFileName to recast.parse to tell the
  24. // reprinter to keep track of reused code so that it can construct a
  25. // source map automatically.
  26. sourceFileName: null,
  27. // Pass a string as options.sourceMapName to recast.print, and
  28. // (provided you passed options.sourceFileName earlier) the
  29. // PrintResult of recast.print will have a .map property for the
  30. // generated source map.
  31. sourceMapName: null,
  32. // If provided, this option will be passed along to the source map
  33. // generator as a root directory for relative source file paths.
  34. sourceRoot: null,
  35. // If you provide a source map that was generated from a previous call
  36. // to recast.print as options.inputSourceMap, the old source map will
  37. // be composed with the new source map.
  38. inputSourceMap: null,
  39. // If you want esprima to generate .range information (recast only
  40. // uses .loc internally), pass true for this option.
  41. range: false,
  42. // If you want esprima not to throw exceptions when it encounters
  43. // non-fatal errors, keep this option true.
  44. tolerant: true,
  45. // If you want to override the quotes used in string literals, specify
  46. // either "single", "double", or "auto" here ("auto" will select the one
  47. // which results in the shorter literal)
  48. // Otherwise, double quotes are used.
  49. quote: null,
  50. // Controls the printing of trailing commas in object literals,
  51. // array expressions and function parameters.
  52. //
  53. // This option could either be:
  54. // * Boolean - enable/disable in all contexts (objects, arrays and function params).
  55. // * Object - enable/disable per context.
  56. //
  57. // Example:
  58. // trailingComma: {
  59. // objects: true,
  60. // arrays: true,
  61. // parameters: false,
  62. // }
  63. trailingComma: false,
  64. // Controls the printing of spaces inside array brackets.
  65. // See: http://eslint.org/docs/rules/array-bracket-spacing
  66. arrayBracketSpacing: false,
  67. // Controls the printing of spaces inside object literals,
  68. // destructuring assignments, and import/export specifiers.
  69. // See: http://eslint.org/docs/rules/object-curly-spacing
  70. objectCurlySpacing: true,
  71. // If you want parenthesis to wrap single-argument arrow function parameter
  72. // lists, pass true for this option.
  73. arrowParensAlways: false,
  74. // There are 2 supported syntaxes (`,` and `;`) in Flow Object Types;
  75. // The use of commas is in line with the more popular style and matches
  76. // how objects are defined in JS, making it a bit more natural to write.
  77. flowObjectCommas: true,
  78. }, hasOwn = defaults.hasOwnProperty;
  79. // Copy options and fill in default values.
  80. exports.normalize = function(options) {
  81. options = options || defaults;
  82. function get(key) {
  83. return hasOwn.call(options, key)
  84. ? options[key]
  85. : defaults[key];
  86. }
  87. return {
  88. tabWidth: +get("tabWidth"),
  89. useTabs: !!get("useTabs"),
  90. reuseWhitespace: !!get("reuseWhitespace"),
  91. lineTerminator: get("lineTerminator"),
  92. wrapColumn: Math.max(get("wrapColumn"), 0),
  93. sourceFileName: get("sourceFileName"),
  94. sourceMapName: get("sourceMapName"),
  95. sourceRoot: get("sourceRoot"),
  96. inputSourceMap: get("inputSourceMap"),
  97. parser: get("esprima") || get("parser"),
  98. range: get("range"),
  99. tolerant: get("tolerant"),
  100. quote: get("quote"),
  101. trailingComma: get("trailingComma"),
  102. arrayBracketSpacing: get("arrayBracketSpacing"),
  103. objectCurlySpacing: get("objectCurlySpacing"),
  104. arrowParensAlways: get("arrowParensAlways"),
  105. flowObjectCommas: get("flowObjectCommas"),
  106. };
  107. };