diff --git a/package.json b/package.json index 1b8cf61b2..7403fd1cc 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,6 @@ "diff": "5.1.0", "eastasianwidth": "0.2.0", "editorconfig": "0.15.3", - "editorconfig-to-prettier": "1.0.0", "emoji-regex": "10.2.1", "escape-string-regexp": "5.0.0", "espree": "9.6.1", diff --git a/src/config/editorconfig-to-prettier.js b/src/config/editorconfig-to-prettier.js new file mode 100644 index 000000000..f07e957f0 --- /dev/null +++ b/src/config/editorconfig-to-prettier.js @@ -0,0 +1,75 @@ +function removeUnset(editorConfig) { + const result = {}; + const keys = Object.keys(editorConfig); + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + if (editorConfig[key] === "unset") { + continue; + } + result[key] = editorConfig[key]; + } + return result; +} + +function editorConfigToPrettier(editorConfig) { + if (!editorConfig) { + return null; + } + + editorConfig = removeUnset(editorConfig); + + if (Object.keys(editorConfig).length === 0) { + return null; + } + + const result = {}; + + if (editorConfig.indent_style) { + result.useTabs = editorConfig.indent_style === "tab"; + } + + if (editorConfig.indent_size === "tab") { + result.useTabs = true; + } + + if (result.useTabs && editorConfig.tab_width) { + result.tabWidth = editorConfig.tab_width; + } else if ( + editorConfig.indent_style === "space" && + editorConfig.indent_size && + editorConfig.indent_size !== "tab" + ) { + result.tabWidth = editorConfig.indent_size; + } else if (editorConfig.tab_width !== undefined) { + result.tabWidth = editorConfig.tab_width; + } + + if (editorConfig.max_line_length) { + if (editorConfig.max_line_length === "off") { + result.printWidth = Number.POSITIVE_INFINITY; + } else { + result.printWidth = editorConfig.max_line_length; + } + } + + if (editorConfig.quote_type === "single") { + result.singleQuote = true; + } else if (editorConfig.quote_type === "double") { + result.singleQuote = false; + } + + if (["cr", "crlf", "lf"].includes(editorConfig.end_of_line)) { + result.endOfLine = editorConfig.end_of_line; + } + + if ( + editorConfig.insert_final_newline === false || + editorConfig.insert_final_newline === true + ) { + result.insertFinalNewline = editorConfig.insert_final_newline; + } + + return result; +} + +export default editorConfigToPrettier; diff --git a/src/config/resolve-editorconfig.js b/src/config/resolve-editorconfig.js index 90e16067c..5877c94f1 100644 --- a/src/config/resolve-editorconfig.js +++ b/src/config/resolve-editorconfig.js @@ -1,6 +1,6 @@ import path from "node:path"; import editorconfig from "editorconfig"; -import editorConfigToPrettier from "editorconfig-to-prettier"; +import editorConfigToPrettier from "./editorconfig-to-prettier.js"; import findProjectRoot from "./find-project-root.js"; async function loadEditorConfig(filePath) { diff --git a/tests/unit/editorconfig-to-prettier.js b/tests/unit/editorconfig-to-prettier.js new file mode 100644 index 000000000..b911382fe --- /dev/null +++ b/tests/unit/editorconfig-to-prettier.js @@ -0,0 +1,149 @@ +import editorconfigToPrettier from "../../src/config/editorconfig-to-prettier.js"; + +test("editorconfigToPrettier", () => { + expect( + editorconfigToPrettier({ + indent_style: "tab", + tab_width: 8, + indent_size: 2, + max_line_length: 100, + }), + ).toStrictEqual({ + useTabs: true, + tabWidth: 8, + printWidth: 100, + }); + + expect( + editorconfigToPrettier({ + indent_style: "space", + tab_width: 8, + indent_size: 2, + max_line_length: 100, + }), + ).toStrictEqual({ + useTabs: false, + tabWidth: 2, + printWidth: 100, + }); + + expect( + editorconfigToPrettier({ + indent_style: "space", + tab_width: 8, + indent_size: 8, + max_line_length: 100, + }), + ).toStrictEqual({ + useTabs: false, + tabWidth: 8, + printWidth: 100, + }); + + expect( + editorconfigToPrettier({ + tab_width: 4, + indent_size: "tab", + }), + ).toStrictEqual({ + tabWidth: 4, + useTabs: true, + }); + + expect( + editorconfigToPrettier({ + indent_size: "tab", + }), + ).toStrictEqual({ + useTabs: true, + }); + + expect( + editorconfigToPrettier({ + tab_width: 0, + indent_size: 0, + }), + ).toStrictEqual({ + tabWidth: 0, + }); + + expect( + editorconfigToPrettier({ + quote_type: "single", + }), + ).toStrictEqual({ + singleQuote: true, + }); + + expect( + editorconfigToPrettier({ + quote_type: "double", + }), + ).toStrictEqual({ + singleQuote: false, + }); + + expect( + editorconfigToPrettier({ + quote_type: "double", + max_line_length: "off", + }), + ).toStrictEqual({ + printWidth: Number.POSITIVE_INFINITY, + singleQuote: false, + }); + + expect( + editorconfigToPrettier({ + end_of_line: "cr", + }), + ).toStrictEqual({ + endOfLine: "cr", + }); + + expect( + editorconfigToPrettier({ + end_of_line: "crlf", + }), + ).toStrictEqual({ + endOfLine: "crlf", + }); + + expect( + editorconfigToPrettier({ + end_of_line: "lf", + }), + ).toStrictEqual({ + endOfLine: "lf", + }); + + expect( + editorconfigToPrettier({ + endOfLine: 123, + }), + ).toStrictEqual({}); + + expect( + editorconfigToPrettier({ + indent_style: "space", + indent_size: 2, + max_line_length: "unset", + }), + ).toStrictEqual({ + useTabs: false, + tabWidth: 2, + }); + + expect(editorconfigToPrettier({ insert_final_newline: false })).toStrictEqual( + { + insertFinalNewline: false, + }, + ); + + expect(editorconfigToPrettier({ insert_final_newline: true })).toStrictEqual({ + insertFinalNewline: true, + }); + + expect(editorconfigToPrettier({})).toBeNull(); + expect(editorconfigToPrettier(null)).toBeNull(); +}); diff --git a/yarn.lock b/yarn.lock index abeb58bc3..53ea7f32d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3244,13 +3244,6 @@ __metadata: languageName: node linkType: hard -"editorconfig-to-prettier@npm:1.0.0": - version: 1.0.0 - resolution: "editorconfig-to-prettier@npm:1.0.0" - checksum: 8054f8593c2f93abca9f3c6db91030fbd80a15fd36b076a5f3fbeb246267173e290817adfb3389d9066026bcc85822e72b7635596e9dda6e74927ae110cbc474 - languageName: node - linkType: hard - "editorconfig@npm:0.15.3": version: 0.15.3 resolution: "editorconfig@npm:0.15.3" @@ -7176,7 +7169,6 @@ __metadata: diff: 5.1.0 eastasianwidth: 0.2.0 editorconfig: 0.15.3 - editorconfig-to-prettier: 1.0.0 emoji-regex: 10.2.1 enquirer: 2.4.1 esbuild: 0.19.2