diff --git a/src/lib/index.ts b/src/lib/index.ts index 515d26f..67434db 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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' diff --git a/test/tab-size.test.ts b/test/tab-size.test.ts index 6332db4..1a2f17a 100644 --- a/test/tab-size.test.ts +++ b/test/tab-size.test.ts @@ -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', () => {