Move editorconfig-to-prettier package into repo (#15394)
I'd like to vendor/upstream this code because it's been the topic of a handful of issues in its repo over the years, including https://github.com/josephfrazier/editorconfig-to-prettier/issues/9, https://github.com/josephfrazier/editorconfig-to-prettier/issues/21, and most recently https://github.com/josephfrazier/editorconfig-to-prettier/issues/24, and I feel that having it in-repo would be more appropriate (not to mention [the issues in _this_ repo that pertain to editorconfig parsing](https://github.com/search?q=repo%3Aprettier%2Fprettier+editorconfig++&type=issues&state=open)). I've elaborated on my thoughts in https://github.com/josephfrazier/editorconfig-to-prettier/issues/24#issuecomment-1718405985, which I'll quote below: > hey @Drakmyth, thanks for continuing this conversation! I appreciate all the links you've provided that indicate existing intention and implementations of parsing non-standard/custom properties. It seems like this question might not be as simple as I thought it was previously. > > @fisker, thanks for chiming in as well! > > As you (@fisker) mentioned in [#16 (comment)](https://github.com/josephfrazier/editorconfig-to-prettier/pull/16#issuecomment-1486130040), this package was originally intended for use by Prettier, and while there are some other dependents (according to [github](https://github.com/josephfrazier/editorconfig-to-prettier/network/dependents?dependent_type=PACKAGE) and [npm](https://www.npmjs.com/package/editorconfig-to-prettier?activeTab=dependents)), Prettier is still the vast majority of the usage this package sees. > > Given that, I think it might make sense for this package's code/functionality to move into the Prettier repository, so that any issues relating to it can be discussed where it's used. Personally, I don't use `.editorconfig` with Prettier, so I don't feel especially well-positioned to make decisions on how things should work, especially when [the output of this package is subsequently modified by Prettier](https://github.com/prettier/prettier/blob/7c512651f8814924a042034ff821190c68ef409d/src/config/resolve-editorconfig.js#L13-L16) (I think you had also [mentioned](https://github.com/prettier/prettier/issues/14514#issuecomment-1471056464) this previously, @fisker), indicating that its behavior is already divergent from the intended use case. > > What do y'all think of that proposal? If we have https://github.com/prettier/prettier/ vendor this repository's code, then I can archive this repo and direct people to discuss functionality in the Prettier issues. This also implements @fisker's suggested change to ES module syntax from https://github.com/josephfrazier/editorconfig-to-prettier/pull/16/, as it's required for lint/test etc to pass. Once this is merged, I'll likely archive the https://github.com/josephfrazier/editorconfig-to-prettier/ repo after adding a note directing any visitors here instead. --- * Vendor editorconfig-to-prettier.js from https://github.com/josephfrazier/editorconfig-to-prettier/blob/8bb8226c40d7f2c8cf629301576850876c8617d4/index.js * WIP Use `require` instead of `import` * Revert "WIP Use `require` instead of `import`" This reverts commit 4656b727960822b3d9d0659b68c99d8fa9a9ba58. * Use `export` in editorconfig-to-prettier.js instead of `module.exports`, see https://github.com/josephfrazier/editorconfig-to-prettier/pull/16/ Copied from https://raw.githubusercontent.com/josephfrazier/editorconfig-to-prettier/e245d7051f3e2a1153b82362ed241f3f7482ab18/index.js * `yarn fix` * Add tests/unit/editorconfig-to-prettier.js from https://github.com/josephfrazier/editorconfig-to-prettier/pull/16/ Copied from https://raw.githubusercontent.com/josephfrazier/editorconfig-to-prettier/9effd580e7b6722007e1ace9913512e450cda46f/test.js * tests/unit/editorconfig-to-prettier.js: Fix path to implementation * WIP Naively convert assert.deepEqual to expect/toStrictEqual, see https://github.com/prettier/prettier/pull/15394#issuecomment-1718623556 * Wrap `test()` around `expect` calls * XXX Force a middle test to fail to ensure the syntax works * Revert "XXX Force a middle test to fail to ensure the syntax works" This reverts commit 947eb34e1175944ebebf0a6271e13af99adcd663. * `yarn fix`
This commit is contained in:
1 parent
721a050d0b
commit
ae6f9ac25b
5 files changed
+225
-10
No files matched your search
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user