Skip to content
Merged
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
7 changes: 5 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,11 @@ export function format(
css: string,
{ minify = false, tab_size = undefined }: FormatOptions = Object.create(null),
): string {
if (tab_size !== undefined && Number(tab_size) < 1) {
throw new TypeError('tab_size must be a number greater than 0')
if (tab_size !== undefined) {
let normalized = Number(tab_size)
// An invalid tab_size (non-numeric, fractional, NaN, Infinity, < 1) falls
// back to the default tab indentation instead of throwing.
tab_size = Number.isInteger(normalized) && normalized >= 1 ? normalized : undefined
}

const NEWLINE = minify ? EMPTY_STRING : '\n'
Expand Down
33 changes: 27 additions & 6 deletions test/tab-size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,35 @@ test('tab_size: 2', () => {
expect(actual).toEqual(expected)
})

test('invalid tab_size: 0', () => {
expect(() => format(fixture, { tab_size: 0 })).toThrow('tab_size must be a number greater than 0')
let default_indentation = format(fixture)

test('invalid tab_size: 0 falls back to default tab indentation', () => {
expect(format(fixture, { tab_size: 0 })).toEqual(default_indentation)
})

test('invalid tab_size: negative', () => {
expect(() => format(fixture, { tab_size: -1 })).toThrow(
'tab_size must be a number greater than 0',
)
test('invalid tab_size: negative falls back to default tab indentation', () => {
expect(format(fixture, { tab_size: -1 })).toEqual(default_indentation)
})

test('invalid tab_size: fractional falls back to default tab indentation', () => {
expect(format(fixture, { tab_size: 2.5 })).toEqual(default_indentation)
})

test('invalid tab_size: NaN falls back to default tab indentation', () => {
expect(format(fixture, { tab_size: NaN })).toEqual(default_indentation)
})

test('invalid tab_size: Infinity falls back to default tab indentation', () => {
expect(format(fixture, { tab_size: Infinity })).toEqual(default_indentation)
})

test('invalid tab_size: non-numeric string falls back to default tab indentation (bypassing TypeScript types)', () => {
// @ts-expect-error tab_size is typed as number, but JS callers can pass anything at runtime
expect(format(fixture, { tab_size: 'abc' })).toEqual(default_indentation)
})

test('invalid tab_size does not throw', () => {
expect(() => format(fixture, { tab_size: 0 }), 'invalid tab_size should not throw').not.toThrow()
})

test('combine tab_size and minify', () => {
Expand Down
Loading