@@ -67,7 +67,7 @@ export type FindNvimResult = {
6767} ;
6868
6969const versionRegex = / ^ ( \d + ) \. ( \d + ) \. ( \d + ) (?: - ( .+ ) ) ? $ / ;
70- const nvimVersionRegex = / ^ N V I M \s + v ( .+ ) $ / m;
70+ const nvimVersionRegex = / ^ [ n N ] [ v V ] [ i I ] [ m M ] \s + v ? ( .+ ) $ / m;
7171const buildTypeRegex = / ^ B u i l d \s + t y p e : \s + ( .+ ) $ / m;
7272const luaJitVersionRegex = / ^ L u a J I T \s + ( .+ ) $ / m;
7373const windows = process . platform === 'win32' ;
@@ -83,6 +83,9 @@ function parseVersion(version: string): (number | string)[] | undefined {
8383 }
8484
8585 const [ , major , minor , patch , prerelease ] = match ;
86+ if ( major === undefined || minor === undefined || patch === undefined ) {
87+ throw new TypeError ( `Invalid version string: "${ version } "` ) ;
88+ }
8689 const majorNumber = Number ( major ) ;
8790 const minorNumber = Number ( minor ) ;
8891 const patchNumber = Number ( patch ) ;
@@ -114,11 +117,17 @@ function parseVersion(version: string): (number | string)[] | undefined {
114117function compareVersions ( a : string , b : string ) : number {
115118 const versionA = parseVersion ( a ) ;
116119 const versionB = parseVersion ( b ) ;
117- const length = Math . min ( versionA ?. length ?? 0 , versionB ?. length ?? 0 ) ;
120+ if ( versionA === undefined ) {
121+ throw new TypeError ( `Invalid version: "${ a } "` ) ;
122+ }
123+ if ( versionB === undefined ) {
124+ return 1 ;
125+ }
118126
127+ const length = Math . min ( versionA . length , versionB . length ) ;
119128 for ( let i = 0 ; i < length ; i = i + 1 ) {
120- const partA = versionA ?. [ i ] ?? 0 ;
121- const partB = versionB ?. [ i ] ?? 0 ;
129+ const partA = versionA [ i ] ?? 0 ;
130+ const partB = versionB [ i ] ?? 0 ;
122131 if ( partA < partB ) {
123132 return - 1 ;
124133 }
@@ -127,7 +136,7 @@ function compareVersions(a: string, b: string): number {
127136 }
128137 }
129138
130- if ( ( versionB ? .length ?? 0 ) > ( versionA ? .length ?? 0 ) ) {
139+ if ( versionB . length > versionA . length ) {
131140 return - 1 ;
132141 }
133142
@@ -209,6 +218,7 @@ export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
209218 if ( existsSync ( nvimPath ) || normalizedPathsFromUser . includes ( nvimPath ) ) {
210219 try {
211220 accessSync ( nvimPath , constants . X_OK ) ;
221+ // TODO: fallback to `echo 'print(vim.version())' | nvim -l -` if parsing --version fails.
212222 const nvimVersionFull = execFileSync ( nvimPath , [
213223 '--version' ,
214224 ] ) . toString ( ) ;
0 commit comments