definition-header
Version:
DefinitelyTyped definition header tools
146 lines • 5.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var P = require("parsimmon");
var regex = require("../regex");
var utils = require("../utils");
var id = P.regex(regex.label).desc('project name');
var semver = P.regex(regex.semverV).desc('semver');
var space = P.string(' ');
var colon = P.string(':');
var optColon = P.regex(/:?/);
var linebreak = P.regex(/\r?\n/).desc('linebreak');
var lineTrail = P.regex(/[ \t]*\r?\n/).desc('linebreak');
var tabSpace = P.regex(/[ \t]/).desc('tab or space');
var optTabSpace = P.regex(/[ \t]*/).desc('tab or space');
var optComma = P.regex(/,?/);
var url = P.regex(regex.uri).desc('url');
var urlBracket = P.string('<').then(url).skip(P.string('>'));
var bomOpt = P.regex(regex.bomOpt);
var comment = P.string('//');
var commentSpace = comment.skip(space);
var commentTab = comment.skip(tabSpace);
var comment3 = P.string('///').skip(space);
var nameUTF = P.regex(regex.nameUTF).desc('name');
var separatorComma = P.string(',')
.then(space.or(optTabSpace
.then(linebreak)
.then(comment)
.then(tabSpace)
.then(optTabSpace)));
var separatorOptComma = P.seq(P.string(','), space)
.or(optTabSpace
.then(optComma)
.then(linebreak)
.then(comment)
.then(tabSpace)
.then(optTabSpace));
var separatorProject = P.seq(P.string(','), space)
.or(optTabSpace
.then(optComma)
.then(linebreak)
.then(comment)
.then(tabSpace)
.then(P.seq(P.string('Project:'), tabSpace).or(optTabSpace)));
var untilEndOfLine = P.takeWhile(function (c) {
return c !== '\r' &&
c !== '\n';
});
exports.person = P
.seq(
// name: Grab everything up to the URL bracket
P.takeWhile(function (c) { return c !== '<'; }).skip(P.string('<')),
// url: Grab everything between the URL brackets
P.takeWhile(function (c) { return c !== '>'; }).skip(P.string('>')))
.map(function (arr) {
return {
name: arr[0].trim(),
url: arr[1] ? utils.untrail(arr[1]) : null
};
})
.skip(optTabSpace);
exports.label = P
.string('// Type definitions for ')
.then(untilEndOfLine)
.map(function (result) {
// Name is everything that is not the version number
// Version number is separated from the label by a space
// - Expected format is MAJOR.MINOR but authors might deviate from it
// - Can be omitted
// - Can have leading 'v'
// - Can have trailing 'x'
// - Can have trailing '+'
// - Can be in the middle of the label
// - Can indicate multiple versions (e.g. '1.10.x / 2.0.x')
var matchVersion = /(?:[\- ])(v?[\d.x+]{2,})/ig;
var start = result.length - 1;
var end = 0;
var match;
while ((match = matchVersion.exec(result)) !== null) {
if (match.index < start) {
start = match.index;
}
end = matchVersion.lastIndex;
}
var name = result;
var version = null;
if (start < end) {
var nameStart = result.slice(0, start).trim();
version = result.slice(start + 1, end);
var nameEnd = result.slice(end).trim();
name = nameStart + ' ' + nameEnd;
}
return {
name: name.trim(),
version: version
};
})
.skip(optTabSpace);
exports.project = P.string('// Project: ')
.then(P.seq(url, separatorProject.then(url).many()))
.map(function (arr) {
var ret = [];
ret.push({
url: utils.untrail(arr[0])
});
arr[1].forEach(function (url) {
ret.push({
url: utils.untrail(url)
});
});
return ret;
})
.skip(untilEndOfLine);
var multilineAuthorsSeparator = P.seq(
// Line can end with optional comma followed by any number of spaces and tabs
optComma, optTabSpace,
// Check if there's more authors
linebreak.notFollowedBy(P.string('// Definitions: ')),
// Grab the beginning of the line up to the start of the next author
comment, optTabSpace).map(function (result) { return result.join(''); });
// Authors can be separated on the same line by a comma followed by any number of spaces and tabs
var sameLineAuthorsSeparator = P.seq(P.string(','), optTabSpace).map(function (result) { return result.join(''); });
exports.authors = P.string('// Definitions by: ')
.then(P.sepBy(exports.person, P.alt(
// Multi-line must go first or else same-line will eat its optional comma
multilineAuthorsSeparator, sameLineAuthorsSeparator)))
.skip(optTabSpace);
exports.repo = P.string('// Definitions: ')
.then(untilEndOfLine)
.map(function (url) {
return {
url: utils.untrail(url.replace(/\s/, ''))
};
})
.skip(optTabSpace);
exports.header = bomOpt
.then(P.seq(exports.label.skip(linebreak).or(P.of(null)), exports.project.skip(linebreak), exports.authors.skip(linebreak), exports.repo.skip(linebreak)))
.skip(P.all)
.map(function (arr) {
return {
label: arr[0],
project: arr[1],
authors: arr[2],
repository: arr[3]
};
});
//# sourceMappingURL=lax.js.map