mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Migrate code formatting across the monorepo from Prettier to `oxfmt`
(`0.79.0`, pinned) for much faster formatting checks locally and in CI
while keeping code churn minimal (78 files total, 0.7% of the repo).
Studio's `@ianvs/prettier-plugin-sort-imports` ordering
(`<BUILTIN_MODULES>`, `<THIRD_PARTY_MODULES>`, blank line, then `@/` and
relative imports) is preserved via `oxfmt`'s native `sortImports`
override for `apps/studio/**/*.{js,jsx,ts,tsx}`.
### Performance (before vs after, uncached)
| Benchmark | Prettier (`3.8.x`) | oxfmt (`0.79.0`) | Speedup |
| --- | ---: | ---: | ---: |
| CI formatting step (`blacksmith-4vcpu-ubuntu-2404`, full repo) | `73s`
| **`7s`** | **~10×** |
| Local `apps/studio` JS/TS (`5,157` files) | `17.74s` | **`0.36s`** |
**~49×** |
| Local full monorepo (`11,131` files) | — | **`2.39s`** | — |
**Added:**
- `.oxfmtrc.jsonc` at repo root with Prettier-matching options,
`sortPackageJson: false`, the Studio `sortImports` override, and ignore
patterns migrated from `.prettierignore`
- `oxfmt` (`0.79.0` exact pin) in `pnpm-workspace.yaml` catalog and
workspace `devDependencies` / `dependencies`
- Shared `.agents/scripts/format_and_lint.sh` hook script and
`.agents/hooks.json` (used by both Claude Code and Antigravity)
**Changed:**
- Replaced `test:prettier` with `format:check` and switched `format` in
root `package.json` to `oxfmt`
- Updated `packages/api-types` (`codegen`,
`verify-production-types.mjs`), `apps/design-system` (`build:registry`),
`apps/docs` (`CodeSample.ts`, `generate-sitemap.ts`, `next.config.mjs`,
`spec/Makefile`), `apps/www` (`generate-sitemap.mjs`), and
`packages/build-icons` (`generateIconFiles.mjs`) from Prettier to
`oxfmt`
- Updated `.github/workflows/prettier.yml` (keeping the `format` job ID
intact), `.github/workflows/autofix_linters.yml`,
`.claude/settings.json`, `AGENTS.md`, `CONTRIBUTING.md`, and
`.github/copilot-instructions.md`
- Applied one-time `oxfmt` formatting pass in a separate commit (78
files: 30 `i18n/README.*.md` trailing spaces, 19 Studio files, 17 other
TS/TSX/generated types, 8 MD/MDX, 3 `package.json`, 2 example CSS files)
**Removed:**
- `prettier`, `@ianvs/prettier-plugin-sort-imports`, and
`prettier-plugin-sql-cst` from `devDependencies` (note: `.sql` files
were not part of the CI formatting glob; `eslint-config-prettier` is
kept for ESLint)
- `prettier.config.mjs`, `.prettierignore`, and
`.claude/scripts/format_and_lint.sh`
## To test
- Run `pnpm run format:check` and confirm it passes cleanly across all
matched files
- Run `pnpm --filter api-types test`, `pnpm --filter docs exec vitest
run features/directives/CodeSample.test.ts`, and `pnpm --filter www exec
vitest run generate-sitemap.test.ts`
- Edit an `apps/studio` file with out-of-order imports and run `pnpm run
format` to confirm builtin/third-party vs `@/`/relative grouping matches
prior behavior
- Check that the `Check Code Formatting` CI workflow passes on this PR
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
172 lines
6.1 KiB
TypeScript
172 lines
6.1 KiB
TypeScript
import { untrustedSql } from '@supabase/pg-meta'
|
|
import isEqual from 'lodash/isEqual'
|
|
import { type Snapshot } from 'valtio'
|
|
|
|
import { type ExplorerQueryModel } from '../QueryEditor'
|
|
import { type QueryDisplay } from '../types'
|
|
import { type ChartConfig, type QueryCell } from '@/data/content/notebooks/notebook-schema'
|
|
import { untrustedLogSql } from '@/data/logs/safe-analytics-sql'
|
|
import {
|
|
getQuerySourceBinding,
|
|
type QuerySourceBinding,
|
|
} from '@/data/query-sources/query-source-registry'
|
|
|
|
/** Row limit a database cell starts with when it has no saved one to carry over. */
|
|
export const DEFAULT_CELL_ROW_LIMIT = 100
|
|
|
|
/**
|
|
* Valtio snapshots are deep-readonly. Readonly properties assign to mutable ones, so only
|
|
* the array needs rebuilding to turn a snapshot's chart back into a writable config.
|
|
*/
|
|
type ReadonlyChartConfig = Omit<ChartConfig, 'y_series'> & {
|
|
readonly y_series: readonly string[]
|
|
}
|
|
|
|
export const cloneChartConfig = (chart: ReadonlyChartConfig | undefined): ChartConfig | undefined =>
|
|
chart ? { ...chart, y_series: [...chart.y_series] } : undefined
|
|
|
|
/** The display state a query cell hands the shared editor. */
|
|
// `view` is already defaulted to 'table' by the domain transform, so there is nothing to
|
|
// fall back to here — only the chart needs copying out of the snapshot.
|
|
export const getCellDisplay = (cell: Snapshot<QueryCell>): QueryDisplay => ({
|
|
view: cell.view,
|
|
chart: cloneChartConfig(cell.chart),
|
|
})
|
|
|
|
/** Fields every query cell carries, copied out of a snapshot so the result is writable. */
|
|
const copyQueryCellBase = (cell: Snapshot<QueryCell>) => ({
|
|
_id: cell._id,
|
|
title: cell.title,
|
|
view: cell.view,
|
|
chart: cloneChartConfig(cell.chart),
|
|
})
|
|
|
|
/** A writable copy of a query cell, preserving its backend and every backend-specific field. */
|
|
export const cloneQueryCell = (cell: Snapshot<QueryCell>): QueryCell =>
|
|
cell._tag === 'log_cell'
|
|
? {
|
|
...copyQueryCellBase(cell),
|
|
_tag: 'log_cell',
|
|
unchecked_sql: cell.unchecked_sql,
|
|
time_range: cell.time_range,
|
|
}
|
|
: {
|
|
...copyQueryCellBase(cell),
|
|
_tag: 'database_cell',
|
|
unchecked_sql: cell.unchecked_sql,
|
|
row_limit: cell.row_limit,
|
|
database_identifier: cell.database_identifier,
|
|
}
|
|
|
|
/**
|
|
* Applies a source binding to a query cell, carrying the query text across unchanged and
|
|
* rebranding it for the new backend's dialect.
|
|
*
|
|
* NOTE — carrying the text over is very likely not what a user wants when the backend
|
|
* actually changes. Postgres SQL and logs SQL are separate dialects over separate schemas,
|
|
* so a carried-over query will almost always fail to run, and the rebrand asserts a
|
|
* dialect the text was never written in. We keep it for now because it is the
|
|
* least-destructive option and needs no confirmation prompt; revisit once we know whether
|
|
* people switch source to port an existing query or to start a fresh one, at which point
|
|
* clearing the body (behind a confirmation) is the likely answer.
|
|
*/
|
|
export function changeCellSource(cell: Snapshot<QueryCell>, source: QuerySourceBinding): QueryCell {
|
|
const base = copyQueryCellBase(cell)
|
|
|
|
if (source._tag === 'logs') {
|
|
return {
|
|
...base,
|
|
_tag: 'log_cell',
|
|
unchecked_sql: untrustedLogSql(cell.unchecked_sql),
|
|
time_range: source.time_range,
|
|
}
|
|
}
|
|
|
|
return {
|
|
...base,
|
|
_tag: 'database_cell',
|
|
unchecked_sql: untrustedSql(cell.unchecked_sql),
|
|
row_limit: cell._tag === 'database_cell' ? cell.row_limit : DEFAULT_CELL_ROW_LIMIT,
|
|
database_identifier: source.database_identifier,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Whether a source change makes a cell's last in-session result stale enough to clear.
|
|
* A backend change (database ↔ logs) invalidates outright, since another engine returns
|
|
* unrelated columns. A log cell's time range is a request parameter rather than part of the
|
|
* SQL text (see notebookToMarkdown), so a plain SQL-text comparison wouldn't catch a result
|
|
* that's stale only because the range moved — that has to be checked here instead.
|
|
*/
|
|
export function shouldInvalidateResultOnSourceChange(
|
|
cell: Snapshot<QueryCell>,
|
|
source: QuerySourceBinding
|
|
): boolean {
|
|
const isBackendChange = (source._tag === 'logs') !== (cell._tag === 'log_cell')
|
|
const isTimeRangeChange =
|
|
source._tag === 'logs' &&
|
|
cell._tag === 'log_cell' &&
|
|
!isEqual(source.time_range, cell.time_range)
|
|
|
|
return isBackendChange || isTimeRangeChange
|
|
}
|
|
|
|
/**
|
|
* Writes the editor's text back onto a cell, branded for that cell's dialect. Separate
|
|
* from `cloneQueryCell` so the brand stays correlated with the cell tag in one narrowing
|
|
* rather than being re-derived at each call site.
|
|
*/
|
|
export function setCellSql(cell: Snapshot<QueryCell>, sql: string): QueryCell {
|
|
const base = copyQueryCellBase(cell)
|
|
|
|
if (cell._tag === 'log_cell') {
|
|
return {
|
|
...base,
|
|
_tag: 'log_cell',
|
|
unchecked_sql: untrustedLogSql(sql),
|
|
time_range: cell.time_range,
|
|
}
|
|
}
|
|
|
|
return {
|
|
...base,
|
|
_tag: 'database_cell',
|
|
unchecked_sql: untrustedSql(sql),
|
|
row_limit: cell.row_limit,
|
|
database_identifier: cell.database_identifier,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Writes a new row limit onto a database cell. A log cell has no row limit concept, so it
|
|
* passes through unchanged.
|
|
*/
|
|
export function setCellRowLimit(cell: Snapshot<QueryCell>, rowLimit: number): QueryCell {
|
|
if (cell._tag === 'log_cell') return cloneQueryCell(cell)
|
|
|
|
return {
|
|
...copyQueryCellBase(cell),
|
|
_tag: 'database_cell',
|
|
unchecked_sql: cell.unchecked_sql,
|
|
row_limit: rowLimit,
|
|
database_identifier: cell.database_identifier,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Builds the editor's query model from a cell and the editor's live text buffer. Branding
|
|
* the buffer is the editor boundary the safe-SQL model expects; which brand applies is
|
|
* decided by the cell's tag, so the dialect can't drift from the cell it belongs to.
|
|
*/
|
|
export function toQueryModel(cell: Snapshot<QueryCell>, sql: string): ExplorerQueryModel {
|
|
if (cell._tag === 'log_cell') {
|
|
return { ...getQuerySourceBinding(cell), uncheckedSql: untrustedLogSql(sql) }
|
|
}
|
|
|
|
return {
|
|
...getQuerySourceBinding(cell),
|
|
uncheckedSql: untrustedSql(sql),
|
|
rowLimit: cell.row_limit,
|
|
}
|
|
}
|