Skip to content

Commit 932a875

Browse files
authored
infra(unicorn): prefer-module (#2510)
1 parent 7e3c92e commit 932a875

8 files changed

+17
-15
lines changed

.eslintrc.js

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ module.exports = defineConfig({
5656
'unicorn/numeric-separators-style': 'off',
5757
'unicorn/prefer-code-point': 'off',
5858
'unicorn/prefer-export-from': 'off',
59-
'unicorn/prefer-module': 'off',
6059
'unicorn/prefer-string-slice': 'off',
6160
'unicorn/prevent-abbreviations': 'off',
6261
'unicorn/require-array-join-separator': 'off',

scripts/apidoc/utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createHash } from 'node:crypto';
2-
import { resolve } from 'node:path';
2+
import { dirname, resolve } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
45

56
// Types
@@ -31,7 +32,7 @@ export interface DocsApiDiff {
3132

3233
// Paths
3334

34-
const pathRoot = resolve(__dirname, '..', '..');
35+
const pathRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..');
3536
export const pathDocsDir = resolve(pathRoot, 'docs');
3637
const pathPublicDir = resolve(pathDocsDir, 'public');
3738
export const nameDocsDiffIndexFile = 'api-diff-index.json';

scripts/generate-locales.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ import {
2121
readFileSync,
2222
writeFileSync,
2323
} from 'node:fs';
24-
import { resolve } from 'node:path';
24+
import { dirname, resolve } from 'node:path';
25+
import { fileURLToPath } from 'node:url';
2526
import type { Options } from 'prettier';
2627
import { format } from 'prettier';
2728
import options from '../.prettierrc.js';
2829
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';
2930

3031
// Constants
3132

32-
const pathRoot = resolve(__dirname, '..');
33+
const pathRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
3334
const pathLocale = resolve(pathRoot, 'src', 'locale');
3435
const pathLocales = resolve(pathRoot, 'src', 'locales');
3536
const pathLocaleIndex = resolve(pathLocale, 'index.ts');
@@ -359,8 +360,8 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
359360
}
360361

361362
const fileContentPreData = fileContent.substring(0, compareIndex);
362-
// eslint-disable-next-line @typescript-eslint/no-var-requires
363-
const localeData = normalizeDataRecursive(require(filePath).default);
363+
const fileImport = await import(`file:${filePath}`);
364+
const localeData = normalizeDataRecursive(fileImport.default);
364365

365366
// We reattach the content before the actual data implementation to keep stuff like comments.
366367
// In the long term we should probably define a whether we want those in the files at all.
@@ -388,8 +389,8 @@ async function main(): Promise<void> {
388389
const pathMetadata = resolve(pathModules, 'metadata.ts');
389390
let localeTitle = 'No title found';
390391
try {
391-
// eslint-disable-next-line @typescript-eslint/no-var-requires
392-
const metadata: MetadataDefinition = require(pathMetadata).default;
392+
const metadataImport = await import(`file:${pathMetadata}`);
393+
const metadata: MetadataDefinition = metadataImport.default;
393394
const { title } = metadata;
394395
if (!title) {
395396
throw new Error(

test/all-functional.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const BROKEN_LOCALE_METHODS = {
7373
jobType: ['ur'],
7474
},
7575
} satisfies {
76-
[module in keyof Faker]?: SkipConfig<Faker[module]>;
76+
[module_ in keyof Faker]?: SkipConfig<Faker[module_]>;
7777
};
7878

7979
function isWorkingLocaleForMethod(

test/faker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('faker', () => {
1919
vi.spyOn(console, methodName as keyof typeof console)
2020
);
2121

22-
// eslint-disable-next-line @typescript-eslint/no-var-requires
22+
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module -- Using import() requires types being build but the CI / TS-Check runs without them.
2323
require('..').faker;
2424

2525
new Faker({ locale: { metadata: { title: '' } } });

test/locale-imports.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { allLocales } from '../src';
44

55
describe.each(Object.keys(allLocales))('locale imports', (locale) => {
66
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
7-
// eslint-disable-next-line @typescript-eslint/no-var-requires
7+
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
88
const { faker } = require(`../dist/cjs/locale/${locale}`) as {
99
faker: Faker;
1010
};

test/scripts/apidoc/verify-jsdoc-tags.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
2-
import { resolve } from 'node:path';
2+
import { dirname, resolve } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import type { ReflectionType, SomeType } from 'typedoc';
45
import validator from 'validator';
56
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
@@ -25,7 +26,7 @@ import { loadProjectModules } from './utils';
2526

2627
beforeAll(initMarkdownRenderer);
2728

28-
const tempDir = resolve(__dirname, 'temp');
29+
const tempDir = resolve(dirname(fileURLToPath(import.meta.url)), 'temp');
2930

3031
afterAll(() => {
3132
// Remove temp folder

test/simple-faker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('simpleFaker', () => {
1010
vi.spyOn(console, methodName as keyof typeof console)
1111
);
1212

13-
// eslint-disable-next-line @typescript-eslint/no-var-requires
13+
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module -- Using import() requires types being build but the CI / TS-Check runs without them.
1414
require('..').simpleFaker;
1515

1616
new SimpleFaker();

0 commit comments

Comments
 (0)