Skip to content

Commit

Permalink
chore: remove redundant assignments & use "createRequire" (#437)
Browse files Browse the repository at this point in the history
Redundant Assignments:

These lines are no longer needed because these fields are added by `createRule` function when the rules are initialized
```
rule.meta.docs.ruleName = ruleName
rule.meta.docs.ruleId = ruleId
```
Make it more ES module by using `createRequire`:

- No longer suppress the ESLint, the ESLint was warned for using `require` in a ts file. [@typescript-eslint/no-require-imports](https://typescript-eslint.io/rules/no-require-imports)
  • Loading branch information
Foxeye-Rinx authored Oct 25, 2024
1 parent 66f55f8 commit fd8bc14
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions tools/lib/load-rules.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import path from "path"
import fs from "fs"
import { createRequire } from "module"
import type { RuleModule } from "../../src/types"

const url = import.meta.url
const require = createRequire(url)

/**
* Get the all rules
* @returns {Array} The all rules
* Import all rules from `src/rules` and return them as an array.
* @returns {RuleModule[]}
*/
function readRules() {
const rulesLibRoot = path.resolve(__dirname, "../../src/rules")
const rules = []
for (const name of fs
.readdirSync(rulesLibRoot)
.filter((n) => n.endsWith(".ts"))) {
const ruleName = name.replace(/\.ts$/u, "")
const ruleId = `astro/${ruleName}`
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- ignore
const rule = require(path.join(rulesLibRoot, name)).default

rule.meta.docs.ruleName = ruleName
rule.meta.docs.ruleId = ruleId

rules.push(rule)
}
return rules
function readRules(): RuleModule[] {
const rulesPath = path.resolve(__dirname, "../../src/rules")
return fs
.readdirSync(rulesPath)
.filter((n) => n.endsWith(".ts"))
.map((fileName) => require(path.join(rulesPath, fileName)).default)
}

export const rules = readRules()

0 comments on commit fd8bc14

Please sign in to comment.