eslint-plugin-local-rules icon indicating copy to clipboard operation
eslint-plugin-local-rules copied to clipboard

Automatically pick up all files in `./eslint-local-rules`

Open fregante opened this issue 1 year ago • 2 comments

Once the rules start to pile up, it's easier to keep them in separate files. Currently I have this in index.js:

const fs = require("node:fs");
const path = require("node:path");

const ruleFiles = fs
  .readdirSync(__dirname)
  .filter((file) => !file.endsWith("test.js") && !file.endsWith("index.js"));

const rules = Object.fromEntries(
  ruleFiles.map((file) => [
    path.basename(file, ".js"),
    require("./" + file),
  ])
);

module.exports = rules;

It would be good to add something like it on the condition that:

  • eslint-local-rules.js isn't found
  • eslint-local-rules/index.js isn't found
  • eslint-local-rules/*.js files exist

This follows #22 in that it reduces the amount of boilerplate code that one needs to copy-paste. For people who want custom loading logic, they can keep using the index file.

fregante avatar Nov 25 '23 06:11 fregante

That sounds reasonable to me! Feel free to send a PR and I'll try not to take as long this time 😬

cletusw avatar Jun 23 '24 04:06 cletusw

I'm not sure what I meant by this, the configs in https://github.com/cletusw/eslint-plugin-local-rules/pull/23 already do this.

I think the only part missing is the automatic exclusion of .test.js files after getting the list of .js files:

https://github.com/cletusw/eslint-plugin-local-rules/blob/7c47aa84046839d8aba8688878632ea43617ba01/index.js#L8

fregante avatar Jun 23 '24 05:06 fregante