From df688ea273124f6503528b5372f37ded7b41d072 Mon Sep 17 00:00:00 2001 From: anymaniax Date: Sat, 2 Apr 2022 10:03:34 +0200 Subject: [PATCH] feat(writers): header handling directly return string or eslint-disable --- src/core/writers/specs.ts | 20 +++++++++++++++----- src/types/index.ts | 4 ++-- src/utils/doc.ts | 11 +++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/core/writers/specs.ts b/src/core/writers/specs.ts index 7d3d7631a..a7cf510d6 100644 --- a/src/core/writers/specs.ts +++ b/src/core/writers/specs.ts @@ -3,6 +3,7 @@ import { log } from 'console'; import execa from 'execa'; import { appendFile, outputFile, pathExists, readFile } from 'fs-extra'; import uniq from 'lodash.uniq'; +import { InfoObject } from 'openapi3-ts'; import { join } from 'upath'; import { NormalizedOptions, OutputMode } from '../../types'; import { WriteSpecsProps } from '../../types/writers'; @@ -16,6 +17,19 @@ import { writeSplitMode } from './splitMode'; import { writeSplitTagsMode } from './splitTagsMode'; import { writeTagsMode } from './tagsMode'; +const getHeader = ( + option: false | ((info: InfoObject) => string | string[]), + info: InfoObject, +): string => { + if (!option) { + return ''; + } + + const header = option(info); + + return Array.isArray(header) ? jsDoc({ description: header }) : header; +}; + export const writeSpecs = async ( { operations, schemas, target, info }: WriteSpecsProps, workspace: string, @@ -34,11 +48,7 @@ export const writeSpecs = async ( return acc; }, {} as Record); - const header = output.override.header - ? jsDoc({ - description: output.override.header(info), - }) - : ''; + const header = getHeader(output.override.header, info); if (output.schemas) { const rootSchemaPath = output.schemas; diff --git a/src/types/index.ts b/src/types/index.ts index 59eea31b0..aa7e947b2 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -58,7 +58,7 @@ export type NormalizedOverrideOutput = { required?: boolean; baseUrl?: string; }; - header: false | ((info: InfoObject) => string[]); + header: false | ((info: InfoObject) => string[] | string); formData: boolean | NormalizedMutator; formUrlEncoded: boolean | NormalizedMutator; components: { @@ -210,7 +210,7 @@ export type OverrideOutput = { required?: boolean; baseUrl?: string; }; - header?: boolean | ((info: InfoObject) => string[]); + header?: boolean | ((info: InfoObject) => string[] | string); formData?: boolean | Mutator; formUrlEncoded?: boolean | Mutator; components?: { diff --git a/src/utils/doc.ts b/src/utils/doc.ts index 66c0efa42..ffd88def9 100644 --- a/src/utils/doc.ts +++ b/src/utils/doc.ts @@ -17,7 +17,9 @@ export function jsDoc( ): string { // Ensure there aren't any comment terminations in doc const lines = ( - Array.isArray(description) ? description : [description || ''] + Array.isArray(description) + ? description.filter((d) => !d.includes('eslint-disable')) + : [description || ''] ).map((line) => line.replace(regex, replacement)); const count = [description, deprecated, summary].reduce( @@ -30,7 +32,12 @@ export function jsDoc( } const oneLine = count === 1 && tryOneLine; - let doc = '/**'; + const eslintDisable = Array.isArray(description) + ? description + .find((d) => d.includes('eslint-disable')) + ?.replace(regex, replacement) + : undefined; + let doc = `${eslintDisable ? `/* ${eslintDisable} */\n` : ''}/**`; if (description) { if (!oneLine) {