From a4090f534a511170c5cf80c1a7f6f0e217272b50 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 6 Jul 2020 23:31:28 +0300 Subject: [PATCH] chore: prettier all --- __tests__/index.test.ts | 17 +++++++------- __tests__/specific-snapshot.js | 6 ++--- resources/local-plugin.js | 2 +- src/__tests__/utils.ts | 2 +- src/cli.ts | 20 ++++++++++++----- src/cli/preview-docs.ts | 6 ++--- src/cli/preview-server/hot.js | 2 +- src/cli/preview-server/preview-server.ts | 2 +- src/cli/preview-server/server.ts | 2 +- src/config/all.ts | 2 +- src/config/config.ts | 2 +- src/config/recommended.ts | 2 +- src/format/format.ts | 11 +++++++++- src/index.ts | 2 +- src/ref-utils.ts | 22 ++++++++++--------- src/rules/ajv.ts | 2 +- src/rules/common/info-contact.ts | 2 +- src/rules/common/info-description.ts | 2 +- src/rules/common/no-enum-type-mismatch.ts | 2 +- src/rules/common/spec.ts | 2 +- src/rules/no-unresolved-refs.ts | 4 ++-- .../oas2/__tests__/spec/operation.test.ts | 6 ++--- .../oas3/__tests__/spec/operation.test.ts | 6 ++--- .../spec/referenceableScalars.test.ts | 16 +++++++------- .../oas3/no-invalid-media-type-examples.ts | 2 +- .../oas3/no-undefined-server-variable.ts | 12 +++++----- src/types/index.ts | 5 ++--- src/typings/swagger.ts | 1 - src/visitors.ts | 1 - src/walk.ts | 10 ++++++--- 30 files changed, 95 insertions(+), 78 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 68f5a026..f841b62e 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -1,9 +1,8 @@ import { readdirSync, statSync, existsSync } from 'fs'; import { spawnSync } from 'child_process'; -import {join} from 'path'; +import { join } from 'path'; import { toMatchSpecificSnapshot, addSerializer } from './specific-snapshot'; - expect.extend({ toMatchExtendedSpecificSnapshot(received, snapshotFile) { return toMatchSpecificSnapshot.call(this, received + 1, snapshotFile); @@ -27,19 +26,19 @@ describe('E2E', () => { cwd: testPath, env: { ...process.env, - 'NODE_ENV': 'test', - 'NO_COLOR': 'TRUE', - } + NODE_ENV: 'test', + NO_COLOR: 'TRUE', + }, }); const out = r.stdout.toString('utf-8'); - const err = r.stderr.toString('utf-8') + const err = r.stderr.toString('utf-8'); - const result = `${out}\n${err}` + const result = `${out}\n${err}`; it(file, () => { // we need this cause TS types not actually allows to 'extend' (expect(result) as any).toMatchSpecificSnapshot(join(testPath, 'snapshot.js')); - }) + }); } -}); \ No newline at end of file +}); diff --git a/__tests__/specific-snapshot.js b/__tests__/specific-snapshot.js index 1b1bacde..e07daa19 100644 --- a/__tests__/specific-snapshot.js +++ b/__tests__/specific-snapshot.js @@ -1,7 +1,6 @@ const { SnapshotState, toMatchSnapshot, addSerializer } = require('jest-snapshot'); const path = require('path'); - const snapshotsStateMap = new Map(); let commonSnapshotState; @@ -28,7 +27,6 @@ function toMatchSpecificSnapshot(received, snapshotFile, ...rest) { expect.extend({ toMatchSpecificSnapshot }); - function getAbsolutePathToSnapshot(testPath, snapshotFile) { return path.isAbsolute(snapshotFile) ? snapshotFile @@ -36,7 +34,7 @@ function getAbsolutePathToSnapshot(testPath, snapshotFile) { } afterAll(() => { - snapshotsStateMap.forEach(snapshotState => { + snapshotsStateMap.forEach((snapshotState) => { const uncheckedCount = snapshotState.getUncheckedCount(); if (uncheckedCount) { @@ -56,4 +54,4 @@ afterAll(() => { }); }); -module.exports = { addSerializer, toMatchSpecificSnapshot }; \ No newline at end of file +module.exports = { addSerializer, toMatchSpecificSnapshot }; diff --git a/resources/local-plugin.js b/resources/local-plugin.js index 8ca6c14a..7664c9db 100644 --- a/resources/local-plugin.js +++ b/resources/local-plugin.js @@ -41,7 +41,7 @@ export const decorators = { 'inject-x-stats': () => { return { Info(info) { - info['x-stats'] = {test: 1} + info['x-stats'] = { test: 1 }; }, }; }, diff --git a/src/__tests__/utils.ts b/src/__tests__/utils.ts index 08ba0a91..94e480dc 100644 --- a/src/__tests__/utils.ts +++ b/src/__tests__/utils.ts @@ -29,7 +29,7 @@ export function replaceSourceWithRef(results: NormalizedReportMessage[], cwd?: s ...mapped.from, source: cwd ? path.relative(cwd, mapped.from.source.absoluteRef) - : mapped.from.source.absoluteRef as any, + : (mapped.from.source.absoluteRef as any), }; } return mapped; diff --git a/src/cli.ts b/src/cli.ts index 5ec76dc9..d7d78f04 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -99,7 +99,10 @@ yargs // eslint-disable-line }); } - const elapsed = process.env.NODE_ENV === 'test' ? 'ms': `in ${Math.ceil(performance.now() - startedAt)}ms`; + const elapsed = + process.env.NODE_ENV === 'test' + ? 'ms' + : `in ${Math.ceil(performance.now() - startedAt)}ms`; process.stderr.write(gray(`${entryPoint}: validated in ${elapsed}\n\n`)); } catch (e) { totals.errors++; @@ -421,7 +424,10 @@ function pluralize(label: string, num: number) { return num === 1 ? `${label}` : `${label}s`; } -export function getFallbackEntryPointsOrExit(argsEntrypoints: string[] | undefined, config: Config) { +export function getFallbackEntryPointsOrExit( + argsEntrypoints: string[] | undefined, + config: Config, +) { let res = argsEntrypoints; if ( (!argsEntrypoints || !argsEntrypoints.length) && @@ -445,15 +451,17 @@ function printUnusedWarnings(config: LintConfig) { const { preprocessors, rules, decorators } = config.getUnusedRules(); if (rules.length) { process.stderr.write( - yellow(`[WARNING] Unused rules found in ${blue(config.configFile || '')}: ${rules.join(', ')}.\n`), + yellow( + `[WARNING] Unused rules found in ${blue(config.configFile || '')}: ${rules.join(', ')}.\n`, + ), ); } if (preprocessors.length) { process.stderr.write( yellow( - `[WARNING] Unused preprocessors found in ${blue(config.configFile || '')}: ${preprocessors.join( - ', ', - )}.\n`, + `[WARNING] Unused preprocessors found in ${blue( + config.configFile || '', + )}: ${preprocessors.join(', ')}.\n`, ), ); } diff --git a/src/cli/preview-docs.ts b/src/cli/preview-docs.ts index 14beca70..20e43e7a 100644 --- a/src/cli/preview-docs.ts +++ b/src/cli/preview-docs.ts @@ -7,7 +7,7 @@ import { getFallbackEntryPointsOrExit, getTotals } from '../cli'; import startPreviewServer from './preview-server/preview-server'; export async function previewDocs(argv: { - port: number, + port: number; useCommunityEdition?: boolean; config?: string; entrypoint: string; @@ -48,7 +48,7 @@ export async function previewDocs(argv: { process.stdout.write( fileTotals.errors === 0 ? `Created a bundle for ${entrypoint} ${ - fileTotals.warnings > 0 ? 'with warnings' : 'successfully' + fileTotals.warnings > 0 ? 'with warnings' : 'successfully' }\n` : colorette.yellow( `Created a bundle for ${entrypoint} with errors. Docs may be broken or not accurate\n`, @@ -144,4 +144,4 @@ export function debounce(func: Function, wait: number, immediate?: boolean) { if (callNow) func.apply(context, args); }; -} \ No newline at end of file +} diff --git a/src/cli/preview-server/hot.js b/src/cli/preview-server/hot.js index 92925d93..dffb6a4b 100644 --- a/src/cli/preview-server/hot.js +++ b/src/cli/preview-server/hot.js @@ -40,4 +40,4 @@ socket.destroy(); }); } -}()); \ No newline at end of file +})(); diff --git a/src/cli/preview-server/preview-server.ts b/src/cli/preview-server/preview-server.ts index 2015754c..16e4785d 100644 --- a/src/cli/preview-server/preview-server.ts +++ b/src/cli/preview-server/preview-server.ts @@ -87,7 +87,7 @@ export default async function startPreviewServer( }[request.url || ''] || path.resolve(htmlTemplate ? path.dirname(htmlTemplate) : process.cwd(), `.${request.url}`); - const extname = String(path.extname(filePath)).toLowerCase() as keyof (typeof mimeTypes); + const extname = String(path.extname(filePath)).toLowerCase() as keyof typeof mimeTypes; const contentType = mimeTypes[extname] || 'application/octet-stream'; try { diff --git a/src/cli/preview-server/server.ts b/src/cli/preview-server/server.ts index 30fc5661..ea947ad6 100644 --- a/src/cli/preview-server/server.ts +++ b/src/cli/preview-server/server.ts @@ -31,7 +31,7 @@ export function respondWithGzip( code = 200, ) { let compressedStream; - const acceptEncoding = request.headers['accept-encoding'] as string || ''; + const acceptEncoding = (request.headers['accept-encoding'] as string) || ''; if (acceptEncoding.match(/\bdeflate\b/)) { response.writeHead(code, { ...headers, 'content-encoding': 'deflate' }); compressedStream = zlib.createDeflate(); diff --git a/src/config/all.ts b/src/config/all.ts index f4500b92..cd0bbd3e 100644 --- a/src/config/all.ts +++ b/src/config/all.ts @@ -43,5 +43,5 @@ export default { 'no-example-value-and-externalValue': 'error', 'no-unused-components': 'error', 'no-undefined-server-variable': 'error', - } + }, } as LintRawConfig; diff --git a/src/config/config.ts b/src/config/config.ts index 38919e8c..3a4c3acd 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -380,7 +380,7 @@ export class Config { this.resolve = { http: { headers: rawConfig?.resolve?.http?.headers ?? [], - customFetch: undefined + customFetch: undefined, }, }; } diff --git a/src/config/recommended.ts b/src/config/recommended.ts index bb3802ed..c0f42719 100644 --- a/src/config/recommended.ts +++ b/src/config/recommended.ts @@ -46,5 +46,5 @@ export default { 'no-example-value-and-externalValue': 'error', 'no-unused-components': 'warn', 'no-undefined-server-variable': 'warn', - } + }, } as LintRawConfig; diff --git a/src/format/format.ts b/src/format/format.ts index 898bfc24..86382c43 100644 --- a/src/format/format.ts +++ b/src/format/format.ts @@ -1,5 +1,14 @@ import * as path from 'path'; -import { options as colorOptions, gray, blue, bgRed, bgYellow, black, yellow, red } from 'colorette'; +import { + options as colorOptions, + gray, + blue, + bgRed, + bgYellow, + black, + yellow, + red, +} from 'colorette'; import { NormalizedReportMessage, diff --git a/src/index.ts b/src/index.ts index 7d6b2a89..d342e729 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export type { Oas3Rule, OasRule, Oas3Preprocessor, OasPreprocessor} from './visitors'; +export type { Oas3Rule, OasRule, Oas3Preprocessor, OasPreprocessor } from './visitors'; export type { NodeType } from './types/index'; export type { Config, diff --git a/src/ref-utils.ts b/src/ref-utils.ts index 2d66927e..e3904123 100644 --- a/src/ref-utils.ts +++ b/src/ref-utils.ts @@ -24,11 +24,11 @@ export class Location { } key() { - return { ...this, reportOnKey: true } + return { ...this, reportOnKey: true }; } get absolutePointer() { - return this.source.absoluteRef + (this.pointer === '#/' ? '' : this.pointer) + return this.source.absoluteRef + (this.pointer === '#/' ? '' : this.pointer); } } @@ -45,7 +45,7 @@ export function parseRef(ref: string): { uri: string | null; pointer: string[] } const [uri, pointer] = ref.split('#/'); return { uri: uri || null, - pointer: pointer ? pointer.split('/').map(escapePointer).filter(Boolean) : [] + pointer: pointer ? pointer.split('/').map(escapePointer).filter(Boolean) : [], }; } @@ -67,11 +67,13 @@ export function isAbsoluteUrl(ref: string) { return ref.startsWith('http://') || ref.startsWith('https://'); } -export function isMappingRef(mapping: string ) { +export function isMappingRef(mapping: string) { // TODO: proper detection of mapping refs - return mapping.startsWith('#') || - mapping.startsWith('https://') || - mapping.startsWith('./') || - mapping.startsWith('../') || - mapping.indexOf('/') > -1 -} \ No newline at end of file + return ( + mapping.startsWith('#') || + mapping.startsWith('https://') || + mapping.startsWith('./') || + mapping.startsWith('../') || + mapping.indexOf('/') > -1 + ); +} diff --git a/src/rules/ajv.ts b/src/rules/ajv.ts index a56e535d..f28728e5 100644 --- a/src/rules/ajv.ts +++ b/src/rules/ajv.ts @@ -24,7 +24,7 @@ function getAjv(resolve: ResolveFn, disallowAdditionalProperties: boolean) validateSchema: false, defaultAdditionalProperties: !disallowAdditionalProperties, loadSchemaSync(base: string, $ref: string, id: string) { - const resolvedRef = resolve({$ref}, base.replace(/#$/, '')); + const resolvedRef = resolve({ $ref }, base.replace(/#$/, '')); if (!resolvedRef || !resolvedRef.location) return undefined; return { id, ...resolvedRef.node }; }, diff --git a/src/rules/common/info-contact.ts b/src/rules/common/info-contact.ts index b960c4e5..27f9de12 100644 --- a/src/rules/common/info-contact.ts +++ b/src/rules/common/info-contact.ts @@ -1,7 +1,7 @@ import { Oas3Rule, Oas2Rule } from '../../visitors'; import { missingRequiredField } from '../utils'; -export const InfoContact: (Oas3Rule | Oas2Rule) = () => { +export const InfoContact: Oas3Rule | Oas2Rule = () => { return { Info(info, { report, location }) { if (!info.contact) { diff --git a/src/rules/common/info-description.ts b/src/rules/common/info-description.ts index 5629582d..29c358e3 100644 --- a/src/rules/common/info-description.ts +++ b/src/rules/common/info-description.ts @@ -1,7 +1,7 @@ import { Oas3Rule, Oas2Rule } from '../../visitors'; import { validateDefinedAndNonEmpty } from '../utils'; -export const InfoDescription: (Oas3Rule | Oas2Rule) = () => { +export const InfoDescription: Oas3Rule | Oas2Rule = () => { return { Info(info, ctx) { validateDefinedAndNonEmpty('description', info, ctx); diff --git a/src/rules/common/no-enum-type-mismatch.ts b/src/rules/common/no-enum-type-mismatch.ts index 86565d82..e5892d76 100644 --- a/src/rules/common/no-enum-type-mismatch.ts +++ b/src/rules/common/no-enum-type-mismatch.ts @@ -4,7 +4,7 @@ import { Oas2Schema } from '../../typings/swagger'; import { Oas3Schema } from '../../typings/openapi'; import { UserContext } from '../../walk'; -export const NoEnumTypeMismatch: (Oas3Rule | Oas2Rule) = () => { +export const NoEnumTypeMismatch: Oas3Rule | Oas2Rule = () => { return { Schema(schema: Oas2Schema | Oas3Schema, { report, location }: UserContext) { if (schema.enum && schema.type) { diff --git a/src/rules/common/spec.ts b/src/rules/common/spec.ts index 333c8600..f50c4e90 100644 --- a/src/rules/common/spec.ts +++ b/src/rules/common/spec.ts @@ -62,7 +62,7 @@ export const OasSpec: Oas3Rule | Oas2Rule = () => { } if (propSchema.referenceable && isRef(propValue)) { - propValue = resolve(propValue).node + propValue = resolve(propValue).node; } if (propSchema.enum) { diff --git a/src/rules/no-unresolved-refs.ts b/src/rules/no-unresolved-refs.ts index cc576405..ba4f1155 100644 --- a/src/rules/no-unresolved-refs.ts +++ b/src/rules/no-unresolved-refs.ts @@ -9,11 +9,11 @@ export const NoUnresolvedRefs: Oas3Rule = () => { leave(_, { report, location }, resolved) { if (resolved.node !== undefined) return; reportUnresolvedRef(resolved, report, location); - } + }, }, DiscriminatorMapping(mapping, { report, resolve, location }) { for (const mappingName of Object.keys(mapping)) { - const resolved = resolve({ $ref: mapping[mappingName] } ); + const resolved = resolve({ $ref: mapping[mappingName] }); if (resolved.node !== undefined) return; console.log(mapping[mappingName], location.absolutePointer); diff --git a/src/rules/oas2/__tests__/spec/operation.test.ts b/src/rules/oas2/__tests__/spec/operation.test.ts index 20635eb8..cb4a1e92 100644 --- a/src/rules/oas2/__tests__/spec/operation.test.ts +++ b/src/rules/oas2/__tests__/spec/operation.test.ts @@ -1,5 +1,5 @@ -import { outdent } from "outdent"; -import { validateDoc } from "./utils"; +import { outdent } from 'outdent'; +import { validateDoc } from './utils'; describe('OpenAPI Schema 2.0', () => { it('should not report of a valid GET operation object', async () => { @@ -120,4 +120,4 @@ describe('OpenAPI Schema 2.0', () => { }), ).toMatchInlineSnapshot(`Array []`); }); -}); \ No newline at end of file +}); diff --git a/src/rules/oas3/__tests__/spec/operation.test.ts b/src/rules/oas3/__tests__/spec/operation.test.ts index 1f624ffe..8427c0de 100644 --- a/src/rules/oas3/__tests__/spec/operation.test.ts +++ b/src/rules/oas3/__tests__/spec/operation.test.ts @@ -1,5 +1,5 @@ -import { outdent } from "outdent"; -import { validateDoc } from "./utils"; +import { outdent } from 'outdent'; +import { validateDoc } from './utils'; it('should not report if summary field is valid', async () => { const source = outdent` @@ -250,4 +250,4 @@ it('should not report of a valid delete operation object', async () => { spec: 'error', }), ).toMatchInlineSnapshot(`Array []`); -}); \ No newline at end of file +}); diff --git a/src/rules/oas3/__tests__/spec/referenceableScalars.test.ts b/src/rules/oas3/__tests__/spec/referenceableScalars.test.ts index 1053b69c..13e26d05 100644 --- a/src/rules/oas3/__tests__/spec/referenceableScalars.test.ts +++ b/src/rules/oas3/__tests__/spec/referenceableScalars.test.ts @@ -1,10 +1,10 @@ -import { outdent } from "outdent"; +import { outdent } from 'outdent'; -import { parseYamlToDocument, replaceSourceWithRef } from "../../../../__tests__/utils"; +import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils'; -import { validateDocument } from "../../../../validate"; -import { LintConfig } from "../../../.."; -import { BaseResolver } from "../../../../resolve"; +import { validateDocument } from '../../../../validate'; +import { LintConfig } from '../../../..'; +import { BaseResolver } from '../../../../resolve'; describe('Referenceable scalars', () => { it('should not report $ref description', async () => { @@ -18,7 +18,7 @@ describe('Referenceable scalars', () => { $ref: fixtures/description.md paths: {} `, - __dirname+ '/foobar.yaml', + __dirname + '/foobar.yaml', ); const results = await validateDocument({ @@ -27,11 +27,11 @@ describe('Referenceable scalars', () => { config: new LintConfig({ extends: [], rules: { - 'spec': 'error', + spec: 'error', }, }), }); expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`); }); -}); \ No newline at end of file +}); diff --git a/src/rules/oas3/no-invalid-media-type-examples.ts b/src/rules/oas3/no-invalid-media-type-examples.ts index 874243b8..2615863c 100644 --- a/src/rules/oas3/no-invalid-media-type-examples.ts +++ b/src/rules/oas3/no-invalid-media-type-examples.ts @@ -35,7 +35,7 @@ export const ValidContentExamples: Oas3Rule = (opts) => { location.child('schema'), dataLoc.pointer, resolve, - disallowAdditionalProperties + disallowAdditionalProperties, ); if (!valid) { for (let error of errors) { diff --git a/src/rules/oas3/no-undefined-server-variable.ts b/src/rules/oas3/no-undefined-server-variable.ts index 779aeec1..74b2350c 100644 --- a/src/rules/oas3/no-undefined-server-variable.ts +++ b/src/rules/oas3/no-undefined-server-variable.ts @@ -3,15 +3,15 @@ import { Oas3Rule } from '../../visitors'; export const NoUndefinedServerVariable: Oas3Rule = () => { return { Server(server, { report, location }) { - const urlVariables = server.url.match(/{[^}]+}/g)?.map(e => e.slice(1, e.length - 1)) || []; - const definedVariables = server?.variables && Object.keys(server.variables) || []; + const urlVariables = server.url.match(/{[^}]+}/g)?.map((e) => e.slice(1, e.length - 1)) || []; + const definedVariables = (server?.variables && Object.keys(server.variables)) || []; for (const serverVar of urlVariables) { if (!definedVariables.includes(serverVar)) { report({ message: `The \`${serverVar}\` variable is not defined in the \`variables\` objects.`, - location: location.child(['url']) - }) + location: location.child(['url']), + }); } } @@ -21,9 +21,9 @@ export const NoUndefinedServerVariable: Oas3Rule = () => { message: `The \`${definedServerVar}\` variable is not used in the server's \`url\` field.`, location: location.child(['variables', definedServerVar]).key(), from: location.child('url'), - }) + }); } } - } + }, }; }; diff --git a/src/types/index.ts b/src/types/index.ts index 772888b4..eee07d04 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -109,16 +109,15 @@ export function normalizeTypes( return { ...type, directResolveAs: resolveType(type.directResolveAs), - } + }; } else { return type; } } } - export function isNamedType( t: NormalizedNodeType | NormalizedScalarSchema | null | undefined, ): t is NormalizedNodeType { return typeof t?.name === 'string'; -} \ No newline at end of file +} diff --git a/src/typings/swagger.ts b/src/typings/swagger.ts index e4d52546..2bf96467 100644 --- a/src/typings/swagger.ts +++ b/src/typings/swagger.ts @@ -178,7 +178,6 @@ export interface Oas2Responses { [code: string]: Oas2Response; } - export type Oas2Header = Oas2Items & { description?: 'string' }; export interface Oas2Response { diff --git a/src/visitors.ts b/src/visitors.ts index fb3b396b..0f884ebe 100644 --- a/src/visitors.ts +++ b/src/visitors.ts @@ -45,7 +45,6 @@ import { Oas2SecurityScheme, } from './typings/swagger'; - import { NormalizedNodeType } from './types'; import { Stack } from './utils'; import { UserContext, ResolveResult, MessageSeverity } from './walk'; diff --git a/src/walk.ts b/src/walk.ts index 4b4809b0..1d3d3206 100644 --- a/src/walk.ts +++ b/src/walk.ts @@ -228,7 +228,7 @@ export function walkDocument(opts: { // visit in order from type-tree first const props = Object.keys(type.properties); if (type.additionalProperties) { - props.push(...Object.keys(resolvedNode).filter(k => !props.includes(k))); + props.push(...Object.keys(resolvedNode).filter((k) => !props.includes(k))); } for (const propName of props) { let value = resolvedNode[propName]; @@ -251,7 +251,6 @@ export function walkDocument(opts: { walkNode(value, propType, newLocation.child([propName]), resolvedNode, propName); } - } } @@ -357,7 +356,12 @@ export function walkDocument(opts: { return { location: newLocation, node, error }; } - function reportFn(ruleId: string, severity: MessageSeverity, location: Location, opts: ReportMessage) { + function reportFn( + ruleId: string, + severity: MessageSeverity, + location: Location, + opts: ReportMessage, + ) { const loc = opts.location ? Array.isArray(opts.location) ? opts.location