Skip to content

Commit d59e7b6

Browse files
Kornilimjoshin
andauthored
chore(gatsby): convert sanitize-node to typescript (#36327)
* Convert sanitize-node * Convert sanitize-node test * Change to trigger build * Change to trigger build * Change to trigger build Co-authored-by: Josh Johnson <[email protected]>
1 parent 14792cf commit d59e7b6

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

packages/gatsby/src/redux/actions/public.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
const { splitComponentPath } = require(`gatsby-core-utils/parse-component-path`)
1616
const { hasNodeChanged } = require(`../../utils/nodes`)
1717
const { getNode, getDataStore } = require(`../../datastore`)
18-
const sanitizeNode = require(`../../utils/sanitize-node`)
18+
import sanitizeNode from "../../utils/sanitize-node"
1919
const { store } = require(`../index`)
2020
const { validatePageComponent } = require(`../../utils/validate-page-component`)
2121
import { nodeSchema } from "../../joi-schemas/joi"

packages/gatsby/src/utils/__tests__/sanitize-node.js renamed to packages/gatsby/src/utils/__tests__/sanitize-node.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
const sanitizeNode = require(`../sanitize-node`)
1+
import sanitizeNode from "../sanitize-node"
22

33
describe(`node sanitization`, () => {
44
let testNode
55

66
beforeEach(() => {
77
const circularReference = {}
8+
// @ts-ignore edge test case
89
circularReference.self = circularReference
910
const indirectCircular = {
1011
down1: {
1112
down2: {},
1213
},
1314
}
15+
// @ts-ignore edge test case
1416
indirectCircular.down1.down2.deepCircular = indirectCircular
1517

1618
testNode = {
1719
id: `id1`,
1820
parent: null,
1921
children: [],
20-
unsupported: () => {},
22+
unsupported: (): void => {},
2123
inlineObject: {
2224
field: `fieldOfFirstNode`,
2325
re: /re/,
@@ -26,7 +28,7 @@ describe(`node sanitization`, () => {
2628
repeat2: `bar`,
2729
repeat3: {
2830
repeat3: {
29-
test: () => {},
31+
test: (): void => {},
3032
repeat: `bar`,
3133
},
3234
},
@@ -46,8 +48,11 @@ describe(`node sanitization`, () => {
4648
it(`Remove not supported fields / values`, () => {
4749
const result = sanitizeNode(testNode)
4850
expect(result).toMatchSnapshot()
51+
// @ts-ignore test properties that shouldn't be there are not there
4952
expect(result.unsupported).not.toBeDefined()
53+
// @ts-ignore test properties that shouldn't be there are not there
5054
expect(result.inlineObject.re).not.toBeDefined()
55+
// @ts-ignore test properties that shouldn't be there are not there
5156
expect(result.inlineArray[3]).not.toBeDefined()
5257
})
5358

@@ -66,7 +71,7 @@ describe(`node sanitization`, () => {
6671
it(`Doesn't create clones if it doesn't have to`, () => {
6772
const testNodeWithoutUnserializableData = {
6873
id: `id1`,
69-
parent: null,
74+
parent: ``,
7075
children: [],
7176
inlineObject: {
7277
field: `fieldOfFirstNode`,
@@ -76,7 +81,9 @@ describe(`node sanitization`, () => {
7681
type: `Test`,
7782
contentDigest: `digest1`,
7883
owner: `test`,
84+
counter: 0,
7985
},
86+
fields: [],
8087
}
8188

8289
const result = sanitizeNode(testNodeWithoutUnserializableData)
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,60 @@
11
import _ from "lodash"
22

3+
import type { IGatsbyNode } from "../redux/types"
4+
import type { GatsbyIterable } from "../datastore/common/iterable"
5+
6+
type data = IGatsbyNode | GatsbyIterable<IGatsbyNode>
7+
8+
/**
9+
* @param {Object|Array} data
10+
* @returns {Object|Array} data without undefined values
11+
*/
12+
type omitUndefined = (data: data) => Partial<data>
13+
14+
const omitUndefined: omitUndefined = data => {
15+
const isPlainObject = _.isPlainObject(data)
16+
if (isPlainObject) {
17+
return _.pickBy(data, p => p !== undefined)
18+
}
19+
20+
return (data as GatsbyIterable<IGatsbyNode>).filter(p => p !== undefined)
21+
}
22+
23+
/**
24+
* @param {*} data
25+
* @return {boolean}
26+
*/
27+
type isTypeSupported = (data: data) => boolean
28+
29+
const isTypeSupported: isTypeSupported = data => {
30+
if (data === null) {
31+
return true
32+
}
33+
34+
const type = typeof data
35+
const isSupported =
36+
type === `number` ||
37+
type === `string` ||
38+
type === `boolean` ||
39+
data instanceof Date
40+
41+
return isSupported
42+
}
43+
344
/**
445
* Make data serializable
546
* @param {(Object|Array)} data to sanitize
647
* @param {boolean} isNode = true
748
* @param {Set<string>} path = new Set
849
*/
9-
const sanitizeNode = (data, isNode = true, path = new Set()) => {
50+
51+
type sanitizeNode = (
52+
data: data,
53+
isNode?: boolean,
54+
path?: Set<unknown>
55+
) => data | undefined
56+
57+
const sanitizeNode: sanitizeNode = (data, isNode = true, path = new Set()) => {
1058
const isPlainObject = _.isPlainObject(data)
1159

1260
if (isPlainObject || _.isArray(data)) {
@@ -20,15 +68,15 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => {
2068
returnData[key] = o
2169
return
2270
}
23-
returnData[key] = sanitizeNode(o, false, path)
71+
returnData[key] = sanitizeNode(o as data, false, path)
2472

2573
if (returnData[key] !== o) {
2674
anyFieldChanged = true
2775
}
2876
})
2977

3078
if (anyFieldChanged) {
31-
data = omitUndefined(returnData)
79+
data = omitUndefined(returnData as data) as data
3280
}
3381

3482
// arrays and plain objects are supported - no need to to sanitize
@@ -42,36 +90,4 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => {
4290
}
4391
}
4492

45-
/**
46-
* @param {Object|Array} data
47-
* @returns {Object|Array} data without undefined values
48-
*/
49-
const omitUndefined = data => {
50-
const isPlainObject = _.isPlainObject(data)
51-
if (isPlainObject) {
52-
return _.pickBy(data, p => p !== undefined)
53-
}
54-
55-
return data.filter(p => p !== undefined)
56-
}
57-
58-
/**
59-
* @param {*} data
60-
* @return {boolean}
61-
*/
62-
const isTypeSupported = data => {
63-
if (data === null) {
64-
return true
65-
}
66-
67-
const type = typeof data
68-
const isSupported =
69-
type === `number` ||
70-
type === `string` ||
71-
type === `boolean` ||
72-
data instanceof Date
73-
74-
return isSupported
75-
}
76-
77-
module.exports = sanitizeNode
93+
export default sanitizeNode

0 commit comments

Comments
 (0)