Currently it clones SymbolId, ScopeId and ReferenceId along with all other parts of AST being cloned.
I don't think this is correct behavior, but am not sure what the correct behavior should be.
If you clone part of an AST which has been through Semantic, and insert it elsewhere in same AST, that AST will now contain duplicate IDs.
If you insert it into another AST, the IDs will be invalid. It can cause panic if try to look up the symbol for a SymbolId, if the destination AST has less symbols than the source AST, so SymbolId is out of bounds. And if it doesn't panic, it'll be the wrong symbol.
Ideally we should provide another API where you provide the destination AST's ScopeTree and SymbolTable, and what scope the cloned AST fragment will be inserted into, and then it can create new scopes/symbols/references, and patch up all the IDs in the clone automatically for you.
oxc-project/oxc#4732 introduced
CloneIn
trait.Currently it clones
SymbolId
,ScopeId
andReferenceId
along with all other parts of AST being cloned.I don't think this is correct behavior, but am not sure what the correct behavior should be.
If you clone part of an AST which has been through
Semantic
, and insert it elsewhere in same AST, that AST will now contain duplicate IDs.If you insert it into another AST, the IDs will be invalid. It can cause panic if try to look up the symbol for a
SymbolId
, if the destination AST has less symbols than the source AST, soSymbolId
is out of bounds. And if it doesn't panic, it'll be the wrong symbol.Ideally we should provide another API where you provide the destination AST's
ScopeTree
andSymbolTable
, and what scope the cloned AST fragment will be inserted into, and then it can create new scopes/symbols/references, and patch up all the IDs in the clone automatically for you.But in meantime, to avoid invalid IDs, perhaps
CloneIn
should setscope_id
,symbol_id
andreference_id
fields toNone
?