@@ -20,15 +20,16 @@ const FUNCTION_NAMES = [
20
20
21
21
const DESCRIPTOR_PROPS = new Set ( [ 'id' , 'description' , 'defaultMessage' ] ) ;
22
22
23
- const EXTRACTED_TAG = Symbol ( 'ReactIntlExtracted' ) ;
23
+ const EXTRACTED = Symbol ( 'ReactIntlExtracted' ) ;
24
+ const MESSAGES = Symbol ( 'ReactIntlMessages' ) ;
24
25
25
26
export default function ( { types : t } ) {
26
27
function getModuleSourceName ( opts ) {
27
28
return opts . moduleSourceName || 'react-intl' ;
28
29
}
29
30
30
31
function evaluatePath ( path ) {
31
- let evaluated = path . evaluate ( ) ;
32
+ const evaluated = path . evaluate ( ) ;
32
33
if ( evaluated . confident ) {
33
34
return evaluated . value ;
34
35
}
@@ -62,7 +63,7 @@ export default function ({types: t}) {
62
63
}
63
64
64
65
function getICUMessageValue ( messagePath , { isJSXSource = false } = { } ) {
65
- let message = getMessageDescriptorValue ( messagePath ) ;
66
+ const message = getMessageDescriptorValue ( messagePath ) ;
66
67
67
68
try {
68
69
return printICUMessage ( message ) ;
@@ -90,7 +91,7 @@ export default function ({types: t}) {
90
91
91
92
function createMessageDescriptor ( propPaths ) {
92
93
return propPaths . reduce ( ( hash , [ keyPath , valuePath ] ) => {
93
- let key = getMessageDescriptorKey ( keyPath ) ;
94
+ const key = getMessageDescriptorKey ( keyPath ) ;
94
95
95
96
if ( DESCRIPTOR_PROPS . has ( key ) ) {
96
97
hash [ key ] = valuePath ;
@@ -102,7 +103,7 @@ export default function ({types: t}) {
102
103
103
104
function evaluateMessageDescriptor ( { ...descriptor } , { isJSXSource = false } = { } ) {
104
105
Object . keys ( descriptor ) . forEach ( ( key ) => {
105
- let valuePath = descriptor [ key ] ;
106
+ const valuePath = descriptor [ key ] ;
106
107
107
108
if ( key === 'defaultMessage' ) {
108
109
descriptor [ key ] = getICUMessageValue ( valuePath , { isJSXSource} ) ;
@@ -115,16 +116,17 @@ export default function ({types: t}) {
115
116
}
116
117
117
118
function storeMessage ( { id, description, defaultMessage} , path , state ) {
118
- const { file, opts, reactIntl } = state ;
119
+ const { file, opts} = state ;
119
120
120
121
if ( ! ( id && defaultMessage ) ) {
121
122
throw path . buildCodeFrameError (
122
123
'[React Intl] Message Descriptors require an `id` and `defaultMessage`.'
123
124
) ;
124
125
}
125
126
126
- if ( reactIntl . messages . has ( id ) ) {
127
- let existing = reactIntl . messages . get ( id ) ;
127
+ const messages = file . get ( MESSAGES ) ;
128
+ if ( messages . has ( id ) ) {
129
+ const existing = messages . get ( id ) ;
128
130
129
131
if ( description !== existing . description ||
130
132
defaultMessage !== existing . defaultMessage ) {
@@ -155,7 +157,7 @@ export default function ({types: t}) {
155
157
} ;
156
158
}
157
159
158
- reactIntl . messages . set ( id , { id, description, defaultMessage, ...loc } ) ;
160
+ messages . set ( id , { id, description, defaultMessage, ...loc } ) ;
159
161
}
160
162
161
163
function referencesImport ( path , mod , importedNames ) {
@@ -167,51 +169,50 @@ export default function ({types: t}) {
167
169
}
168
170
169
171
function tagAsExtracted ( path ) {
170
- path . node [ EXTRACTED_TAG ] = true ;
172
+ path . node [ EXTRACTED ] = true ;
171
173
}
172
174
173
175
function wasExtracted ( path ) {
174
- return ! ! path . node [ EXTRACTED_TAG ] ;
176
+ return ! ! path . node [ EXTRACTED ] ;
175
177
}
176
178
177
179
return {
178
- visitor : {
179
- Program : {
180
- enter ( path , state ) {
181
- state . reactIntl = {
182
- messages : new Map ( ) ,
183
- } ;
184
- } ,
185
-
186
- exit ( path , state ) {
187
- const { file, opts, reactIntl} = state ;
188
- const { basename, filename} = file . opts ;
189
-
190
- let descriptors = [ ...reactIntl . messages . values ( ) ] ;
191
- file . metadata [ 'react-intl' ] = { messages : descriptors } ;
192
-
193
- if ( opts . messagesDir && descriptors . length > 0 ) {
194
- // Make sure the relative path is "absolute" before
195
- // joining it with the `messagesDir`.
196
- let relativePath = p . join (
197
- p . sep ,
198
- p . relative ( process . cwd ( ) , filename )
199
- ) ;
180
+ pre ( file ) {
181
+ if ( ! file . has ( MESSAGES ) ) {
182
+ file . set ( MESSAGES , new Map ( ) ) ;
183
+ }
184
+ } ,
200
185
201
- let messagesFilename = p . join (
202
- opts . messagesDir ,
203
- p . dirname ( relativePath ) ,
204
- basename + '.json'
205
- ) ;
186
+ post ( file ) {
187
+ const { opts} = this ;
188
+ const { basename, filename} = file . opts ;
206
189
207
- let messagesFile = JSON . stringify ( descriptors , null , 2 ) ;
190
+ const messages = file . get ( MESSAGES ) ;
191
+ const descriptors = [ ...messages . values ( ) ] ;
192
+ file . metadata [ 'react-intl' ] = { messages : descriptors } ;
208
193
209
- mkdirpSync ( p . dirname ( messagesFilename ) ) ;
210
- writeFileSync ( messagesFilename , messagesFile ) ;
211
- }
212
- } ,
213
- } ,
194
+ if ( opts . messagesDir && descriptors . length > 0 ) {
195
+ // Make sure the relative path is "absolute" before
196
+ // joining it with the `messagesDir`.
197
+ const relativePath = p . join (
198
+ p . sep ,
199
+ p . relative ( process . cwd ( ) , filename )
200
+ ) ;
214
201
202
+ const messagesFilename = p . join (
203
+ opts . messagesDir ,
204
+ p . dirname ( relativePath ) ,
205
+ basename + '.json'
206
+ ) ;
207
+
208
+ const messagesFile = JSON . stringify ( descriptors , null , 2 ) ;
209
+
210
+ mkdirpSync ( p . dirname ( messagesFilename ) ) ;
211
+ writeFileSync ( messagesFilename , messagesFile ) ;
212
+ }
213
+ } ,
214
+
215
+ visitor : {
215
216
JSXOpeningElement ( path , state ) {
216
217
if ( wasExtracted ( path ) ) {
217
218
return ;
@@ -232,7 +233,7 @@ export default function ({types: t}) {
232
233
}
233
234
234
235
if ( referencesImport ( name , moduleSourceName , COMPONENT_NAMES ) ) {
235
- let attributes = path . get ( 'attributes' )
236
+ const attributes = path . get ( 'attributes' )
236
237
. filter ( ( attr ) => attr . isJSXAttribute ( ) ) ;
237
238
238
239
let descriptor = createMessageDescriptor (
@@ -260,7 +261,7 @@ export default function ({types: t}) {
260
261
261
262
// Remove description since it's not used at runtime.
262
263
attributes . some ( ( attr ) => {
263
- let ketPath = attr . get ( 'name' ) ;
264
+ const ketPath = attr . get ( 'name' ) ;
264
265
if ( getMessageDescriptorKey ( ketPath ) === 'description' ) {
265
266
attr . remove ( ) ;
266
267
return true ;
@@ -295,7 +296,7 @@ export default function ({types: t}) {
295
296
return ;
296
297
}
297
298
298
- let properties = messageObj . get ( 'properties' ) ;
299
+ const properties = messageObj . get ( 'properties' ) ;
299
300
300
301
let descriptor = createMessageDescriptor (
301
302
properties . map ( ( prop ) => [
@@ -325,7 +326,7 @@ export default function ({types: t}) {
325
326
}
326
327
327
328
if ( referencesImport ( callee , moduleSourceName , FUNCTION_NAMES ) ) {
328
- let messagesObj = path . get ( 'arguments' ) [ 0 ] ;
329
+ const messagesObj = path . get ( 'arguments' ) [ 0 ] ;
329
330
330
331
assertObjectExpression ( messagesObj ) ;
331
332
0 commit comments