|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + codes: { |
| 4 | + ERR_TEST_FAILURE, |
| 5 | + } |
| 6 | +} = require('internal/errors'); |
| 7 | +const AssertionError = require('internal/assert/assertion_error'); |
| 8 | +const { |
| 9 | + ArrayPrototypeJoin, |
| 10 | + Error, |
| 11 | + Number, |
| 12 | + NumberIsNaN, |
| 13 | + RegExpPrototypeExec, |
| 14 | + StringPrototypeEndsWith, |
| 15 | + StringPrototypeRepeat, |
| 16 | + StringPrototypeSlice, |
| 17 | + StringPrototypeStartsWith, |
| 18 | + StringPrototypeSubstring, |
| 19 | +} = primordials; |
| 20 | + |
| 21 | +const kYamlKeyRegex = /^(\s+)?(\w+):(\s)+([>|][-+])?(.*)$/; |
| 22 | +const kStackDelimiter = ' at '; |
| 23 | + |
| 24 | +function reConstructError(parsedYaml) { |
| 25 | + if (!('error' in parsedYaml)) { |
| 26 | + return parsedYaml; |
| 27 | + } |
| 28 | + const isAssertionError = parsedYaml.code === 'ERR_ASSERTION' || |
| 29 | + 'actual' in parsedYaml || 'expected' in parsedYaml || 'operator' in parsedYaml; |
| 30 | + const isTestFauilure = parsedYaml.code === 'ERR_TEST_FAILURE' || 'failureType' in parsedYaml; |
| 31 | + const stack = parsedYaml.stack ? kStackDelimiter + ArrayPrototypeJoin(parsedYaml.stack, `\n${kStackDelimiter}`) : ''; |
| 32 | + |
| 33 | + // eslint-disable-next-line no-restricted-syntax |
| 34 | + let error = new Error(parsedYaml.error); |
| 35 | + error.stack = stack; |
| 36 | + error.code = parsedYaml.code; |
| 37 | + |
| 38 | + if (isAssertionError) { |
| 39 | + error = new AssertionError({ |
| 40 | + message: parsedYaml.error, |
| 41 | + actual: parsedYaml.actual, |
| 42 | + expected: parsedYaml.expected, |
| 43 | + operator: parsedYaml.operator |
| 44 | + }); |
| 45 | + error.stack = stack; |
| 46 | + } |
| 47 | + |
| 48 | + if (isTestFauilure) { |
| 49 | + error = new ERR_TEST_FAILURE(error, parsedYaml.failureType); |
| 50 | + error.stack = stack; |
| 51 | + } |
| 52 | + |
| 53 | + parsedYaml.error = error; |
| 54 | + delete parsedYaml.stack; |
| 55 | + delete parsedYaml.code; |
| 56 | + delete parsedYaml.failureType; |
| 57 | + delete parsedYaml.actual; |
| 58 | + delete parsedYaml.expected; |
| 59 | + delete parsedYaml.operator; |
| 60 | + |
| 61 | + return parsedYaml; |
| 62 | +} |
| 63 | + |
| 64 | +function getYamlValue(value, key) { |
| 65 | + if (StringPrototypeStartsWith(value, "'") && StringPrototypeEndsWith(value, "'")) { |
| 66 | + return StringPrototypeSlice(value, 1, -1); |
| 67 | + } |
| 68 | + if (value === 'true') { |
| 69 | + return true; |
| 70 | + } |
| 71 | + if (value === 'false') { |
| 72 | + return false; |
| 73 | + } |
| 74 | + if (value && !NumberIsNaN(value)) { |
| 75 | + return Number(value); |
| 76 | + } |
| 77 | + return value; |
| 78 | +} |
| 79 | + |
| 80 | +function parseYAML(lines) { |
| 81 | + const result = {}; |
| 82 | + let isInYamlBlock = false; |
| 83 | + for (const line of lines) { |
| 84 | + if (isInYamlBlock && !StringPrototypeStartsWith(line, StringPrototypeRepeat(' ', isInYamlBlock.indent))) { |
| 85 | + result[isInYamlBlock.key] = isInYamlBlock.key === 'stack' ? |
| 86 | + result[isInYamlBlock.key] : ArrayPrototypeJoin(result[isInYamlBlock.key], '\n'); |
| 87 | + isInYamlBlock = false; |
| 88 | + } |
| 89 | + if (isInYamlBlock) { |
| 90 | + const blockLine = StringPrototypeSubstring(line, isInYamlBlock.indent); |
| 91 | + result[isInYamlBlock.key].push(blockLine); |
| 92 | + continue; |
| 93 | + } |
| 94 | + const match = RegExpPrototypeExec(kYamlKeyRegex, line); |
| 95 | + if (match !== null) { |
| 96 | + const { 1: leadingSpaces, 2: key, 4: block, 5: value } = match; |
| 97 | + if (block) { |
| 98 | + isInYamlBlock = { key, indent: (leadingSpaces?.length ?? 0) + 2 }; |
| 99 | + result[key] = []; |
| 100 | + } else { |
| 101 | + result[key] = getYamlValue(value, key); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return reConstructError(result); |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { |
| 109 | + parseYAML, |
| 110 | +}; |
0 commit comments