ScuttleBot

Source Blame History 221 lines
f7eb47b… lmata 1 "use strict";
f7eb47b… lmata 2 var __create = Object.create;
f7eb47b… lmata 3 var __defProp = Object.defineProperty;
f7eb47b… lmata 4 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
f7eb47b… lmata 5 var __getOwnPropNames = Object.getOwnPropertyNames;
f7eb47b… lmata 6 var __getProtoOf = Object.getPrototypeOf;
f7eb47b… lmata 7 var __hasOwnProp = Object.prototype.hasOwnProperty;
f7eb47b… lmata 8 var __export = (target, all) => {
f7eb47b… lmata 9 for (var name in all)
f7eb47b… lmata 10 __defProp(target, name, { get: all[name], enumerable: true });
f7eb47b… lmata 11 };
f7eb47b… lmata 12 var __copyProps = (to, from, except, desc) => {
f7eb47b… lmata 13 if (from && typeof from === "object" || typeof from === "function") {
f7eb47b… lmata 14 for (let key of __getOwnPropNames(from))
f7eb47b… lmata 15 if (!__hasOwnProp.call(to, key) && key !== except)
f7eb47b… lmata 16 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
f7eb47b… lmata 17 }
f7eb47b… lmata 18 return to;
f7eb47b… lmata 19 };
f7eb47b… lmata 20 var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
f7eb47b… lmata 21 // If the importer is in node compatibility mode or this is not an ESM
f7eb47b… lmata 22 // file that has been converted to a CommonJS file using a Babel-
f7eb47b… lmata 23 // compatible transform (i.e. "__esModule" has not been set), then set
f7eb47b… lmata 24 // "default" to the CommonJS "module.exports" for node compatibility.
f7eb47b… lmata 25 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
f7eb47b… lmata 26 mod
f7eb47b… lmata 27 ));
f7eb47b… lmata 28 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
f7eb47b… lmata 29 var md_exports = {};
f7eb47b… lmata 30 __export(md_exports, {
f7eb47b… lmata 31 transformMDToTS: () => transformMDToTS
f7eb47b… lmata 32 });
f7eb47b… lmata 33 module.exports = __toCommonJS(md_exports);
f7eb47b… lmata 34 var import_fs = __toESM(require("fs"));
f7eb47b… lmata 35 var import_path = __toESM(require("path"));
f7eb47b… lmata 36 var import_utilsBundle = require("../utilsBundle");
f7eb47b… lmata 37 var import_babelBundle = require("./babelBundle");
f7eb47b… lmata 38 function transformMDToTS(code, filename) {
f7eb47b… lmata 39 const parsed = parseSpec(code, filename);
f7eb47b… lmata 40 let fixtures = resolveFixtures(filename, parsed.props.find((prop) => prop[0] === "fixtures")?.[1]);
f7eb47b… lmata 41 const seed = parsed.props.find((prop) => prop[0] === "seed")?.[1];
f7eb47b… lmata 42 if (seed) {
f7eb47b… lmata 43 const seedFile = import_path.default.resolve(import_path.default.dirname(filename), seed.text);
f7eb47b… lmata 44 const seedContents = import_fs.default.readFileSync(seedFile, "utf-8");
f7eb47b… lmata 45 const parsedSeed = parseSpec(seedContents, seedFile);
f7eb47b… lmata 46 if (parsedSeed.tests.length !== 1)
f7eb47b… lmata 47 throw new Error(`while parsing ${seedFile}: seed file must contain exactly one test`);
f7eb47b… lmata 48 if (parsedSeed.tests[0].props.length)
f7eb47b… lmata 49 throw new Error(`while parsing ${seedFile}: seed test must not have properties`);
f7eb47b… lmata 50 for (const test of parsed.tests)
f7eb47b… lmata 51 test.lines = parsedSeed.tests[0].lines.concat(test.lines);
f7eb47b… lmata 52 const seedFixtures = resolveFixtures(seedFile, parsedSeed.props.find((prop) => prop[0] === "fixtures")?.[1]);
f7eb47b… lmata 53 if (seedFixtures && fixtures)
f7eb47b… lmata 54 throw new Error(`while parsing ${filename}: either seed or test can specify fixtures, but not both`);
f7eb47b… lmata 55 fixtures ??= seedFixtures;
f7eb47b… lmata 56 }
f7eb47b… lmata 57 const map = new import_babelBundle.genMapping.GenMapping({});
f7eb47b… lmata 58 const lines = [];
f7eb47b… lmata 59 const addLine = (line) => {
f7eb47b… lmata 60 lines.push(line.text);
f7eb47b… lmata 61 if (line.source) {
f7eb47b… lmata 62 import_babelBundle.genMapping.addMapping(map, {
f7eb47b… lmata 63 generated: { line: lines.length, column: 0 },
f7eb47b… lmata 64 source: line.source.filename,
f7eb47b… lmata 65 original: { line: line.source.line, column: line.source.column - 1 }
f7eb47b… lmata 66 });
f7eb47b… lmata 67 }
f7eb47b… lmata 68 };
f7eb47b… lmata 69 if (fixtures)
f7eb47b… lmata 70 addLine({ text: `import { test, expect } from ${escapeString(import_path.default.relative(import_path.default.dirname(filename), fixtures.text))};`, source: fixtures.source });
f7eb47b… lmata 71 else
f7eb47b… lmata 72 addLine({ text: `import { test, expect } from '@playwright/test';` });
f7eb47b… lmata 73 addLine({ text: `test.describe(${escapeString(parsed.describe.text)}, () => {`, source: parsed.describe.source });
f7eb47b… lmata 74 for (const test of parsed.tests) {
f7eb47b… lmata 75 const tags = [];
f7eb47b… lmata 76 const annotations = [];
f7eb47b… lmata 77 for (const [key, value] of test.props) {
f7eb47b… lmata 78 if (key === "tag") {
f7eb47b… lmata 79 tags.push(...value.text.split(" ").map((s) => s.trim()).filter((s) => !!s));
f7eb47b… lmata 80 } else if (key === "annotation") {
f7eb47b… lmata 81 if (!value.text.includes("="))
f7eb47b… lmata 82 throw new Error(`while parsing ${filename}: annotation must be in format "type=description", found "${value}"`);
f7eb47b… lmata 83 const [type, description] = value.text.split("=").map((s) => s.trim());
f7eb47b… lmata 84 annotations.push({ type, description });
f7eb47b… lmata 85 }
f7eb47b… lmata 86 }
f7eb47b… lmata 87 let props = "";
f7eb47b… lmata 88 if (tags.length || annotations.length) {
f7eb47b… lmata 89 props = "{ ";
f7eb47b… lmata 90 if (tags.length)
f7eb47b… lmata 91 props += `tag: [${tags.map((tag) => escapeString(tag)).join(", ")}], `;
f7eb47b… lmata 92 if (annotations.length)
f7eb47b… lmata 93 props += `annotation: [${annotations.map((a) => `{ type: ${escapeString(a.type)}, description: ${escapeString(a.description)} }`).join(", ")}], `;
f7eb47b… lmata 94 props += "}, ";
f7eb47b… lmata 95 }
f7eb47b… lmata 96 addLine({ text: ` test(${escapeString(test.title.text)}, ${props}async ({ page, agent }) => {`, source: test.title.source });
f7eb47b… lmata 97 for (const line of test.lines)
f7eb47b… lmata 98 addLine({ text: " " + line.text, source: line.source });
f7eb47b… lmata 99 addLine({ text: ` });`, source: test.title.source });
f7eb47b… lmata 100 }
f7eb47b… lmata 101 addLine({ text: `});`, source: parsed.describe.source });
f7eb47b… lmata 102 addLine({ text: `` });
f7eb47b… lmata 103 const encodedMap = import_babelBundle.genMapping.toEncodedMap(map);
f7eb47b… lmata 104 const result = lines.join("\n");
f7eb47b… lmata 105 return { code: result, map: encodedMap };
f7eb47b… lmata 106 }
f7eb47b… lmata 107 function resolveFixtures(filename, prop) {
f7eb47b… lmata 108 if (!prop)
f7eb47b… lmata 109 return;
f7eb47b… lmata 110 return { text: import_path.default.resolve(import_path.default.dirname(filename), prop.text), source: prop.source };
f7eb47b… lmata 111 }
f7eb47b… lmata 112 function escapeString(s) {
f7eb47b… lmata 113 return `'` + s.replace(/\n/g, " ").replace(/'/g, `\\'`) + `'`;
f7eb47b… lmata 114 }
f7eb47b… lmata 115 function parsingError(filename, node, message) {
f7eb47b… lmata 116 const position = node?.position?.start ? ` at ${node.position.start.line}:${node.position.start.column}` : "";
f7eb47b… lmata 117 return new Error(`while parsing ${filename}${position}: ${message}`);
f7eb47b… lmata 118 }
f7eb47b… lmata 119 function asText(filename, node, errorMessage, skipChild) {
f7eb47b… lmata 120 let children = node.children.filter((child) => child !== skipChild);
f7eb47b… lmata 121 while (children.length === 1 && children[0].type === "paragraph")
f7eb47b… lmata 122 children = children[0].children;
f7eb47b… lmata 123 if (children.length !== 1 || children[0].type !== "text")
f7eb47b… lmata 124 throw parsingError(filename, node, errorMessage);
f7eb47b… lmata 125 return { text: children[0].value, source: node.position ? { filename, line: node.position.start.line, column: node.position.start.column } : void 0 };
f7eb47b… lmata 126 }
f7eb47b… lmata 127 function parseSpec(content, filename) {
f7eb47b… lmata 128 const root = (0, import_utilsBundle.parseMarkdown)(content);
f7eb47b… lmata 129 const props = [];
f7eb47b… lmata 130 const children = [...root.children];
f7eb47b… lmata 131 const describeNode = children[0];
f7eb47b… lmata 132 children.shift();
f7eb47b… lmata 133 if (describeNode?.type !== "heading" || describeNode.depth !== 2)
f7eb47b… lmata 134 throw parsingError(filename, describeNode, `describe title must be ##`);
f7eb47b… lmata 135 const describe = asText(filename, describeNode, `describe title must be ##`);
f7eb47b… lmata 136 if (children[0]?.type === "list") {
f7eb47b… lmata 137 parseProps(filename, children[0], props);
f7eb47b… lmata 138 children.shift();
f7eb47b… lmata 139 }
f7eb47b… lmata 140 const tests = [];
f7eb47b… lmata 141 while (children.length) {
f7eb47b… lmata 142 let nextIndex = children.findIndex((n, i) => i > 0 && n.type === "heading" && n.depth === 3);
f7eb47b… lmata 143 if (nextIndex === -1)
f7eb47b… lmata 144 nextIndex = children.length;
f7eb47b… lmata 145 const testNodes = children.splice(0, nextIndex);
f7eb47b… lmata 146 tests.push(parseTest(filename, testNodes));
f7eb47b… lmata 147 }
f7eb47b… lmata 148 return { describe, tests, props };
f7eb47b… lmata 149 }
f7eb47b… lmata 150 function parseProp(filename, node, props) {
f7eb47b… lmata 151 const propText = asText(filename, node, `property must be a list item without children`);
f7eb47b… lmata 152 const match = propText.text.match(/^([^:]+):(.*)$/);
f7eb47b… lmata 153 if (!match)
f7eb47b… lmata 154 throw parsingError(filename, node, `property must be in format "key: value"`);
f7eb47b… lmata 155 props.push([match[1].trim(), { text: match[2].trim(), source: propText.source }]);
f7eb47b… lmata 156 }
f7eb47b… lmata 157 function parseProps(filename, node, props) {
f7eb47b… lmata 158 for (const prop of node.children || []) {
f7eb47b… lmata 159 if (prop.type !== "listItem")
f7eb47b… lmata 160 throw parsingError(filename, prop, `property must be a list item without children`);
f7eb47b… lmata 161 parseProp(filename, prop, props);
f7eb47b… lmata 162 }
f7eb47b… lmata 163 }
f7eb47b… lmata 164 function parseTest(filename, nodes) {
f7eb47b… lmata 165 const titleNode = nodes[0];
f7eb47b… lmata 166 nodes.shift();
f7eb47b… lmata 167 if (titleNode.type !== "heading" || titleNode.depth !== 3)
f7eb47b… lmata 168 throw parsingError(filename, titleNode, `test title must be ###`);
f7eb47b… lmata 169 const title = asText(filename, titleNode, `test title must be ###`);
f7eb47b… lmata 170 const props = [];
f7eb47b… lmata 171 let handlingProps = true;
f7eb47b… lmata 172 const lines = [];
f7eb47b… lmata 173 const visit = (node, indent) => {
f7eb47b… lmata 174 if (node.type === "list") {
f7eb47b… lmata 175 for (const child of node.children)
f7eb47b… lmata 176 visit(child, indent);
f7eb47b… lmata 177 return;
f7eb47b… lmata 178 }
f7eb47b… lmata 179 if (node.type === "listItem") {
f7eb47b… lmata 180 const listItem = node;
f7eb47b… lmata 181 const lastChild = listItem.children[listItem.children.length - 1];
f7eb47b… lmata 182 if (lastChild?.type === "code") {
f7eb47b… lmata 183 handlingProps = false;
f7eb47b… lmata 184 const { text, source } = asText(filename, listItem, `code step must be a list item with a single code block`, lastChild);
f7eb47b… lmata 185 lines.push({ text: `${indent}await test.step(${escapeString(text)}, async () => {`, source });
f7eb47b… lmata 186 for (const [index, code] of lastChild.value.split("\n").entries())
f7eb47b… lmata 187 lines.push({ text: indent + " " + code, source: lastChild.position ? { filename, line: lastChild.position.start.line + 1 + index, column: lastChild.position.start.column } : void 0 });
f7eb47b… lmata 188 lines.push({ text: `${indent}});`, source });
f7eb47b… lmata 189 } else {
f7eb47b… lmata 190 const { text, source } = asText(filename, listItem, `step must contain a single instruction`, lastChild?.type === "list" ? lastChild : void 0);
f7eb47b… lmata 191 let isGroup = false;
f7eb47b… lmata 192 if (handlingProps && lastChild?.type !== "list" && ["tag:", "annotation:"].some((prefix) => text.startsWith(prefix))) {
f7eb47b… lmata 193 parseProp(filename, listItem, props);
f7eb47b… lmata 194 } else if (text.startsWith("group:")) {
f7eb47b… lmata 195 isGroup = true;
f7eb47b… lmata 196 lines.push({ text: `${indent}await test.step(${escapeString(text.substring("group:".length).trim())}, async () => {`, source });
f7eb47b… lmata 197 } else if (text.startsWith("expect:")) {
f7eb47b… lmata 198 handlingProps = false;
f7eb47b… lmata 199 const assertion = text.substring("expect:".length).trim();
f7eb47b… lmata 200 lines.push({ text: `${indent}await agent.expect(${escapeString(assertion)});`, source });
f7eb47b… lmata 201 } else if (!text.startsWith("//")) {
f7eb47b… lmata 202 handlingProps = false;
f7eb47b… lmata 203 lines.push({ text: `${indent}await agent.perform(${escapeString(text)});`, source });
f7eb47b… lmata 204 }
f7eb47b… lmata 205 if (lastChild?.type === "list")
f7eb47b… lmata 206 visit(lastChild, indent + (isGroup ? " " : ""));
f7eb47b… lmata 207 if (isGroup)
f7eb47b… lmata 208 lines.push({ text: `${indent}});`, source });
f7eb47b… lmata 209 }
f7eb47b… lmata 210 } else {
f7eb47b… lmata 211 throw parsingError(filename, node, `test step must be a markdown list item`);
f7eb47b… lmata 212 }
f7eb47b… lmata 213 };
f7eb47b… lmata 214 for (const node of nodes)
f7eb47b… lmata 215 visit(node, "");
f7eb47b… lmata 216 return { title, lines, props };
f7eb47b… lmata 217 }
f7eb47b… lmata 218 // Annotate the CommonJS export names for ESM import in node:
f7eb47b… lmata 219 0 && (module.exports = {
f7eb47b… lmata 220 transformMDToTS
f7eb47b… lmata 221 });

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button