aboutsummaryrefslogtreecommitdiff
path: root/ext/js/general/text-source-map.js
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-12-06 03:53:16 +0000
committerGitHub <noreply@github.com>2023-12-06 03:53:16 +0000
commitbd5bc1a5db29903bc098995cd9262c4576bf76af (patch)
treec9214189e0214480fcf6539ad1c6327aef6cbd1c /ext/js/general/text-source-map.js
parentfd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff)
parent23e6fb76319c9ed7c9bcdc3efba39bc5dd38f288 (diff)
Merge pull request #339 from toasted-nutbread/type-annotations
Type annotations
Diffstat (limited to 'ext/js/general/text-source-map.js')
-rw-r--r--ext/js/general/text-source-map.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/ext/js/general/text-source-map.js b/ext/js/general/text-source-map.js
index 6a136451..b03f6eb2 100644
--- a/ext/js/general/text-source-map.js
+++ b/ext/js/general/text-source-map.js
@@ -17,15 +17,26 @@
*/
export class TextSourceMap {
+ /**
+ * @param {string} source
+ * @param {number[]|null} [mapping=null]
+ */
constructor(source, mapping=null) {
+ /** @type {string} */
this._source = source;
+ /** @type {?number[]} */
this._mapping = (mapping !== null ? TextSourceMap.normalizeMapping(mapping) : null);
}
+ /** @type {string} */
get source() {
return this._source;
}
+ /**
+ * @param {unknown} other
+ * @returns {boolean}
+ */
equals(other) {
if (this === other) {
return true;
@@ -61,6 +72,10 @@ export class TextSourceMap {
return true;
}
+ /**
+ * @param {number} finalLength
+ * @returns {number}
+ */
getSourceLength(finalLength) {
const mapping = this._mapping;
if (mapping === null) {
@@ -74,6 +89,10 @@ export class TextSourceMap {
return sourceLength;
}
+ /**
+ * @param {number} index
+ * @param {number} count
+ */
combine(index, count) {
if (count <= 0) { return; }
@@ -89,6 +108,10 @@ export class TextSourceMap {
this._mapping[index] = sum;
}
+ /**
+ * @param {number} index
+ * @param {number[]} items
+ */
insert(index, ...items) {
if (this._mapping === null) {
this._mapping = TextSourceMap.createMapping(this._source);
@@ -97,14 +120,25 @@ export class TextSourceMap {
this._mapping.splice(index, 0, ...items);
}
+ /**
+ * @returns {?number[]}
+ */
getMappingCopy() {
return this._mapping !== null ? [...this._mapping] : null;
}
+ /**
+ * @param {string} text
+ * @returns {number[]}
+ */
static createMapping(text) {
return new Array(text.length).fill(1);
}
+ /**
+ * @param {number[]} mapping
+ * @returns {number[]}
+ */
static normalizeMapping(mapping) {
const result = [];
for (const value of mapping) {