diff --git a/vscode/src/codeReader/rules/pydantic.test.ts b/vscode/src/codeReader/rules/pydantic.test.ts index 4374538..e81ead5 100644 --- a/vscode/src/codeReader/rules/pydantic.test.ts +++ b/vscode/src/codeReader/rules/pydantic.test.ts @@ -262,8 +262,8 @@ describe('pydantic 库规则(Python)', () => { const traceFields = properties(trace).findIndex(row => row.label === '字段') const cell = traceChange.marks?.rows?.changed[traceFields].items?.changed[2].cells?.[2] expect(cell?.map(part => part.change)).toEqual(['del', 'add']) - expect(cell?.[0].segments.map(segment => segment.text).join('')).toBe('空') - expect(cell?.[1].segments.map(segment => segment.text).join('')).toBe('""') + expect(flatSegments(cell?.[0].segments ?? []).map(segment => segment.text).join('')).toBe('空') + expect(flatSegments(cell?.[1].segments ?? []).map(segment => segment.text).join('')).toBe('""') expect(diff.stats).toEqual({ add: 0, mod: 2, del: 0 }) }) }) diff --git a/vscode/src/codeReader/rules/pydantic.ts b/vscode/src/codeReader/rules/pydantic.ts index 3a408c3..81656c8 100644 --- a/vscode/src/codeReader/rules/pydantic.ts +++ b/vscode/src/codeReader/rules/pydantic.ts @@ -161,6 +161,8 @@ function namesInSource(root: Node): LibraryNames { for (const statement of syntaxChildren(root)) { if (statement.type === 'import_statement') { for (const item of statement.childrenForFieldName('name')) { + if (!item) + continue const original = item.type === 'aliased_import' ? item.childForFieldName('name') : item const alias = item.type === 'aliased_import' ? item.childForFieldName('alias') : null if (original && fromPydantic(original.text)) @@ -173,6 +175,8 @@ function namesInSource(root: Node): LibraryNames { if (!module || !fromPydantic(module.text)) continue for (const item of statement.childrenForFieldName('name')) { + if (!item) + continue const original = item.type === 'aliased_import' ? item.childForFieldName('name') : item const alias = item.type === 'aliased_import' ? item.childForFieldName('alias') : null if (original)