From 4e778bc4995cd1b6a250557c4651fd53eabf1aaf Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 12 Dec 2022 07:30:47 +0100 Subject: [PATCH] chore: refactor `tap_lexer` to use more primordials PR-URL: https://github.com/nodejs/node/pull/45744 Reviewed-By: Moshe Atlow Reviewed-By: Colin Ihrig (cherry picked from commit 2483da743cbb48f31c6b3f8cb186d89f31d73611) --- lib/internal/test_runner/tap_lexer.js | 38 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/internal/test_runner/tap_lexer.js b/lib/internal/test_runner/tap_lexer.js index 9fcf93e..022f75f 100644 --- a/lib/internal/test_runner/tap_lexer.js +++ b/lib/internal/test_runner/tap_lexer.js @@ -1,7 +1,14 @@ -// https://github.com/nodejs/node/blob/f8ce9117b19702487eb600493d941f7876e00e01/lib/internal/test_runner/tap_lexer.js +// https://github.com/nodejs/node/blob/2483da743cbb48f31c6b3f8cb186d89f31d73611/lib/internal/test_runner/tap_lexer.js 'use strict' -const { SafeSet, MathMax, StringPrototypeIncludes } = require('#internal/per_context/primordials') +const { + ArrayPrototypePop, + ArrayPrototypePush, + MathMax, + SafeSet, + StringPrototypeIncludes, + StringPrototypeTrim +} = require('#internal/per_context/primordials') const { codes: { ERR_TAP_LEXER_ERROR } } = require('#internal/errors') @@ -137,21 +144,21 @@ class TapLexer { this.#lastScannedToken = token } + ArrayPrototypePush(chunk, token) if (token.kind === TokenKind.NEWLINE) { // Store the current chunk + NEWLINE token - tokens.push([...chunk, token]) + ArrayPrototypePush(tokens, chunk) chunk = [] - } else { - chunk.push(token) } } if (chunk.length > 0) { - tokens.push([...chunk, this.#scanEOL()]) + ArrayPrototypePush(chunk, this.#scanEOL()) + ArrayPrototypePush(tokens, chunk) } // send EOF as a separate chunk - tokens.push([this.#scanEOF()]) + ArrayPrototypePush(tokens, [this.#scanEOF()]) return tokens } @@ -239,7 +246,7 @@ class TapLexer { this.#hasTheCurrentCharacterBeenEscaped() || this.#source.peek(1) === TokenKind.WHITESPACE ) { - this.#escapeStack.pop() + ArrayPrototypePop(this.#escapeStack) return new Token({ kind: TokenKind.LITERAL, value: char, @@ -250,7 +257,7 @@ class TapLexer { // Otherwise, consume the escape symbol as an escape symbol that should be ignored by the parser // we also need to push the escape symbol to the escape stack // and consume the next character as a literal (done in the next turn) - this.#escapeStack.push(char) + ArrayPrototypePush(this.#escapeStack, char) return new Token({ kind: TokenKind.ESCAPE, value: char, @@ -327,7 +334,7 @@ class TapLexer { const charHasBeenEscaped = this.#hasTheCurrentCharacterBeenEscaped() if (this.#isComment || charHasBeenEscaped) { if (charHasBeenEscaped) { - this.#escapeStack.pop() + ArrayPrototypePop(this.#escapeStack) } return new Token({ @@ -356,7 +363,7 @@ class TapLexer { } } - word = word.trim() + word = StringPrototypeTrim(word) if (TapLexer.Keywords.has(word)) { const token = this.#scanTAPKeyword(word) @@ -381,10 +388,9 @@ class TapLexer { } #scanTAPKeyword (word) { - const isLastScannedTokenEOLorNewLine = StringPrototypeIncludes( - [TokenKind.EOL, TokenKind.NEWLINE], - this.#lastScannedToken.kind - ) + const isLastScannedTokenEOLorNewLine = + TokenKind.EOL === this.#lastScannedToken.kind || + TokenKind.NEWLINE === this.#lastScannedToken.kind if (word === 'TAP' && isLastScannedTokenEOLorNewLine) { return new Token({ @@ -480,7 +486,7 @@ class TapLexer { // We deliberately do not include "# \ + -"" in this list // these are used for comments/reasons explanations, pragma and escape characters // whitespace is not included because it is handled separately - return '!"$%&\'()*,./:;<=>?@[]^_`{|}~'.indexOf(char) > -1 + return StringPrototypeIncludes('!"$%&\'()*,./:;<=>?@[]^_`{|}~', char) } #isWhitespaceSymbol (char) {