fix: 修正Admin JSDoc入口角色识别

This commit is contained in:
sunlei 2026-07-25 11:36:51 +08:00
parent 56ab9b9359
commit f77be76c4e
2 changed files with 58 additions and 5 deletions

View File

@ -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' };
}

View File

@ -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 = `
/** 接口说明。 */