|
| 1 | +module.exports = function({types: t}) { |
| 2 | + let tapeCount = 0; |
| 3 | + return { |
| 4 | + visitor: { |
| 5 | + Program(path, state) { |
| 6 | + // console.log("filename" ,state.file.opts); |
| 7 | + // if there are comments in the program file |
| 8 | + if (path.parent.comments.length) { |
| 9 | + const comments = path.parent.comments |
| 10 | + // loop through the comments |
| 11 | + for (let i = 0; i < comments.length; i += 1) { |
| 12 | + // console.log('comments', path.parent.comments[i].value) |
| 13 | + // require tape library setup |
| 14 | + // const reqTape = " dabTape */" |
| 15 | + // require file name setup |
| 16 | + const reqFileName = " ß∂dNß0j1const " + state.file.opts.sourceMapTarget.slice(0, state.file.opts.sourceMapTarget.length - 3) + " = require('" + state.file.opts.filename + "');"; |
| 17 | + // if a comments requires Tape then replace with complete require statement |
| 18 | + // TODO FIX TAPE SYNTAX!!!!!!!!! |
| 19 | + if (comments[i].value.includes('%tapered')) { |
| 20 | + // if (tapeCount === 0) { |
| 21 | + comments[i].value = reqFileName; |
| 22 | + // comments[i].value = reqTape + "\n" + "/*" + reqFileName; |
| 23 | + // tapeCount++; |
| 24 | + // } else { |
| 25 | + // comments[i].value = reqFileName; |
| 26 | + // tapeCount++; |
| 27 | + // } |
| 28 | + } |
| 29 | +// ---------------------------------------------------------------------------- |
| 30 | + // SECTION INCLUDES NAME/DESCRIPTION - ASSERTION AND VARIABLES |
| 31 | + // Split on squigglys/tildas |
| 32 | + if (comments[i].value.includes('~')) { |
| 33 | + let currcomments = comments[i].value.split("~"); |
| 34 | + // currcomments[0] is empty |
| 35 | + // currcomments[1] = name/description and is required |
| 36 | + let test = ' ß∂dNß0j1test'; |
| 37 | + let description = currcomments[1].replace(/\r\n/, "\n").split(/\n/)[0].replace(/^[ ]+|[ ]+$/g, ''); |
| 38 | + if (description[0] === 'x' && description[1] === ':') { |
| 39 | + description = description.slice(2).replace(/^[ ]+|[ ]+$/g, ''); |
| 40 | + test = ' ß∂dNß0j1test.skip'; |
| 41 | + }; |
| 42 | + if (description[0] === 'o' && description[1] === ':') { |
| 43 | + description = description.slice(2).replace(/^[ ]+|[ ]+$/g, ''); |
| 44 | + test = ' ß∂dNß0j1test.only'; |
| 45 | + }; |
| 46 | + let actual; |
| 47 | + let expression; |
| 48 | + let expected; |
| 49 | + let errMessage; |
| 50 | + let resultOfAssertion = ""; |
| 51 | + let assertion; |
| 52 | + let startIndExpression; |
| 53 | + let expressionEndPoint; |
| 54 | + let argumentLength = 0; |
| 55 | + let variables; |
| 56 | + let finalCommentsTranspiled = ""; |
| 57 | + let endTest = "\t" + "t.end();" + "\n"; |
| 58 | + // Helper function that splits the ASSERTION: actual, expected, and expression |
| 59 | + function assertions(string) { |
| 60 | + let argumentSplit = string.split("|"); |
| 61 | + let threeArgExpression = [ |
| 62 | + "equal", |
| 63 | + "notEqual", |
| 64 | + "deepEqual", |
| 65 | + "notDeepEqual", |
| 66 | + "deepLooseEqual", |
| 67 | + "notDeepLooseEqual", |
| 68 | + "throws", |
| 69 | + "doesNotThrow", |
| 70 | + "same", |
| 71 | + "notSame", |
| 72 | + "strictSame", |
| 73 | + "strictNotSame" |
| 74 | + ]; |
| 75 | + let twoArgExpression = ["ok", "notOk", "error"]; |
| 76 | + // Find the expression start index and end index |
| 77 | + for (let three = 0; three < threeArgExpression.length; three++) { |
| 78 | + if (argumentSplit[0].indexOf(threeArgExpression[three]) !== -1) { |
| 79 | + startIndExpression = argumentSplit[0].indexOf(threeArgExpression[three]); |
| 80 | + expressionEndPoint = startIndExpression + threeArgExpression[three].length; |
| 81 | + argumentLength = 3; |
| 82 | + } |
| 83 | + } |
| 84 | + for (let two = 0; two < twoArgExpression.length; two++) { |
| 85 | + if (argumentSplit[0].indexOf(twoArgExpression[two]) !== -1) { |
| 86 | + startIndExpression = argumentSplit[0].indexOf(twoArgExpression[two]); |
| 87 | + expressionEndPoint = startIndExpression + twoArgExpression[two].length; |
| 88 | + argumentLength = 2; |
| 89 | + } |
| 90 | + } |
| 91 | + // If assertion contains an error message |
| 92 | + if (argumentLength === 3) { |
| 93 | + if (argumentSplit.length > 1) { |
| 94 | + // console.log("1st", argumentSplit); |
| 95 | + actual = argumentSplit[0].slice(0, startIndExpression).replace(/^[ ]+|[ ]+$/g, ''); |
| 96 | + expression = argumentSplit[0].slice(startIndExpression, expressionEndPoint).replace(/^[ ]+|[ ]+$/g, ''); |
| 97 | + expected = argumentSplit[0].slice(expressionEndPoint).replace(/\r\n/, "\n").split(/\n/)[0]; |
| 98 | + let message = argumentSplit[1].replace(/\s*[\r\n]+\s*/g, "\n").split(/\n/)[0].replace(/^[ ]+|[ ]+$/g, ''); |
| 99 | + errMessage = "'" + message + "'"; |
| 100 | + } |
| 101 | + // If assertion does not contain an error message |
| 102 | + if (argumentSplit.length < 2) { |
| 103 | + // console.log("2nd", argumentSplit[0]); |
| 104 | + actual = argumentSplit[0].slice(0, startIndExpression).replace(/^[ ]+|[ ]+$/g, ''); |
| 105 | + expression = argumentSplit[0].slice(startIndExpression, expressionEndPoint).replace(/^[ ]+|[ ]+$/g, ''); |
| 106 | + expected = argumentSplit[0].slice(expressionEndPoint).replace(/\r\n/, "\n").split(/\n/)[0]; |
| 107 | + errMessage = "'" + "Error: " + description + "'"; |
| 108 | + } |
| 109 | + resultOfAssertion = "\t" + "t." + expression + "(" + actual + ", " + expected + ", " + errMessage + ");" + "\n"; |
| 110 | + return resultOfAssertion; |
| 111 | + } |
| 112 | + if (argumentLength === 2) { |
| 113 | + if (argumentSplit.length > 1) { |
| 114 | + // console.log("1st", argumentSplit); |
| 115 | + expression = argumentSplit[0].slice(startIndExpression, expressionEndPoint).replace(/^[ ]+|[ ]+$/g, ''); |
| 116 | + expected = argumentSplit[0].slice(expressionEndPoint).replace(/\r\n/, "\n").split(/\n/)[0]; |
| 117 | + let message = argumentSplit[1].replace(/\s*[\r\n]+\s*/g, "\n").split(/\n/)[0].replace(/^[ ]+|[ ]+$/g, ''); |
| 118 | + errMessage = "'" + message + "'"; |
| 119 | + } |
| 120 | + // If assertion does not contain an error message |
| 121 | + if (argumentSplit.length < 2) { |
| 122 | + // console.log("2nd", argumentSplit[0]); |
| 123 | + expression = argumentSplit[0].slice(startIndExpression, expressionEndPoint).replace(/^[ ]+|[ ]+$/g, ''); |
| 124 | + expected = argumentSplit[0].slice(expressionEndPoint).replace(/\r\n/, "\n").split(/\n/)[0]; |
| 125 | + errMessage = "'" + "Error: " + description + "'"; |
| 126 | + } |
| 127 | + resultOfAssertion = "\t" + "t." + expression + "(" + expected + ", " + errMessage + ");" + "\n"; |
| 128 | + return resultOfAssertion; |
| 129 | + } |
| 130 | + } |
| 131 | + for (let index = 2; index < currcomments.length; index++) { |
| 132 | + // if comments[index] is an assertion |
| 133 | + if (currcomments[index].indexOf("a:") !== -1 && currcomments[index][0] === 'a' && currcomments[index][1] === ':') { |
| 134 | + // console.log("this is assertion", currcomments[index].slice(2).replace(/^[ ]+|[ ]+$/g, '')); |
| 135 | + assertion = assertions(currcomments[index].slice(2).replace(/^[ ]+|[ ]+$/g, '')); |
| 136 | + finalCommentsTranspiled += assertion; |
| 137 | + } |
| 138 | + // if currcomments[index] is a variable (optional) |
| 139 | + if (currcomments[index][0] !== 'a' && currcomments[index][1] !== ':' && currcomments[index] !== undefined && /\S/.test(currcomments[index])) { |
| 140 | + variables = "\t" + currcomments[index].replace(/^[ ]+|[ ]+$/g, ''); |
| 141 | + finalCommentsTranspiled += variables; |
| 142 | + } |
| 143 | + //if curcomments[index] is ending in plan |
| 144 | + if (currcomments[index].indexOf("p:") !== -1 && currcomments[index][0] === 'p' && currcomments[index][1] === ':') { |
| 145 | + // let plan = "\t" + currcomments[index].slice(2).replace(/^[ ]+|[ ]+$/g, ''); |
| 146 | + let planInput = currcomments[index].slice(2).replace(/\s/g, ''); |
| 147 | + endTest = ''; |
| 148 | + finalCommentsTranspiled += `\tt.plan(${planInput});\n` |
| 149 | + } |
| 150 | + } |
| 151 | + comments[i].value = test + "('" + description + "', function (t) {" + "\n" + finalCommentsTranspiled + endTest + "});" ; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + }; |
| 158 | +} |
0 commit comments