Skip to content

Commit a3bc04a

Browse files
authored
fix: 型引数名の重複に関するエラーが適切に発生しない問題を修正 (#1003)
* 型引数名の重複に関するエラーが適切に発生しない問題を修正 * AiScriptSyntaxErrorに変更
1 parent 85389f3 commit a3bc04a

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/parser/plugins/validate-type.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { getTypeBySource } from '../../type.js';
22
import { visitNode } from '../visit.js';
3+
import { AiScriptSyntaxError } from '../../error.js';
34
import type * as Ast from '../../node.js';
45

6+
function validateTypeParams(node: Ast.Fn): void {
7+
const typeParamNames = new Set<string>();
8+
for (const typeParam of node.typeParams) {
9+
if (typeParamNames.has(typeParam.name)) {
10+
throw new AiScriptSyntaxError(`type parameter name ${typeParam.name} is duplicate`, node.loc.start);
11+
}
12+
typeParamNames.add(typeParam.name);
13+
}
14+
}
15+
516
function collectTypeParams(node: Ast.Node, ancestors: Ast.Node[]): Ast.TypeParam[] {
617
const items = [];
718
if (node.type === 'fn') {
8-
const typeParamNames = new Set<string>();
9-
for (const typeParam of node.typeParams) {
10-
if (typeParamNames.has(typeParam.name)) {
11-
throw new Error(`type parameter name ${typeParam.name} is duplicate`);
12-
}
13-
typeParamNames.add(typeParam.name);
14-
}
1519
items.push(...node.typeParams);
1620
}
1721
for (let i = ancestors.length - 1; i >= 0; i--) {
@@ -32,6 +36,7 @@ function validateNode(node: Ast.Node, ancestors: Ast.Node[]): Ast.Node {
3236
break;
3337
}
3438
case 'fn': {
39+
validateTypeParams(node);
3540
for (const param of node.params) {
3641
if (param.argType != null) {
3742
getTypeBySource(param.argType, collectTypeParams(node, ancestors));

test/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ describe('generics', () => {
9898
});
9999

100100
test.concurrent('duplicate', async () => {
101-
await assert.rejects(() => exe(`
101+
await expect(() => exe(`
102102
@f<T, T>(v: T) {}
103-
`));
103+
`)).rejects.toThrow(AiScriptSyntaxError);
104+
});
105+
106+
test.concurrent('duplicate (no param and ret types)', async () => {
107+
await expect(() => exe(`
108+
@f<T, T>() {}
109+
`)).rejects.toThrow(AiScriptSyntaxError);
104110
});
105111

106112
test.concurrent('empty', async () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix: 関数の型引数の名前が重複した場合に適切なエラーが発生しない問題を修正
2+
- Fix: 関数の引数や返り値に型注釈が無く、型引数の名前が重複した場合にエラーが発生しない問題を修正

0 commit comments

Comments
 (0)