-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Identifier.js
100 lines (87 loc) · 2.73 KB
/
Identifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Node from '../Node.js';
import isReference from 'is-reference';
import { UNKNOWN_ASSIGNMENT } from '../values';
export default class Identifier extends Node {
bindAssignmentAtPath ( path, expression ) {
this._bindVariableIfMissing();
if ( this.variable ) {
this.variable.bindAssignmentAtPath( path, expression );
}
}
bindCallAtPath ( path, callOptions ) {
this._bindVariableIfMissing();
if ( this.variable ) {
this.variable.bindCallAtPath( path, callOptions );
}
}
bindNode () {
this._bindVariableIfMissing();
}
_bindVariableIfMissing () {
if ( !this.variable && isReference( this, this.parent ) ) {
this.variable = this.scope.findVariable( this.name );
this.variable.addReference( this );
}
}
forEachReturnExpressionWhenCalledAtPath ( path, callOptions, callback ) {
this._bindVariableIfMissing();
if ( this.variable ) {
this.variable.forEachReturnExpressionWhenCalledAtPath( path, callOptions, callback );
}
}
hasEffectsWhenAccessedAtPath ( path, options ) {
return this.variable
&& this.variable.hasEffectsWhenAccessedAtPath( path, options );
}
hasEffectsWhenAssignedAtPath ( path, options ) {
return !this.variable
|| this.variable.hasEffectsWhenAssignedAtPath( path, options );
}
hasEffectsWhenCalledAtPath ( path, callOptions, options ) {
return !this.variable
|| this.variable.hasEffectsWhenCalledAtPath( path, callOptions, options );
}
includeInBundle () {
if ( this.included ) return false;
this.included = true;
this.variable && this.variable.includeVariable();
return true;
}
initialiseAndDeclare ( parentScope, kind, init ) {
this.initialiseScope( parentScope );
switch ( kind ) {
case 'var':
case 'function':
this.variable = this.scope.addDeclaration( this, { isHoisted: true, init } );
break;
case 'let':
case 'const':
case 'class':
this.variable = this.scope.addDeclaration( this, { init } );
break;
case 'parameter':
this.variable = this.scope.addParameterDeclaration( this );
break;
default:
throw new Error( 'Unexpected identifier kind', kind );
}
}
render ( code, es ) {
if ( this.variable ) {
const name = this.variable.getName( es );
if ( name !== this.name ) {
code.overwrite( this.start, this.end, name, { storeName: true, contentOnly: false } );
// special case
if ( this.parent.type === 'Property' && this.parent.shorthand ) {
code.appendLeft( this.start, `${this.name}: ` );
}
}
}
}
someReturnExpressionWhenCalledAtPath ( path, callOptions, predicateFunction, options ) {
if ( this.variable ) {
return this.variable.someReturnExpressionWhenCalledAtPath( path, callOptions, predicateFunction, options );
}
return predicateFunction( options )( UNKNOWN_ASSIGNMENT );
}
}