1
- import * as es from 'estree'
1
+ import type es from 'estree'
2
2
3
3
import { ConstAssignment , UndefinedVariable } from '../errors/errors'
4
4
import { NoAssignmentToForVariable } from '../errors/validityErrors'
5
5
import { 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'
8
7
import {
9
8
getFunctionDeclarationNamesInProgram ,
10
9
getIdentifiersInNativeStorage ,
11
10
getIdentifiersInProgram ,
12
11
getNativeIds ,
13
- NativeIds
12
+ type NativeIds
14
13
} 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'
16
16
17
17
class Declaration {
18
18
public accessedBeforeDeclaration : boolean = false
@@ -30,7 +30,7 @@ export function validateAndAnnotate(
30
30
for ( const statement of node . body ) {
31
31
if ( statement . type === 'VariableDeclaration' ) {
32
32
initialisedIdentifiers . set (
33
- getVariableDeclarationName ( statement ) ,
33
+ getSourceVariableDeclaration ( statement ) . id . name ,
34
34
new Declaration ( statement . kind === 'const' )
35
35
)
36
36
} else if ( statement . type === 'FunctionDeclaration' ) {
@@ -64,7 +64,9 @@ export function validateAndAnnotate(
64
64
if ( init . type === 'VariableDeclaration' ) {
65
65
accessedBeforeDeclarationMap . set (
66
66
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
+ ] )
68
70
)
69
71
scopeHasCallExpressionMap . set ( forStatement , false )
70
72
}
@@ -105,7 +107,7 @@ export function validateAndAnnotate(
105
107
{
106
108
VariableDeclaration ( node : NodeWithInferredType < es . VariableDeclaration > , ancestors : Node [ ] ) {
107
109
const lastAncestor = ancestors [ ancestors . length - 2 ]
108
- const name = getVariableDeclarationName ( node )
110
+ const { name } = getSourceVariableDeclaration ( node ) . id
109
111
const accessedBeforeDeclaration = accessedBeforeDeclarationMap
110
112
. get ( lastAncestor ) !
111
113
. get ( name ) ! . accessedBeforeDeclaration
@@ -180,7 +182,10 @@ export function checkForUndefinedVariables(
180
182
const identifiers = new Set < string > ( )
181
183
for ( const statement of node . body ) {
182
184
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 )
184
189
} else if ( statement . type === 'FunctionDeclaration' ) {
185
190
if ( statement . id === null ) {
186
191
throw new Error (
@@ -217,13 +222,13 @@ export function checkForUndefinedVariables(
217
222
BlockStatement : processBlock ,
218
223
FunctionDeclaration : processFunction ,
219
224
ArrowFunctionExpression : processFunction ,
220
- ForStatement ( forStatement : es . ForStatement , ancestors : es . Node [ ] ) {
225
+ ForStatement ( forStatement : es . ForStatement ) {
221
226
const init = forStatement . init !
222
227
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 ] ) )
227
232
}
228
233
} ,
229
234
Identifier ( identifier : es . Identifier , ancestors : es . Node [ ] ) {
0 commit comments