Skip to content

Commit 627fdea

Browse files
authored
Merge pull request #44 from Pythagora-io/z_improvements
Z improvements
2 parents 1feb00a + 2221915 commit 627fdea

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

src/helpers/unitTests.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
getRelatedFunctions,
1414
getModuleTypeFromFilePath
1515
} = require("../utils/code");
16-
const {getRelativePath, getFolderTreeItem, getTestFilePath, checkPathType, isPathInside} = require("../utils/files");
16+
const {getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside} = require("../utils/files");
1717
const {initScreenForUnitTests} = require("./cmdGUI");
1818
const {green, red, blue, bold, reset} = require('../utils/cmdPrint').colors;
1919

@@ -42,13 +42,13 @@ async function processFile(filePath) {
4242
let ast = await getAstFromFilePath(filePath);
4343
let syntaxType = await getModuleTypeFromFilePath(ast);
4444
processAst(ast, (funcName, path, type) => {
45-
if (type === 'exportFnDef') {
46-
exportsFn.push(funcName);
47-
} else if (type === 'exportFn') {
45+
if (type === 'exportFn' || type === 'exportFnDef') {
4846
exportsFn.push(funcName);
4947
} else if (type === 'exportObj') {
5048
exportsObj.push(funcName);
51-
} else {
49+
}
50+
51+
if (!['exportFn', 'exportObj'].includes(type)) {
5252
functions.push({
5353
funcName,
5454
code: generator(path.node).code,
@@ -96,23 +96,23 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
9696
if (file.fileName === filePath) {
9797
relatedCodeInSameFile = relatedCodeInSameFile.concat(file.functionNames);
9898
} else {
99-
let fileName = getRelativePath(file.fileName, filePath);
99+
let fileName = getRelativePath(file.fileName, path.dirname(filePath));
100100
let code = await stripUnrelatedFunctions(file.fileName, file.functionNames);
101101
let fullPath = filePath.substring(0, filePath.lastIndexOf('/')) + '/' + fileName;
102-
code = replaceRequirePaths(code, filePath, path.resolve(PYTHAGORA_UNIT_DIR) + '/brija.test.js');
102+
code = replaceRequirePaths(code, filePath, getTestFolderPath(filePath, rootPath));
103103
funcData.relatedCode.push({
104104
fileName,
105105
code,
106106
functionNames: file.functionNames,
107107
exportedAsObject: file.exportedAsObject,
108108
syntaxType: file.syntaxType,
109-
pathRelativeToTest: getRelativePath(fullPath, testFilePath + '/brija.test.js')
109+
pathRelativeToTest: getRelativePath(fullPath, testFilePath)
110110
});
111111
}
112112
}
113113
funcData.functionCode = await stripUnrelatedFunctions(filePath, relatedCodeInSameFile);
114-
funcData.functionCode = replaceRequirePaths(funcData.functionCode, path.dirname(filePath), path.resolve(PYTHAGORA_UNIT_DIR) + '/brija.test.js');
115-
funcData.pathRelativeToTest = getRelativePath(filePath, testFilePath + '/brija.test.js');
114+
funcData.functionCode = replaceRequirePaths(funcData.functionCode, path.dirname(filePath), getTestFolderPath(filePath, rootPath));
115+
funcData.pathRelativeToTest = getRelativePath(filePath, testFilePath);
116116
return funcData;
117117
}
118118

@@ -157,14 +157,14 @@ async function createTests(filePath, prefix, funcToTest) {
157157
);
158158
spinner.start(folderStructureTree, indexToPush);
159159

160-
let testFilePath = path.join(getTestFilePath(filePath, rootPath), `/${funcData.functionName}.test.js`);
160+
let testFilePath = path.join(getTestFolderPath(filePath, rootPath), `/${funcData.functionName}.test.js`);
161161
if (fs.existsSync(testFilePath) && !force) {
162162
await spinner.stop();
163163
folderStructureTree[indexToPush].line = `${green}${folderStructureTree[indexToPush].line}${reset}`;
164164
continue;
165165
}
166166

167-
let formattedData = await reformatDataForPythagoraAPI(funcData, filePath, getTestFilePath(filePath, rootPath));
167+
let formattedData = await reformatDataForPythagoraAPI(funcData, filePath, getTestFolderPath(filePath, rootPath));
168168
let { tests, error } = await getUnitTests(formattedData, (content) => {
169169
scrollableContent.setContent(content);
170170
scrollableContent.setScrollPerc(100);
@@ -197,7 +197,7 @@ async function createTests(filePath, prefix, funcToTest) {
197197
}
198198

199199
async function saveTests(filePath, name, testData) {
200-
let dir = getTestFilePath(filePath, rootPath);
200+
let dir = getTestFolderPath(filePath, rootPath);
201201

202202
if (!await checkDirectoryExists(dir)) {
203203
fs.mkdirSync(dir, { recursive: true });

src/utils/code.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,30 +212,40 @@ function processAst(ast, cb) {
212212
const expression = path.node.expression;
213213
if (expression && expression.type === 'AssignmentExpression') {
214214
const left = expression.left;
215-
if (left.type === 'MemberExpression' &&
215+
if (left.object.type === 'MemberExpression' &&
216+
left.object.object.name === 'module' &&
217+
left.object.property.name === 'exports') {
218+
if (expression.right.type === 'Identifier') {
219+
// module.exports.func1 = func1
220+
return cb(left.property.name, null, 'exportObj');
221+
} else if (expression.right.type === 'FunctionExpression') {
222+
// module.exports.funcName = function() { ... }
223+
// module.exports = function() { ... }
224+
const loc = path.node.loc.start;
225+
let funcName = (left.property.name) || `anon_func_${loc.line}_${loc.column}`;
226+
return cb(funcName, path, 'exportObj');
227+
}
228+
} else if (left.type === 'MemberExpression' &&
216229
left.object.name === 'module' &&
217230
left.property.name === 'exports') {
218-
// When module.exports is set to a single function
219231
if (expression.right.type === 'Identifier') {
232+
// module.exports = func1
220233
return cb(expression.right.name, null, 'exportFn');
221-
}
222-
// When module.exports is set to an anonymous function
223-
else if (expression.right.type === 'FunctionExpression') {
234+
} else if (expression.right.type === 'FunctionExpression') {
224235
let funcName;
225236
if (expression.right.id) {
237+
// module.exports = function func1() { ... }
226238
funcName = expression.right.id.name;
227239
} else {
228-
// If function is anonymous, we will generate a name
229-
// based on the file name, line and column number
240+
// module.exports = function() { ... }
230241
const loc = path.node.loc.start;
231242
funcName = `anon_func_${loc.line}_${loc.column}`;
232243
}
233244
return cb(funcName, path, 'exportFnDef');
234-
}
235-
// When module.exports is set to an object containing multiple functions
236-
else if (expression.right.type === 'ObjectExpression') {
245+
} else if (expression.right.type === 'ObjectExpression') {
237246
expression.right.properties.forEach(prop => {
238247
if (prop.type === 'ObjectProperty') {
248+
// module.exports = { func1 };
239249
return cb(prop.key.name, null, 'exportObj');
240250
}
241251
});
@@ -244,7 +254,9 @@ function processAst(ast, cb) {
244254
// Handle TypeScript transpiled exports
245255
else if (left.type === 'MemberExpression' &&
246256
left.object.name === 'exports') {
247-
return cb(left.property.name, null, 'exportFn');
257+
// exports.func1 = function() { ... }
258+
// exports.func1 = func1
259+
return cb(left.property.name, null, 'exportObj');
248260
}
249261
}
250262
}
@@ -253,25 +265,39 @@ function processAst(ast, cb) {
253265
if (path.isExportDefaultDeclaration()) {
254266
const declaration = path.node.declaration;
255267
if (declaration.type === 'FunctionDeclaration' || declaration.type === 'Identifier') {
268+
// export default func1;
269+
// TODO export default function() { ... }
270+
// TODO cover anonimous functions - add "anon_" name
256271
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFn');
257272
} else if (declaration.type === 'ObjectExpression') {
258273
declaration.properties.forEach(prop => {
259274
if (prop.type === 'ObjectProperty') {
275+
// export default { func1: func }
276+
// export default { func1 }
260277
return cb(prop.key.name, null, 'exportObj');
261278
}
262279
});
280+
} else if (declaration.type === 'ClassDeclaration') {
281+
// export default class Class1 { ... }
282+
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFnDef');
263283
}
264284
} else if (path.isExportNamedDeclaration()) {
265285
if (path.node.declaration) {
266286
if (path.node.declaration.type === 'FunctionDeclaration') {
267-
return cb(path.node.declaration.id.name, null, 'exportFn');
287+
// export function func1 () { ... }
288+
// export class Class1 () { ... }
289+
return cb(path.node.declaration.id.name, null, 'exportFnDef');
268290
} else if (path.node.declaration.type === 'VariableDeclaration') {
269291
path.node.declaration.declarations.forEach(declaration => {
270292
return cb(declaration.id.name, null, 'exportFn');
271293
});
294+
} else if (path.node.declaration.type === 'ClassDeclaration') {
295+
// export class Class1 { ... }
296+
return cb(path.node.declaration.id.name, null, 'exportFnDef');
272297
}
273298
} else if (path.node.specifiers.length > 0) {
274299
path.node.specifiers.forEach(spec => {
300+
// export { func as func1 }
275301
return cb(spec.exported.name, null, 'exportObj');
276302
});
277303
}

src/utils/files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ async function checkPathType(path) {
88
return stats.isFile() ? 'file' : 'directory';
99
}
1010

11-
function getRelativePath(filePath, referenceFilePath) {
12-
let relativePath = path.relative(path.dirname(referenceFilePath), filePath);
11+
function getRelativePath(filePath, referenceFolderPath) {
12+
let relativePath = path.relative(path.resolve(referenceFolderPath), filePath);
1313
if (!relativePath.startsWith('../') && !relativePath.startsWith('./')) {
1414
relativePath = './' + relativePath;
1515
}
@@ -29,7 +29,7 @@ function isPathInside(basePath, targetPath) {
2929
return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
3030
}
3131

32-
function getTestFilePath(filePath, rootPath) {
32+
function getTestFolderPath(filePath, rootPath) {
3333
return path.join(
3434
path.resolve(PYTHAGORA_UNIT_DIR),
3535
path.dirname(filePath).replace(rootPath, ''),
@@ -42,5 +42,5 @@ module.exports = {
4242
getRelativePath,
4343
getFolderTreeItem,
4444
isPathInside,
45-
getTestFilePath
45+
getTestFolderPath
4646
}

0 commit comments

Comments
 (0)