0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-28 16:12:17 +01:00
svelte/codemod-lazy-props.mjs
gtmnayan 783bd9899e
chore: swap mocha with vitest (#8584)
Also swap out the require hook hacks with a less-hacky-but-still-somewhat-hacky loader for the Svelte files

---------

Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
2023-05-17 21:00:20 +02:00

48 lines
1.2 KiB
JavaScript

import { existsSync, fstat, readFileSync, readdirSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { parse } from 'acorn';
import { walk } from 'estree-walker';
import { inspect } from 'util';
import { p, print } from 'code-red';
const samples = resolve(`vitest/runtime/runtime/samples`);
for (const dir of readdirSync(samples)) {
const cwd = resolve(samples, dir);
const file = resolve(cwd, '_config.js');
if (!existsSync(file)) continue;
const contents = readFileSync(file, 'utf-8');
const ast = parse(contents, {
sourceType: 'module',
ecmaVersion: 'latest',
sourceFile: file,
ranges: true
});
walk(ast, {
enter(node) {
if (
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression'
) {
this.skip();
const props = node.declaration.properties.find((prop) => prop.key.name === 'props');
if (!props) return;
const { range } = props;
const [start, end] = range;
const code =
contents.slice(0, start) +
print(p`get ${props.key}() { return ${props.value}}`).code +
contents.slice(end);
writeFileSync(file, code);
}
}
});
}