Skip to content

Commit 4e778bc

Browse files
aduh95MoLow
authored andcommitted
chore: refactor tap_lexer to use more primordials
PR-URL: nodejs/node#45744 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> (cherry picked from commit 2483da743cbb48f31c6b3f8cb186d89f31d73611)
1 parent 9b49978 commit 4e778bc

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

lib/internal/test_runner/tap_lexer.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
// https://github.com/nodejs/node/blob/f8ce9117b19702487eb600493d941f7876e00e01/lib/internal/test_runner/tap_lexer.js
1+
// https://github.com/nodejs/node/blob/2483da743cbb48f31c6b3f8cb186d89f31d73611/lib/internal/test_runner/tap_lexer.js
22
'use strict'
33

4-
const { SafeSet, MathMax, StringPrototypeIncludes } = require('#internal/per_context/primordials')
4+
const {
5+
ArrayPrototypePop,
6+
ArrayPrototypePush,
7+
MathMax,
8+
SafeSet,
9+
StringPrototypeIncludes,
10+
StringPrototypeTrim
11+
} = require('#internal/per_context/primordials')
512
const {
613
codes: { ERR_TAP_LEXER_ERROR }
714
} = require('#internal/errors')
@@ -137,21 +144,21 @@ class TapLexer {
137144
this.#lastScannedToken = token
138145
}
139146

147+
ArrayPrototypePush(chunk, token)
140148
if (token.kind === TokenKind.NEWLINE) {
141149
// Store the current chunk + NEWLINE token
142-
tokens.push([...chunk, token])
150+
ArrayPrototypePush(tokens, chunk)
143151
chunk = []
144-
} else {
145-
chunk.push(token)
146152
}
147153
}
148154

149155
if (chunk.length > 0) {
150-
tokens.push([...chunk, this.#scanEOL()])
156+
ArrayPrototypePush(chunk, this.#scanEOL())
157+
ArrayPrototypePush(tokens, chunk)
151158
}
152159

153160
// send EOF as a separate chunk
154-
tokens.push([this.#scanEOF()])
161+
ArrayPrototypePush(tokens, [this.#scanEOF()])
155162

156163
return tokens
157164
}
@@ -239,7 +246,7 @@ class TapLexer {
239246
this.#hasTheCurrentCharacterBeenEscaped() ||
240247
this.#source.peek(1) === TokenKind.WHITESPACE
241248
) {
242-
this.#escapeStack.pop()
249+
ArrayPrototypePop(this.#escapeStack)
243250
return new Token({
244251
kind: TokenKind.LITERAL,
245252
value: char,
@@ -250,7 +257,7 @@ class TapLexer {
250257
// Otherwise, consume the escape symbol as an escape symbol that should be ignored by the parser
251258
// we also need to push the escape symbol to the escape stack
252259
// and consume the next character as a literal (done in the next turn)
253-
this.#escapeStack.push(char)
260+
ArrayPrototypePush(this.#escapeStack, char)
254261
return new Token({
255262
kind: TokenKind.ESCAPE,
256263
value: char,
@@ -327,7 +334,7 @@ class TapLexer {
327334
const charHasBeenEscaped = this.#hasTheCurrentCharacterBeenEscaped()
328335
if (this.#isComment || charHasBeenEscaped) {
329336
if (charHasBeenEscaped) {
330-
this.#escapeStack.pop()
337+
ArrayPrototypePop(this.#escapeStack)
331338
}
332339

333340
return new Token({
@@ -356,7 +363,7 @@ class TapLexer {
356363
}
357364
}
358365

359-
word = word.trim()
366+
word = StringPrototypeTrim(word)
360367

361368
if (TapLexer.Keywords.has(word)) {
362369
const token = this.#scanTAPKeyword(word)
@@ -381,10 +388,9 @@ class TapLexer {
381388
}
382389

383390
#scanTAPKeyword (word) {
384-
const isLastScannedTokenEOLorNewLine = StringPrototypeIncludes(
385-
[TokenKind.EOL, TokenKind.NEWLINE],
386-
this.#lastScannedToken.kind
387-
)
391+
const isLastScannedTokenEOLorNewLine =
392+
TokenKind.EOL === this.#lastScannedToken.kind ||
393+
TokenKind.NEWLINE === this.#lastScannedToken.kind
388394

389395
if (word === 'TAP' && isLastScannedTokenEOLorNewLine) {
390396
return new Token({
@@ -480,7 +486,7 @@ class TapLexer {
480486
// We deliberately do not include "# \ + -"" in this list
481487
// these are used for comments/reasons explanations, pragma and escape characters
482488
// whitespace is not included because it is handled separately
483-
return '!"$%&\'()*,./:;<=>?@[]^_`{|}~'.indexOf(char) > -1
489+
return StringPrototypeIncludes('!"$%&\'()*,./:;<=>?@[]^_`{|}~', char)
484490
}
485491

486492
#isWhitespaceSymbol (char) {

0 commit comments

Comments
 (0)