Skip to content

Commit

Permalink
add advance method and JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Feb 15, 2024
1 parent 448d9a9 commit b1cf05f
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/rules/utils/char-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class SourceReader {
this.pos = 0;
}

/**
* Advances the reading position of the specified number of characters.
* @param {number} length Number of characters to advance.
* @returns {void}
*/
advance(length) {
this.pos += length;
}

/**
* Reads characters from the source.
* @param {number} [offset=0] The offset where reading starts, relative to the current position.
* @param {number} [length=1] Number of characters to read.
* @returns {string} A substring of source characters.
*/
read(offset = 0, length = 1) {
const start = offset + this.pos;

Expand All @@ -53,7 +68,7 @@ function readHexSequence(reader, length) {
const str = reader.read(0, length);
const charCode = parseInt(str, 16);

reader.pos += length;
reader.advance(length);
return String.fromCharCode(charCode);
}

Expand Down Expand Up @@ -86,7 +101,7 @@ function readUnicodeSequence(reader) {
function readOctalSequence(reader, maxLength) {
const [octalStr] = reader.read(-1, maxLength).match(/^[0-7]+/u);

reader.pos += octalStr.length - 1;
reader.advance(octalStr.length - 1);
const octal = parseInt(octalStr, 8);

return String.fromCharCode(octal);
Expand All @@ -100,7 +115,7 @@ function readOctalSequence(reader, maxLength) {
function readEscapeSequenceOrLineContinuation(reader) {
const char = reader.read(1);

reader.pos += 2;
reader.advance(2);
const unitChar = SIMPLE_ESCAPE_SEQUENCES[char];

if (unitChar) {
Expand All @@ -113,7 +128,7 @@ function readEscapeSequenceOrLineContinuation(reader) {
return readUnicodeSequence(reader);
case "\r":
if (reader.read() === "\n") {
reader.pos += 1;
reader.advance(1);
}

// fallthrough
Expand Down Expand Up @@ -169,7 +184,7 @@ function parseStringLiteral(source) {
const reader = new SourceReader(source);
const quote = reader.read();

reader.pos = 1;
reader.advance(1);
const codeUnits = [];

for (;;) {
Expand All @@ -182,7 +197,7 @@ function parseStringLiteral(source) {
codeUnits.push(...mapEscapeSequenceOrLineContinuation(reader));
} else {
codeUnits.push(new CodeUnit(reader.pos, char));
reader.pos += 1;
reader.advance(1);
}
}
return codeUnits;
Expand All @@ -196,7 +211,7 @@ function parseStringLiteral(source) {
function parseTemplateToken(source) {
const reader = new SourceReader(source);

reader.pos = 1;
reader.advance(1);
const codeUnits = [];

for (;;) {
Expand All @@ -216,7 +231,7 @@ function parseTemplateToken(source) {
unitSource = char;
}
codeUnits.push(new CodeUnit(reader.pos, unitSource));
reader.pos += unitSource.length;
reader.advance(unitSource.length);
}
}
return codeUnits;
Expand Down

0 comments on commit b1cf05f

Please sign in to comment.