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

lex-cli: Fix TS Errors with codegen #3282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/lex-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"chalk": "^4.1.2",
"commander": "^9.4.0",
"prettier": "^3.2.5",
"ts-morph": "^16.0.0",
"ts-morph": "^24.0.0",
"yesno": "^0.4.0",
"zod": "^3.23.8"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/lex-cli/src/codegen/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
SourceFile,
VariableDeclarationKind,
} from 'ts-morph'
import { Lexicons, LexiconDoc, LexRecord } from '@atproto/lexicon'
import { Lexicons, type LexiconDoc, type LexRecord } from '@atproto/lexicon'
import { NSID } from '@atproto/syntax'
import { gen, utilTs, lexiconsTs } from './common'
import { GeneratedAPI } from '../types'
import { type GeneratedAPI } from '../types'
import {
genImports,
genUserType,
Expand All @@ -19,7 +19,7 @@ import {
} from './lex-gen'
import {
lexiconsToDefTree,
DefTreeNode,
type DefTreeNode,
schemasToNsidTokens,
toCamelCase,
toTitleCase,
Expand Down Expand Up @@ -61,14 +61,14 @@ const indexTs = (
nsidTokens: Record<string, string[]>,
) =>
gen(project, '/index.ts', async (file) => {
//= import { XrpcClient, FetchHandler, FetchHandlerOptions } from '@atproto/xrpc'
//= import { XrpcClient, type FetchHandler, type FetchHandlerOptions } from '@atproto/xrpc'
const xrpcImport = file.addImportDeclaration({
moduleSpecifier: '@atproto/xrpc',
})
xrpcImport.addNamedImports([
{ name: 'XrpcClient' },
{ name: 'FetchHandler' },
{ name: 'FetchHandlerOptions' },
{ name: 'FetchHandler', isTypeOnly: true },
{ name: 'FetchHandlerOptions', isTypeOnly: true },
])
//= import {schemas} from './lexicons'
file
Expand Down Expand Up @@ -446,12 +446,12 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
{ name: 'XRPCError' },
])
}
//= import {ValidationResult, BlobRef} from '@atproto/lexicon'
//= import { type ValidationResult, BlobRef } from '@atproto/lexicon'
file
.addImportDeclaration({
moduleSpecifier: '@atproto/lexicon',
})
.addNamedImports([{ name: 'ValidationResult' }, { name: 'BlobRef' }])
.addNamedImports([{ name: 'ValidationResult', isTypeOnly: true }, { name: 'BlobRef' }])
//= import {isObj, hasProp} from '../../util.ts'
file
.addImportDeclaration({
Expand Down
18 changes: 9 additions & 9 deletions packages/lex-cli/src/codegen/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Project, SourceFile, VariableDeclarationKind } from 'ts-morph'
import { LexiconDoc } from '@atproto/lexicon'
import { type LexiconDoc } from '@atproto/lexicon'
import prettier from 'prettier'
import { GeneratedFile } from '../types'
import { type GeneratedFile } from '../types'

const PRETTIER_OPTS = {
parser: 'typescript',
Expand All @@ -18,10 +18,10 @@ export function isObj(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null
}

export function hasProp<K extends PropertyKey>(
data: object,
export function hasProp<T extends object, K extends PropertyKey>(
data: T,
prop: K,
): data is Record<K, unknown> {
): data is T & Record<K, unknown> {
return prop in data
}
`)
Expand All @@ -36,12 +36,12 @@ export const lexiconsTs = (project, lexicons: LexiconDoc[]) =>
.join('')
}

//= import {LexiconDoc} from '@atproto/lexicon'
//= import { type LexiconDoc, Lexicons } from '@atproto/lexicon'
file
.addImportDeclaration({
moduleSpecifier: '@atproto/lexicon',
})
.addNamedImports([{ name: 'LexiconDoc' }, { name: 'Lexicons' }])
.addNamedImports([{ name: 'LexiconDoc', isTypeOnly: true }, { name: 'Lexicons' }])

//= export const schemaDict = {...} as const satisfies Record<string, LexiconDoc>
file.addVariableStatement({
Expand All @@ -66,14 +66,14 @@ export const lexiconsTs = (project, lexicons: LexiconDoc[]) =>
],
})

//= export const schemas = Object.values(schemaDict)
//= export const schemas = Object.values(schemaDict) satisfies LexiconDoc[]
file.addVariableStatement({
isExported: true,
declarationKind: VariableDeclarationKind.Const,
declarations: [
{
name: 'schemas',
initializer: 'Object.values(schemaDict)',
initializer: 'Object.values(schemaDict) satisfies LexiconDoc[]',
},
],
})
Expand Down
18 changes: 9 additions & 9 deletions packages/lex-cli/src/codegen/lex-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { SourceFile, VariableDeclarationKind } from 'ts-morph'
import { relative as getRelativePath } from 'path'
import {
Lexicons,
LexUserType,
LexObject,
LexArray,
LexPrimitive,
LexBlob,
LexToken,
LexCidLink,
LexBytes,
LexIpldType,
type LexUserType,
type LexObject,
type LexArray,
type LexPrimitive,
type LexBlob,
type LexToken,
type LexCidLink,
type LexBytes,
type LexIpldType,
} from '@atproto/lexicon'
import { toCamelCase, toTitleCase, toScreamingSnakeCase } from './util'

Expand Down
15 changes: 8 additions & 7 deletions packages/lex-cli/src/codegen/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
SourceFile,
VariableDeclarationKind,
} from 'ts-morph'
import { Lexicons, LexiconDoc } from '@atproto/lexicon'
import { Lexicons, type LexiconDoc } from '@atproto/lexicon'
import { NSID } from '@atproto/syntax'
import { gen, lexiconsTs, utilTs } from './common'
import { GeneratedAPI } from '../types'
import { type GeneratedAPI } from '../types'
import {
genImports,
genUserType,
Expand All @@ -19,7 +19,7 @@ import {
} from './lex-gen'
import {
lexiconsToDefTree,
DefTreeNode,
type DefTreeNode,
schemasToNsidTokens,
toCamelCase,
toTitleCase,
Expand Down Expand Up @@ -68,9 +68,10 @@ const indexTs = (
{
name: 'Options',
alias: 'XrpcOptions',
isTypeOnly: true
},
{ name: 'AuthVerifier' },
{ name: 'StreamAuthVerifier' },
{ name: 'AuthVerifier', isTypeOnly: true },
{ name: 'StreamAuthVerifier', isTypeOnly: true },
],
})
//= import {schemas} from './lexicons'
Expand Down Expand Up @@ -342,12 +343,12 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
})
}
}
//= import {ValidationResult, BlobRef} from '@atproto/lexicon'
//= import { type ValidationResult, BlobRef } from '@atproto/lexicon'
file
.addImportDeclaration({
moduleSpecifier: '@atproto/lexicon',
})
.addNamedImports([{ name: 'ValidationResult' }, { name: 'BlobRef' }])
.addNamedImports([{ name: 'ValidationResult', isTypeOnly: true }, { name: 'BlobRef' }])
//= import {lexicons} from '../../lexicons.ts'
file
.addImportDeclaration({
Expand Down
2 changes: 1 addition & 1 deletion packages/lex-cli/src/codegen/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LexiconDoc, LexUserType } from '@atproto/lexicon'
import { type LexiconDoc, type LexUserType } from '@atproto/lexicon'
import { NSID } from '@atproto/syntax'

export interface DefTreeNodeUserType {
Expand Down
2 changes: 1 addition & 1 deletion packages/lex-cli/src/mdgen/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import { LexiconDoc } from '@atproto/lexicon'
import { type LexiconDoc } from '@atproto/lexicon'

const INSERT_START = [
'<!-- START lex generated content. Please keep comment here to allow auto update -->',
Expand Down
6 changes: 3 additions & 3 deletions packages/lex-cli/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs'
import { join } from 'path'
import { parseLexiconDoc, LexiconDoc } from '@atproto/lexicon'
import { ZodError, ZodFormattedError } from 'zod'
import { parseLexiconDoc, type LexiconDoc } from '@atproto/lexicon'
import { ZodError, type ZodFormattedError } from 'zod'
import chalk from 'chalk'
import { GeneratedAPI, FileDiff } from './types'
import { type GeneratedAPI, type FileDiff } from './types'

export function readAllLexicons(paths: string[]): LexiconDoc[] {
const docs: LexiconDoc[] = []
Expand Down
Loading