Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc-multiple-rulesdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
'rulesdir/fake-rule': 'error',
'rulesdir/another-fake-rule': 'error',
'rulesdir/yet-another-fake-rule': 'error',
'rulesdir/a-fake-directory-rule': 'error',
},
plugins: [PACKAGE_NAME],
};
6 changes: 6 additions & 0 deletions fake-rule-dir-three/a-fake-directory-rule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This rule doesn't do anything.
// it only exists to make sure a rule is found and the plugin is working.

'use strict';

module.exports = { create: () => ({}) };
1 change: 1 addition & 0 deletions fake-rule-dir-three/unrelated-dir.ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 2;
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,31 @@ module.exports = {
const rules = Array.isArray(RULES_DIR) ? RULES_DIR : [RULES_DIR];
const rulesObject = {};
rules.forEach((rulesDir) => {
fs.readdirSync(rulesDir)
.filter(filename => ruleExtensions.has(path.extname(filename)))
.map(filename => path.resolve(rulesDir, filename))
.forEach((absolutePath) => {
fs.readdirSync(rulesDir, { withFileTypes: true })
.forEach((entry) => {
const absolutePath = path.resolve(rulesDir, entry.name);
const ruleName = path.basename(absolutePath, path.extname(absolutePath));
if (rulesObject[ruleName]) {
throw new Error(`eslint-plugin-rulesdir found two rules with the same name: ${ruleName}`);
}
rulesObject[ruleName] = require(absolutePath);
if (entry.isFile()) {
if (!ruleExtensions.has(path.extname(entry.name))) return;
rulesObject[ruleName] = require(absolutePath);
}
if (entry.isDirectory()) {
const [index] = fs.readdirSync(absolutePath, { withFileTypes: true })
.filter(subentry => subentry.isFile())
.filter(subentry => subentry.name.startsWith('index.'))
.filter(subentry => ruleExtensions.has(path.extname(subentry.name)))
.map(subentry => path.resolve(absolutePath, subentry.name));
if (index) {
try {
rulesObject[ruleName] = require(path.resolve(absolutePath, index));
} catch (err) {
// Skip unrelated directories
}
}
}
});
});
cache[cacheKey] = rulesObject;
Expand Down