1- import * as es from 'estree'
1+ import type es from 'estree'
22
33import { ConstAssignment , UndefinedVariable } from '../errors/errors'
44import { NoAssignmentToForVariable } from '../errors/validityErrors'
55import { parse } from '../parser/parser'
6- import { Context , Node , NodeWithInferredType } from '../types'
7- import { getVariableDeclarationName } from '../utils/ast/astCreator'
6+ import type { Context , Node , NodeWithInferredType } from '../types'
87import {
98 getFunctionDeclarationNamesInProgram ,
109 getIdentifiersInNativeStorage ,
1110 getIdentifiersInProgram ,
1211 getNativeIds ,
13- NativeIds
12+ type NativeIds
1413} from '../utils/uniqueIds'
15- import { ancestor , base , FullWalkerCallback } from '../utils/walkers'
14+ import { ancestor , base , type FullWalkerCallback } from '../utils/walkers'
15+ import { getSourceVariableDeclaration } from '../utils/ast/helpers'
1616
1717class Declaration {
1818 public accessedBeforeDeclaration : boolean = false
@@ -30,7 +30,7 @@ export function validateAndAnnotate(
3030 for ( const statement of node . body ) {
3131 if ( statement . type === 'VariableDeclaration' ) {
3232 initialisedIdentifiers . set (
33- getVariableDeclarationName ( statement ) ,
33+ getSourceVariableDeclaration ( statement ) . id . name ,
3434 new Declaration ( statement . kind === 'const' )
3535 )
3636 } else if ( statement . type === 'FunctionDeclaration' ) {
@@ -64,7 +64,9 @@ export function validateAndAnnotate(
6464 if ( init . type === 'VariableDeclaration' ) {
6565 accessedBeforeDeclarationMap . set (
6666 forStatement ,
67- new Map ( [ [ getVariableDeclarationName ( init ) , new Declaration ( init . kind === 'const' ) ] ] )
67+ new Map ( [
68+ [ getSourceVariableDeclaration ( init ) . id . name , new Declaration ( init . kind === 'const' ) ]
69+ ] )
6870 )
6971 scopeHasCallExpressionMap . set ( forStatement , false )
7072 }
@@ -105,7 +107,7 @@ export function validateAndAnnotate(
105107 {
106108 VariableDeclaration ( node : NodeWithInferredType < es . VariableDeclaration > , ancestors : Node [ ] ) {
107109 const lastAncestor = ancestors [ ancestors . length - 2 ]
108- const name = getVariableDeclarationName ( node )
110+ const { name } = getSourceVariableDeclaration ( node ) . id
109111 const accessedBeforeDeclaration = accessedBeforeDeclarationMap
110112 . get ( lastAncestor ) !
111113 . get ( name ) ! . accessedBeforeDeclaration
@@ -180,7 +182,10 @@ export function checkForUndefinedVariables(
180182 const identifiers = new Set < string > ( )
181183 for ( const statement of node . body ) {
182184 if ( statement . type === 'VariableDeclaration' ) {
183- identifiers . add ( ( statement . declarations [ 0 ] . id as es . Identifier ) . name )
185+ const {
186+ id : { name }
187+ } = getSourceVariableDeclaration ( statement )
188+ identifiers . add ( name )
184189 } else if ( statement . type === 'FunctionDeclaration' ) {
185190 if ( statement . id === null ) {
186191 throw new Error (
@@ -217,13 +222,13 @@ export function checkForUndefinedVariables(
217222 BlockStatement : processBlock ,
218223 FunctionDeclaration : processFunction ,
219224 ArrowFunctionExpression : processFunction ,
220- ForStatement ( forStatement : es . ForStatement , ancestors : es . Node [ ] ) {
225+ ForStatement ( forStatement : es . ForStatement ) {
221226 const init = forStatement . init !
222227 if ( init . type === 'VariableDeclaration' ) {
223- identifiersIntroducedByNode . set (
224- forStatement ,
225- new Set ( [ ( init . declarations [ 0 ] . id as es . Identifier ) . name ] )
226- )
228+ const {
229+ id : { name }
230+ } = getSourceVariableDeclaration ( init )
231+ identifiersIntroducedByNode . set ( forStatement , new Set ( [ name ] ) )
227232 }
228233 } ,
229234 Identifier ( identifier : es . Identifier , ancestors : es . Node [ ] ) {
0 commit comments