Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit 621be6e

Browse files
committed
Rewrite label matching algorithm.
Fixes issue with handling parentheses.
1 parent cdb08da commit 621be6e

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

src/parser/lax.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,40 @@ export let person: P.Parser<model.Person> = P.seq(
7171
})
7272
.skip(optTabSpace);
7373

74-
export let label: P.Parser<model.Label> = P.string('// Type definitions for ')
75-
.then(P.seq(
76-
id,
77-
space.then(
78-
P.string('(')
79-
.then(P.seq(
80-
id,
81-
P.string(', ').then(id).many()
82-
).map((arr: any[]) => {
83-
return [arr[0]].concat(arr[1]);
84-
}))
85-
.skip(P.string(')'))
86-
)
87-
.or(P.succeed(null))
88-
))
89-
.map((arr: any[]) => {
90-
regex.semverExtract.lastIndex = 0;
91-
let match = regex.semverExtract.exec(arr[0]);
74+
export let label: P.Parser<model.Label> = P
75+
// Starts with '// Type definitions for '
76+
.string('// Type definitions for ')
77+
// Grab the rest of the line
78+
.then(P.takeWhile((c) => {
79+
return c !== '\r' &&
80+
c !== '\n';
81+
}))
82+
.map((result) => {
83+
// Label is everything that is not the version number
84+
// Version number is separated from the label by a space
85+
// - Expected format is MAJOR.MINOR but authors might deviate from it
86+
// - Can be omitted
87+
// - Can have leading 'v'
88+
// - Can have trailing 'x'
89+
// - Can have trailing '+'
90+
// - Can be in the middle of the label
91+
// - Can indicate multiple versions (e.g. '1.10.x / 2.0.x')
92+
let match = /(.*)[ \-](v?[\d.x+ /]+)(.*)/i.exec(result);
93+
let label: string = null;
9294
let semver: string = null;
93-
let label: string = arr[0];
9495
if (match) {
9596
label = match[1];
96-
semver = match[2];
97+
// If the version number is in the middle of the label, concatenate the disconnected part
98+
if (match[3]) {
99+
label += ' ' + match[3];
100+
}
101+
label = label.trim();
102+
semver = match[2].trim();
103+
} else {
104+
label = result;
97105
}
98106
return {
99-
name: label + (arr[1] ? ' (' + arr[1].join(', ') + ')' : ''),
107+
name: label,
100108
version: semver
101109
};
102110
})

test/fixtures/headers/async/fields.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
parsed:
33
label:
44
name: "Async"
5-
version: "0.1"
5+
version: "v0.1"
66
project:
77
-
88
url: "https://github.com/caolan/async"

test/fixtures/headers/jquery/fields.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
parsed:
33
label:
4-
name: jQuery 1.10.x / 2.0.x
5-
version: null
4+
name: jQuery
5+
version: 1.10.x / 2.0.x
66
project:
77
-
88
url: "http://jquery.com"

test/src/module.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ describe('partials', () => {
7777
name: 'Angular JS (ui.router module)',
7878
version: '1.2'
7979
});
80+
// If we keep '.x' as part of the version number, we should keep '+' too.
8081
assertPart(DH.parts.label, 'label-plus', {
8182
name: 'Angular JS',
82-
version: '1.2'
83+
version: '1.2+'
8384
});
8485
assertPart(DH.parts.label, 'label-simple', {
8586
name: 'FooModule',

0 commit comments

Comments
 (0)