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