Skip to content

Commit 90f9ab4

Browse files
marvinjudeLekoArts
andauthored
chore(packages): make unstable_shouldOnCreateNode stable (#36516)
Co-authored-by: LekoArts <lekoarts@gmail.com>
1 parent 4423795 commit 90f9ab4

File tree

30 files changed

+219
-217
lines changed

30 files changed

+219
-217
lines changed

benchmarks/gabe-csv-markdown/gatsby-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ exports.createPages = async ({ graphql, actions }) => {
4242
})
4343
}
4444

45-
exports.unstable_shouldOnCreateNode = ({ node }) =>
45+
exports.shouldOnCreateNode = ({ node }) =>
4646
node.internal.type === `GendataCsv`
4747

4848
// Not sure if there is a better way than to create a proxy node for markdown to pick up

benchmarks/gabe-fs-text/gatsby-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ exports.createPages = async ({ graphql, actions }) => {
4040
})
4141
}
4242

43-
exports.unstable_shouldOnCreateNode = ({ node }) =>
43+
exports.shouldOnCreateNode = ({ node }) =>
4444
node.internal.type === "File"
4545

4646
exports.onCreateNode = ({ node, actions, createNodeId }) => {

packages/gatsby-plugin-mdx/src/gatsby-node.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
282282
}
283283

284284
// eslint-disable-next-line @typescript-eslint/naming-convention
285-
export const unstable_shouldOnCreateNode: GatsbyNode["unstable_shouldOnCreateNode"] =
286-
({ node }: { node: FileSystemNode }, pluginOptions) => {
287-
const { extensions } = defaultOptions(pluginOptions)
288-
return node.internal.type === `File` && extensions.includes(node.ext)
289-
}
285+
export const shouldOnCreateNode: GatsbyNode["shouldOnCreateNode"] = (
286+
{ node }: { node: FileSystemNode },
287+
pluginOptions
288+
) => {
289+
const { extensions } = defaultOptions(pluginOptions)
290+
return node.internal.type === `File` && extensions.includes(node.ext)
291+
}
290292

291293
/**
292294
* Create Mdx nodes from MDX files.

packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require(`path`)
2-
const { onCreateNode } = require(`../gatsby-node`)
2+
const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`)
33

44
jest.mock(`asciidoctor`, () => () => {
55
return {
@@ -47,35 +47,50 @@ describe(`gatsby-transformer-asciidoc`, () => {
4747
createContentDigest = jest.fn(() => `digest`)
4848
})
4949

50-
it(`should do nothing when extension is not whitelisted`, async () => {
50+
it(`should do nothing when extension is not allowed`, async () => {
5151
node.extension = `foo`
52-
await onCreateNode(
53-
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
54-
{}
55-
)
52+
const shouldCreateNode = shouldOnCreateNode({ node }, {})
53+
54+
if (shouldCreateNode) {
55+
await onCreateNode(
56+
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
57+
{}
58+
)
59+
}
5660
expect(actions.createNode).not.toHaveBeenCalled()
5761
})
5862

5963
it(`should enhance available extension`, async () => {
6064
node.extension = `ad`
61-
await onCreateNode(
62-
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
65+
const shouldCreateNode = shouldOnCreateNode(
66+
{ node },
6367
{ fileExtensions: [`ad`] }
6468
)
69+
70+
if (shouldCreateNode) {
71+
await onCreateNode(
72+
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
73+
{ fileExtensions: [`ad`] }
74+
)
75+
}
6576
expect(actions.createNode).toHaveBeenCalled()
6677
})
6778

6879
it(`should create node based on loaded asciidoc file`, async () => {
69-
await onCreateNode(
70-
{
71-
node,
72-
actions,
73-
loadNodeContent,
74-
createNodeId,
75-
createContentDigest,
76-
},
77-
{}
78-
)
80+
const shouldCreateNode = shouldOnCreateNode({ node }, {})
81+
82+
if (shouldCreateNode) {
83+
await onCreateNode(
84+
{
85+
node,
86+
actions,
87+
loadNodeContent,
88+
createNodeId,
89+
createContentDigest,
90+
},
91+
{}
92+
)
93+
}
7994
expect(actions.createNode).toHaveBeenCalledWith({
8095
author: null,
8196
children: [],

packages/gatsby-transformer-asciidoc/src/gatsby-node.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const asciidoc = require(`asciidoctor`)()
22
const _ = require(`lodash`)
33

4-
function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) {
4+
function shouldOnCreateNode({ node }, pluginOptions = {}) {
55
const extensionsConfig = pluginOptions.fileExtensions
66

77
// make extensions configurable and use adoc and asciidoc as default
@@ -23,10 +23,6 @@ async function onCreateNode(
2323
},
2424
pluginOptions
2525
) {
26-
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
27-
return
28-
}
29-
3026
// register custom converter if given
3127
if (pluginOptions.converterFactory) {
3228
asciidoc.ConverterFactory.register(
@@ -140,5 +136,5 @@ const extractPageAttributes = allAttributes =>
140136
return pageAttributes
141137
}, {})
142138

143-
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
139+
exports.shouldOnCreateNode = shouldOnCreateNode
144140
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-csv/src/gatsby-node.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const convertToJson = (data, options) =>
88
.fromString(data)
99
.then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`))
1010

11-
function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) {
11+
function shouldOnCreateNode({ node }, pluginOptions = {}) {
1212
const { extension } = node
1313
const { extensions } = pluginOptions
1414

@@ -19,10 +19,6 @@ async function onCreateNode(
1919
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
2020
pluginOptions
2121
) {
22-
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
23-
return
24-
}
25-
2622
const { createNode, createParentChildLink } = actions
2723

2824
// Destructure out our custom options
@@ -84,5 +80,5 @@ async function onCreateNode(
8480
return
8581
}
8682

87-
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
83+
exports.shouldOnCreateNode = shouldOnCreateNode
8884
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-documentationjs/src/gatsby-node.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ exports.createResolvers = ({ createResolvers }) => {
185185
})
186186
}
187187

188-
function unstable_shouldOnCreateNode({ node }) {
188+
function shouldOnCreateNode({ node }) {
189189
return (
190190
node.internal.type === `File` &&
191191
(node.internal.mediaType === `application/javascript` ||
@@ -195,17 +195,13 @@ function unstable_shouldOnCreateNode({ node }) {
195195
)
196196
}
197197

198-
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
198+
exports.shouldOnCreateNode = shouldOnCreateNode
199199

200200
/**
201201
* Implement the onCreateNode API to create documentation.js nodes
202202
* @param {Object} super this is a super param
203203
*/
204204
exports.onCreateNode = async ({ node, actions, ...helpers }) => {
205-
if (!unstable_shouldOnCreateNode({ node })) {
206-
return null
207-
}
208-
209205
const { createNodeId, createContentDigest } = helpers
210206
const { createNode, createParentChildLink } = actions
211207

packages/gatsby-transformer-excel/src/gatsby-node.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ const extensions = [
2828
`numbers`,
2929
]
3030

31-
function unstable_shouldOnCreateNode({ node }) {
31+
function shouldOnCreateNode({ node }) {
3232
return extensions.includes((node.extension || ``).toLowerCase())
3333
}
3434

3535
async function onCreateNode(
3636
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
3737
options = {}
3838
) {
39-
if (!unstable_shouldOnCreateNode({ node })) {
40-
return
41-
}
42-
4339
const { createNode, createParentChildLink } = actions
4440

4541
// accept *all* options to pass to the sheet_to_json function
@@ -108,5 +104,5 @@ async function onCreateNode(
108104
return
109105
}
110106

111-
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
107+
exports.shouldOnCreateNode = shouldOnCreateNode
112108
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-hjson/src/gatsby-node.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const _ = require(`lodash`)
22
const path = require(`path`)
33
const HJSON = require(`hjson`)
44

5-
function unstable_shouldOnCreateNode({ node }) {
5+
function shouldOnCreateNode({ node }) {
66
// We only care about HJSON content.
77
// NOTE the mime package does not recognize HJSON yet
88
// See RFC https://hjson.org/rfc.html#rfc.section.1.3
@@ -19,10 +19,6 @@ async function onCreateNode({
1919
createNodeId,
2020
createContentDigest,
2121
}) {
22-
if (!unstable_shouldOnCreateNode({ node })) {
23-
return
24-
}
25-
2622
const { createNode, createParentChildLink } = actions
2723

2824
function transformObject(obj, id, type) {
@@ -63,5 +59,5 @@ async function onCreateNode({
6359
}
6460
}
6561

66-
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
62+
exports.shouldOnCreateNode = shouldOnCreateNode
6763
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-javascript-frontmatter/src/__tests__/gatsby-node.js

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { onCreateNode } = require(`../gatsby-node`)
1+
const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`)
22

33
describe(`gatsby-transformer-javascript-frontmatter`, () => {
44
describe(`onCreateNode`, () => {
@@ -43,39 +43,61 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => {
4343
`(
4444
`should loadNodeContent if file has extension $extension`,
4545
async ({ extension }) => {
46-
await onCreateNode({
46+
const shouldCreateNode = shouldOnCreateNode({
4747
node: {
4848
...node,
4949
extension,
5050
},
51-
actions,
52-
loadNodeContent,
53-
createContentDigest,
5451
})
52+
53+
if (shouldCreateNode) {
54+
await onCreateNode({
55+
node: {
56+
...node,
57+
extension,
58+
},
59+
actions,
60+
loadNodeContent,
61+
createContentDigest,
62+
})
63+
}
5564
expect(loadNodeContent).toBeCalled()
5665
}
5766
)
5867

5968
it(`should not loadNodeContent for not javascript file`, async () => {
60-
await onCreateNode({
69+
const shouldCreateNode = shouldOnCreateNode({
6170
node: {
6271
...node,
6372
extension: `csv`,
6473
},
65-
actions,
66-
loadNodeContent,
67-
createContentDigest,
6874
})
75+
76+
if (shouldCreateNode) {
77+
await onCreateNode({
78+
node: {
79+
...node,
80+
extension: `csv`,
81+
},
82+
actions,
83+
loadNodeContent,
84+
createContentDigest,
85+
})
86+
}
6987
expect(loadNodeContent).not.toBeCalled()
7088
})
7189

7290
it(`should load frontmatter data with exported object`, async () => {
73-
await onCreateNode({
74-
node,
75-
actions,
76-
loadNodeContent,
77-
createContentDigest,
78-
})
91+
const shouldCreateNode = shouldOnCreateNode({ node })
92+
93+
if (shouldCreateNode) {
94+
await onCreateNode({
95+
node,
96+
actions,
97+
loadNodeContent,
98+
createContentDigest,
99+
})
100+
}
79101
expect(actions.createNode).toBeCalled()
80102
expect(actions.createNode.mock.calls[0]).toMatchSnapshot()
81103
})
@@ -91,25 +113,33 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => {
91113
description: "Things about the choropleth.",
92114
}
93115
`)
94-
await onCreateNode({
95-
node,
96-
actions,
97-
loadNodeContent,
98-
createContentDigest,
99-
})
116+
const shouldCreateNode = shouldOnCreateNode({ node })
117+
118+
if (shouldCreateNode) {
119+
await onCreateNode({
120+
node,
121+
actions,
122+
loadNodeContent,
123+
createContentDigest,
124+
})
125+
}
100126
expect(actions.createNode).toBeCalled()
101127
expect(actions.createNode.mock.calls[0]).toMatchSnapshot()
102128
})
103129

104130
it(`should pass fileAbsolutePath to node if file type is "File"`, async () => {
105131
node.internal.type = `File`
106132
node.absolutePath = `bar`
107-
await onCreateNode({
108-
node,
109-
actions,
110-
loadNodeContent,
111-
createContentDigest,
112-
})
133+
const shouldCreateNode = shouldOnCreateNode({ node })
134+
135+
if (shouldCreateNode) {
136+
await onCreateNode({
137+
node,
138+
actions,
139+
loadNodeContent,
140+
createContentDigest,
141+
})
142+
}
113143
expect(actions.createNode.mock.calls[0][0].fileAbsolutePath).toEqual(
114144
node.absolutePath
115145
)

0 commit comments

Comments
 (0)