Skip to content

Diff not parsing for added file #31

Closed
@kentcdodds

Description

@kentcdodds

Here's a test case:

import parseGitDiff from 'parse-git-diff'

const diffOutput = `diff --git a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.test.tsx b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.test.tsx
new file mode 100644
index 0000000..e69de29
diff --git a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.problem/7h2jowvfi2q/index.tsx b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.tsx
index 9913856..4d68325 100644
--- a/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.problem/7h2jowvfi2q/index.tsx
+++ b/var/folders/kt/zd3bfncd0c3gjx25hbcq483c0000gn/T/epicshop/diff/advanced-react-apis/04.01.solution/7h2jowvfi2q/index.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useState } from 'react'
+import { createContext, useEffect, useState, use, useCallback } from 'react'
 import * as ReactDOM from 'react-dom/client'
 import {
        type BlogPost,
@@ -7,15 +7,16 @@ import {
 } from '#shared/blog-posts'
 import { setGlobalSearchParams } from '#shared/utils'
 
-// 🦺 create a SearchParamsTuple type here that's a readonly array of two elements:
-// - the first element is a URLSearchParams instance
-// - the second element is typeof setGlobalSearchParams
-// 🐨 create a SearchParamsContext that is of this type
-// 💰 let's start with this as the default value (we'll improve it next):
-// [new URLSearchParams(window.location.search), setGlobalSearchParams]
+type SearchParamsTuple = readonly [
+       URLSearchParams,
+       typeof setGlobalSearchParams,
+]
+const SearchParamsContext = createContext<SearchParamsTuple>([
+       new URLSearchParams(window.location.search),
+       setGlobalSearchParams,
+])
 
-// 🐨 change this to SearchParamsProvider and accept children
-function useSearchParams() {
+function SearchParamsProvider({ children }: { children: React.ReactNode }) {
        const [searchParams, setSearchParamsState] = useState(
                () => new URLSearchParams(window.location.search),
        )
@@ -46,23 +47,29 @@ function useSearchParams() {
                [],
        )
 
-       // 🐨 instead of returning this, render the SearchParamsContext and
-       // provide this tuple as the value
-       // 💰 make sure to render the children as well!
-       return [searchParams, setSearchParams] as const
+       const searchParamsTuple = [searchParams, setSearchParams] as const
+
+       return (
+               <SearchParamsContext value={searchParamsTuple}>
+                       {children}
+               </SearchParamsContext>
+       )
 }
 
-// 🐨 create a useSearchParams hook here that returns use(SearchParamsContext)
+function useSearchParams() {
+       return use(SearchParamsContext)
+}
 
 const getQueryParam = (params: URLSearchParams) => params.get('query') ?? ''
 
 function App() {
        return (
-               // 🐨 wrap this in the SearchParamsProvider
-               <div className="app">
-                       <Form />
-                       <MatchingPosts />
-               </div>
+               <SearchParamsProvider>
+                       <div className="app">
+                               <Form />
+                               <MatchingPosts />
+                       </div>
+               </SearchParamsProvider>
        )
 }
 `

const parsed = parseGitDiff(diffOutput)
console.log(parsed)

// outputs: { type: 'GitDiff', files: [] }

I'm not certain why the index.test.tsx file is causing an issue here, but if I remove that from the diff the rest parses just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions