🐛 fix(vscode): React 规则在导入了 react 的 .ts / .js 文件上也启用,自定义钩子和钩子调用照常出卡片

This commit is contained in:
cruldra committed 2026-10-16 20:50:12 +11:00
1 parent 2ccec54747
commit 9f736d92e7
23 files changed
+141 -36

No files matched your search

+11 -4
View File
@@ -48,7 +48,7 @@ const throwing: LibraryRule = {
}
async function views(source: string, rules: LibraryRule[], reports: string[] = []): Promise<BlockView[]> {
const run: RuleRun = { rules, report: message => reports.push(message) }
const run: RuleRun = { languageId: 'python', rules, report: message => reports.push(message) }
return withBlocks(py, 'python', source, blocks => toBlockViews(blocks, { source, language: py, markers: [] }, pythonZhTranslator, run))
}
@@ -56,9 +56,9 @@ function flatten(list: BlockView[]): BlockView[] {
return list.flatMap(view => [view, ...flatten(view.children)])
}
/** 语法树用完即释放,节点要在里面读完。 */
function activates(source: string, rule: LibraryRule): Promise<boolean> {
return withBlocks(py, 'python', source, blocks => activeRules([rule], importsIn(blocks)).length > 0)
/** 按 Python 解析的源码当成 languageId 这种语言的,规则启不启用;语法树用完即释放,节点要在里面读完。 */
function activates(source: string, rule: LibraryRule, languageId = 'python'): Promise<boolean> {
return withBlocks(py, 'python', source, blocks => activeRules([rule], importsIn(blocks), languageId).length > 0)
}
describe('库规则:启用', () => {
@@ -94,6 +94,13 @@ describe('库规则:启用', () => {
expect(await activates('import os', { ...cardRule('any'), libraries: [] })).toBe(true)
expect(await activates('x = 1', { ...cardRule('any'), libraries: [] })).toBe(true)
})
it('alwaysIn 里的语言不看导入就启用,别的语言照 libraries 看导入(React:TSX 不用导入 react,.ts 导入了 react 才启用)', async () => {
const rule: LibraryRule = { ...cardRule('any'), languageIds: ['python', 'cython'], alwaysIn: ['cython'] }
expect(await activates('import os', rule, 'cython')).toBe(true)
expect(await activates('import os', rule, 'python')).toBe(false)
expect(await activates('import sqlalchemy', rule, 'python')).toBe(true)
})
})
describe('库规则:认领', () => {