Skip to content

Commit

Permalink
prettier: Use the default setting for arrowParens
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Jan 11, 2021
1 parent d57267f commit bd57325
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"arrowParens": "avoid",
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"scripts": {
"build": "tsc",
"lint": "eslint '**/*.ts'",
"lint": "eslint '**/*.ts' && prettier --check \"**/*.{json,md}\"",
"lint:fix": "eslint --fix '**/*.ts' && prettier --write '**/*.{json,md}'",
"test": "jest",
"prepublishOnly": "yarn build"
},
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const outputFileName = (sourceFileName: string, ext: string): string =>
const main = () => {
const args = parseArgs()

const sourceFiles = args._.map(x => x.toString())
const sourceFiles = args._.map((x) => x.toString())
const ext = args.format === 'ts' ? '.openapi.ts' : '.json'

const results = generate(sourceFiles, { strict: true }, { log }).map(
result => ({
(result) => ({
...result,
outputFileName: outputFileName(result.fileName, ext),
})
Expand Down
30 changes: 16 additions & 14 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const generate = (
if (!fileNames.includes(sourceFile.fileName)) continue
if (sourceFile.isDeclarationFile) continue

ts.forEachChild(sourceFile, node => {
ts.forEachChild(sourceFile, (node) => {
const paths = visit(context(checker, sourceFile, log, node), node)
if (paths) {
result.push({ fileName: sourceFile.fileName, paths })
Expand All @@ -63,7 +63,7 @@ const visit = (

const paths: OpenAPIV3.PathsObject = {}

argSymbols.forEach(symbol => {
argSymbols.forEach((symbol) => {
const location = symbol.valueDeclaration
const routeDeclaration = getRouteDeclaration(
withLocation(ctx, location),
Expand Down Expand Up @@ -92,7 +92,7 @@ const getRouterCallArgSymbols = (

const argSymbols = args
.filter(ts.isIdentifier)
.map(arg => ctx.checker.getSymbolAtLocation(arg))
.map((arg) => ctx.checker.getSymbolAtLocation(arg))
.filter(isDefined)

if (argSymbols.length !== args.length) return
Expand Down Expand Up @@ -280,7 +280,7 @@ const getResponseTypes = (
const responseDef = getResponseDefinition(ctx, responseType)
if (responseDef) result[responseDef.status] = responseDef.response
} else if (responseType.isUnion()) {
responseType.types.forEach(type => {
responseType.types.forEach((type) => {
const responseDef = getResponseDefinition(ctx, type)
if (responseDef) result[responseDef.status] = responseDef.response
})
Expand Down Expand Up @@ -368,7 +368,7 @@ const typeToParameters = (
if (!type) return []

const props = ctx.checker.getPropertiesOfType(type)
return props.map(prop => ({
return props.map((prop) => ({
name: prop.name,
in: in_,
required: in_ === 'path' ? true : !isOptional(prop),
Expand All @@ -382,7 +382,7 @@ interface Headers {
const typeToHeaders = (ctx: Context, type: ts.Type): Headers => {
const result: Headers = {}
const props = ctx.checker.getPropertiesOfType(type)
props.forEach(prop => {
props.forEach((prop) => {
result[prop.name] = {
required: !isOptional(prop),
}
Expand All @@ -401,13 +401,13 @@ const typeToSchema = (
let elems = type.types

if (optional) {
elems = type.types.filter(elem => !isUndefinedType(elem))
elems = type.types.filter((elem) => !isUndefinedType(elem))
}

if (elems.some(isNullType)) {
// One of the union elements is null
nullable = { nullable: true }
elems = elems.filter(elem => !isNullType(elem))
elems = elems.filter((elem) => !isNullType(elem))
}

if (elems.every(isBooleanLiteralType)) {
Expand All @@ -417,20 +417,20 @@ const typeToSchema = (
// All elements are number literals => enum
return {
type: 'number',
enum: elems.map(elem => elem.value),
enum: elems.map((elem) => elem.value),
...nullable,
}
} else if (elems.every(isStringLiteralType)) {
// All elements are string literals => enum
return {
type: 'string',
enum: elems.map(elem => elem.value),
enum: elems.map((elem) => elem.value),
...nullable,
}
} else if (elems.length >= 2) {
// 2 or more types remain => anyOf
return {
anyOf: elems.map(elem => typeToSchema(ctx, elem)).filter(isDefined),
anyOf: elems.map((elem) => typeToSchema(ctx, elem)).filter(isDefined),
...nullable,
}
} else {
Expand All @@ -442,16 +442,18 @@ const typeToSchema = (

if (
isObjectType(type) ||
(type.isIntersection() && type.types.every(part => isObjectType(part)))
(type.isIntersection() && type.types.every((part) => isObjectType(part)))
) {
const props = ctx.checker.getPropertiesOfType(type)
return {
type: 'object',
required: props.filter(prop => !isOptional(prop)).map(prop => prop.name),
required: props
.filter((prop) => !isOptional(prop))
.map((prop) => prop.name),
...nullable,
properties: Object.fromEntries(
props
.map(prop => {
.map((prop) => {
const propType = ctx.checker.getTypeOfSymbolAtLocation(
prop,
ctx.location
Expand Down
10 changes: 5 additions & 5 deletions tests/test-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const requestBody: Route<
> = route
.post('/request-body')
.use(Parser.body(codec))
.handler(async request => {
.handler(async (request) => {
if (request.body.optionalBool) {
return Response.ok(request.body.str)
} else {
Expand Down Expand Up @@ -73,7 +73,7 @@ const noExplicitRouteType = route
const unusedRequest: Route<Response.Ok<string>> = route
.get('/unused-request')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.handler(async request => {
.handler(async (request) => {
return Response.ok('xyzzy')
})

Expand All @@ -86,14 +86,14 @@ const queryCodec = t.intersection([
const query: Route<Response.Ok<string> | Response.BadRequest<string>> = route
.get('/query')
.use(Parser.query(queryCodec))
.handler(async request => {
.handler(async (request) => {
return Response.ok(request.query.str)
})

// Route params
const routeParams: Route<Response.Ok<{ id: number }>> = route
.get('/user/:id(int)')
.handler(async request => {
.handler(async (request) => {
return Response.ok({ id: request.routeParams.id })
})

Expand All @@ -105,7 +105,7 @@ const brandedRequestBody: Route<
> = route
.post('/branded-request-body')
.use(Parser.body(brandedCodec))
.handler(async request => {
.handler(async (request) => {
return Response.ok(request.body.param)
})

Expand Down

0 comments on commit bd57325

Please sign in to comment.