@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
55 lines (54 loc) • 1.66 kB
JavaScript
import { SyntaxKind, } from 'ts-morph';
export class AstUtils {
callExpressions = [];
sourceParent;
constructor(sourceParent) {
this.sourceParent = sourceParent;
}
static fromSourceFile(sourceFile) {
return new AstUtils(sourceFile);
}
static fromArrowFunction(parent) {
return new AstUtils(parent);
}
listDescendantCallExpressions() {
this.callExpressions = this.sourceParent.getDescendantsOfKind(SyntaxKind.CallExpression);
return this;
}
matchExpressionName(text) {
this.callExpressions = this.callExpressions.filter((expression) => {
return expression.getExpression().getText() === text;
});
return this;
}
matchExpressionArg(argRequired) {
this.callExpressions = this.callExpressions.filter((expression) => {
return expression
.getArguments()
.find((arg) => AstUtils.isString(arg.getKind()))
?.getText()
.replace(/^['"`]|['"`]$/g, '')
.includes(argRequired);
});
return this;
}
textMatchRegex(regex) {
this.callExpressions = this.callExpressions.filter((expression) => {
return regex.test(expression.getText());
});
return this;
}
getAll() {
return this.callExpressions;
}
getOne() {
return this.callExpressions.at(0);
}
static isString(kind) {
return [
SyntaxKind.StringLiteral,
SyntaxKind.NoSubstitutionTemplateLiteral,
SyntaxKind.TemplateExpression,
].includes(kind);
}
}