@@ -26,7 +26,7 @@ type Original = {|
2626 b : string ,
2727| } ;
2828
29- type Diff = { diff : string , isDifferent : boolean } ;
29+ type Diff = string | null ;
3030
3131type Hunk = { |
3232 lines : Array < string > ,
@@ -159,25 +159,23 @@ const getterForChunks = (original: Original) => {
159159
160160// jest --expand
161161const formatChunks = ( a : string , b : string , original ? : Original ) : Diff => {
162+ const chunks = diffLines ( a , b ) ;
163+ if ( chunks . every ( chunk => ! chunk . removed && ! chunk . added ) ) {
164+ return null ;
165+ }
166+
162167 const getOriginal = original && getterForChunks ( original ) ;
163- let isDifferent = false ;
164-
165- return {
166- diff : diffLines ( a , b )
167- . map ( chunk => {
168- const { added, removed, value} = chunk ;
169- if ( added || removed ) {
170- isDifferent = true ;
171- }
172- const char = getDiffChar ( chunk ) ;
173-
174- return splitIntoLines ( value )
175- . map ( line => formatLine ( char , line , getOriginal ) )
176- . join ( '\n' ) ;
177- } )
178- . join ( '\n' ) ,
179- isDifferent,
180- } ;
168+ return chunks
169+ . reduce ( ( lines , chunk ) => {
170+ const char = getDiffChar ( chunk ) ;
171+
172+ splitIntoLines ( chunk . value ) . forEach ( line => {
173+ lines . push ( formatLine ( char , line , getOriginal ) ) ;
174+ } ) ;
175+
176+ return lines ;
177+ } , [ ] )
178+ . join ( '\n' ) ;
181179} ;
182180
183181// Only show patch marks ("@@ ... @@") if the diff is big.
@@ -225,8 +223,6 @@ const formatHunks = (
225223 ? contextLines
226224 : DIFF_CONTEXT_DEFAULT ,
227225 } ;
228- const getter = original && getterForHunks ( original ) ;
229- let isDifferent = false ;
230226 // Make sure the strings end with a newline.
231227 if ( ! a . endsWith ( '\n' ) ) {
232228 a += '\n' ;
@@ -235,26 +231,27 @@ const formatHunks = (
235231 b += '\n' ;
236232 }
237233
238- const oldLinesCount = ( a . match ( / \n / g) || [ ] ) . length ;
234+ const { hunks} = structuredPatch ( '' , '' , a , b , '' , '' , options ) ;
235+ if ( hunks . length === 0 ) {
236+ return null ;
237+ }
239238
240- return {
241- diff : structuredPatch ( '' , '' , a , b , '' , '' , options )
242- . hunks . map ( ( hunk : Hunk ) => {
243- // Hunk properties are one-based but index args are zero-based.
244- const getOriginal =
245- getter && getter ( hunk . oldStart - 1 , hunk . newStart - 1 ) ;
246- const lines = hunk . lines
247- . map ( line => formatLine ( line [ 0 ] , line . slice ( 1 ) , getOriginal ) )
248- . join ( '\n' ) ;
249-
250- isDifferent = true ;
251- return shouldShowPatchMarks ( hunk , oldLinesCount )
252- ? createPatchMark ( hunk ) + lines
253- : lines ;
254- } )
255- . join ( '\n' ) ,
256- isDifferent,
257- } ;
239+ const getter = original && getterForHunks ( original ) ;
240+ const oldLinesCount = ( a . match ( / \n / g) || [ ] ) . length ;
241+ return hunks
242+ . map ( ( hunk : Hunk ) => {
243+ // Hunk properties are one-based but index args are zero-based.
244+ const getOriginal =
245+ getter && getter ( hunk . oldStart - 1 , hunk . newStart - 1 ) ;
246+ const lines = hunk . lines
247+ . map ( line => formatLine ( line [ 0 ] , line . slice ( 1 ) , getOriginal ) )
248+ . join ( '\n' ) ;
249+
250+ return shouldShowPatchMarks ( hunk , oldLinesCount )
251+ ? createPatchMark ( hunk ) + lines
252+ : lines ;
253+ } )
254+ . join ( '\n' ) ;
258255} ;
259256
260257export default function diffStrings (
@@ -272,9 +269,5 @@ export default function diffStrings(
272269 ? formatHunks ( a , b , options && options . contextLines , original )
273270 : formatChunks ( a , b , original ) ;
274271
275- if ( result . isDifferent ) {
276- return getAnnotation ( options ) + result . diff ;
277- } else {
278- return NO_DIFF_MESSAGE ;
279- }
272+ return result === null ? NO_DIFF_MESSAGE : getAnnotation ( options ) + result ;
280273}
0 commit comments