文件
shanghaipeiwan/server/tests/miniapp-script-syntax.spec.ts
xcbyy 4e3283ff01 fix(miniapp): 修复 hall.vue 混入 TS 类型标注导致编译失败
- countByType 的 (cls: string)/(o: any) 改为纯 JS 写法

- 新增 miniapp-script-syntax 护栏:未声明 lang=ts 的 vue 脚本禁止 TS 类型标注(112 套件/734 用例全绿)
2026-10-21 16:48:32 +08:00

39 行
1.5 KiB
TypeScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

// 已读取AGENTS.md
import * as fs from 'fs';
import * as path from 'path';
/**
* 第九十七批:小程序 Vue 单文件不得混入 TypeScript 语法。
* 背景:hall.vue 的 `<script setup>` 未声明 lang="ts",却写了 `(cls: string)`,
* 导致 HBuilderX/Vite 编译直接失败,只能在人工编译时发现。
*/
describe('第九十七批小程序脚本语法闸门', () => {
const root = path.resolve(__dirname, '../../pwuniapp');
const walk = (dir: string, out: string[]): string[] => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'unpackage' || entry.name.startsWith('.')) continue;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) walk(full, out);
else if (entry.name.endsWith('.vue')) out.push(full);
}
return out;
};
it('未声明 lang="ts" 的 <script> 中不得出现 TS 类型标注', () => {
const problems: string[] = [];
const typeAnno = /\(\s*[A-Za-z_$][\w$]*\s*:\s*(string|number|boolean|any|unknown|void|Record|Array)\b/;
for (const file of walk(root, [])) {
const src = fs.readFileSync(file, 'utf-8');
const scriptMatch = src.match(/<script([^>]*)>([\s\S]*?)<\/script>/);
if (!scriptMatch) continue;
const attrs = scriptMatch[1] || '';
if (/lang\s*=\s*["']ts["']/.test(attrs)) continue;
if (typeAnno.test(scriptMatch[2])) {
problems.push(path.relative(root, file));
}
}
expect(problems).toEqual([]);
});
});