Skip to content

Commit

Permalink
Disable prettier warning with custom linter (#356)
Browse files Browse the repository at this point in the history
* test: check no prettier warning with custom linter

Failing to reproduce issue right now; fix to come

* fix: put prettify inside writeFile

Co-authored-by: Misha Kaletsky <mmkal@users.noreply.github.com>
  • Loading branch information
mmkal and mmkal authored Dec 8, 2021
1 parent 05ffb9b commit 9331426
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/typegen/src/write/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {addTags} from '../query/tag'
import * as assert from 'assert'
import * as fs from 'fs'
import * as path from 'path'
import {prettifyOne} from './prettify'

export type WriteFile = (filepath: string, content: string) => Promise<void>

export const defaultWriteFile: WriteFile = async (filepath, content) => {
await fs.promises.mkdir(path.dirname(filepath), {recursive: true})
await fs.promises.writeFile(filepath, content)
const pretty = prettifyOne({filepath, content})
await fs.promises.writeFile(filepath, pretty)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/typegen/src/write/inline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as lodash from 'lodash'
import {TaggedQuery} from '../types'
import {relativeUnixPath} from '../util'
import {prettifyOne, tsPrettify} from './prettify'
import {tsPrettify} from './prettify'
import type * as ts from 'typescript'
import * as path from 'path'
import {queryInterfaces} from './typescript'
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getFileWriter({getQueriesModulePath = defaultGetQueriesModule, w
})
} else {
let content = queryInterfaces(group)
await writeFile(destPath, prettifyOne({filepath: destPath, content}))
await writeFile(destPath, content)

const importPath = relativeUnixPath(destPath, path.dirname(file))
const importStatement = `import * as queries from './${importPath.replace(/\.(js|ts|tsx)$/, '')}'`
Expand All @@ -67,7 +67,7 @@ export function getFileWriter({getQueriesModulePath = defaultGetQueriesModule, w

const newSource = applyEdits(originalSource, edits)

await writeFile(file, prettifyOne({filepath: file, content: newSource}))
await writeFile(file, newSource)

function visit(node: ts.Node) {
if (ts.isModuleDeclaration(node) && node.name.getText() === 'queries') {
Expand Down
4 changes: 2 additions & 2 deletions packages/typegen/src/write/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {dedent, relativeUnixPath, typeName, truncateQuery} from '../util'
import * as path from 'path'
import {getterExpression, jsdocComment, queryInterfaces, quotePropKey} from './typescript'
import {TaggedQuery} from '../types'
import {prettifyOne, tsPrettify} from './prettify'
import {tsPrettify} from './prettify'
import {WriteFile} from '.'

export interface WriteSQLFileOptions {
Expand All @@ -19,7 +19,7 @@ export const getSQLHelperWriter =
const destPath = getModulePath(query.file)
const newContent = getSQLHelperContent(query, destPath)

await writeFile(destPath, prettifyOne({content: newContent, filepath: destPath}))
await writeFile(destPath, newContent)
}

export function getSQLHelperContent(query: TaggedQuery, destPath: string) {
Expand Down
62 changes: 62 additions & 0 deletions packages/typegen/test/ugly.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as fsSyncer from 'fs-syncer'
import * as typegen from '../src'
import {defaultWriteTypes} from '../src/write'
import {getHelper} from './helper'
import * as fs from 'fs'
import * as path from 'path'

export const {typegenOptions, logger, poolHelper: helper} = getHelper({__filename})

Expand Down Expand Up @@ -119,3 +122,62 @@ test('prettier can fail', async () => {
"
`)
})

test('no prettier warning for custom writeTypes', async () => {
const syncer = fsSyncer.jestFixture({
targetState: {
'index.ts': `
import {sql} from 'slonik';
export default sql\`select id, n from test_table\`;
`,
},
})

syncer.sync()

mockFormat.mockImplementationOnce(() => {
throw Object.assign(new Error('prettier not found'), {code: 'MODULE_NOT_FOUND'})
})
const mockWarn = jest.spyOn(console, 'warn').mockReset()

const options = typegenOptions(syncer.baseDir)
await typegen.generate({
...options,
writeTypes: queries => {
return defaultWriteTypes({
writeFile: async (filepath, content) => {
await fs.promises.mkdir(path.dirname(filepath), {recursive: true})
// dummy lint
const linted = content.replace(/;/g, '')
await fs.promises.writeFile(filepath, linted)
},
})(queries)
},
})

expect(mockWarn).not.toHaveBeenCalled()

expect(syncer.yaml()).toMatchInlineSnapshot(`
"---
index.ts: |-
import {sql} from 'slonik'
export default sql<queries.TestTable>\`select id, n from test_table\`
export declare namespace queries {
// Generated by @slonik/typegen
/** - query: \`select id, n from test_table\` */
export interface TestTable {
/** column: \`ugly_test.test_table.id\`, not null: \`true\`, regtype: \`integer\` */
id: number
/** column: \`ugly_test.test_table.n\`, regtype: \`integer\` */
n: (number) | null
}
}
"
`)
})

0 comments on commit 9331426

Please sign in to comment.