Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra(unicorn): prefer-module #2510

Merged
merged 14 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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}`);
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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