-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the UX of using an npm installed formatter #5900
Comments
@sindresorhus To be clear, you are looking for this enhancement in CLIEngine and the CLI interface, if possible? |
@platinumazure Yes, more specifically, in |
I was wondering if we should instead add formatters to be distributed through plugins? Formatters are the only thing we currently have that we don't have a recommended way of shearing. |
I fear this is more complicated than it looks. That said, @sindresorhus if you want to volunteer to investigate feasibility, we can consider it. |
I've already looked into it, and doesn't seem that hard to me, unless I'm missing something. I would just use Untested patch: @@ -1,3 +1,5 @@
+var resolveFrom = require("resolve-from");
+
CLIEngine.getFormatter = function(format) {
var formatterPath;
@@ -11,13 +13,20 @@
// replace \ with / for Windows compatibility
format = format.replace(/\\/g, "/");
+ var cwd = this.options ? this.options.cwd : process.cwd();
+
// if there's a slash, then it's a file
if (format.indexOf("/") > -1) {
- var cwd = this.options ? this.options.cwd : process.cwd();
formatterPath = path.resolve(cwd, format);
} else {
- formatterPath = "./formatters/" + format;
+ var npmFormat = /^eslint-formatter-/.test(format) ? format : "eslint-formatter-" + format;
+
+ formatterPath = resolveFrom(cwd, npmFormat);
+
+ if (!formatterPath) {
+ formatterPath = "./formatters/" + format;
+ }
}
try { |
I'd prefer not to add a second way to resolve modules (we already have a utility for that), but otherwise looks okay. The next step would be to submit a pull request for the change. We'd need tests as well as documentation in order to merge. |
As per our discussion in today's TSC meeting, this issue has been added to the Core Roadmap. |
I forgot to mention, but I'm working on this one. It's in review. |
This was added in #9464. |
This is currently what you have to write to use a custom npm installed formatter with ESLint:
That's not very user-friendly.
I propose the following:
getFormatter()
try to require the formatter from cwd first. That way you could just do:eslint-formatter
prefix. This matches the convention for shareable configs. This combined with1.
would mean you could just do:Much nicer! ✨
I would also document the recommendation of using the
eslintformatter
keyword in package.json, like you do with shareable configs. This would make it easier for users to find third-party formatters.The text was updated successfully, but these errors were encountered: