diff --git a/lib/rules/utils/char-source.js b/lib/rules/utils/char-source.js index bb211e2fb6eb..4a31cfc2f8fb 100644 --- a/lib/rules/utils/char-source.js +++ b/lib/rules/utils/char-source.js @@ -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; @@ -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); } @@ -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); @@ -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) { @@ -113,7 +128,7 @@ function readEscapeSequenceOrLineContinuation(reader) { return readUnicodeSequence(reader); case "\r": if (reader.read() === "\n") { - reader.pos += 1; + reader.advance(1); } // fallthrough @@ -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 (;;) { @@ -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; @@ -196,7 +211,7 @@ function parseStringLiteral(source) { function parseTemplateToken(source) { const reader = new SourceReader(source); - reader.pos = 1; + reader.advance(1); const codeUnits = []; for (;;) { @@ -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;