@@ -67,7 +67,7 @@ export type FindNvimResult = {
67
67
} ;
68
68
69
69
const 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;
71
71
const buildTypeRegex = / ^ B u i l d \s + t y p e : \s + ( .+ ) $ / m;
72
72
const luaJitVersionRegex = / ^ L u a J I T \s + ( .+ ) $ / m;
73
73
const windows = process . platform === 'win32' ;
@@ -83,6 +83,9 @@ function parseVersion(version: string): (number | string)[] | undefined {
83
83
}
84
84
85
85
const [ , major , minor , patch , prerelease ] = match ;
86
+ if ( major === undefined || minor === undefined || patch === undefined ) {
87
+ throw new TypeError ( `Invalid version string: "${ version } "` ) ;
88
+ }
86
89
const majorNumber = Number ( major ) ;
87
90
const minorNumber = Number ( minor ) ;
88
91
const patchNumber = Number ( patch ) ;
@@ -114,11 +117,17 @@ function parseVersion(version: string): (number | string)[] | undefined {
114
117
function compareVersions ( a : string , b : string ) : number {
115
118
const versionA = parseVersion ( a ) ;
116
119
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
+ }
118
126
127
+ const length = Math . min ( versionA . length , versionB . length ) ;
119
128
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 ;
122
131
if ( partA < partB ) {
123
132
return - 1 ;
124
133
}
@@ -127,7 +136,7 @@ function compareVersions(a: string, b: string): number {
127
136
}
128
137
}
129
138
130
- if ( ( versionB ? .length ?? 0 ) > ( versionA ? .length ?? 0 ) ) {
139
+ if ( versionB . length > versionA . length ) {
131
140
return - 1 ;
132
141
}
133
142
@@ -209,6 +218,7 @@ export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
209
218
if ( existsSync ( nvimPath ) || normalizedPathsFromUser . includes ( nvimPath ) ) {
210
219
try {
211
220
accessSync ( nvimPath , constants . X_OK ) ;
221
+ // TODO: fallback to `echo 'print(vim.version())' | nvim -l -` if parsing --version fails.
212
222
const nvimVersionFull = execFileSync ( nvimPath , [
213
223
'--version' ,
214
224
] ) . toString ( ) ;
0 commit comments