Skip to content
Draft
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
6 changes: 4 additions & 2 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
import type { Column } from '../../types/Column'
import type { Column_CoreProperties } from './coreColumnsFeature.types'

const IS_DEV = process.env.NODE_ENV !== 'production'

/**
* Creates or retrieves the column prototype for a table.
* The prototype is cached on the table and shared by all column instances.
Expand Down Expand Up @@ -75,7 +77,7 @@ export function constructColumn<
for (let i = 0; i < keys.length; i++) {
const key = keys[i]!
result = result?.[key]
if (process.env.NODE_ENV === 'development' && result === undefined) {
if (IS_DEV && result === undefined) {
console.warn(
`"${key}" in deeply nested key "${accessorKey}" returned undefined.`,
)
Expand All @@ -91,7 +93,7 @@ export function constructColumn<
}

if (!id) {
if (process.env.NODE_ENV === 'development') {
if (IS_DEV) {
throw new Error(
resolvedColumnDef.accessorFn
? `coreColumnsFeature require an id when using an accessorFn`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export function table_getAllLeafColumnsById<
return result
}

const IS_DEV = process.env.NODE_ENV !== 'production'

/**
* Looks up a column by id from the flat column map.
*
Expand All @@ -267,7 +269,7 @@ export function table_getColumn<
): Column<TFeatures, TData, unknown> | undefined {
const column = table.getAllFlatColumnsById()[columnId]

if (process.env.NODE_ENV === 'development' && !column) {
if (IS_DEV && !column) {
console.warn(`[Table] Column with id '${columnId}' does not exist.`)
}

Expand Down
4 changes: 3 additions & 1 deletion packages/table-core/src/core/rows/coreRowsFeature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
import type { Row_RowExpanding } from '../../features/row-expanding/rowExpandingFeature.types'

const IS_DEV = process.env.NODE_ENV !== 'production'

/**
* Returns this row's zero-based position in the current pre-pagination row
* model. Rows outside that model return `-1`.
Expand Down Expand Up @@ -341,7 +343,7 @@ export function table_getRow<
if (!row) {
row = table.getCoreRowModel().rowsById[rowId]
if (!row) {
if (process.env.NODE_ENV === 'development') {
if (IS_DEV) {
throw new Error(`getRow could not find row with ID: ${rowId}`)
}
throw new Error()
Expand Down
7 changes: 3 additions & 4 deletions packages/table-core/src/core/table/constructTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { Table, Table_Internal } from '../../types/Table'
import type { TableOptions } from '../../types/TableOptions'
import type { TableState, TableState_All } from '../../types/TableState'

const IS_DEV = process.env.NODE_ENV === 'development'

/**
* Builds the initial table state from registered features and user initial state.
*
Expand Down Expand Up @@ -170,10 +172,7 @@ export function constructTable<
featuresList[i]!.initTableInstanceData?.(table)
}

if (
process.env.NODE_ENV === 'development' &&
(tableOptions.debugAll || tableOptions.debugTable)
) {
if (IS_DEV && (tableOptions.debugAll || tableOptions.debugTable)) {
const features = Object.keys(table._features)
const rowModels = Object.entries({
coreRowModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
FilterFn,
} from './columnFilteringFeature.types'

const IS_DEV = process.env.NODE_ENV !== 'production'

/**
* Creates the default column filter state.
*
Expand Down Expand Up @@ -71,7 +73,7 @@ export function column_getAutoFilterFn<

const filterFn = filterFns?.[filterFnName]

if (process.env.NODE_ENV === 'development' && !filterFn) {
if (IS_DEV && !filterFn) {
console.warn(
`filterFn '${filterFnName}' (auto) for column '${column.id}' is not registered`,
)
Expand Down Expand Up @@ -109,7 +111,7 @@ export function column_getFilterFn<
: filterFns?.[column.columnDef.filterFn as string]

if (
process.env.NODE_ENV === 'development' &&
IS_DEV &&
!filterFn &&
column.columnDef.filterFn !== 'auto' // the auto picker warns on its own
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table_Internal } from '../../types/Table'

const IS_DEV = process.env.NODE_ENV !== 'production'

/**
* Checks whether this accessor column participates in global filtering.
*
Expand Down Expand Up @@ -74,11 +76,7 @@ export function table_getGlobalFilterFn<
? table_getGlobalAutoFilterFn()
: filterFns?.[globalFilterFn as string]

if (
process.env.NODE_ENV === 'development' &&
!filterFn &&
globalFilterFn != null
) {
if (IS_DEV && !filterFn && globalFilterFn != null) {
console.warn(`globalFilterFn '${String(globalFilterFn)}' is not registered`)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
ResolvedAggregationFn,
} from './rowAggregationFeature.types'

const IS_DEV = process.env.NODE_ENV !== 'production'

interface AggregationCacheEntry {
aggregationFnOption: unknown
dependency: unknown
Expand Down Expand Up @@ -48,7 +50,7 @@ function isAggregationFnDescriptor(
}

function warn(message: string) {
if (process.env.NODE_ENV === 'development') {
if (IS_DEV) {
console.warn(message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
SortingState,
} from './rowSortingFeature.types'

const IS_DEV = process.env.NODE_ENV !== 'production'

// State Utils

/**
Expand Down Expand Up @@ -120,7 +122,7 @@ export function column_getAutoSortFn<
let sortFn = sortFns?.[sortFnName]

if (!sortFn) {
if (process.env.NODE_ENV === 'development') {
if (IS_DEV) {
console.warn(
`sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`,
)
Expand Down Expand Up @@ -197,7 +199,7 @@ export function column_getSortFn<

const sortFn = sortFns?.[column.columnDef.sortFn as string]

if (process.env.NODE_ENV === 'development' && !sortFn) {
if (IS_DEV && !sortFn) {
console.warn(
`sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`,
)
Expand Down
77 changes: 39 additions & 38 deletions packages/table-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { NoInfer, RowData, Updater } from './types/type-utils'
import type { TableFeatures } from './types/TableFeatures'
import type { TableState, TableState_All } from './types/TableState'

const IS_DEV = process.env.NODE_ENV === 'development'

/**
* Applies a TanStack updater to a value.
*
Expand Down Expand Up @@ -253,7 +255,7 @@ export function tableMemo<
let debug: boolean | undefined
let debugCache: boolean | undefined

if (process.env.NODE_ENV === 'development') {
if (IS_DEV) {
const { debugAll } = table.options
const { parentName } = getFunctionNameInfo(fnName, '.')

Expand Down Expand Up @@ -314,44 +316,43 @@ export function tableMemo<
schedule(() => untrack(() => onAfterUpdate()))
}

const debugOptions =
process.env.NODE_ENV === 'development'
? {
onBeforeCompare: () => {
if (debugCache) {
beforeCompareTime = performance.now()
}
},
onAfterCompare: (depsChanged: boolean) => {
if (debugCache) {
afterCompareTime = performance.now()
const compareTime =
Math.round((afterCompareTime - beforeCompareTime) * 100) / 100
if (!depsChanged) {
logTime(compareTime, depsChanged)
}
}
},
onBeforeUpdate: () => {
if (debug) {
startCalcTime = performance.now()
}
},
onAfterUpdate: () => {
if (debug) {
endCalcTime = performance.now()
const executionTime =
Math.round((endCalcTime - startCalcTime) * 100) / 100
logTime(executionTime, true)
const debugOptions = IS_DEV
? {
onBeforeCompare: () => {
if (debugCache) {
beforeCompareTime = performance.now()
}
},
onAfterCompare: (depsChanged: boolean) => {
if (debugCache) {
afterCompareTime = performance.now()
const compareTime =
Math.round((afterCompareTime - beforeCompareTime) * 100) / 100
if (!depsChanged) {
logTime(compareTime, depsChanged)
}
onAfterUpdateHandler()
},
}
: {
onAfterUpdate: () => {
onAfterUpdateHandler()
},
}
}
},
onBeforeUpdate: () => {
if (debug) {
startCalcTime = performance.now()
}
},
onAfterUpdate: () => {
if (debug) {
endCalcTime = performance.now()
const executionTime =
Math.round((endCalcTime - startCalcTime) * 100) / 100
logTime(executionTime, true)
}
onAfterUpdateHandler()
},
}
: {
onAfterUpdate: () => {
onAfterUpdateHandler()
},
}

return memo({
...memoOptions,
Expand Down
4 changes: 3 additions & 1 deletion packages/table-core/src/worker/createWorkerRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { RowData } from '../types/type-utils'
import type { TableWorker } from './createTableWorker'
import type { TableWorkerStage } from './tableWorkerProtocol'

const IS_DEV = process.env.NODE_ENV !== 'production'

type AnyTable = Table_Internal<any, any>

function capitalize(stage: string) {
Expand Down Expand Up @@ -60,7 +62,7 @@ export function createWorkerRowModel(
let warned = false

const warnOnce = (message: string) => {
if (process.env.NODE_ENV === 'development' && !warned) {
if (IS_DEV && !warned) {
warned = true
console.warn(`[table-worker] ${message}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,10 @@ describe('createFilteredRowModel', () => {

describe('unresolvable global filter fn', () => {
afterEach(() => {
vi.unstubAllEnvs()
vi.restoreAllMocks()
})

it('should apply no global filtering and warn in dev when the globalFilterFn name is not registered', () => {
vi.stubEnv('NODE_ENV', 'development')
it('should apply no global filtering and warn when the globalFilterFn name is not registered', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})

const table = constructTable<typeof features, TestRow>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { ColumnDef } from '../../../../src'

afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllEnvs()
})

describe('rowAggregationFeature', () => {
Expand Down Expand Up @@ -289,7 +288,6 @@ describe('rowAggregationFeature', () => {
})

it('warns and preserves undefined keys for invalid multi configurations', () => {
vi.stubEnv('NODE_ENV', 'development')
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const features = testFeatures({ rowAggregationFeature, aggregationFns })
const table = constructTable({
Expand Down
Loading