Skip to content

Commit

Permalink
refact: fix no-require-imports eslint warning (#239)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danielpza authored Aug 8, 2024
1 parent f984dc7 commit 5402e25
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 9 deletions.
8 changes: 7 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ export default [
},
},
{
// overrides for cjs files
files: ["*.js"],
rules: {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-require-imports": "off",
},
},
{
rules: {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "warn",
},
},
Expand Down
1 change: 1 addition & 0 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ register.initialize = function initialize(): {
} {
let tsNode: typeof TSNode;
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
tsNode = require("ts-node");
} catch {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions src/utils/ts-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function createSyntheticEmitHost(
export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
let tsNodeSymbol: typeof REGISTER_INSTANCE;
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
tsNodeSymbol = require("ts-node")?.["REGISTER_INSTANCE"];
} catch {
return undefined;
Expand Down
8 changes: 3 additions & 5 deletions test/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import ts from "typescript";
import TypeScriptThree from "typescript-three";
import TypeScriptFourSeven from "typescript-four-seven";
import tsThree from "typescript-three";
import tsFourSeven from "typescript-four-seven";
import path from "path";

/* ****************************************************************************************************************** */
// region: TS Instances
/* ****************************************************************************************************************** */

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

// endregion

Expand Down
4 changes: 2 additions & 2 deletions test/tests/extras.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createTsProgram, getEmitResultFromProgram } from "../utils";
import { createTsProgram, getEmitResultFromProgram, ModuleNotFoundError } from "../utils";
import { projectsPaths } from "../config";
import path from "path";
import ts from "typescript";
Expand All @@ -21,7 +21,7 @@ describe(`Extra Tests`, () => {
jest.doMock(
"ts-node",
() => {
require("sdf0s39rf3333d@fake-module");
throw new ModuleNotFoundError("ts-node");
},
{ virtual: true },
);
Expand Down
3 changes: 2 additions & 1 deletion test/tests/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as tsNode from "ts-node";
import * as transformerModule from "typescript-transform-paths/dist/transformer";
import { REGISTER_INSTANCE } from "ts-node";
import { CustomTransformers, PluginImport, Program } from "typescript";
import { ModuleNotFoundError } from "../utils";

/* ****************************************************************************************************************** *
* Config
Expand Down Expand Up @@ -91,7 +92,7 @@ describe(`Register script`, () => {
jest.doMock(
"ts-node",
() => {
require("sdf0s39rf3333d@fake-module");
throw new ModuleNotFoundError("ts-node");
},
{ virtual: true },
);
Expand Down
1 change: 1 addition & 0 deletions test/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export function getTsNodeEmitResult(
const compiler = tsNode.create({
transpileOnly: true,
transformers: {
// eslint-disable-next-line @typescript-eslint/no-require-imports
before: [tstpTransform(void 0, pluginConfig, <any>{ ts: require(tsSpecifier) })],
},
project: pcl.options.configFilePath,
Expand Down
1 change: 1 addition & 0 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./helpers";
export * from "./module-not-found-error";
8 changes: 8 additions & 0 deletions test/utils/module-not-found-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Mimicks a module not found nodejs error, see https://nodejs.org/docs/v20.16.0/api/errors.html */
export class ModuleNotFoundError extends Error {
code = "MODULE_NOT_FOUND";

constructor(packageName: string, options?: ErrorOptions) {
super(`Uncaught Error: Cannot find module '${packageName}'`, options);
}
}

0 comments on commit 5402e25

Please sign in to comment.