























@@ -1,6 +1,7 @@
11import { readFileSync, readdirSync } from "node:fs";
22import { dirname, relative, resolve, sep } from "node:path";
33import { fileURLToPath } from "node:url";
4+import ts from "typescript";
45import { describe, expect, it } from "vitest";
5667const SRC_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
@@ -165,14 +166,46 @@ function isAllowedBundledExtensionImport(specifier: string): boolean {
165166}
166167167168function collectBundledExtensionImports(source: string): string[] {
168-const matches = [
169- ...source.matchAll(/from\s+["']([^"']*extensions\/[^"']+)["']/gu),
170- ...source.matchAll(/vi\.(?:mock|doMock)\(\s*["']([^"']*extensions\/[^"']+)["']/gu),
171- ...source.matchAll(/importActual(?:<[^>]*>)?\(\s*["']([^"']*extensions\/[^"']+)["']/gu),
172-];
173-return matches
174-.map((match) => match[1])
175-.filter((specifier): specifier is string => typeof specifier === "string");
169+const sourceFile = ts.createSourceFile(
170+"boundary-invariants-input.ts",
171+source,
172+ts.ScriptTarget.Latest,
173+true,
174+ts.ScriptKind.TS,
175+);
176+const specifiers: string[] = [];
177+178+function visit(node: ts.Node): void {
179+if (
180+(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
181+node.moduleSpecifier &&
182+ts.isStringLiteralLike(node.moduleSpecifier)
183+) {
184+specifiers.push(node.moduleSpecifier.text);
185+}
186+if (ts.isCallExpression(node) && isBundledExtensionImportHelperCall(node.expression)) {
187+const firstArgument = node.arguments[0];
188+if (firstArgument && ts.isStringLiteralLike(firstArgument)) {
189+specifiers.push(firstArgument.text);
190+}
191+}
192+ts.forEachChild(node, visit);
193+}
194+195+visit(sourceFile);
196+return specifiers.filter((specifier) => specifier.includes("extensions/"));
197+}
198+199+function isBundledExtensionImportHelperCall(expression: ts.Expression): boolean {
200+if (ts.isPropertyAccessExpression(expression)) {
201+return (
202+((expression.name.text === "mock" || expression.name.text === "doMock") &&
203+ts.isIdentifier(expression.expression) &&
204+expression.expression.text === "vi") ||
205+expression.name.text === "importActual"
206+);
207+}
208+return ts.isIdentifier(expression) && expression.text === "importActual";
176209}
177210178211function collectTypedHookNames(source: string): string[] {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。