index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module.exports = editorConfigToPrettier;
  2. function removeUnset(editorConfig) {
  3. const result = {};
  4. const keys = Object.keys(editorConfig);
  5. for (let i = 0; i < keys.length; i++) {
  6. const key = keys[i];
  7. if (editorConfig[key] === "unset") {
  8. continue;
  9. }
  10. result[key] = editorConfig[key];
  11. }
  12. return result;
  13. }
  14. function editorConfigToPrettier(editorConfig) {
  15. if (!editorConfig) {
  16. return null;
  17. }
  18. editorConfig = removeUnset(editorConfig);
  19. if (Object.keys(editorConfig).length === 0) {
  20. return null;
  21. }
  22. const result = {};
  23. if (editorConfig.indent_style) {
  24. result.useTabs = editorConfig.indent_style === "tab";
  25. }
  26. if (editorConfig.indent_size === "tab") {
  27. result.useTabs = true;
  28. }
  29. if (result.useTabs && editorConfig.tab_width) {
  30. result.tabWidth = editorConfig.tab_width;
  31. } else if (
  32. editorConfig.indent_style === "space" &&
  33. editorConfig.indent_size &&
  34. editorConfig.indent_size !== "tab"
  35. ) {
  36. result.tabWidth = editorConfig.indent_size;
  37. } else if (editorConfig.tab_width !== undefined) {
  38. result.tabWidth = editorConfig.tab_width;
  39. }
  40. if (editorConfig.max_line_length && editorConfig.max_line_length !== "off") {
  41. result.printWidth = editorConfig.max_line_length;
  42. }
  43. if (editorConfig.quote_type === "single") {
  44. result.singleQuote = true;
  45. } else if (editorConfig.quote_type === "double") {
  46. result.singleQuote = false;
  47. }
  48. if (["cr", "crlf", "lf"].indexOf(editorConfig.end_of_line) !== -1) {
  49. result.endOfLine = editorConfig.end_of_line;
  50. }
  51. if (
  52. editorConfig.insert_final_newline === false ||
  53. editorConfig.insert_final_newline === true
  54. ) {
  55. result.insertFinalNewline = editorConfig.insert_final_newline;
  56. }
  57. return result;
  58. }