@@ -2,19 +2,19 @@ import path from 'path'
22import gitDiffTree from 'git-diff-tree'
33import { findUpOne } from 'vfile-find-up'
44
5- var own = { } . hasOwnProperty
5+ const own = { } . hasOwnProperty
66
7- var previousRange
7+ let previousRange
88
99export default function diff ( ) {
10- var cache = { }
10+ let cache = { }
1111
1212 return transform
1313
1414 function transform ( tree , file , next ) {
15- var base = file . dirname
16- var commitRange
17- var range
15+ const base = file . dirname
16+ let commitRange
17+ let range
1818
1919 // Looks like Travis.
2020 if ( process . env . TRAVIS_COMMIT_RANGE ) {
@@ -53,114 +53,84 @@ export default function diff() {
5353
5454 function ongit ( error , git ) {
5555 // Never happens.
56- /* c8 ignore next 3 */
57- if ( error ) {
58- return next ( error )
59- }
56+ /* c8 ignore next */
57+ if ( error ) return next ( error )
6058
6159 // Not testable in a Git repo…
62- /* c8 ignore next 3 */
63- if ( ! git ) {
64- return next ( new Error ( 'Not in a git repository' ) )
65- }
60+ /* c8 ignore next */
61+ if ( ! git ) return next ( new Error ( 'Not in a git repository' ) )
6662
6763 cache [ base ] = git . dirname
6864 tick ( git . dirname )
6965 }
7066
7167 function tick ( root ) {
72- var diffs = { }
73- var revs = { originalRev : range [ 0 ] , rev : range [ 1 ] }
68+ const diffs = { }
7469
75- gitDiffTree ( path . join ( root , '.git' ) , revs )
70+ gitDiffTree ( path . join ( root , '.git' ) , {
71+ originalRev : range [ 0 ] ,
72+ rev : range [ 1 ]
73+ } )
7674 . on ( 'error' , next )
77- . on ( 'data' , ondata )
78- . on ( 'end' , onend )
79-
80- function ondata ( type , data ) {
81- var info = type === 'patch' && parse ( data )
82- var fp
75+ . on ( 'data' , ( type , data ) => {
76+ if ( type !== 'patch' ) return
77+
78+ const lines = data . lines
79+ const re = / ^ @ @ - ( \d + ) , ? ( \d + ) ? \+ ( \d + ) , ? ( \d + ) ? @ @ /
80+ const match = lines [ 0 ] . match ( re )
81+
82+ // Should not happen, maybe if Git returns weird diffs?
83+ /* c8 ignore next */
84+ if ( ! match ) return
85+
86+ const ranges = [ ]
87+ const start = Number . parseInt ( match [ 3 ] , 10 ) - 1
88+ let index = 0
89+ let position
90+
91+ while ( ++ index < lines . length ) {
92+ const line = lines [ index ]
93+
94+ if ( line . charAt ( 0 ) === '+' ) {
95+ const no = start + index
96+
97+ if ( position === undefined ) {
98+ position = ranges . length
99+ ranges . push ( [ no , no ] )
100+ } else {
101+ ranges [ position ] [ 1 ] = no
102+ }
103+ } else {
104+ position = undefined
105+ }
106+ }
83107
84- if ( info ) {
85- fp = path . resolve ( root , info . path )
108+ const fp = path . resolve ( root , data . bPath )
86109
87110 // Long diffs.
88- /* c8 ignore next 3 */
89- if ( ! ( fp in diffs ) ) {
90- diffs [ fp ] = [ ]
111+ /* c8 ignore next */
112+ if ( ! ( fp in diffs ) ) diffs [ fp ] = [ ]
113+
114+ diffs [ fp ] . push ( ...ranges )
115+ } )
116+ . on ( 'end' , ( ) => {
117+ const fp = path . resolve ( file . cwd , file . path )
118+ const ranges = diffs [ fp ]
119+
120+ // Unchanged file.
121+ if ( ! ranges || ranges . length === 0 ) {
122+ file . messages = [ ]
123+ return next ( )
91124 }
92125
93- diffs [ fp ] = diffs [ fp ] . concat ( info . ranges )
94- }
95- }
96-
97- function onend ( ) {
98- tock ( diffs )
99- }
100-
101- function tock ( patches ) {
102- var fp = path . resolve ( file . cwd , file . path )
103- var ranges = patches [ fp ]
104-
105- // Unchanged file.
106- if ( ! ranges || ranges . length === 0 ) {
107- file . messages = [ ]
108- return next ( )
109- }
110-
111- file . messages = file . messages . filter ( ( message ) =>
112- ranges . some (
113- ( range ) => message . line >= range [ 0 ] && message . line <= range [ 1 ]
126+ file . messages = file . messages . filter ( ( message ) =>
127+ ranges . some (
128+ ( range ) => message . line >= range [ 0 ] && message . line <= range [ 1 ]
129+ )
114130 )
115- )
116131
117- next ( )
118- }
132+ next ( )
133+ } )
119134 }
120135 }
121136}
122-
123- function parse ( data ) {
124- var lines = data . lines
125- var line = lines [ 0 ]
126- var re = / ^ @ @ - ( \d + ) , ? ( \d + ) ? \+ ( \d + ) , ? ( \d + ) ? @ @ /
127- var match = line . match ( re )
128- var result = { path : data . bPath }
129- var ranges = [ ]
130- var start
131- var index
132- var length
133- var position
134- var no
135-
136- // Should not happen, maybe if Git returns weird diffs?
137- /* c8 ignore next 3 */
138- if ( ! match ) {
139- return
140- }
141-
142- index = 0
143- length = lines . length
144- start = parseInt ( match [ 3 ] , 10 ) - 1
145- result . ranges = ranges
146-
147- while ( ++ index < length ) {
148- line = lines [ index ]
149-
150- if ( line . charAt ( 0 ) !== '+' ) {
151- position = null
152- continue
153- }
154-
155- no = start + index
156-
157- if ( position === null || position === undefined ) {
158- position = ranges . length
159- ranges . push ( [ no , no ] )
160- } else {
161- ranges [ position ] [ 1 ] = no
162- }
163- }
164-
165- return result
166- }
0 commit comments