Skip to content

Commit

Permalink
infra(unicorn): prefer-module (#2510)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Nov 14, 2023
1 parent 7e3c92e commit 932a875
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Expand Up @@ -56,7 +56,6 @@ module.exports = defineConfig({
'unicorn/numeric-separators-style': 'off',
'unicorn/prefer-code-point': 'off',
'unicorn/prefer-export-from': 'off',
'unicorn/prefer-module': 'off',
'unicorn/prefer-string-slice': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/require-array-join-separator': 'off',
Expand Down
5 changes: 3 additions & 2 deletions scripts/apidoc/utils.ts
@@ -1,5 +1,6 @@
import { createHash } from 'node:crypto';
import { resolve } from 'node:path';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';

// Types
Expand Down Expand Up @@ -31,7 +32,7 @@ export interface DocsApiDiff {

// Paths

const pathRoot = resolve(__dirname, '..', '..');
const pathRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..');
export const pathDocsDir = resolve(pathRoot, 'docs');
const pathPublicDir = resolve(pathDocsDir, 'public');
export const nameDocsDiffIndexFile = 'api-diff-index.json';
Expand Down
13 changes: 7 additions & 6 deletions scripts/generate-locales.ts
Expand Up @@ -21,15 +21,16 @@ import {
readFileSync,
writeFileSync,
} from 'node:fs';
import { resolve } from 'node:path';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Options } from 'prettier';
import { format } from 'prettier';
import options from '../.prettierrc.js';
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';

// Constants

const pathRoot = resolve(__dirname, '..');
const pathRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const pathLocale = resolve(pathRoot, 'src', 'locale');
const pathLocales = resolve(pathRoot, 'src', 'locales');
const pathLocaleIndex = resolve(pathLocale, 'index.ts');
Expand Down Expand Up @@ -359,8 +360,8 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
}

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

// We reattach the content before the actual data implementation to keep stuff like comments.
// In the long term we should probably define a whether we want those in the files at all.
Expand Down Expand Up @@ -388,8 +389,8 @@ async function main(): Promise<void> {
const pathMetadata = resolve(pathModules, 'metadata.ts');
let localeTitle = 'No title found';
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const metadata: MetadataDefinition = require(pathMetadata).default;
const metadataImport = await import(`file:${pathMetadata}`);
const metadata: MetadataDefinition = metadataImport.default;
const { title } = metadata;
if (!title) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion test/all-functional.spec.ts
Expand Up @@ -73,7 +73,7 @@ const BROKEN_LOCALE_METHODS = {
jobType: ['ur'],
},
} satisfies {
[module in keyof Faker]?: SkipConfig<Faker[module]>;
[module_ in keyof Faker]?: SkipConfig<Faker[module_]>;
};

function isWorkingLocaleForMethod(
Expand Down
2 changes: 1 addition & 1 deletion test/faker.spec.ts
Expand Up @@ -19,7 +19,7 @@ describe('faker', () => {
vi.spyOn(console, methodName as keyof typeof console)
);

// eslint-disable-next-line @typescript-eslint/no-var-requires
// 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.
require('..').faker;

new Faker({ locale: { metadata: { title: '' } } });
Expand Down
2 changes: 1 addition & 1 deletion test/locale-imports.spec.ts
Expand Up @@ -4,7 +4,7 @@ import { allLocales } from '../src';

describe.each(Object.keys(allLocales))('locale imports', (locale) => {
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const { faker } = require(`../dist/cjs/locale/${locale}`) as {
faker: Faker;
};
Expand Down
5 changes: 3 additions & 2 deletions test/scripts/apidoc/verify-jsdoc-tags.spec.ts
@@ -1,5 +1,6 @@
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { ReflectionType, SomeType } from 'typedoc';
import validator from 'validator';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
Expand All @@ -25,7 +26,7 @@ import { loadProjectModules } from './utils';

beforeAll(initMarkdownRenderer);

const tempDir = resolve(__dirname, 'temp');
const tempDir = resolve(dirname(fileURLToPath(import.meta.url)), 'temp');

afterAll(() => {
// Remove temp folder
Expand Down
2 changes: 1 addition & 1 deletion test/simple-faker.spec.ts
Expand Up @@ -10,7 +10,7 @@ describe('simpleFaker', () => {
vi.spyOn(console, methodName as keyof typeof console)
);

// eslint-disable-next-line @typescript-eslint/no-var-requires
// 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.
require('..').simpleFaker;

new SimpleFaker();
Expand Down

0 comments on commit 932a875

Please sign in to comment.