Skip to content

Commit 224d862

Browse files
committed
refactor: fix no-require-imports eslint warning (#239)
Removed `require`s in favor of `import`. Kept it in a couple of places where using import was not possible. Also replaced the `require('fake-module')` in favor of simulating a nodejs MODULE_NOT_FOUND error for testing
1 parent dc2e8f7 commit 224d862

9 files changed

+26
-9
lines changed

eslint.config.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ export default [
1717
},
1818
},
1919
{
20+
// overrides for cjs files
21+
files: ["*.js"],
2022
rules: {
21-
"@typescript-eslint/no-explicit-any": "off",
2223
"@typescript-eslint/no-require-imports": "off",
24+
},
25+
},
26+
{
27+
rules: {
28+
"@typescript-eslint/no-explicit-any": "off",
2329
"@typescript-eslint/no-unused-vars": "warn",
2430
},
2531
},

src/register.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ register.initialize = function initialize(): {
9696
} {
9797
let tsNode: typeof TSNode;
9898
try {
99+
// eslint-disable-next-line @typescript-eslint/no-require-imports
99100
tsNode = require("ts-node");
100101
} catch {
101102
throw new Error(

src/utils/ts-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function createSyntheticEmitHost(
8383
export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
8484
let tsNodeSymbol: typeof REGISTER_INSTANCE;
8585
try {
86+
// eslint-disable-next-line @typescript-eslint/no-require-imports
8687
tsNodeSymbol = require("ts-node")?.["REGISTER_INSTANCE"];
8788
} catch {
8889
return undefined;

test/config.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import ts from "typescript";
2-
import TypeScriptThree from "typescript-three";
3-
import TypeScriptFourSeven from "typescript-four-seven";
2+
import tsThree from "typescript-three";
3+
import tsFourSeven from "typescript-four-seven";
44
import path from "path";
55

66
/* ****************************************************************************************************************** */
77
// region: TS Instances
88
/* ****************************************************************************************************************** */
99

10-
export { ts };
11-
export const tsThree: typeof TypeScriptThree = require("typescript-three");
12-
export const tsFourSeven: typeof TypeScriptFourSeven = require("typescript-four-seven");
10+
export { ts, tsThree, tsFourSeven };
1311

1412
// endregion
1513

test/tests/extras.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createTsProgram, getEmitResultFromProgram } from "../utils";
1+
import { createTsProgram, getEmitResultFromProgram, ModuleNotFoundError } from "../utils";
22
import { projectsPaths } from "../config";
33
import path from "path";
44
import ts from "typescript";
@@ -21,7 +21,7 @@ describe(`Extra Tests`, () => {
2121
jest.doMock(
2222
"ts-node",
2323
() => {
24-
require("sdf0s39rf3333d@fake-module");
24+
throw new ModuleNotFoundError("ts-node");
2525
},
2626
{ virtual: true },
2727
);

test/tests/register.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as tsNode from "ts-node";
44
import * as transformerModule from "typescript-transform-paths/dist/transformer";
55
import { REGISTER_INSTANCE } from "ts-node";
66
import { CustomTransformers, PluginImport, Program } from "typescript";
7+
import { ModuleNotFoundError } from "../utils";
78

89
/* ****************************************************************************************************************** *
910
* Config
@@ -91,7 +92,7 @@ describe(`Register script`, () => {
9192
jest.doMock(
9293
"ts-node",
9394
() => {
94-
require("sdf0s39rf3333d@fake-module");
95+
throw new ModuleNotFoundError("ts-node");
9596
},
9697
{ virtual: true },
9798
);

test/utils/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export function getTsNodeEmitResult(
185185
const compiler = tsNode.create({
186186
transpileOnly: true,
187187
transformers: {
188+
// eslint-disable-next-line @typescript-eslint/no-require-imports
188189
before: [tstpTransform(void 0, pluginConfig, <any>{ ts: require(tsSpecifier) })],
189190
},
190191
project: pcl.options.configFilePath,

test/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./helpers";
2+
export * from "./module-not-found-error";

test/utils/module-not-found-error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** Mimicks a module not found nodejs error, see https://nodejs.org/docs/v20.16.0/api/errors.html */
2+
export class ModuleNotFoundError extends Error {
3+
code = "MODULE_NOT_FOUND";
4+
5+
constructor(packageName: string, options?: ErrorOptions) {
6+
super(`Uncaught Error: Cannot find module '${packageName}'`, options);
7+
}
8+
}

0 commit comments

Comments
 (0)