Skip to content

Commit

Permalink
Remove MethodShorthand and ArrayPrototypeValues
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Nov 14, 2023
1 parent a0054f7 commit ae33b94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 54 deletions.
5 changes: 0 additions & 5 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ By default, all feature flags are enabled. The following are the feature flags a

- [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError)
- Compiles down to `Error` instead.
- [`ArrayPrototypeValues`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values)
- Used for `Iterable`, uses `Symbol.iterator` instead.
- [`ArrowFunction`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
- Uses function expressions for top-level and for deferred `Promise` values
- method shorthands (if `MethodShortand` is not set) or function expressions for `Iterable`.
Expand All @@ -58,9 +56,6 @@ By default, all feature flags are enabled. The following are the feature flags a
- Affects both `Error` and `AggregateError`
- [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
- Disables serialization of `Map`
- [`MethodShorthand`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions)
- Uses function expressions instead.
- Only affects `Iterable`
- [`ObjectAssign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
- Uses manual object assignments instead.
- Affects `Iterable`, `Error`, `AggregateError` and `Object.create(null)`
Expand Down
2 changes: 0 additions & 2 deletions packages/seroval/src/core/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

export const enum Feature {
AggregateError = 0x01,
ArrayPrototypeValues = 0x02,
ArrowFunction = 0x04,
BigInt = 0x08,
ErrorPrototypeStack = 0x10,
Map = 0x20,
MethodShorthand = 0x40,
ObjectAssign = 0x80,
Promise = 0x100,
Set = 0x200,
Expand Down
8 changes: 1 addition & 7 deletions packages/seroval/test/__snapshots__/iterable.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Iterable > compat > should use Symbol.iterator instead of Array.values. 1`] = `"(h=>(h={[Symbol.iterator]:(s=>((i,c,d)=>(i=0,{next:()=>{c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})))({v:[h,void 0],t:-1,d:1})}))()"`;

exports[`Iterable > compat > should use functions instead of method shorthand. 1`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`;

exports[`Iterable > compat > should use method shorthand instead of arrow functions. 1`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`;

exports[`Iterable > compat#toJSON > should use Symbol.iterator instead of Array.values. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16381,\\"m\\":[0]}"`;

exports[`Iterable > compat#toJSON > should use Symbol.iterator instead of Array.values. 2`] = `"(h=>(h={[Symbol.iterator]:(s=>((i,c,d)=>(i=0,{next:()=>{c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})))({v:[h,void 0],t:-1,d:1})}))()"`;

exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16315,\\"m\\":[0]}"`;
exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 1`] = `"{\\"t\\":{\\"t\\":10,\\"i\\":0,\\"p\\":{\\"k\\":[0],\\"v\\":[{\\"t\\":10,\\"i\\":1,\\"p\\":{\\"k\\":[\\"v\\",\\"t\\",\\"d\\"],\\"v\\":[{\\"t\\":9,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":4,\\"i\\":0},{\\"t\\":2,\\"s\\":1}],\\"o\\":0},{\\"t\\":0,\\"s\\":-1},{\\"t\\":0,\\"s\\":1}],\\"s\\":3},\\"o\\":0,\\"x\\":{}}],\\"s\\":1},\\"o\\":0,\\"x\\":{\\"1\\":{\\"t\\":41,\\"i\\":4,\\"s\\":1},\\"2\\":{\\"t\\":17,\\"i\\":3,\\"s\\":3}}},\\"f\\":16379,\\"m\\":[0]}"`;

exports[`Iterable > compat#toJSON > should use functions instead of method shorthand. 2`] = `"(function(h){return(h={[Symbol.iterator]:(function(s){return(function(i,c,d){return(i=0,{next:function(){c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}}})})})({v:[h,void 0],t:-1,d:1})})})()"`;

Expand Down
26 changes: 2 additions & 24 deletions packages/seroval/test/iterable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,6 @@ describe('Iterable', () => {
});
});
describe('compat', () => {
it('should use Symbol.iterator instead of Array.values.', () => {
const example = {
* [Symbol.iterator](): unknown {
yield example;
},
};
expect(serialize(example, {
disabledFeatures: Feature.ArrayPrototypeValues,
})).toMatchSnapshot();
});
it('should use method shorthand instead of arrow functions.', () => {
const example = {
* [Symbol.iterator](): unknown {
Expand All @@ -223,23 +213,11 @@ describe('Iterable', () => {
},
};
expect(serialize(example, {
disabledFeatures: Feature.MethodShorthand | Feature.ArrowFunction,
disabledFeatures: Feature.ArrowFunction,
})).toMatchSnapshot();
});
});
describe('compat#toJSON', () => {
it('should use Symbol.iterator instead of Array.values.', () => {
const example = {
* [Symbol.iterator](): unknown {
yield example;
},
};
const result = toJSON(example, {
disabledFeatures: Feature.ArrayPrototypeValues,
});
expect(JSON.stringify(result)).toMatchSnapshot();
expect(compileJSON(result)).toMatchSnapshot();
});
it('should use method shorthand instead of arrow functions.', () => {
const example = {
* [Symbol.iterator](): unknown {
Expand All @@ -259,7 +237,7 @@ describe('Iterable', () => {
},
};
const result = toJSON(example, {
disabledFeatures: Feature.MethodShorthand | Feature.ArrowFunction,
disabledFeatures: Feature.ArrowFunction,
});
expect(JSON.stringify(result)).toMatchSnapshot();
expect(compileJSON(result)).toMatchSnapshot();
Expand Down
48 changes: 32 additions & 16 deletions packages/seroval/test2.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@

import { crossSerializeStream } from './dist/esm/development/index.mjs';

const source = new ReadableStream({
start(controller) {
controller.enqueue('Hello');
controller.enqueue('World');
controller.close();
}
})

crossSerializeStream(source, {
scopeId: 'example',
onSerialize(data) {
console.log([data]);
}
});
import { serialize } from './dist/esm/development/index.mjs';

// const instance = new Serializer({
// globalIdentifier: 'test',
// onData: console.log,
// });

// const example = { foo: 'bar' };

// function* hello() {
// yield example;
// }

// instance.write('foo', hello());
// instance.write('bar', hello());

const example = {
a: new Map(),
b: new Set(),
};

example.a.set(example.a, example.b);

const example2 = {
foo: new Map(),
bar: new Set(),
};

example2.foo.set(example2.foo, example2.bar);
example2.heck = example;

console.log(serialize(example2));

0 comments on commit ae33b94

Please sign in to comment.