Skip to content

Commit 9a0b348

Browse files
thymikeeSimenB
authored andcommitted
chore: migrate jest-serializer to TypeScript (#7841)
1 parent d96ff2c commit 9a0b348

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827))
1919
- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835))
2020
- `[jest-docblock]`: Migrate to TypeScript ([#7836](https://github.com/facebook/jest/pull/7836))
21+
- `[jest-serializer]`: Migrate to TypeScript ([#7841](https://github.com/facebook/jest/pull/7841))
2122

2223
### Performance
2324

packages/jest-serializer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
},
1212
"license": "MIT",
1313
"main": "build/index.js",
14+
"types": "build/index.d.ts",
1415
"gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60"
1516
}

packages/jest-serializer/src/__tests__/index.test.js renamed to packages/jest-serializer/src/__tests__/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const objs = [
3434
{key1: 'foo', key2: 'bar', key3: {array: [null, {}]}},
3535
{minusInf: -Infinity, nan: NaN, plusInf: +Infinity},
3636
{date: new Date(1234567890), re: /foo/gi},
37+
// @ts-ignore - testing NaN
3738
{map: new Map([[NaN, 4], [undefined, 'm']]), set: new Set([undefined, NaN])},
3839
{buf: Buffer.from([0, 255, 127])},
3940
];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
declare module 'v8' {
9+
function serialize(value: unknown): Buffer;
10+
function deserialize(value: Buffer): unknown;
11+
}

packages/jest-serializer/src/index.js renamed to packages/jest-serializer/src/index.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
'use strict';
11-
128
import fs from 'fs';
139
import v8 from 'v8';
1410

15-
import type {Path} from 'types/Config';
11+
type Path = string;
1612

1713
// JSON and V8 serializers are both stable when it comes to compatibility. The
1814
// current JSON specification is well defined in RFC 8259, and V8 ensures that
@@ -23,7 +19,7 @@ const JS_TYPE = '__$t__';
2319
const JS_VALUE = '__$v__';
2420
const JS_VF = '__$f__';
2521

26-
function replacer(key: string, value: any): any {
22+
function replacer(_key: string, value: any): any {
2723
// NaN cannot be in a switch statement, because NaN !== NaN.
2824
if (Number.isNaN(value)) {
2925
return {[JS_TYPE]: 'n'};
@@ -32,72 +28,58 @@ function replacer(key: string, value: any): any {
3228
switch (value) {
3329
case undefined:
3430
return {[JS_TYPE]: 'u'};
35-
3631
case +Infinity:
3732
return {[JS_TYPE]: '+'};
38-
3933
case -Infinity:
4034
return {[JS_TYPE]: '-'};
4135
}
4236

4337
switch (value && value.constructor) {
4438
case Date:
4539
return {[JS_TYPE]: 'd', [JS_VALUE]: value.getTime()};
46-
4740
case RegExp:
4841
return {[JS_TYPE]: 'r', [JS_VALUE]: value.source, [JS_VF]: value.flags};
49-
5042
case Set:
5143
return {[JS_TYPE]: 's', [JS_VALUE]: Array.from(value)};
52-
5344
case Map:
5445
return {[JS_TYPE]: 'm', [JS_VALUE]: Array.from(value)};
55-
5646
case Buffer:
5747
return {[JS_TYPE]: 'b', [JS_VALUE]: value.toString('latin1')};
5848
}
5949

6050
return value;
6151
}
6252

63-
function reviver(key: string, value: any): any {
53+
function reviver(_key: string, value: any): any {
6454
if (!value || (typeof value !== 'object' && !value.hasOwnProperty(JS_TYPE))) {
6555
return value;
6656
}
6757

6858
switch (value[JS_TYPE]) {
6959
case 'u':
7060
return undefined;
71-
7261
case 'n':
7362
return NaN;
74-
7563
case '+':
7664
return +Infinity;
77-
7865
case '-':
7966
return -Infinity;
80-
8167
case 'd':
8268
return new Date(value[JS_VALUE]);
83-
8469
case 'r':
8570
return new RegExp(value[JS_VALUE], value[JS_VF]);
86-
8771
case 's':
8872
return new Set(value[JS_VALUE]);
89-
9073
case 'm':
9174
return new Map(value[JS_VALUE]);
92-
9375
case 'b':
9476
return Buffer.from(value[JS_VALUE], 'latin1');
9577
}
9678

9779
return value;
9880
}
9981

100-
function jsonStringify(content) {
82+
function jsonStringify(content: unknown) {
10183
// Not pretty, but the ES JSON spec says that "toJSON" will be called before
10284
// getting into your replacer, so we have to remove them beforehand. See
10385
// https://www.ecma-international.org/ecma-262/#sec-serializejsonproperty
@@ -109,37 +91,33 @@ function jsonStringify(content) {
10991
/* eslint-disable no-extend-native */
11092

11193
try {
112-
// $FlowFixMe: intentional removal of "toJSON" property.
94+
// @ts-ignore intentional removal of "toJSON" property.
11395
Date.prototype.toJSON = undefined;
114-
// $FlowFixMe: intentional removal of "toJSON" property.
96+
// @ts-ignore intentional removal of "toJSON" property.
11597
Buffer.prototype.toJSON = undefined;
11698

11799
return JSON.stringify(content, replacer);
118100
} finally {
119-
// $FlowFixMe: intentional assignment of "toJSON" property.
120101
Date.prototype.toJSON = dateToJSON;
121-
// $FlowFixMe: intentional assignment of "toJSON" property.
122102
Buffer.prototype.toJSON = bufferToJSON;
123103
}
124104

125105
/* eslint-enable no-extend-native */
126106
}
127107

128-
function jsonParse(content) {
108+
function jsonParse(content: string) {
129109
return JSON.parse(content, reviver);
130110
}
131111

132112
// In memory functions.
133113

134114
export function deserialize(buffer: Buffer): any {
135-
// $FlowFixMe - Node 8+ only
136115
return v8.deserialize
137116
? v8.deserialize(buffer)
138117
: jsonParse(buffer.toString('utf8'));
139118
}
140119

141-
export function serialize(content: any): Buffer {
142-
// $FlowFixMe - Node 8+ only
120+
export function serialize(content: unknown): Buffer {
143121
return v8.serialize
144122
? v8.serialize(content)
145123
: Buffer.from(jsonStringify(content));
@@ -148,14 +126,12 @@ export function serialize(content: any): Buffer {
148126
// Synchronous filesystem functions.
149127

150128
export function readFileSync(filePath: Path): any {
151-
// $FlowFixMe - Node 8+ only
152129
return v8.deserialize
153130
? v8.deserialize(fs.readFileSync(filePath))
154131
: jsonParse(fs.readFileSync(filePath, 'utf8'));
155132
}
156133

157134
export function writeFileSync(filePath: Path, content: any) {
158-
// $FlowFixMe - Node 8+ only
159135
return v8.serialize
160136
? fs.writeFileSync(filePath, v8.serialize(content))
161137
: fs.writeFileSync(filePath, jsonStringify(content), 'utf8');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
}
7+
}

0 commit comments

Comments
 (0)