Skip to content

Commit

Permalink
refactor: fixed biomed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 30, 2024
1 parent 6226ab6 commit c96937d
Show file tree
Hide file tree
Showing 22 changed files with 37 additions and 49 deletions.
4 changes: 1 addition & 3 deletions assets/namespace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export type NullableListFilter<T> = {
};

/** A type to determine how to update a json field */
export type UpdateInput<T> = T extends object
? { [P in keyof T]?: UpdateInput<T[P]> }
: T;
export type UpdateInput<T> = T extends object ? { [P in keyof T]?: UpdateInput<T[P]> } : T;

/** A type to determine how to update a json[] field */
export type UpdateManyInput<T> = T | T[] | { set?: T[]; push?: T | T[] };
Expand Down
9 changes: 4 additions & 5 deletions src/handler/model-payload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from 'typescript';
import type { PrismaEntity } from '../helpers/dmmf';
import { PrismaJsonTypesGeneratorConfig } from '../util/config';
import type { PrismaJsonTypesGeneratorConfig } from '../util/config';
import type { DeclarationWriter } from '../util/declaration-writer';
import { PrismaJsonTypesGeneratorError } from '../util/error';
import { replaceObject } from './replace-object';
Expand All @@ -15,10 +15,9 @@ export function handleModelPayload(
const type = typeAlias.type as ts.TypeLiteralNode;

if (type.kind !== ts.SyntaxKind.TypeLiteral) {
throw new PrismaJsonTypesGeneratorError(
'Provided model payload is not a type literal',
{ type: type.getText() }
);
throw new PrismaJsonTypesGeneratorError('Provided model payload is not a type literal', {
type: type.getText()
});
}

const scalarsField: any = type.members.find((m) => m.name?.getText() === 'scalars');
Expand Down
8 changes: 3 additions & 5 deletions src/handler/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ts from 'typescript';
import type { PrismaEntity } from '../helpers/dmmf';
import { PrismaJsonTypesGeneratorConfig } from '../util/config';
import type { PrismaJsonTypesGeneratorConfig } from '../util/config';
import { PRISMA_NAMESPACE_NAME } from '../util/constants';
import { DeclarationWriter } from '../util/declaration-writer';
import type { DeclarationWriter } from '../util/declaration-writer';
import { PrismaJsonTypesGeneratorError } from '../util/error';
import { handleStatement } from './statement';

Expand All @@ -27,9 +27,7 @@ export function handlePrismaModule(
.find((n): n is ts.ModuleBlock => n.kind === ts.SyntaxKind.ModuleBlock);

if (!content || !content.statements.length) {
throw new PrismaJsonTypesGeneratorError(
'Prisma namespace content could not be found'
);
throw new PrismaJsonTypesGeneratorError('Prisma namespace content could not be found');
}

// Loops through all statements in the prisma namespace
Expand Down
4 changes: 2 additions & 2 deletions src/handler/replace-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ts from 'typescript';
import type { PrismaEntity } from '../helpers/dmmf';
import { findNewSignature } from '../helpers/find-signature';
import { JSON_REGEX } from '../helpers/regex';
import { PrismaJsonTypesGeneratorConfig } from '../util/config';
import type { PrismaJsonTypesGeneratorConfig } from '../util/config';
import { createType } from '../util/create-signature';
import type { DeclarationWriter } from '../util/declaration-writer';
import { PrismaJsonTypesGeneratorError } from '../util/error';
Expand Down Expand Up @@ -39,7 +39,7 @@ export function replaceObject(
const signature = (member as ts.PropertySignature).type;

if (!signature) {
throw new PrismaJsonTypesGeneratorError(`Could not find signature type`, {
throw new PrismaJsonTypesGeneratorError('Could not find signature type', {
type: field.name
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/handler/statement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ts from 'typescript';
import type { PrismaEntity } from '../helpers/dmmf';
import { PrismaJsonTypesGeneratorConfig } from '../util/config';
import { DeclarationWriter } from '../util/declaration-writer';
import type { PrismaJsonTypesGeneratorConfig } from '../util/config';
import type { DeclarationWriter } from '../util/declaration-writer';
import { handleModelPayload } from './model-payload';
import { replaceObject } from './replace-object';

Expand Down
5 changes: 2 additions & 3 deletions src/helpers/find-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export function findNewSignature(

return `TypedStringNullableWithAggregatesFilter<${typeToChange}> | ${typeToChange}`;

case `StringFieldUpdateOperationsInput | string`:
case 'StringFieldUpdateOperationsInput | string':
if (!shouldReplaceStrings) {
return undefined;
}

return `TypedStringFieldUpdateOperationsInput<${typeToChange}> | ${typeToChange}`;

case `NullableStringFieldUpdateOperationsInput | string | null`:
case 'NullableStringFieldUpdateOperationsInput | string | null':
if (!shouldReplaceStrings) {
return undefined;
}
Expand Down Expand Up @@ -127,7 +127,6 @@ export function findNewSignature(
case 'InputJsonValue | InputJsonValue | null':
return `${typeToChange} | null`;

case 'JsonNullValueInput | InputJsonValue':
case 'NullableJsonNullValueInput | InputJsonValue':
// differentiates null in column or a json null value
return `${typeToChange} | NullableJsonNullValueInput`;
Expand Down
4 changes: 1 addition & 3 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ export function parseConfig(

useType: config.useType ? String(config.useType) : undefined,

allowAny: config.allowAny
? String(config.allowAny).toLowerCase().trim() === 'true'
: false
allowAny: config.allowAny ? String(config.allowAny).toLowerCase().trim() === 'true' : false
};
}
2 changes: 1 addition & 1 deletion src/util/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';

/** Name of the namespace in the type declaration */
export const PRISMA_NAMESPACE_NAME = 'Prisma';
Expand Down
2 changes: 1 addition & 1 deletion src/util/create-signature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSON_REGEX, LITERAL_REGEX } from '../helpers/regex';
import { PrismaJsonTypesGeneratorConfig } from './config';
import type { PrismaJsonTypesGeneratorConfig } from './config';

/** Creates the new signature for the provided type. */
export function createType(
Expand Down
15 changes: 7 additions & 8 deletions src/util/declaration-writer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs/promises';
import { PrismaJsonTypesGeneratorConfig } from './config';
import fs from 'node:fs/promises';
import type { PrismaJsonTypesGeneratorConfig } from './config';
import { NAMESPACE_PATH } from './constants';
import { PrismaJsonTypesGeneratorError } from './error';

Expand Down Expand Up @@ -34,16 +34,15 @@ export class DeclarationWriter {
namespace = namespace.replace(/\$\$NAMESPACE\$\$/g, this.options.namespace);

// Includes previous file content
return namespace + '\n' + this.content;
return `${namespace}\n${this.content}`;
}

/** Loads the original file of sourcePath into memory. */
async load() {
if (!(await fs.stat(this.filepath))) {
throw new PrismaJsonTypesGeneratorError(
'Tried to load a file that does not exist',
{ filepath: this.filepath }
);
throw new PrismaJsonTypesGeneratorError('Tried to load a file that does not exist', {
filepath: this.filepath
});
}

if (this.changeset.length) {
Expand Down Expand Up @@ -87,7 +86,7 @@ export class DeclarationWriter {
replace(start: number, end: number, text: string) {
// Adds a trailing space
if (text[0] !== ' ') {
text = ' ' + text;
text = ` ${text}`;
}

// Maps the coordinates to the previous changes to adjust the position for the new text
Expand Down
7 changes: 2 additions & 5 deletions src/util/prisma-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeneratorConfig } from '@prisma/generator-helper';
import type { GeneratorConfig } from '@prisma/generator-helper';
import { PrismaJsonTypesGeneratorError } from './error';

/**
Expand All @@ -15,10 +15,7 @@ export function findPrismaClientGenerator(generators: GeneratorConfig[]) {
}

if (!options.output?.value) {
throw new PrismaJsonTypesGeneratorError(
'`prisma-client-js` output not found',
options
);
throw new PrismaJsonTypesGeneratorError('`prisma-client-js` output not found', options);
}

return options as GeneratorConfig & { output: { value: string } };
Expand Down
2 changes: 1 addition & 1 deletion src/util/source-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';

/**
* Builds the full path to the correct Prisma Client types file.
Expand Down
2 changes: 1 addition & 1 deletion test/types/any.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectNotType, expectType } from 'tsd';
import { Model, Prisma } from '../target/any/index';
import type { Model, Prisma } from '../target/any/index';

expectType<Model>({
id: 0,
Expand Down
2 changes: 1 addition & 1 deletion test/types/cockroach.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { Model, Text } from '../target/cockroach/index';
import type { Model, Text } from '../target/cockroach/index';

declare global {
export namespace PCockroachJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/literal.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import { Model, UpdateManyInput } from '../target/literal/index';
import type { Model, UpdateManyInput } from '../target/literal/index';

expectAssignable<Model>({
id: 0,
Expand Down
2 changes: 1 addition & 1 deletion test/types/mongo.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { Model, Text, UpdateManyInput } from '../target/mongo/index';
import type { Model, Text, UpdateManyInput } from '../target/mongo/index';

declare global {
export namespace PMongoJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/mssql.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectNotType, expectType } from 'tsd';
import { Text } from '../target/mssql/index';
import type { Text } from '../target/mssql/index';

declare global {
export namespace PMssqlJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/mysql.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { Model, Text } from '../target/mysql/index';
import type { Model, Text } from '../target/mysql/index';

declare global {
export namespace PMysqlJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/normal.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import { Model, UpdateManyInput } from '../target/normal/index';
import type { Model, UpdateManyInput } from '../target/normal/index';

declare global {
export namespace PNormalJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/sqlite.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectNotType, expectType } from 'tsd';
import { Text } from '../target/sqlite/index';
import type { Text } from '../target/sqlite/index';

declare global {
export namespace PSqliteJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/string.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectNotType, expectType } from 'tsd';
import { Model } from '../target/string/index';
import type { Model } from '../target/string/index';

declare global {
export namespace PStringJson {
Expand Down
2 changes: 1 addition & 1 deletion test/types/unknown.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectNotType, expectType } from 'tsd';
import { Model } from '../target/unknown/index';
import type { Model } from '../target/unknown/index';

expectType<Model>({
id: 0,
Expand Down

0 comments on commit c96937d

Please sign in to comment.