From e24478e3d7a7fb647a40cff4e66177972db2b934 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 5 Dec 2022 12:29:55 +0100 Subject: [PATCH 1/2] test_runner: refactor `tap_parser` to use more primordials --- lib/internal/test_runner/tap_parser.js | 54 ++++++++++++++------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/lib/internal/test_runner/tap_parser.js b/lib/internal/test_runner/tap_parser.js index 2b79220b632e02..c05e994c8f50fc 100644 --- a/lib/internal/test_runner/tap_parser.js +++ b/lib/internal/test_runner/tap_parser.js @@ -1,28 +1,30 @@ 'use strict'; -const Transform = require('internal/streams/transform'); -const { TapLexer, TokenKind } = require('internal/test_runner/tap_lexer'); -const { TapChecker } = require('internal/test_runner/tap_checker'); -const { - codes: { ERR_TAP_VALIDATION_ERROR, ERR_TAP_PARSER_ERROR }, -} = require('internal/errors'); -const { kEmptyObject } = require('internal/util'); const { ArrayPrototypeFilter, ArrayPrototypeForEach, + ArrayPrototypeIncludes, ArrayPrototypeJoin, ArrayPrototypeMap, + ArrayPrototypePop, ArrayPrototypePush, - ArrayPrototypeIncludes, - ArrayPrototypeSplice, Boolean, Number, RegExpPrototypeExec, - RegExpPrototypeSymbolReplace, String, - StringPrototypeTrim, + StringPrototypeEndsWith, + StringPrototypeReplaceAll, + StringPrototypeSlice, StringPrototypeSplit, + StringPrototypeTrim, } = primordials; +const Transform = require('internal/streams/transform'); +const { TapLexer, TokenKind } = require('internal/test_runner/tap_lexer'); +const { TapChecker } = require('internal/test_runner/tap_checker'); +const { + codes: { ERR_TAP_VALIDATION_ERROR, ERR_TAP_PARSER_ERROR }, +} = require('internal/errors'); +const { kEmptyObject } = require('internal/util'); /** * * TAP14 specifications @@ -149,22 +151,26 @@ class TapParser extends Transform { processChunk(chunk) { const str = this.#lastLine + chunk.toString('utf8'); const lines = StringPrototypeSplit(str, '\n'); - this.#lastLine = ArrayPrototypeSplice(lines, lines.length - 1, 1)[0]; + this.#lastLine = ArrayPrototypePop(lines); - let chunkAsString = lines.join('\n'); + let chunkAsString = ArrayPrototypeJoin(lines, '\n'); // Special case where chunk is emitted by a child process - chunkAsString = RegExpPrototypeSymbolReplace( - /\[out\] /g, + chunkAsString = StringPrototypeReplaceAll( chunkAsString, + '[out] ', '' ); - chunkAsString = RegExpPrototypeSymbolReplace( - /\[err\] /g, + chunkAsString = StringPrototypeReplaceAll( chunkAsString, + '[err] ', '' ); - chunkAsString = RegExpPrototypeSymbolReplace(/\n$/, chunkAsString, ''); - chunkAsString = RegExpPrototypeSymbolReplace(/EOF$/, chunkAsString, ''); + if (StringPrototypeEndsWith(chunkAsString, '\n')) { + chunkAsString = StringPrototypeSlice(chunkAsString, 0, chunkAsString.length - 1); + } + if (StringPrototypeEndsWith(chunkAsString, 'EOF')) { + chunkAsString = StringPrototypeSlice(chunkAsString, 0, chunkAsString.length - 3); + } return chunkAsString; } @@ -371,7 +377,7 @@ class TapParser extends Transform { } #addDiagnosticsToLastTestPoint(currentNode) { - const lastTestPoint = this.#bufferedTestPoints.at(-1); + const { length, [length - 1]: lastTestPoint } = this.#bufferedTestPoints; // Diagnostic nodes are only added to Test points of the same nesting level if (lastTestPoint && lastTestPoint.nesting === currentNode.nesting) { @@ -396,7 +402,7 @@ class TapParser extends Transform { #flushBufferedTestPointNode(shouldClearBuffer = true) { if (this.#bufferedTestPoints.length > 0) { - this.#emit(this.#bufferedTestPoints.at(0)); + this.#emit(this.#bufferedTestPoints[0]); if (shouldClearBuffer) { this.#bufferedTestPoints = []; @@ -797,7 +803,7 @@ class TapParser extends Transform { const commentContent = this.#peek(); if (commentContent) { - if (/^Subtest:/i.test(commentContent.value)) { + if (RegExpPrototypeExec(/^Subtest:/i, commentContent.value) !== null) { this.#next(); // skip subtest keyword const name = StringPrototypeTrim(this.#readNextLiterals()); const node = { @@ -898,7 +904,7 @@ class TapParser extends Transform { // YAMLLine := " " (YAML)* "\n" #YAMLLine() { const yamlLiteral = this.#readNextLiterals(); - const { 0: key, 1: value } = StringPrototypeSplit(yamlLiteral, ':'); + const { 0: key, 1: value } = StringPrototypeSplit(yamlLiteral, ':', 2); // Note that this.#lastTestPointDetails has been cleared when we encounter a YAML start marker @@ -960,7 +966,7 @@ class TapParser extends Transform { // In some cases, pragma key can be followed by a comma separator, // so we need to remove it - pragmaKey = RegExpPrototypeSymbolReplace(/,/g, pragmaKey, ''); + pragmaKey = StringPrototypeReplaceAll(pragmaKey, ',', ''); pragmas[pragmaKey] = isEnabled; nextToken = this.#peek(); From d339cb8267cdcc27c129a7027fbbd668e30aeed8 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 6 Dec 2022 16:02:51 +0100 Subject: [PATCH 2/2] Update lib/internal/test_runner/tap_parser.js Co-authored-by: Mohammed Keyvanzadeh --- lib/internal/test_runner/tap_parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/tap_parser.js b/lib/internal/test_runner/tap_parser.js index c05e994c8f50fc..abb581cda103a2 100644 --- a/lib/internal/test_runner/tap_parser.js +++ b/lib/internal/test_runner/tap_parser.js @@ -166,10 +166,10 @@ class TapParser extends Transform { '' ); if (StringPrototypeEndsWith(chunkAsString, '\n')) { - chunkAsString = StringPrototypeSlice(chunkAsString, 0, chunkAsString.length - 1); + chunkAsString = StringPrototypeSlice(chunkAsString, 0, -1); } if (StringPrototypeEndsWith(chunkAsString, 'EOF')) { - chunkAsString = StringPrototypeSlice(chunkAsString, 0, chunkAsString.length - 3); + chunkAsString = StringPrototypeSlice(chunkAsString, 0, -3); } return chunkAsString;