From f77be76c4ea2444986ceb539dba95a496c473bd9 Mon Sep 17 00:00:00 2001 From: sunlei Date: Sat, 25 Jul 2026 11:36:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3Admin=20JSDoc=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=E8=A7=92=E8=89=B2=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/jsdoc-policy/check-jsdoc.mjs | 10 ++-- internal/jsdoc-policy/check-jsdoc.test.mjs | 53 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/internal/jsdoc-policy/check-jsdoc.mjs b/internal/jsdoc-policy/check-jsdoc.mjs index d1ebc77..9616a08 100644 --- a/internal/jsdoc-policy/check-jsdoc.mjs +++ b/internal/jsdoc-policy/check-jsdoc.mjs @@ -688,11 +688,14 @@ function getFunctionExpressionRole(node, sourceFile) { } const parent = expression.parent; - if (ts.isPropertyAssignment(parent) && parent.initializer === expression) { + if ( + (ts.isPropertyAssignment(parent) || ts.isPropertyDeclaration(parent)) && + parent.initializer === expression + ) { return getNodeName(parent, sourceFile); } - return getNodeName(node, sourceFile); + return null; } function classifyTarget(node, sourceFile) { @@ -705,9 +708,6 @@ function classifyTarget(node, sourceFile) { if (!name) { return { allowed: false, target: 'anonymous-function-declaration' }; } - if (name === 'setup') { - return { allowed: false, target: 'setup' }; - } return { allowed: true, target: 'named-function-declaration' }; } diff --git a/internal/jsdoc-policy/check-jsdoc.test.mjs b/internal/jsdoc-policy/check-jsdoc.test.mjs index c53425d..cc3e863 100644 --- a/internal/jsdoc-policy/check-jsdoc.test.mjs +++ b/internal/jsdoc-policy/check-jsdoc.test.mjs @@ -298,6 +298,59 @@ function mounted() {} ); }); +it('allows ordinary same-name named functions outside entry roles', () => { + const source = ` +/** 执行普通初始化任务。 */ +function setup() {} + +const setupTask = + /** 执行普通具名函数表达式。 */ + function setup() {}; + +const mountedTask = + /** 执行普通具名函数表达式。 */ + function mounted() {}; + +consume( + /** 执行普通具名回调。 */ + function onModuleInit() {}, +); +`; + + assert.deepEqual( + inspectFileSource('ordinary-same-name.ts', source).violations, + [], + ); +}); + +it('uses class property roles for setup and lifecycle function expressions', () => { + const source = ` +class ComponentFields { + setup = + /** 组件初始化类字段入口。 */ + function setupField() {}; + + static mounted = + /** 组件挂载类字段入口。 */ + function mountedField() {}; + + task = + /** 执行普通类字段任务。 */ + function setup() {}; +} +`; + + assert.deepEqual( + inspectFileSource('class-property-role.ts', source).violations.map( + ({ rule, target }) => [rule, target], + ), + [ + ['invalid-target', 'setup'], + ['invalid-target', 'lifecycle-entry'], + ], + ); +}); + it('rejects declarations and callables excluded by the policy', () => { const source = ` /** 接口说明。 */