textlint-rule-helper
Version:
A helper library for textlint rule.
62 lines • 2.33 kB
JavaScript
// LICENSE : MIT
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SourceLocation = void 0;
var structured_source_1 = require("structured-source");
var SourceLocation = /** @class */ (function () {
function SourceLocation(text) {
this.text = text;
}
Object.defineProperty(SourceLocation.prototype, "source", {
// StructuredSource builds a line index of the whole text.
// Create it on first use, because only `{ line }` padding needs it.
get: function () {
if (this._source === undefined) {
this._source = new structured_source_1.StructuredSource(this.text);
}
return this._source;
},
enumerable: false,
configurable: true
});
SourceLocation.prototype.toAbsoluteLocation = function (node, padding) {
var nodeRange = node.range;
var line = node.loc.start.line;
var column = node.loc.start.column;
// when use {index}
if (padding.index !== undefined) {
var paddingIndex = padding.index;
return nodeRange[0] + paddingIndex;
}
// when use {line, column}
if (padding.line !== undefined && padding.column !== undefined) {
var addedLine = line + padding.line;
// when report with padding {line, column}, message.column should be 0 + padding.column.
// In other word, padding line > 0 and message.column start with 0.
if (padding.column > 0) {
return this.source.positionToIndex({
line: addedLine,
column: padding.column
});
}
else {
return this.source.positionToIndex({
line: addedLine,
column: column
});
}
}
// when use { line } only
if (padding.line !== undefined && padding.line > 0) {
var addedLine = line + padding.line;
return this.source.positionToIndex({
line: addedLine,
column: column
});
}
return nodeRange[0];
};
return SourceLocation;
}());
exports.SourceLocation = SourceLocation;
//# sourceMappingURL=SourceLocation.js.map