Repository: gatsbyjs/gatsby
File: packages/babel-plugin-remove-graphql-queries/src/index.js (JavaScript)

Code before:
function isUseStaticQuery(path) {
  return (
    (path.node.callee.type === `MemberExpression` &&
      path.node.callee.property.name === `useStaticQuery` &&
      path.get(`callee`).get(`object`).referencesImport(`gatsby`)) ||
    (path.node.callee.name === `useStaticQuery` &&
      path.get(`callee`).referencesImport(`gatsby`))
  )
}

export default function ({ types: t }) {
  return {
    visitor: {
      Program(path, state) {
        const nestedJSXVistor = {
          JSXIdentifier(path2) {
            if (
              (process.env.NODE_ENV === `test` ||
                state.opts.stage === `build-html`) &&
              path2.isJSXIdentifier({ name: `StaticQuery` }) &&
              path2.referencesImport(`gatsby`) &&
              path2.parent.type !== `JSXClosingElement`
            ) {
              const identifier = t.identifier(`staticQueryData`)
              const filename = state.file.opts.filename
              const shortResultPath = `public/static/d/${this.queryHash}.json`
              const resultPath = nodePath.join(process.cwd(), shortResultPath)
              // Add query
              path2.parent.attributes.push(
                t.jSXAttribute(
                  t.jSXIdentifier(`data`),
                  t.jSXExpressionContainer(identifier)
                )
              )
              // Add import
              const importDefaultSpecifier = t.importDefaultSpecifier(
                identifier
              )
              const importDeclaration = t.importDeclaration(
                [importDefaultSpecifier],
                t.stringLiteral(
                  filename
                    ? nodePath.relative(
                        nodePath.parse(filename).dir,
                        resultPath
                      )
                    : shortResultPath
                )
              )
              path.unshiftContainer(`body`, importDeclaration)
            }

Review comment (suggestion):
this is a breaking change, we need to do this when gatsby core tells us to do so, otherwise older gatsby version will start breaking (especially after cleaning lock files). Alternatively needs major version bump for babel plugin

Code after:
function isUseStaticQuery(path) {
  return (
    (path.node.callee.type === `MemberExpression` &&
      path.node.callee.property.name === `useStaticQuery` &&
      path.get(`callee`).get(`object`).referencesImport(`gatsby`)) ||
    (path.node.callee.name === `useStaticQuery` &&
      path.get(`callee`).referencesImport(`gatsby`))
  )
}

export default function ({ types: t }) {
  return {
    visitor: {
      Program(path, state) {
        const nestedJSXVistor = {
          JSXIdentifier(path2) {
            if (
              (process.env.NODE_ENV === `test` ||
                state.opts.stage === `build-html`) &&
              path2.isJSXIdentifier({ name: `StaticQuery` }) &&
              path2.referencesImport(`gatsby`) &&
              path2.parent.type !== `JSXClosingElement`
            ) {
              const identifier = t.identifier(`staticQueryData`)
              const filename = state.file.opts.filename
              const staticQueryDir = state.opts.staticQueryDir || `static/d`
              const shortResultPath = `public/${staticQueryDir}/${this.queryHash}.json`
              const resultPath = nodePath.join(process.cwd(), shortResultPath)
              // Add query
              path2.parent.attributes.push(
                t.jSXAttribute(
                  t.jSXIdentifier(`data`),
                  t.jSXExpressionContainer(identifier)
                )
              )
              // Add import
              const importDefaultSpecifier = t.importDefaultSpecifier(
                identifier
              )
              const importDeclaration = t.importDeclaration(
                [importDefaultSpecifier],
                t.stringLiteral(
                  filename
                    ? nodePath.relative(
                        nodePath.parse(filename).dir,
                        resultPath
                      )
                    : shortResultPath
                )
              )
              path.unshiftContainer(`body`, importDeclaration)