Skip to content

Commit 6907c8b

Browse files
committed
refactor: remove rewind and skip api
1 parent cf98587 commit 6907c8b

20 files changed

+85
-93
lines changed

src/core/Parser.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ export class Parser {
148148
return this.data.charCodeAt(startPos + offset);
149149
}
150150

151-
rewind(offset: number) {
152-
return (this.pos -= offset);
153-
}
154-
155-
skip(offset: number) {
156-
return (this.pos += offset);
157-
}
158-
159151
startText() {
160152
if (this.textPos === -1) {
161153
this.textPos = this.pos;
@@ -272,11 +264,11 @@ export class Parser {
272264
switch (code) {
273265
case CODE.CARRIAGE_RETURN:
274266
case CODE.NEWLINE:
275-
this.skip(ahead);
267+
this.pos += ahead;
276268
return true;
277269
}
278270
} else {
279-
this.skip(ahead);
271+
this.pos += ahead;
280272
return false;
281273
}
282274

@@ -296,7 +288,7 @@ export class Parser {
296288
) {
297289
ahead++;
298290
}
299-
this.skip(ahead);
291+
this.pos += ahead;
300292
}
301293

302294
parse(data: string, filename: string) {

src/states/ATTRIBUTE.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
9393
if (code === CODE.COLON) {
9494
ensureAttrName(this, attr);
9595
attr.bound = true;
96-
this.skip(2); // skip :=
96+
this.pos += 2; // skip :=
9797
this.consumeWhitespace();
9898
} else if (code === CODE.PERIOD) {
9999
attr.spread = true;
100-
this.skip(3); // skip ...
100+
this.pos += 3; // skip ...
101101
} else {
102102
ensureAttrName(this, attr);
103-
this.skip(1); // skip =
103+
this.pos++; // skip =
104104
this.consumeWhitespace();
105105
}
106106

@@ -111,21 +111,21 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
111111
? CONCISE_VALUE_TERMINATORS
112112
: HTML_VALUE_TERMINATORS;
113113

114-
this.rewind(1);
114+
this.pos--;
115115
} else if (code === CODE.OPEN_PAREN) {
116116
ensureAttrName(this, attr);
117117
attr.stage = ATTR_STAGE.ARGUMENT;
118-
this.skip(1); // skip (
118+
this.pos++; // skip (
119119
this.enterState(STATE.EXPRESSION).terminator = CODE.CLOSE_PAREN;
120-
this.rewind(1);
120+
this.pos--;
121121
} else if (code === CODE.OPEN_CURLY_BRACE && attr.args) {
122122
ensureAttrName(this, attr);
123123
attr.stage = ATTR_STAGE.BLOCK;
124-
this.skip(1); // skip {
124+
this.pos++; // skip {
125125
const expr = this.enterState(STATE.EXPRESSION);
126126
expr.terminatedByWhitespace = false;
127127
expr.terminator = CODE.CLOSE_CURLY_BRACE;
128-
this.rewind(1);
128+
this.pos--;
129129
} else if (attr.stage === ATTR_STAGE.UNKNOWN) {
130130
attr.stage = ATTR_STAGE.NAME;
131131
const expr = this.enterState(STATE.EXPRESSION);
@@ -134,7 +134,7 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
134134
expr.terminator = this.isConcise
135135
? CONCISE_NAME_TERMINATORS
136136
: HTML_NAME_TERMINATORS;
137-
this.rewind(1);
137+
this.pos--;
138138
} else {
139139
this.exitState();
140140
}
@@ -184,7 +184,7 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
184184
}
185185

186186
const start = child.start - 1; // include (
187-
const end = this.skip(1); // include )
187+
const end = ++this.pos; // include )
188188
const value = {
189189
start: child.start,
190190
end: child.end,
@@ -210,7 +210,7 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
210210
case ATTR_STAGE.BLOCK: {
211211
const params = attr.args as Ranges.Value;
212212
const start = params.start;
213-
const end = this.skip(1); // include }
213+
const end = ++this.pos; // include }
214214
this.handlers.onAttrMethod?.({
215215
start,
216216
end,

src/states/BEGIN_DELIMITED_HTML_BLOCK.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const BEGIN_DELIMITED_HTML_BLOCK: StateDefinition<DelimitedHTMLBlockMeta>
3939
if (!this.consumeWhitespaceOnLine()) {
4040
this.pos = startPos + 1;
4141
this.beginHtmlBlock(undefined, true);
42-
this.rewind(1);
42+
this.pos--;
4343
}
4444
}
4545
},
@@ -95,9 +95,9 @@ function handleDelimitedBlockEOL(
9595

9696
if (parser.lookAheadFor(endHtmlBlockLookahead, parser.pos + newLineLength)) {
9797
parser.startText(); // we want to at least include the newline as text.
98-
parser.skip(newLineLength);
98+
parser.pos += newLineLength;
9999
parser.endText();
100-
parser.skip(endHtmlBlockLookahead.length);
100+
parser.pos += endHtmlBlockLookahead.length;
101101

102102
if (parser.consumeWhitespaceOnLine(0)) {
103103
parser.endText();
@@ -116,7 +116,7 @@ function handleDelimitedBlockEOL(
116116
// multiline HTML block
117117

118118
parser.startText();
119-
parser.skip(indent.length);
119+
parser.pos += indent.length;
120120
// We stay in the same state since we are still parsing a multiline, delimited HTML block
121121
} else if (indent && !parser.onlyWhitespaceRemainsOnLine()) {
122122
// the next line does not have enough indentation

src/states/CDATA.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const CDATA: StateDefinition = {
2626

2727
char(code) {
2828
if (code === CODE.CLOSE_SQUARE_BRACKET && this.lookAheadFor("]>")) {
29-
this.skip(3); // skip ]]>
29+
this.pos += 3; // skip ]]>
3030
this.exitState();
3131
return;
3232
}
@@ -45,7 +45,7 @@ export function checkForCDATA(parser: Parser) {
4545
if (parser.lookAheadFor("![CDATA[")) {
4646
parser.endText();
4747
parser.enterState(CDATA);
48-
parser.skip(8); // skip ![CDATA[
48+
parser.pos += 8; // skip ![CDATA[
4949
return true;
5050
}
5151

src/states/CLOSE_TAG.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const CLOSE_TAG: StateDefinition = {
1919

2020
char(code, closeTag) {
2121
if (code === CODE.CLOSE_ANGLE_BRACKET) {
22-
this.skip(1); // skip >
22+
this.pos++; // skip >
2323
this.exitState();
2424
ensureExpectedCloseTag(this, closeTag);
2525
}
@@ -67,7 +67,7 @@ export function checkForClosingTag(parser: Parser) {
6767
if (
6868
ensureExpectedCloseTag(parser, {
6969
start: parser.pos,
70-
end: parser.skip(skip),
70+
end: (parser.pos += skip),
7171
})
7272
) {
7373
parser.exitState();

src/states/CONCISE_HTML_CONTENT.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ export const CONCISE_HTML_CONTENT: StateDefinition = {
8686
switch (code) {
8787
case CODE.OPEN_ANGLE_BRACKET:
8888
this.beginMixedMode = true;
89-
this.rewind(1);
89+
this.pos--;
9090
this.beginHtmlBlock(undefined, false);
9191
return;
9292
case CODE.DOLLAR:
9393
if (isWhitespaceCode(this.lookAtCharCodeAhead(1))) {
94-
this.skip(1); // skip space after $
94+
this.pos++; // skip space after $
9595
this.enterState(STATE.INLINE_SCRIPT);
9696
return;
9797
}
9898
break;
9999
case CODE.HTML_BLOCK_DELIMITER:
100100
if (this.lookAtCharCodeAhead(1) === CODE.HTML_BLOCK_DELIMITER) {
101101
this.enterState(STATE.BEGIN_DELIMITED_HTML_BLOCK);
102-
this.rewind(1);
102+
this.pos--;
103103
} else {
104104
this.emitError(
105105
this.pos,
@@ -113,11 +113,11 @@ export const CONCISE_HTML_CONTENT: StateDefinition = {
113113
switch (this.lookAtCharCodeAhead(1)) {
114114
case CODE.FORWARD_SLASH:
115115
this.enterState(STATE.JS_COMMENT_LINE);
116-
this.skip(1); // skip /
116+
this.pos++; // skip /
117117
return;
118118
case CODE.ASTERISK:
119119
this.enterState(STATE.JS_COMMENT_BLOCK);
120-
this.skip(1); // skip *
120+
this.pos++; // skip *
121121
return;
122122
default:
123123
this.emitError(
@@ -130,7 +130,7 @@ export const CONCISE_HTML_CONTENT: StateDefinition = {
130130
}
131131

132132
this.enterState(STATE.OPEN_TAG);
133-
this.rewind(1); // START_TAG_NAME expects to start at the first character
133+
this.pos--; // START_TAG_NAME expects to start at the first character
134134
}
135135
},
136136

src/states/DECLARATION.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function exitDeclaration(
4747
declaration: Range,
4848
closeOffset: number
4949
) {
50-
parser.skip(closeOffset);
50+
parser.pos += closeOffset;
5151
parser.exitState();
5252
parser.handlers.onDeclaration?.({
5353
start: declaration.start,

src/states/DTD.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const DTD: StateDefinition = {
2828

2929
char(code) {
3030
if (code === CODE.CLOSE_ANGLE_BRACKET) {
31-
this.skip(1); // skip >
31+
this.pos++; // skip >
3232
this.exitState();
3333
}
3434
},

src/states/EXPRESSION.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
5454
if (match.length === 0) {
5555
// We matched a look behind.
5656
this.consumeWhitespace();
57-
this.rewind(1);
57+
this.pos--;
5858
} else {
5959
// We matched a look ahead.
60-
this.skip(match.length - 1);
60+
this.pos += match.length - 1;
6161
}
6262
} else {
6363
this.exitState();
@@ -92,11 +92,11 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
9292
switch (this.lookAtCharCodeAhead(1)) {
9393
case CODE.FORWARD_SLASH:
9494
this.enterState(STATE.JS_COMMENT_LINE);
95-
this.skip(1);
95+
this.pos++;
9696
break;
9797
case CODE.ASTERISK:
9898
this.enterState(STATE.JS_COMMENT_BLOCK);
99-
this.skip(1);
99+
this.pos++;
100100
break;
101101
default: {
102102
if (

src/states/HTML_COMMENT.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const HTML_COMMENT: StateDefinition = {
3232
let offset = 1;
3333
let next: number;
3434
while ((next = this.lookAtCharCodeAhead(offset++)) === CODE.HYPHEN);
35-
this.skip(offset); // skip all -
35+
this.pos += offset; // skip all -
3636

3737
if (next === CODE.CLOSE_ANGLE_BRACKET) {
3838
this.exitState();

0 commit comments

Comments
 (0)