11import { getTypeBySource } from '../../type.js' ;
22import { visitNode } from '../visit.js' ;
3+ import { AiScriptSyntaxError } from '../../error.js' ;
34import 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+
516function 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 ) ) ;
0 commit comments