-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Declaration.js
141 lines (108 loc) · 3.08 KB
/
Declaration.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { blank, forOwn, keys } from './utils/object.js';
import { makeLegal, reservedWords } from './utils/identifierHelpers.js';
import { UNKNOWN } from './ast/values.js';
export default class Declaration {
constructor ( node, isParam ) {
this.node = node;
this.name = node.id ? node.id.name : node.name;
this.exportName = null;
this.isParam = isParam;
this.isReassigned = false;
}
activate () {
if ( this.activated ) return;
this.activated = true;
if ( this.isParam ) return;
this.node.activate();
}
addReference ( reference ) {
reference.declaration = this;
if ( reference.name !== this.name ) {
this.name = makeLegal( reference.name ); // TODO handle differences of opinion
}
if ( reference.isReassignment ) this.isReassigned = true;
}
render ( es ) {
if ( es ) return this.name;
if ( !this.isReassigned || !this.exportName ) return this.name;
return `exports.${this.exportName}`;
}
}
export class SyntheticNamespaceDeclaration {
constructor ( module ) {
this.isNamespace = true;
this.module = module;
this.name = module.basename();
this.needsNamespaceBlock = false;
this.originals = blank();
module.getExports().forEach( name => {
this.originals[ name ] = module.traceExport( name );
});
}
activate () {
this.needsNamespaceBlock = true;
// add synthetic references, in case of chained
// namespace imports
forOwn( this.originals, original => {
original.activate();
});
}
addReference ( node ) {
this.name = node.name;
}
gatherPossibleValues ( values ) {
values.add( UNKNOWN );
}
getName () {
return this.name;
}
renderBlock ( es, legacy, indentString ) {
const members = keys( this.originals ).map( name => {
const original = this.originals[ name ];
if ( original.isReassigned && !legacy ) {
return `${indentString}get ${name} () { return ${original.getName( es )}; }`;
}
if ( legacy && ~reservedWords.indexOf( name ) ) name = `'${name}'`;
return `${indentString}${name}: ${original.getName( es )}`;
});
const callee = legacy ? `(Object.freeze || Object)` : `Object.freeze`;
return `${this.module.bundle.varOrConst} ${this.getName( es )} = ${callee}({\n${members.join( ',\n' )}\n});\n\n`;
}
}
export class ExternalDeclaration {
constructor ( module, name ) {
this.module = module;
this.name = name;
this.safeName = null;
this.isExternal = true;
this.activated = false;
this.isNamespace = name === '*';
}
activate () {
this.module.used = true;
this.activated = true;
}
addReference ( reference ) {
reference.declaration = this;
if ( this.name === 'default' || this.name === '*' ) {
this.module.suggestName( reference.name );
}
}
gatherPossibleValues ( values ) {
values.add( UNKNOWN );
}
getName ( es ) {
if ( this.name === '*' ) {
return this.module.name;
}
if ( this.name === 'default' ) {
return this.module.exportsNamespace || ( !es && this.module.exportsNames ) ?
`${this.module.name}__default` :
this.module.name;
}
return es ? this.safeName : `${this.module.name}.${this.name}`;
}
setSafeName ( name ) {
this.safeName = name;
}
}