Skip to content

Commit 9b49978

Browse files
aduh95MoLow
authored andcommitted
chore: refactor tap_parser to use more primordials
PR-URL: nodejs/node#45745 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> (cherry picked from commit 7a42a206ac37d95060640b4812aaef32535937e1)
1 parent c0854ac commit 9b49978

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

lib/internal/test_runner/tap_parser.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
// https://github.com/nodejs/node/blob/f8ce9117b19702487eb600493d941f7876e00e01/lib/internal/test_runner/tap_parser.js
1+
// https://github.com/nodejs/node/blob/7a42a206ac37d95060640b4812aaef32535937e1/lib/internal/test_runner/tap_parser.js
22
'use strict'
33

4-
const Transform = require('stream').Transform
5-
const { TapLexer, TokenKind } = require('#internal/test_runner/tap_lexer')
6-
const { TapChecker } = require('#internal/test_runner/tap_checker')
7-
const {
8-
codes: { ERR_TAP_VALIDATION_ERROR, ERR_TAP_PARSER_ERROR }
9-
} = require('#internal/errors')
10-
const { kEmptyObject } = require('#internal/util')
114
const {
125
ArrayPrototypeFilter,
136
ArrayPrototypeForEach,
7+
ArrayPrototypeIncludes,
148
ArrayPrototypeJoin,
159
ArrayPrototypeMap,
10+
ArrayPrototypePop,
1611
ArrayPrototypePush,
17-
ArrayPrototypeIncludes,
18-
ArrayPrototypeSplice,
1912
Boolean,
2013
Number,
2114
RegExpPrototypeExec,
22-
RegExpPrototypeSymbolReplace,
2315
String,
24-
StringPrototypeTrim,
25-
StringPrototypeSplit
16+
StringPrototypeEndsWith,
17+
StringPrototypeReplaceAll,
18+
StringPrototypeSlice,
19+
StringPrototypeSplit,
20+
StringPrototypeTrim
2621
} = require('#internal/per_context/primordials')
22+
const Transform = require('stream').Transform
23+
const { TapLexer, TokenKind } = require('#internal/test_runner/tap_lexer')
24+
const { TapChecker } = require('#internal/test_runner/tap_checker')
25+
const {
26+
codes: { ERR_TAP_VALIDATION_ERROR, ERR_TAP_PARSER_ERROR }
27+
} = require('#internal/errors')
28+
const { kEmptyObject } = require('#internal/util')
2729
/**
2830
*
2931
* TAP14 specifications
@@ -150,22 +152,26 @@ class TapParser extends Transform {
150152
processChunk (chunk) {
151153
const str = this.#lastLine + chunk.toString('utf8')
152154
const lines = StringPrototypeSplit(str, '\n')
153-
this.#lastLine = ArrayPrototypeSplice(lines, lines.length - 1, 1)[0]
155+
this.#lastLine = ArrayPrototypePop(lines)
154156

155-
let chunkAsString = lines.join('\n')
157+
let chunkAsString = ArrayPrototypeJoin(lines, '\n')
156158
// Special case where chunk is emitted by a child process
157-
chunkAsString = RegExpPrototypeSymbolReplace(
158-
/\[out\] /g,
159+
chunkAsString = StringPrototypeReplaceAll(
159160
chunkAsString,
161+
'[out] ',
160162
''
161163
)
162-
chunkAsString = RegExpPrototypeSymbolReplace(
163-
/\[err\] /g,
164+
chunkAsString = StringPrototypeReplaceAll(
164165
chunkAsString,
166+
'[err] ',
165167
''
166168
)
167-
chunkAsString = RegExpPrototypeSymbolReplace(/\n$/, chunkAsString, '')
168-
chunkAsString = RegExpPrototypeSymbolReplace(/EOF$/, chunkAsString, '')
169+
if (StringPrototypeEndsWith(chunkAsString, '\n')) {
170+
chunkAsString = StringPrototypeSlice(chunkAsString, 0, -1)
171+
}
172+
if (StringPrototypeEndsWith(chunkAsString, 'EOF')) {
173+
chunkAsString = StringPrototypeSlice(chunkAsString, 0, -3)
174+
}
169175

170176
return chunkAsString
171177
}
@@ -372,7 +378,7 @@ class TapParser extends Transform {
372378
}
373379

374380
#addDiagnosticsToLastTestPoint (currentNode) {
375-
const lastTestPoint = this.#bufferedTestPoints[this.#bufferedTestPoints.length - 1]
381+
const { length, [length - 1]: lastTestPoint } = this.#bufferedTestPoints
376382

377383
// Diagnostic nodes are only added to Test points of the same nesting level
378384
if (lastTestPoint && lastTestPoint.nesting === currentNode.nesting) {
@@ -798,7 +804,7 @@ class TapParser extends Transform {
798804

799805
const commentContent = this.#peek()
800806
if (commentContent) {
801-
if (/^Subtest:/i.test(commentContent.value)) {
807+
if (RegExpPrototypeExec(/^Subtest:/i, commentContent.value) !== null) {
802808
this.#next() // skip subtest keyword
803809
const name = StringPrototypeTrim(this.#readNextLiterals())
804810
const node = {
@@ -899,7 +905,7 @@ class TapParser extends Transform {
899905
// YAMLLine := " " (YAML)* "\n"
900906
#YAMLLine () {
901907
const yamlLiteral = this.#readNextLiterals()
902-
const { 0: key, 1: value } = StringPrototypeSplit(yamlLiteral, ':')
908+
const { 0: key, 1: value } = StringPrototypeSplit(yamlLiteral, ':', 2)
903909

904910
// Note that this.#lastTestPointDetails has been cleared when we encounter a YAML start marker
905911

@@ -961,7 +967,7 @@ class TapParser extends Transform {
961967

962968
// In some cases, pragma key can be followed by a comma separator,
963969
// so we need to remove it
964-
pragmaKey = RegExpPrototypeSymbolReplace(/,/g, pragmaKey, '')
970+
pragmaKey = StringPrototypeReplaceAll(pragmaKey, ',', '')
965971

966972
pragmas[pragmaKey] = isEnabled
967973
nextToken = this.#peek()

0 commit comments

Comments
 (0)