Skip to content

Instantly share code, notes, and snippets.

@bperel
Created May 10, 2025 07:31
Show Gist options
  • Save bperel/815ba90571a955d72d36a8727c2c87bb to your computer and use it in GitHub Desktop.
Save bperel/815ba90571a955d72d36a8727c2c87bb to your computer and use it in GitHub Desktop.
A regex way to find Pinia refs that were used without being unwrapped. Quite a few false positives but a good start
import { readdir, readFile } from "fs/promises";
import { join } from "path";
const regex =
/(?<=const {[^}]+)(?: ([^, ]+\b))(?:[^\n]* = storeToRefs(?:(?!<\/script>).)*?[^\.]\b\1\b(?![ \n]*\.value))/gms;
async function* walkDir(dir) {
const files = await readdir(dir, { withFileTypes: true });
for (const file of files) {
const path = join(dir, file.name);
if (
file.isDirectory() &&
!file.name.startsWith(".") &&
!file.name.startsWith("node_modules")
) {
yield* walkDir(path);
} else if (file.name.endsWith(".vue") || file.name.endsWith(".ts")) {
yield path;
}
}
}
async function main() {
const dir = ".";
for await (const file of walkDir(dir)) {
console.log("Reading", file);
const content = await readFile(file, "utf-8");
const matches = [...content.matchAll(regex)];
if (matches.length > 0) {
console.log(`\n${file}:`);
matches.forEach((match) => {
console.log(` ${match[1]}`);
});
}
}
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment