UNPKG

cucumber-steps-parser

Version:

A utility to parse the cucumber steps defined in the corresponding step definition files

88 lines 4.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const typescript_1 = __importDefault(require("typescript")); const getStatementJsDocTags = (statement, sourceFile) => { const jsDoc = statement.jsDoc; const tags = jsDoc && Array.isArray(jsDoc) ? jsDoc .filter((jsDoc) => typescript_1.default.isJSDoc(jsDoc) && jsDoc.tags) .reduce((jsDocsReduced, jsDoc) => { const tags = jsDoc.tags.map((tag) => { return tag.tagName.getText(sourceFile); }, []); return jsDocsReduced.concat(tags); }, []) : []; return tags; }; const getStepDefinitionText = (callExpression, sourceFile) => { const argument = callExpression.arguments[0]; const rawText = argument.getText(sourceFile); if (typescript_1.default.isRegularExpressionLiteral(argument)) { return { rawText, text: rawText .replace(/^\/\^?/, '') .replace(/\$?\/$/, '') .replace(/\\"/g, '"') .replace(/\\'/g, "'") }; } if (typescript_1.default.isStringLiteral(argument) || typescript_1.default.isNoSubstitutionTemplateLiteral(argument)) { return { rawText: argument.text, text: argument.text }; } return { rawText, text: rawText }; }; const getArguments = (callExpression, sourceFile) => { const functionDeclaration = callExpression.arguments[1]; if (!functionDeclaration || !typescript_1.default.isFunctionLike(functionDeclaration)) { return []; } return functionDeclaration.parameters.map((p) => p.name.getText(sourceFile)); }; exports.getFileCucumberSteps = (filePath) => { if (!fs_1.existsSync(filePath) || !fs_1.lstatSync(filePath).isFile()) { console.log(`No file was found at ${filePath}`); return []; } const sourceFile = typescript_1.default.createSourceFile('N/A', fs_1.readFileSync(filePath).toString(), typescript_1.default.ScriptTarget.ESNext, false); const cucumberSteps = sourceFile.statements.reduce((reduced, statement) => { if (!typescript_1.default.isExpressionStatement(statement) || !typescript_1.default.isCallExpression(statement.expression) || statement.expression.arguments.length === 0 || !statement.expression.expression.getText(sourceFile) || !statement.expression.expression.getText(sourceFile).match(/^(Given|When|Then)$/)) { return reduced; } const args = getArguments(statement.expression, sourceFile); const jsDocTags = getStatementJsDocTags(statement, sourceFile); const text = getStepDefinitionText(statement.expression, sourceFile); const cucumberStep = Object.assign({ arguments: args, jsDocTags }, text); return reduced.concat([cucumberStep]); }, []); return cucumberSteps; }; exports.getFolderCucumberSteps = (folderPath, options = {}) => { if (!fs_1.existsSync(folderPath) || !fs_1.lstatSync(folderPath).isDirectory()) { console.log(`No directory was found at ${folderPath}`); return []; } const recursiveParsing = options.recursive === undefined || options.recursive; const filenameRegExp = new RegExp(options.filenameRegExp || '^.*\\.ts$'); return fs_1.readdirSync(folderPath).reduce((reduced, item) => { const itemPath = path_1.join(folderPath, item); const isDirectoryItem = fs_1.lstatSync(itemPath).isDirectory(); const cucumberSteps = isDirectoryItem && recursiveParsing ? exports.getFolderCucumberSteps(itemPath, options) : !isDirectoryItem && item.match(filenameRegExp) ? exports.getFileCucumberSteps(itemPath) : []; return reduced.concat(cucumberSteps); }, []); }; //# sourceMappingURL=cucumber-step.js.map