From 59e6f8dcbdc96b65d43a6e423db6bff3093076cb Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 8 Aug 2024 12:37:20 -0400 Subject: [PATCH 1/3] refact: fix no-require-imports eslint warning --- eslint.config.mjs | 8 +++++++- src/register.ts | 1 + src/utils/ts-helpers.ts | 1 + test/config.ts | 8 +++----- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index daaf9f61..0f4f8886 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -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", }, }, diff --git a/src/register.ts b/src/register.ts index d951da8a..1da39f14 100755 --- a/src/register.ts +++ b/src/register.ts @@ -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( diff --git a/src/utils/ts-helpers.ts b/src/utils/ts-helpers.ts index fd895b86..cf784059 100755 --- a/src/utils/ts-helpers.ts +++ b/src/utils/ts-helpers.ts @@ -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; diff --git a/test/config.ts b/test/config.ts index 11e8b834..5b081415 100755 --- a/test/config.ts +++ b/test/config.ts @@ -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 From 07e1ad73b1786b7a0e14fdc2dbc01a2e1c4e2c81 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 8 Aug 2024 12:40:52 -0400 Subject: [PATCH 2/3] remove sometimes and ignore others --- test/tests/extras.test.ts | 8 +------- test/tests/register.test.ts | 8 +------- test/utils/helpers.ts | 1 + 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/test/tests/extras.test.ts b/test/tests/extras.test.ts index 8cf715ff..eb0f262a 100755 --- a/test/tests/extras.test.ts +++ b/test/tests/extras.test.ts @@ -18,13 +18,7 @@ describe(`Extra Tests`, () => { describe(`Built Tests`, () => { // see: https://github.com/LeDDGroup/typescript-transform-paths/issues/130 test(`Transformer works without ts-node being present`, () => { - jest.doMock( - "ts-node", - () => { - require("sdf0s39rf3333d@fake-module"); - }, - { virtual: true }, - ); + jest.doMock("ts-node", () => ({}), { virtual: true }); try { const program = createTsProgram({ tsInstance: ts, tsConfigFile }, config.builtTransformerPath); const res = getEmitResultFromProgram(program); diff --git a/test/tests/register.test.ts b/test/tests/register.test.ts index e6ca3a88..2ca30de2 100755 --- a/test/tests/register.test.ts +++ b/test/tests/register.test.ts @@ -88,13 +88,7 @@ describe(`Register script`, () => { describe(`Register`, () => { test(`Throws without ts-node`, () => { - jest.doMock( - "ts-node", - () => { - require("sdf0s39rf3333d@fake-module"); - }, - { virtual: true }, - ); + jest.doMock("ts-node", () => ({}), { virtual: true }); expect(() => register()).toThrow(`Cannot resolve ts-node`); jest.dontMock("ts-node"); }); diff --git a/test/utils/helpers.ts b/test/utils/helpers.ts index 9fe02ffc..e7fe5ba5 100755 --- a/test/utils/helpers.ts +++ b/test/utils/helpers.ts @@ -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, { ts: require(tsSpecifier) })], }, project: pcl.options.configFilePath, From 7f111f93f231d3fce0493bfcf25764c109577be0 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 8 Aug 2024 12:49:42 -0400 Subject: [PATCH 3/3] simulate module not found error for testing --- test/tests/extras.test.ts | 10 ++++++++-- test/tests/register.test.ts | 9 ++++++++- test/utils/index.ts | 1 + test/utils/module-not-found-error.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/utils/module-not-found-error.ts diff --git a/test/tests/extras.test.ts b/test/tests/extras.test.ts index eb0f262a..cd326aca 100755 --- a/test/tests/extras.test.ts +++ b/test/tests/extras.test.ts @@ -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"; @@ -18,7 +18,13 @@ describe(`Extra Tests`, () => { describe(`Built Tests`, () => { // see: https://github.com/LeDDGroup/typescript-transform-paths/issues/130 test(`Transformer works without ts-node being present`, () => { - jest.doMock("ts-node", () => ({}), { virtual: true }); + jest.doMock( + "ts-node", + () => { + throw new ModuleNotFoundError("ts-node"); + }, + { virtual: true }, + ); try { const program = createTsProgram({ tsInstance: ts, tsConfigFile }, config.builtTransformerPath); const res = getEmitResultFromProgram(program); diff --git a/test/tests/register.test.ts b/test/tests/register.test.ts index 2ca30de2..5380d762 100755 --- a/test/tests/register.test.ts +++ b/test/tests/register.test.ts @@ -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 @@ -88,7 +89,13 @@ describe(`Register script`, () => { describe(`Register`, () => { test(`Throws without ts-node`, () => { - jest.doMock("ts-node", () => ({}), { virtual: true }); + jest.doMock( + "ts-node", + () => { + throw new ModuleNotFoundError("ts-node"); + }, + { virtual: true }, + ); expect(() => register()).toThrow(`Cannot resolve ts-node`); jest.dontMock("ts-node"); }); diff --git a/test/utils/index.ts b/test/utils/index.ts index d4e09d7b..8ec4d19a 100755 --- a/test/utils/index.ts +++ b/test/utils/index.ts @@ -1 +1,2 @@ export * from "./helpers"; +export * from "./module-not-found-error"; diff --git a/test/utils/module-not-found-error.ts b/test/utils/module-not-found-error.ts new file mode 100644 index 00000000..9defd405 --- /dev/null +++ b/test/utils/module-not-found-error.ts @@ -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); + } +}