options.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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("esprima-fb"),
  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. // If you want to print trailing commas in object literals,
  51. // array expressions, functions calls and function definitions pass true
  52. // for this option.
  53. trailingComma: false,
  54. }, hasOwn = defaults.hasOwnProperty;
  55. // Copy options and fill in default values.
  56. exports.normalize = function(options) {
  57. options = options || defaults;
  58. function get(key) {
  59. return hasOwn.call(options, key)
  60. ? options[key]
  61. : defaults[key];
  62. }
  63. return {
  64. tabWidth: +get("tabWidth"),
  65. useTabs: !!get("useTabs"),
  66. reuseWhitespace: !!get("reuseWhitespace"),
  67. lineTerminator: get("lineTerminator"),
  68. wrapColumn: Math.max(get("wrapColumn"), 0),
  69. sourceFileName: get("sourceFileName"),
  70. sourceMapName: get("sourceMapName"),
  71. sourceRoot: get("sourceRoot"),
  72. inputSourceMap: get("inputSourceMap"),
  73. parser: get("esprima") || get("parser"),
  74. range: get("range"),
  75. tolerant: get("tolerant"),
  76. quote: get("quote"),
  77. trailingComma: get("trailingComma"),
  78. };
  79. };