From a0c564071e925662e5d03b73df307233403f10ce Mon Sep 17 00:00:00 2001 From: Francesco Agnoletto Date: Fri, 5 Aug 2022 11:07:05 +0200 Subject: [PATCH 1/5] Convert sanitize-node --- packages/gatsby/src/redux/actions/public.js | 2 +- .../{sanitize-node.js => sanitize-node.ts} | 88 +++++++++++-------- 2 files changed, 53 insertions(+), 37 deletions(-) rename packages/gatsby/src/utils/{sanitize-node.js => sanitize-node.ts} (62%) diff --git a/packages/gatsby/src/redux/actions/public.js b/packages/gatsby/src/redux/actions/public.js index 79feb63e3e470..cdec68cb0c7b4 100644 --- a/packages/gatsby/src/redux/actions/public.js +++ b/packages/gatsby/src/redux/actions/public.js @@ -11,7 +11,7 @@ const url = require(`url`) const { slash, createContentDigest } = require(`gatsby-core-utils`) const { hasNodeChanged } = require(`../../utils/nodes`) const { getNode, getDataStore } = require(`../../datastore`) -const sanitizeNode = require(`../../utils/sanitize-node`) +import sanitizeNode from "../../utils/sanitize-node" const { store } = require(`../index`) const { validatePageComponent } = require(`../../utils/validate-page-component`) import { nodeSchema } from "../../joi-schemas/joi" diff --git a/packages/gatsby/src/utils/sanitize-node.js b/packages/gatsby/src/utils/sanitize-node.ts similarity index 62% rename from packages/gatsby/src/utils/sanitize-node.js rename to packages/gatsby/src/utils/sanitize-node.ts index 43cdb19376a54..d86601a6a08fe 100644 --- a/packages/gatsby/src/utils/sanitize-node.js +++ b/packages/gatsby/src/utils/sanitize-node.ts @@ -1,12 +1,60 @@ import _ from "lodash" +import type { IGatsbyNode } from "../redux/types" +import type { GatsbyIterable } from "../datastore/common/iterable" + +type data = IGatsbyNode | GatsbyIterable + +/** + * @param {Object|Array} data + * @returns {Object|Array} data without undefined values + */ +type omitUndefined = (data: data) => Partial + +const omitUndefined: omitUndefined = data => { + const isPlainObject = _.isPlainObject(data) + if (isPlainObject) { + return _.pickBy(data, p => p !== undefined) + } + + return (data as GatsbyIterable).filter(p => p !== undefined) +} + +/** + * @param {*} data + * @return {boolean} + */ +type isTypeSupported = (data: data) => boolean + +const isTypeSupported: isTypeSupported = data => { + if (data === null) { + return true + } + + const type = typeof data + const isSupported = + type === `number` || + type === `string` || + type === `boolean` || + data instanceof Date + + return isSupported +} + /** * Make data serializable * @param {(Object|Array)} data to sanitize * @param {boolean} isNode = true * @param {Set} path = new Set */ -const sanitizeNode = (data, isNode = true, path = new Set()) => { + +type sanitizeNode = ( + data: data, + isNode?: boolean, + path?: Set +) => data | undefined + +const sanitizeNode: sanitizeNode = (data, isNode = true, path = new Set()) => { const isPlainObject = _.isPlainObject(data) if (isPlainObject || _.isArray(data)) { @@ -20,7 +68,7 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => { returnData[key] = o return } - returnData[key] = sanitizeNode(o, false, path) + returnData[key] = sanitizeNode(o as data, false, path) if (returnData[key] !== o) { anyFieldChanged = true @@ -28,7 +76,7 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => { }) if (anyFieldChanged) { - data = omitUndefined(returnData) + data = omitUndefined(returnData as data) as data } // arrays and plain objects are supported - no need to to sanitize @@ -42,36 +90,4 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => { } } -/** - * @param {Object|Array} data - * @returns {Object|Array} data without undefined values - */ -const omitUndefined = data => { - const isPlainObject = _.isPlainObject(data) - if (isPlainObject) { - return _.pickBy(data, p => p !== undefined) - } - - return data.filter(p => p !== undefined) -} - -/** - * @param {*} data - * @return {boolean} - */ -const isTypeSupported = data => { - if (data === null) { - return true - } - - const type = typeof data - const isSupported = - type === `number` || - type === `string` || - type === `boolean` || - data instanceof Date - - return isSupported -} - -module.exports = sanitizeNode +export default sanitizeNode From d5082a9a1be4371a5f666bce4615ccbad17f328a Mon Sep 17 00:00:00 2001 From: Francesco Agnoletto Date: Fri, 5 Aug 2022 11:07:35 +0200 Subject: [PATCH 2/5] Convert sanitize-node test --- ...anitize-node.js.snap => sanitize-node.ts.snap} | 0 .../{sanitize-node.js => sanitize-node.ts} | 15 +++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) rename packages/gatsby/src/utils/__tests__/__snapshots__/{sanitize-node.js.snap => sanitize-node.ts.snap} (100%) rename packages/gatsby/src/utils/__tests__/{sanitize-node.js => sanitize-node.ts} (81%) diff --git a/packages/gatsby/src/utils/__tests__/__snapshots__/sanitize-node.js.snap b/packages/gatsby/src/utils/__tests__/__snapshots__/sanitize-node.ts.snap similarity index 100% rename from packages/gatsby/src/utils/__tests__/__snapshots__/sanitize-node.js.snap rename to packages/gatsby/src/utils/__tests__/__snapshots__/sanitize-node.ts.snap diff --git a/packages/gatsby/src/utils/__tests__/sanitize-node.js b/packages/gatsby/src/utils/__tests__/sanitize-node.ts similarity index 81% rename from packages/gatsby/src/utils/__tests__/sanitize-node.js rename to packages/gatsby/src/utils/__tests__/sanitize-node.ts index 0520885306b8a..6776bd8e2ed7a 100644 --- a/packages/gatsby/src/utils/__tests__/sanitize-node.js +++ b/packages/gatsby/src/utils/__tests__/sanitize-node.ts @@ -1,23 +1,25 @@ -const sanitizeNode = require(`../sanitize-node`) +import sanitizeNode from "../sanitize-node" describe(`node sanitization`, () => { let testNode beforeEach(() => { const circularReference = {} + // @ts-ignore edge test case circularReference.self = circularReference const indirectCircular = { down1: { down2: {}, }, } + // @ts-ignore edge test case indirectCircular.down1.down2.deepCircular = indirectCircular testNode = { id: `id1`, parent: null, children: [], - unsupported: () => {}, + unsupported: (): void => {}, inlineObject: { field: `fieldOfFirstNode`, re: /re/, @@ -26,7 +28,7 @@ describe(`node sanitization`, () => { repeat2: `bar`, repeat3: { repeat3: { - test: () => {}, + test: (): void => {}, repeat: `bar`, }, }, @@ -46,8 +48,11 @@ describe(`node sanitization`, () => { it(`Remove not supported fields / values`, () => { const result = sanitizeNode(testNode) expect(result).toMatchSnapshot() + // @ts-ignore test properties that shouldn't be there are not there expect(result.unsupported).not.toBeDefined() + // @ts-ignore test properties that shouldn't be there are not there expect(result.inlineObject.re).not.toBeDefined() + // @ts-ignore test properties that shouldn't be there are not there expect(result.inlineArray[3]).not.toBeDefined() }) @@ -66,7 +71,7 @@ describe(`node sanitization`, () => { it(`Doesn't create clones if it doesn't have to`, () => { const testNodeWithoutUnserializableData = { id: `id1`, - parent: null, + parent: ``, children: [], inlineObject: { field: `fieldOfFirstNode`, @@ -76,7 +81,9 @@ describe(`node sanitization`, () => { type: `Test`, contentDigest: `digest1`, owner: `test`, + counter: 0, }, + fields: [], } const result = sanitizeNode(testNodeWithoutUnserializableData) From 09e5fa05b1b449dadd746b194c659f3910b29cd8 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 11 Aug 2022 13:35:03 -0400 Subject: [PATCH 3/5] Change to trigger build --- packages/gatsby/src/utils/sanitize-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/sanitize-node.ts b/packages/gatsby/src/utils/sanitize-node.ts index d86601a6a08fe..ac703311024ea 100644 --- a/packages/gatsby/src/utils/sanitize-node.ts +++ b/packages/gatsby/src/utils/sanitize-node.ts @@ -22,7 +22,7 @@ const omitUndefined: omitUndefined = data => { /** * @param {*} data - * @return {boolean} + * @return {boolean} */ type isTypeSupported = (data: data) => boolean From 5c5de8ac218f693ba75ee53f4eb1f912ab9bf385 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 11 Aug 2022 13:53:29 -0400 Subject: [PATCH 4/5] Change to trigger build --- packages/gatsby/src/utils/sanitize-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/sanitize-node.ts b/packages/gatsby/src/utils/sanitize-node.ts index ac703311024ea..d86601a6a08fe 100644 --- a/packages/gatsby/src/utils/sanitize-node.ts +++ b/packages/gatsby/src/utils/sanitize-node.ts @@ -22,7 +22,7 @@ const omitUndefined: omitUndefined = data => { /** * @param {*} data - * @return {boolean} + * @return {boolean} */ type isTypeSupported = (data: data) => boolean From 90d38b0c511c8bbba4a9e9ff756320887fbde3d4 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Fri, 12 Aug 2022 14:06:02 -0400 Subject: [PATCH 5/5] Change to trigger build --- packages/gatsby/src/utils/__tests__/sanitize-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/__tests__/sanitize-node.ts b/packages/gatsby/src/utils/__tests__/sanitize-node.ts index 6776bd8e2ed7a..429bb825d0d0a 100644 --- a/packages/gatsby/src/utils/__tests__/sanitize-node.ts +++ b/packages/gatsby/src/utils/__tests__/sanitize-node.ts @@ -12,7 +12,7 @@ describe(`node sanitization`, () => { down2: {}, }, } - // @ts-ignore edge test case + // @ts-ignore edge test case indirectCircular.down1.down2.deepCircular = indirectCircular testNode = {