Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(packages): make unstable_shouldOnCreateNode stable #36516

Merged
merged 6 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/gabe-csv-markdown/gatsby-node.js
Expand Up @@ -42,7 +42,7 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

exports.unstable_shouldOnCreateNode = ({ node }) =>
exports.shouldOnCreateNode = ({ node }) =>
node.internal.type === `GendataCsv`

// Not sure if there is a better way than to create a proxy node for markdown to pick up
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/gabe-fs-text/gatsby-node.js
Expand Up @@ -40,7 +40,7 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

exports.unstable_shouldOnCreateNode = ({ node }) =>
exports.shouldOnCreateNode = ({ node }) =>
node.internal.type === "File"

exports.onCreateNode = ({ node, actions, createNodeId }) => {
Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby-plugin-mdx/src/gatsby-node.ts
Expand Up @@ -282,11 +282,13 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
}

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

/**
* Create Mdx nodes from MDX files.
Expand Down
51 changes: 33 additions & 18 deletions packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js
@@ -1,5 +1,5 @@
const path = require(`path`)
const { onCreateNode } = require(`../gatsby-node`)
const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`)

jest.mock(`asciidoctor`, () => () => {
return {
Expand Down Expand Up @@ -47,35 +47,50 @@ describe(`gatsby-transformer-asciidoc`, () => {
createContentDigest = jest.fn(() => `digest`)
})

it(`should do nothing when extension is not whitelisted`, async () => {
it(`should do nothing when extension is not allowed`, async () => {
node.extension = `foo`
await onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
{}
)
const shouldCreateNode = shouldOnCreateNode({ node }, {})

if (shouldCreateNode) {
await onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
{}
)
}
expect(actions.createNode).not.toHaveBeenCalled()
})

it(`should enhance available extension`, async () => {
node.extension = `ad`
await onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
const shouldCreateNode = shouldOnCreateNode(
{ node },
{ fileExtensions: [`ad`] }
)

if (shouldCreateNode) {
await onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
{ fileExtensions: [`ad`] }
)
}
expect(actions.createNode).toHaveBeenCalled()
})

it(`should create node based on loaded asciidoc file`, async () => {
await onCreateNode(
{
node,
actions,
loadNodeContent,
createNodeId,
createContentDigest,
},
{}
)
const shouldCreateNode = shouldOnCreateNode({ node }, {})

if (shouldCreateNode) {
await onCreateNode(
{
node,
actions,
loadNodeContent,
createNodeId,
createContentDigest,
},
{}
)
}
expect(actions.createNode).toHaveBeenCalledWith({
author: null,
children: [],
Expand Down
8 changes: 2 additions & 6 deletions packages/gatsby-transformer-asciidoc/src/gatsby-node.js
@@ -1,7 +1,7 @@
const asciidoc = require(`asciidoctor`)()
const _ = require(`lodash`)

function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) {
function shouldOnCreateNode({ node }, pluginOptions = {}) {
const extensionsConfig = pluginOptions.fileExtensions

// make extensions configurable and use adoc and asciidoc as default
Expand All @@ -23,10 +23,6 @@ async function onCreateNode(
},
pluginOptions
) {
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
return
}

// register custom converter if given
if (pluginOptions.converterFactory) {
asciidoc.ConverterFactory.register(
Expand Down Expand Up @@ -140,5 +136,5 @@ const extractPageAttributes = allAttributes =>
return pageAttributes
}, {})

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.shouldOnCreateNode = shouldOnCreateNode
exports.onCreateNode = onCreateNode
8 changes: 2 additions & 6 deletions packages/gatsby-transformer-csv/src/gatsby-node.js
Expand Up @@ -8,7 +8,7 @@ const convertToJson = (data, options) =>
.fromString(data)
.then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`))

function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) {
function shouldOnCreateNode({ node }, pluginOptions = {}) {
const { extension } = node
const { extensions } = pluginOptions

Expand All @@ -19,10 +19,6 @@ async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
pluginOptions
) {
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
return
}

const { createNode, createParentChildLink } = actions

// Destructure out our custom options
Expand Down Expand Up @@ -84,5 +80,5 @@ async function onCreateNode(
return
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.shouldOnCreateNode = shouldOnCreateNode
exports.onCreateNode = onCreateNode
Expand Up @@ -185,7 +185,7 @@ exports.createResolvers = ({ createResolvers }) => {
})
}

function unstable_shouldOnCreateNode({ node }) {
function shouldOnCreateNode({ node }) {
return (
node.internal.type === `File` &&
(node.internal.mediaType === `application/javascript` ||
Expand All @@ -195,17 +195,13 @@ function unstable_shouldOnCreateNode({ node }) {
)
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.shouldOnCreateNode = shouldOnCreateNode

/**
* Implement the onCreateNode API to create documentation.js nodes
* @param {Object} super this is a super param
*/
exports.onCreateNode = async ({ node, actions, ...helpers }) => {
if (!unstable_shouldOnCreateNode({ node })) {
return null
}

const { createNodeId, createContentDigest } = helpers
const { createNode, createParentChildLink } = actions

Expand Down
8 changes: 2 additions & 6 deletions packages/gatsby-transformer-excel/src/gatsby-node.js
Expand Up @@ -28,18 +28,14 @@ const extensions = [
`numbers`,
]

function unstable_shouldOnCreateNode({ node }) {
function shouldOnCreateNode({ node }) {
return extensions.includes((node.extension || ``).toLowerCase())
}

async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
options = {}
) {
if (!unstable_shouldOnCreateNode({ node })) {
return
}

const { createNode, createParentChildLink } = actions

// accept *all* options to pass to the sheet_to_json function
Expand Down Expand Up @@ -108,5 +104,5 @@ async function onCreateNode(
return
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.shouldOnCreateNode = shouldOnCreateNode
exports.onCreateNode = onCreateNode
8 changes: 2 additions & 6 deletions packages/gatsby-transformer-hjson/src/gatsby-node.js
Expand Up @@ -2,7 +2,7 @@ const _ = require(`lodash`)
const path = require(`path`)
const HJSON = require(`hjson`)

function unstable_shouldOnCreateNode({ node }) {
function shouldOnCreateNode({ node }) {
// We only care about HJSON content.
// NOTE the mime package does not recognize HJSON yet
// See RFC https://hjson.org/rfc.html#rfc.section.1.3
Expand All @@ -19,10 +19,6 @@ async function onCreateNode({
createNodeId,
createContentDigest,
}) {
if (!unstable_shouldOnCreateNode({ node })) {
return
}

const { createNode, createParentChildLink } = actions

function transformObject(obj, id, type) {
Expand Down Expand Up @@ -63,5 +59,5 @@ async function onCreateNode({
}
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.shouldOnCreateNode = shouldOnCreateNode
exports.onCreateNode = onCreateNode
@@ -1,4 +1,4 @@
const { onCreateNode } = require(`../gatsby-node`)
const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`)

describe(`gatsby-transformer-javascript-frontmatter`, () => {
describe(`onCreateNode`, () => {
Expand Down Expand Up @@ -43,39 +43,61 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => {
`(
`should loadNodeContent if file has extension $extension`,
async ({ extension }) => {
await onCreateNode({
const shouldCreateNode = shouldOnCreateNode({
node: {
...node,
extension,
},
actions,
loadNodeContent,
createContentDigest,
})

if (shouldCreateNode) {
await onCreateNode({
node: {
...node,
extension,
},
actions,
loadNodeContent,
createContentDigest,
})
}
expect(loadNodeContent).toBeCalled()
}
)

it(`should not loadNodeContent for not javascript file`, async () => {
await onCreateNode({
const shouldCreateNode = shouldOnCreateNode({
node: {
...node,
extension: `csv`,
},
actions,
loadNodeContent,
createContentDigest,
})

if (shouldCreateNode) {
await onCreateNode({
node: {
...node,
extension: `csv`,
},
actions,
loadNodeContent,
createContentDigest,
})
}
expect(loadNodeContent).not.toBeCalled()
})

it(`should load frontmatter data with exported object`, async () => {
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
const shouldCreateNode = shouldOnCreateNode({ node })

if (shouldCreateNode) {
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
}
expect(actions.createNode).toBeCalled()
expect(actions.createNode.mock.calls[0]).toMatchSnapshot()
})
Expand All @@ -91,25 +113,33 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => {
description: "Things about the choropleth.",
}
`)
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
const shouldCreateNode = shouldOnCreateNode({ node })

if (shouldCreateNode) {
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
}
expect(actions.createNode).toBeCalled()
expect(actions.createNode.mock.calls[0]).toMatchSnapshot()
})

it(`should pass fileAbsolutePath to node if file type is "File"`, async () => {
node.internal.type = `File`
node.absolutePath = `bar`
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
const shouldCreateNode = shouldOnCreateNode({ node })

if (shouldCreateNode) {
await onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
})
}
expect(actions.createNode.mock.calls[0][0].fileAbsolutePath).toEqual(
node.absolutePath
)
Expand Down