1f94389af9166d11de19b69d8e656f140aeaa7384a850542613208323a0e393f248f44c2bf9f42a45a7ce797cb2d672adc7c66ab1777dc0aa11675ff8614bc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*jshint node:true */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. 'use strict';
  24. function Options(options, merge_child_field) {
  25. this.raw_options = _mergeOpts(options, merge_child_field);
  26. // Support passing the source text back with no change
  27. this.disabled = this._get_boolean('disabled');
  28. this.eol = this._get_characters('eol', 'auto');
  29. this.end_with_newline = this._get_boolean('end_with_newline');
  30. this.indent_size = this._get_number('indent_size', 4);
  31. this.indent_char = this._get_characters('indent_char', ' ');
  32. this.indent_level = this._get_number('indent_level');
  33. this.preserve_newlines = this._get_boolean('preserve_newlines', true);
  34. this.max_preserve_newlines = this._get_number('max_preserve_newlines', 32786);
  35. if (!this.preserve_newlines) {
  36. this.max_preserve_newlines = 0;
  37. }
  38. this.indent_with_tabs = this._get_boolean('indent_with_tabs', this.indent_char === '\t');
  39. if (this.indent_with_tabs) {
  40. this.indent_char = '\t';
  41. // indent_size behavior changed after 1.8.6
  42. // It used to be that indent_size would be
  43. // set to 1 for indent_with_tabs. That is no longer needed and
  44. // actually doesn't make sense - why not use spaces? Further,
  45. // that might produce unexpected behavior - tabs being used
  46. // for single-column alignment. So, when indent_with_tabs is true
  47. // and indent_size is 1, reset indent_size to 4.
  48. if (this.indent_size === 1) {
  49. this.indent_size = 4;
  50. }
  51. }
  52. // Backwards compat with 1.3.x
  53. this.wrap_line_length = this._get_number('wrap_line_length', this._get_number('max_char'));
  54. this.indent_empty_lines = this._get_boolean('indent_empty_lines');
  55. // valid templating languages ['django', 'erb', 'handlebars', 'php']
  56. // For now, 'auto' = all off for javascript, all on for html (and inline javascript).
  57. // other values ignored
  58. this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php'], ['auto']);
  59. }
  60. Options.prototype._get_array = function(name, default_value) {
  61. var option_value = this.raw_options[name];
  62. var result = default_value || [];
  63. if (typeof option_value === 'object') {
  64. if (option_value !== null && typeof option_value.concat === 'function') {
  65. result = option_value.concat();
  66. }
  67. } else if (typeof option_value === 'string') {
  68. result = option_value.split(/[^a-zA-Z0-9_\/\-]+/);
  69. }
  70. return result;
  71. };
  72. Options.prototype._get_boolean = function(name, default_value) {
  73. var option_value = this.raw_options[name];
  74. var result = option_value === undefined ? !!default_value : !!option_value;
  75. return result;
  76. };
  77. Options.prototype._get_characters = function(name, default_value) {
  78. var option_value = this.raw_options[name];
  79. var result = default_value || '';
  80. if (typeof option_value === 'string') {
  81. result = option_value.replace(/\\r/, '\r').replace(/\\n/, '\n').replace(/\\t/, '\t');
  82. }
  83. return result;
  84. };
  85. Options.prototype._get_number = function(name, default_value) {
  86. var option_value = this.raw_options[name];
  87. default_value = parseInt(default_value, 10);
  88. if (isNaN(default_value)) {
  89. default_value = 0;
  90. }
  91. var result = parseInt(option_value, 10);
  92. if (isNaN(result)) {
  93. result = default_value;
  94. }
  95. return result;
  96. };
  97. Options.prototype._get_selection = function(name, selection_list, default_value) {
  98. var result = this._get_selection_list(name, selection_list, default_value);
  99. if (result.length !== 1) {
  100. throw new Error(
  101. "Invalid Option Value: The option '" + name + "' can only be one of the following values:\n" +
  102. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  103. }
  104. return result[0];
  105. };
  106. Options.prototype._get_selection_list = function(name, selection_list, default_value) {
  107. if (!selection_list || selection_list.length === 0) {
  108. throw new Error("Selection list cannot be empty.");
  109. }
  110. default_value = default_value || [selection_list[0]];
  111. if (!this._is_valid_selection(default_value, selection_list)) {
  112. throw new Error("Invalid Default Value!");
  113. }
  114. var result = this._get_array(name, default_value);
  115. if (!this._is_valid_selection(result, selection_list)) {
  116. throw new Error(
  117. "Invalid Option Value: The option '" + name + "' can contain only the following values:\n" +
  118. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  119. }
  120. return result;
  121. };
  122. Options.prototype._is_valid_selection = function(result, selection_list) {
  123. return result.length && selection_list.length &&
  124. !result.some(function(item) { return selection_list.indexOf(item) === -1; });
  125. };
  126. // merges child options up with the parent options object
  127. // Example: obj = {a: 1, b: {a: 2}}
  128. // mergeOpts(obj, 'b')
  129. //
  130. // Returns: {a: 2}
  131. function _mergeOpts(allOptions, childFieldName) {
  132. var finalOpts = {};
  133. allOptions = _normalizeOpts(allOptions);
  134. var name;
  135. for (name in allOptions) {
  136. if (name !== childFieldName) {
  137. finalOpts[name] = allOptions[name];
  138. }
  139. }
  140. //merge in the per type settings for the childFieldName
  141. if (childFieldName && allOptions[childFieldName]) {
  142. for (name in allOptions[childFieldName]) {
  143. finalOpts[name] = allOptions[childFieldName][name];
  144. }
  145. }
  146. return finalOpts;
  147. }
  148. function _normalizeOpts(options) {
  149. var convertedOpts = {};
  150. var key;
  151. for (key in options) {
  152. var newKey = key.replace(/-/g, "_");
  153. convertedOpts[newKey] = options[key];
  154. }
  155. return convertedOpts;
  156. }
  157. module.exports.Options = Options;
  158. module.exports.normalizeOpts = _normalizeOpts;
  159. module.exports.mergeOpts = _mergeOpts;