import fs from "fs"; import type { InvalidTestCase, ValidTestCase, } from "@typescript-eslint/rule-tester"; import { create } from "enhanced-resolve"; const resolver = create.sync({ extensions: [".ts", ".tsx", ".js", ".jsx"], }); export function getCode( dirname: string, name: string, options: TOpts, ): ValidTestCase; export function getCode( dirname: string, name: string, options: TOpts, expectedError: TIds, repeat?: number, ): InvalidTestCase; export function getCode( dirname: string, name: string, options: TOpts, expectedError?: TIds, repeat?: number, ): ValidTestCase | InvalidTestCase { const resolvedPath = resolver(dirname, `./${name}`); if (!resolvedPath) { throw new Error("getCode(): Unable to resolve path"); } const base = { name, code: fs.readFileSync(resolvedPath, "utf-8"), filename: resolvedPath, options: options, }; if (!expectedError) { return base; } const expectedValuePath = resolver(dirname, `./${name}.expected`); if (!expectedValuePath) { throw new Error("getCode(): Unable to resolve expected value path"); } return { ...base, errors: new Array(repeat ?? 1).fill({ messageId: expectedError }), output: fs.readFileSync(expectedValuePath, "utf-8"), }; }