Skip to content

Commit

Permalink
feat: add typescript error check utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jan 18, 2024
1 parent a8ab89d commit 0735473
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/loader/built-in/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import {
} from '../../../locator';
import {
handleException,
hasStringProperty, isFilePath,
hasStringProperty,
isFilePath,
isObject,
isTsNodeRuntimeEnvironment,
isTypeScriptError,
} from '../../../utils';
import type { Loader } from '../../type';
import type { ModuleLoadOptions } from './type';
import { isTsNodeRuntimeEnvironment, toModuleRecord } from './utils';
import { toModuleRecord } from './utils';

export class ModuleLoader implements Loader {
protected jiti : JITI;
Expand All @@ -42,7 +45,8 @@ export class ModuleLoader implements Loader {
} catch (e) {
if (
e instanceof SyntaxError ||
e instanceof ReferenceError
e instanceof ReferenceError ||
isTypeScriptError(e)
) {
throw e;
}
Expand All @@ -67,7 +71,8 @@ export class ModuleLoader implements Loader {
} catch (e) {
if (
e instanceof SyntaxError ||
e instanceof ReferenceError
e instanceof ReferenceError ||
isTypeScriptError(e)
) {
throw e;
}
Expand Down
11 changes: 0 additions & 11 deletions src/loader/built-in/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
import { hasOwnProperty, isObject } from '../../../utils';
import type { LoaderFilterFn, ModuleExport } from './type';

export function isJestRuntimeEnvironment() : boolean {
return process.env &&
process.env.JEST_WORKER_ID !== undefined;
}

export function isTsNodeRuntimeEnvironment() : boolean {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return !!process[Symbol.for('ts-node.register.instance')];
}

type ESModule = { [key: string]: any, __esModule: boolean };
export function isESModule(input: unknown) : input is ESModule {
return isObject(input) &&
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export * from './has-property';
export * from './object';
export * from './to-array';
export * from './file-path';
export * from './runtime';
export * from './typescript';
17 changes: 17 additions & 0 deletions src/utils/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export function isJestRuntimeEnvironment() : boolean {
return process.env &&
process.env.JEST_WORKER_ID !== undefined;
}

export function isTsNodeRuntimeEnvironment(): boolean {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return !!process[Symbol.for('ts-node.register.instance')];
}
21 changes: 21 additions & 0 deletions src/utils/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import { isObject } from './object';

export function isTypeScriptError(input: unknown) : input is Error {
if (!isObject(input)) {
return false;
}

if (typeof input.diagnosticCodes !== 'undefined') {
return true;
}

return typeof input.message === 'string' &&
/TS\d+/.test(input.message);
}
3 changes: 1 addition & 2 deletions test/unit/loader/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import {
LoaderManager,
getModuleExport,
load,
loadSync,
load, loadSync,
} from '../../../src';
import { LoaderId } from '../../../src/loader/constants';

Expand Down
7 changes: 6 additions & 1 deletion test/unit/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

import { removeFileNameExtension } from '../../src';
import { isJestRuntimeEnvironment, removeFileNameExtension } from '../../src';

describe('src/utils/*.ts', () => {
it('should remove file name extension', () => {
Expand All @@ -21,4 +21,9 @@ describe('src/utils/*.ts', () => {
data = removeFileNameExtension('test.js');
expect(data).toEqual('test');
});

it('should detect environment', () => {
const jestEnv = isJestRuntimeEnvironment();
expect(jestEnv).toEqual(true);
});
});

0 comments on commit 0735473

Please sign in to comment.