|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { parseSync } from 'oxc-parser' |
| 3 | +import type { Expression, VariableDeclaration } from 'oxc-parser' |
| 4 | + |
| 5 | +import { isSerializable } from '../src/pages/utils.ts' |
| 6 | + |
| 7 | +function parseExpression (source: string): { code: string, node: Expression } { |
| 8 | + // Parse as a variable initialiser so a leading `{` isn't ambiguous with a block, and we get the |
| 9 | + // raw expression node rather than a ParenthesizedExpression wrapper. |
| 10 | + const code = `const __x = ${source}` |
| 11 | + const ast = parseSync('test.js', code, { lang: 'js' }) |
| 12 | + const decl = ast.program.body[0] as VariableDeclaration |
| 13 | + return { code, node: decl.declarations[0]!.init as Expression } |
| 14 | +} |
| 15 | + |
| 16 | +function check (source: string) { |
| 17 | + const { code, node } = parseExpression(source) |
| 18 | + return isSerializable(code, node) |
| 19 | +} |
| 20 | + |
| 21 | +describe('isSerializable', () => { |
| 22 | + it('accepts a plain literal object', () => { |
| 23 | + expect(check(`{ foo: 'bar', baz: 1, qux: true, quux: null }`)).toEqual({ |
| 24 | + value: { foo: 'bar', baz: 1, qux: true, quux: null }, |
| 25 | + serializable: true, |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + it('accepts a nested object containing an array', () => { |
| 30 | + expect(check(`{ a: { b: [1, 2, 3] } }`)).toEqual({ |
| 31 | + value: { a: { b: [1, 2, 3] } }, |
| 32 | + serializable: true, |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('accepts signed numeric literals', () => { |
| 37 | + expect(check(`{ x: -1, y: +2 }`)).toEqual({ |
| 38 | + value: { x: -1, y: 2 }, |
| 39 | + serializable: true, |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('accepts string-keyed properties', () => { |
| 44 | + expect(check(`{ 'foo-bar': 1, '123': 'baz' }`)).toEqual({ |
| 45 | + value: { 'foo-bar': 1, '123': 'baz' }, |
| 46 | + serializable: true, |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('rejects identifier references', () => { |
| 51 | + expect(check(`{ foo: bar }`)).toEqual({ serializable: false }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('rejects call expressions', () => { |
| 55 | + expect(check(`{ foo: bar() }`)).toEqual({ serializable: false }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('rejects spread elements in objects', () => { |
| 59 | + expect(check(`{ ...rest }`)).toEqual({ serializable: false }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('rejects spread elements in arrays', () => { |
| 63 | + expect(check(`[1, ...rest, 3]`)).toEqual({ serializable: false }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('rejects computed keys', () => { |
| 67 | + expect(check(`{ [k]: 1 }`)).toEqual({ serializable: false }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('rejects template literals', () => { |
| 71 | + expect(check('{ foo: `hello` }')).toEqual({ serializable: false }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('rejects regex literals', () => { |
| 75 | + expect(check(`{ foo: /x/ }`)).toEqual({ serializable: false }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('rejects bigint literals', () => { |
| 79 | + expect(check(`{ foo: 1n }`)).toEqual({ serializable: false }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('rejects method shorthand', () => { |
| 83 | + expect(check(`{ foo () {} }`)).toEqual({ serializable: false }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('rejects getters', () => { |
| 87 | + expect(check(`{ get foo () { return 1 } }`)).toEqual({ serializable: false }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('rejects setters', () => { |
| 91 | + expect(check(`{ set foo (v) {} }`)).toEqual({ serializable: false }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('rejects sparse arrays', () => { |
| 95 | + expect(check(`[1, , 3]`)).toEqual({ serializable: false }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('rejects unary operators other than + and -', () => { |
| 99 | + expect(check(`{ x: !true }`)).toEqual({ serializable: false }) |
| 100 | + expect(check(`{ x: ~1 }`)).toEqual({ serializable: false }) |
| 101 | + }) |
| 102 | + |
| 103 | + it('rejects signed non-numeric literals', () => { |
| 104 | + expect(check(`{ x: -'foo' }`)).toEqual({ serializable: false }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('rejects arrow function values', () => { |
| 108 | + expect(check(`{ foo: () => 1 }`)).toEqual({ serializable: false }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments