|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// EsLint rule dir loader |
| 6 | +// https://github.com/eslint-community/eslint-plugin-rulesdir |
| 7 | +// but modified as we need to use it with ESM |
| 8 | +// This code is licensed under the MIT license |
| 9 | +/** |
| 10 | + * The MIT License (MIT) |
| 11 | + * ===================== |
| 12 | + * |
| 13 | + * Copyright © 2017 Teddy Katz |
| 14 | + * |
| 15 | + * Permission is hereby granted, free of charge, to any person |
| 16 | + * obtaining a copy of this software and associated documentation |
| 17 | + * files (the “Software”), to deal in the Software without |
| 18 | + * restriction, including without limitation the rights to use, |
| 19 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 | + * copies of the Software, and to permit persons to whom the |
| 21 | + * Software is furnished to do so, subject to the following |
| 22 | + * conditions: |
| 23 | + * |
| 24 | + * The above copyright notice and this permission notice shall be |
| 25 | + * included in all copies or substantial portions of the Software. |
| 26 | + * |
| 27 | + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, |
| 28 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 29 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 31 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 32 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 33 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 34 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * @fileoverview Allows a local ESLint rules directory to be used without a command-line flag |
| 39 | + * @author Teddy Katz |
| 40 | + */ |
| 41 | + |
| 42 | +import { readdirSync } from 'fs'; |
| 43 | +import { extname, resolve, basename, join } from 'path'; |
| 44 | +import { pathToFileURL } from 'url'; |
| 45 | + |
| 46 | +const RULES_DIR = join(import.meta.dirname, 'lib'); |
| 47 | + |
| 48 | +const ruleExtensions = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts']); |
| 49 | + |
| 50 | +const rulesModules = {}; |
| 51 | + |
| 52 | +for (const filename of readdirSync(RULES_DIR)) { |
| 53 | + if (!ruleExtensions.has(extname(filename))) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + const absolutePath = resolve(RULES_DIR, filename); |
| 57 | + const ruleName = basename(absolutePath, extname(absolutePath)); |
| 58 | + if (rulesModules[ruleName]) { |
| 59 | + throw new Error( |
| 60 | + `eslint-plugin-rulesdir found two rules with the same name: ${ruleName}`, |
| 61 | + ); |
| 62 | + } |
| 63 | + const moduleURL = |
| 64 | + process.platform === 'win32' |
| 65 | + ? pathToFileURL(absolutePath).href |
| 66 | + : absolutePath; |
| 67 | + const loadedRule = await import(moduleURL); |
| 68 | + rulesModules[ruleName] = loadedRule.default ? loadedRule.default : loadedRule; |
| 69 | +} |
| 70 | + |
| 71 | +export default { |
| 72 | + get rules() { |
| 73 | + return rulesModules; |
| 74 | + }, |
| 75 | +}; |
0 commit comments