Skip to content

Commit

Permalink
chore: refactor tap_parser to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#45745
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
(cherry picked from commit 7a42a206ac37d95060640b4812aaef32535937e1)
  • Loading branch information
aduh95 authored and MoLow committed Feb 2, 2023
1 parent c0854ac commit 9b49978
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions lib/internal/test_runner/tap_parser.js
@@ -1,29 +1,31 @@
// https://github.com/nodejs/node/blob/f8ce9117b19702487eb600493d941f7876e00e01/lib/internal/test_runner/tap_parser.js
// https://github.com/nodejs/node/blob/7a42a206ac37d95060640b4812aaef32535937e1/lib/internal/test_runner/tap_parser.js
'use strict'

const Transform = require('stream').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,
StringPrototypeSplit
StringPrototypeEndsWith,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeTrim
} = require('#internal/per_context/primordials')
const Transform = require('stream').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
Expand Down Expand Up @@ -150,22 +152,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, -1)
}
if (StringPrototypeEndsWith(chunkAsString, 'EOF')) {
chunkAsString = StringPrototypeSlice(chunkAsString, 0, -3)
}

return chunkAsString
}
Expand Down Expand Up @@ -372,7 +378,7 @@ class TapParser extends Transform {
}

#addDiagnosticsToLastTestPoint (currentNode) {
const lastTestPoint = this.#bufferedTestPoints[this.#bufferedTestPoints.length - 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) {
Expand Down Expand Up @@ -798,7 +804,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 = {
Expand Down Expand Up @@ -899,7 +905,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

Expand Down Expand Up @@ -961,7 +967,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()
Expand Down

0 comments on commit 9b49978

Please sign in to comment.