| 1 |
/* eslint-disable no-confusing-arrow */ |
| 2 |
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); |
| 3 |
const clone = value => JSON.parse(JSON.stringify(value)); |
| 4 |
|
| 5 |
const { TEST_FUZZ } = process.env; |
| 6 |
|
| 7 |
const toDescriptorAssignedObject = (delta, object) => |
| 8 |
delta.reduce( |
| 9 |
(object, [key, descriptor]) => ( |
| 10 |
!descriptor || descriptor.delete |
| 11 |
? delete object[key] |
| 12 |
: Object.defineProperty(object, key, descriptor), |
| 13 |
object |
| 14 |
), |
| 15 |
clone(object), |
| 16 |
); |
| 17 |
|
| 18 |
const toAdjustFunction = adjustments => |
| 19 |
!adjustments || Object.keys(adjustments).length === 0 |
| 20 |
? null |
| 21 |
: Object.assign( |
| 22 |
(adjust, value, key, parent) => |
| 23 |
key in adjustments |
| 24 |
? adjustments[key](adjust, value, key, parent) |
| 25 |
: value, |
| 26 |
adjustments, |
| 27 |
); |
| 28 |
|
| 29 |
const SyntaxErrorMessageRegExp = /\((\d+):(\d+)\)$/; |
| 30 |
const toAdjustedSyntaxError = (adjust, error) => |
| 31 |
error && SyntaxErrorMessageRegExp.test(error.message) |
| 32 |
? SyntaxError( |
| 33 |
error.message.replace(/\((\d+):(\d+)\)$/, function (_, line, column) { |
| 34 |
const loc = { |
| 35 |
line: parseInt(line, 10), |
| 36 |
column: parseInt(column, 10), |
| 37 |
}; |
| 38 |
return `(${adjust( |
| 39 |
adjust, |
| 40 |
loc.line, |
| 41 |
"line", |
| 42 |
loc, |
| 43 |
)}:${adjust(adjust, loc.column, "column", loc)})`; |
| 44 |
}), |
| 45 |
) |
| 46 |
: error; |
| 47 |
|
| 48 |
export default function toFuzzedOptions(options) { |
| 49 |
if (TEST_FUZZ !== "true") return [[false, options]]; |
| 50 |
|
| 51 |
const { startLine = 1, startColumn = 0 } = options; |
| 52 |
|
| 53 |
// If the test supplies its own position, then make sure we choose |
| 54 |
// a different position. Also, make sure we stay within the "reasonable" |
| 55 |
// bounds in case the test is testing negative startLine or startColumn |
| 56 |
// for example. |
| 57 |
const randomLine = Math.max(2, random(startLine + 1, 1000)); |
| 58 |
const randomColumn = Math.max(1, random(startColumn + 1, 100)); |
| 59 |
|
| 60 |
// Now assemble our deltas... |
| 61 |
const fuzzedOptions = [ |
| 62 |
[false, false], |
| 63 |
[1, 0], |
| 64 |
[1, randomColumn], |
| 65 |
[randomLine, 0], |
| 66 |
[randomLine, randomColumn], |
| 67 |
[randomLine, false], |
| 68 |
[false, randomColumn], |
| 69 |
] |
| 70 |
.map(([line, column]) => [ |
| 71 |
["startLine", line !== false && { enumerable: true, value: line }], |
| 72 |
["startColumn", column !== false && { enumerable: true, value: column }], |
| 73 |
]) |
| 74 |
.map(delta => toDescriptorAssignedObject(delta, options)); |
| 75 |
|
| 76 |
// Make sure to include the original options in our set as well if the user |
| 77 |
// is wanting to test a specific start position. |
| 78 |
const totalOptions = |
| 79 |
startLine !== 1 || startColumn !== 0 |
| 80 |
? [options, ...fuzzedOptions] |
| 81 |
: fuzzedOptions; |
| 82 |
|
| 83 |
// The last step is to create our fuzzing function for traversing the resulting AST. |
| 84 |
// This allows us to efficiently try these different options without having to modify |
| 85 |
// the expected results. |
| 86 |
return totalOptions |
| 87 |
.map(options => [options, options.startLine || 1, options.startColumn || 0]) |
| 88 |
.map(([options, fStartLine, fStartColumn]) => [ |
| 89 |
toAdjustFunction({ |
| 90 |
...(startLine !== fStartLine && { |
| 91 |
line: (_, line) => line - startLine + fStartLine, |
| 92 |
}), |
| 93 |
...(startColumn !== fStartColumn && { |
| 94 |
column: (_, column, __, { line }) => |
| 95 |
line !== startLine ? column : column - startColumn + fStartColumn, |
| 96 |
}), |
| 97 |
}), |
| 98 |
options, |
| 99 |
]) |
| 100 |
.map(([adjust, options]) => [ |
| 101 |
adjust && |
| 102 |
toAdjustFunction({ |
| 103 |
...adjust, |
| 104 |
threw: (adjust, error) => |
| 105 |
error && toAdjustedSyntaxError(adjust, error), |
| 106 |
errors: (adjust, errors) => |
| 107 |
errors && errors.map(error => toAdjustedSyntaxError(adjust, error)), |
| 108 |
}), |
| 109 |
options, |
| 110 |
]); |
| 111 |
} |