Skip to content

Commit

Permalink
fix typings
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Sep 22, 2021
1 parent d2064c8 commit f1c48c4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
48 changes: 31 additions & 17 deletions packages/babel-plugin-remove-graphql-queries/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function getTagImport(tag: NodePath<Identifier>): NodePath | null {
const parent = path.parentPath

if (
parent &&
binding.kind === `module` &&
parent.isImportDeclaration() &&
parent.node.source.value === `gatsby`
Expand Down Expand Up @@ -202,9 +203,14 @@ function removeImport(tag: NodePath<Expression>): void {
const parent = importPath.parentPath

if (importPath.isImportSpecifier()) {
if ((parent as NodePath<ImportDeclaration>).node.specifiers.length === 1) {
if (
parent &&
(parent as NodePath<ImportDeclaration>).node.specifiers.length === 1
) {
parent.remove()
} else importPath.remove()
} else {
importPath.remove()
}
}
if (importPath.isObjectProperty()) {
if ((parent as NodePath<ObjectExpression>).node.properties.length === 1) {
Expand Down Expand Up @@ -344,11 +350,14 @@ export default function ({ types: t }): PluginObj {
const parent = importPath.parentPath
if (importPath.isImportSpecifier())
if (
parent &&
(parent as NodePath<ImportDeclaration>).node.specifiers
.length === 1
)
) {
parent.remove()
else importPath.remove()
} else {
importPath.remove()
}
}

// Add query
Expand Down Expand Up @@ -401,7 +410,7 @@ export default function ({ types: t }): PluginObj {

// traverse upwards until we find top-level JSXOpeningElement or Program
// this handles exported queries and variable queries
let parent = templatePath as NodePath
let parent: null | NodePath = templatePath as NodePath
while (
parent &&
![`Program`, `JSXOpeningElement`].includes(parent.node.type)
Expand All @@ -410,17 +419,19 @@ export default function ({ types: t }): PluginObj {
}

// modify StaticQuery elements and import data only if query is inside StaticQuery
parent.traverse(nestedJSXVistor, {
queryHash,
query,
})

// modify useStaticQuery elements and import data only if query is inside useStaticQuery
parent.traverse(nestedHookVisitor, {
queryHash,
query,
templatePath,
})
if (parent) {
parent.traverse(nestedJSXVistor, {
queryHash,
query,
})

// modify useStaticQuery elements and import data only if query is inside useStaticQuery
parent.traverse(nestedHookVisitor, {
queryHash,
query,
templatePath,
})
}

return null
}
Expand Down Expand Up @@ -525,7 +536,10 @@ export default function ({ types: t }): PluginObj {
// update or not.
// By removing the page query export, FastRefresh works properly with page components
const potentialExportPath = path2.parentPath?.parentPath?.parentPath
if (potentialExportPath?.isExportNamedDeclaration()) {
if (
path2?.parentPath?.parentPath &&
potentialExportPath?.isExportNamedDeclaration()
) {
potentialExportPath.replaceWith(path2.parentPath.parentPath)
}

Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-gatsby/src/__tests__/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import preset from "../dependencies"
import * as pathSerializer from "../utils/path-serializer"

// @ts-ignore pathSerializer type is not available
expect.addSnapshotSerializer(pathSerializer)

describe(`dependencies`, () => {
Expand Down
22 changes: 15 additions & 7 deletions packages/babel-preset-gatsby/src/utils/path-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { slash } from "gatsby-core-utils"

interface ISerializer {
test(val: unknown): boolean
print(val: string, serialize: (val: string) => string): string
}

const cleanNodeModules = (dir: string): string => {
const x = dir.split(`node_modules/`)

Expand All @@ -10,10 +15,13 @@ const cleanNodeModules = (dir: string): string => {
return slash(`<PROJECT_ROOT>/node_modules/${x[1]}`)
}

export const test = (val: unknown): boolean =>
typeof val === `string` && val !== cleanNodeModules(val)

export const print = (
val: string,
serialize: (val: string) => string
): string => serialize(cleanNodeModules(val))
export function createSeralizer(): ISerializer {
return {
test(val: unknown): boolean {
return typeof val === `string` && val !== cleanNodeModules(val)
},
print(val: string, serialize: (val: string) => string): string {
return serialize(cleanNodeModules(val))
},
}
}

0 comments on commit f1c48c4

Please sign in to comment.