11import { MessageDataModelError } from '../errors.js' ;
2- import type {
3- Expression ,
4- Message ,
5- MessageNode ,
6- VariableRef ,
7- Variant
8- } from './types.js' ;
2+ import type { Expression , Message , MessageNode , Variant } from './types.js' ;
93import { visit } from './visit.js' ;
104
115/**
@@ -55,19 +49,16 @@ export function validate(
5549 /** Tracks declared variables for `duplicate-declaration` */
5650 const declared = new Set < string > ( ) ;
5751
58- const declRefs = new Map < string , VariableRef [ ] > ( ) ;
5952 const functions = new Set < string > ( ) ;
6053 const localVars = new Set < string > ( ) ;
6154 const variables = new Set < string > ( ) ;
6255
56+ let setArgAsDeclared = true ;
6357 visit ( msg , {
6458 declaration ( decl ) {
6559 // Skip all ReservedStatement
6660 if ( ! decl . name ) return undefined ;
6761
68- if ( declared . has ( decl . name ) ) onError ( 'duplicate-declaration' , decl ) ;
69- else declared . add ( decl . name ) ;
70-
7162 if (
7263 decl . value . annotation ||
7364 ( decl . type === 'local' &&
@@ -79,12 +70,10 @@ export function validate(
7970
8071 if ( decl . type === 'local' ) localVars . add ( decl . name ) ;
8172
73+ setArgAsDeclared = decl . type === 'local' ;
8274 return ( ) => {
83- for ( const ref of declRefs . get ( decl . name ) ?? [ ] ) {
84- if ( decl . type !== 'input' || ref !== decl . value . arg ) {
85- onError ( 'forward-reference' , ref ) ;
86- }
87- }
75+ if ( declared . has ( decl . name ) ) onError ( 'duplicate-declaration' , decl ) ;
76+ else declared . add ( decl . name ) ;
8877 } ;
8978 } ,
9079
@@ -112,13 +101,14 @@ export function validate(
112101 }
113102 } ,
114103
115- value ( value , context ) {
104+ value ( value , context , position ) {
116105 if ( value . type === 'variable' ) {
117106 variables . add ( value . name ) ;
118- if ( context === 'declaration' ) {
119- const prev = declRefs . get ( value . name ) ;
120- if ( prev ) prev . push ( value ) ;
121- else declRefs . set ( value . name , [ value ] ) ;
107+ if (
108+ context === 'declaration' &&
109+ ( position !== 'arg' || setArgAsDeclared )
110+ ) {
111+ declared . add ( value . name ) ;
122112 }
123113 }
124114 } ,
0 commit comments