4
4
* See the accompanying LICENSE file for terms.
5
5
*/
6
6
7
- import * as p from " path" ;
8
- import { writeFileSync } from "fs" ;
9
- import { sync as mkdirpSync } from " mkdirp" ;
10
- import printICUMessage from " ./print-icu-message" ;
7
+ import * as p from ' path' ;
8
+ import { writeFileSync } from 'fs' ;
9
+ import { sync as mkdirpSync } from ' mkdirp' ;
10
+ import printICUMessage from ' ./print-icu-message' ;
11
11
12
- const COMPONENT_NAMES = [ " FormattedMessage" , " FormattedHTMLMessage" ] ;
12
+ const COMPONENT_NAMES = [ ' FormattedMessage' , ' FormattedHTMLMessage' ] ;
13
13
14
- const FUNCTION_NAMES = [ " defineMessages" ] ;
14
+ const FUNCTION_NAMES = [ ' defineMessages' ] ;
15
15
16
- const DESCRIPTOR_PROPS = new Set ( [ "id" , " description" , " defaultMessage" ] ) ;
16
+ const DESCRIPTOR_PROPS = new Set ( [ 'id' , ' description' , ' defaultMessage' ] ) ;
17
17
18
- const EXTRACTED = Symbol ( " ReactIntlExtracted" ) ;
19
- const MESSAGES = Symbol ( " ReactIntlMessages" ) ;
18
+ const EXTRACTED = Symbol ( ' ReactIntlExtracted' ) ;
19
+ const MESSAGES = Symbol ( ' ReactIntlMessages' ) ;
20
20
21
21
export default function ( { types : t } ) {
22
22
function getModuleSourceName ( opts ) {
23
- return opts . moduleSourceName || " react-intl" ;
23
+ return opts . moduleSourceName || ' react-intl' ;
24
24
}
25
25
26
26
function evaluatePath ( path ) {
@@ -30,7 +30,7 @@ export default function({ types: t }) {
30
30
}
31
31
32
32
throw path . buildCodeFrameError (
33
- " [React Intl] Messages must be statically evaluate-able for extraction."
33
+ ' [React Intl] Messages must be statically evaluate-able for extraction.'
34
34
) ;
35
35
}
36
36
@@ -44,13 +44,13 @@ export default function({ types: t }) {
44
44
45
45
function getMessageDescriptorValue ( path ) {
46
46
if ( path . isJSXExpressionContainer ( ) ) {
47
- path = path . get ( " expression" ) ;
47
+ path = path . get ( ' expression' ) ;
48
48
}
49
49
50
50
// Always trim the Message Descriptor values.
51
51
const descriptorValue = evaluatePath ( path ) ;
52
52
53
- if ( typeof descriptorValue === " string" ) {
53
+ if ( typeof descriptorValue === ' string' ) {
54
54
return descriptorValue . trim ( ) ;
55
55
}
56
56
@@ -66,20 +66,20 @@ export default function({ types: t }) {
66
66
if (
67
67
isJSXSource &&
68
68
messagePath . isLiteral ( ) &&
69
- message . indexOf ( " \\\\" ) >= 0
69
+ message . indexOf ( ' \\\\' ) >= 0
70
70
) {
71
71
throw messagePath . buildCodeFrameError (
72
- " [React Intl] Message failed to parse. " +
73
- " It looks like `\\`s were used for escaping, " +
72
+ ' [React Intl] Message failed to parse. ' +
73
+ ' It looks like `\\`s were used for escaping, ' +
74
74
"this won't work with JSX string literals. " +
75
- " Wrap with `{}`. " +
76
- " See: http://facebook.github.io/react/docs/jsx-gotchas.html"
75
+ ' Wrap with `{}`. ' +
76
+ ' See: http://facebook.github.io/react/docs/jsx-gotchas.html'
77
77
) ;
78
78
}
79
79
80
80
throw messagePath . buildCodeFrameError (
81
- " [React Intl] Message failed to parse. " +
82
- " See: http://formatjs.io/guides/message-syntax/" +
81
+ ' [React Intl] Message failed to parse. ' +
82
+ ' See: http://formatjs.io/guides/message-syntax/' +
83
83
`\n${ parseError } `
84
84
) ;
85
85
}
@@ -104,7 +104,7 @@ export default function({ types: t }) {
104
104
Object . keys ( descriptor ) . forEach ( key => {
105
105
const valuePath = descriptor [ key ] ;
106
106
107
- if ( key === " defaultMessage" ) {
107
+ if ( key === ' defaultMessage' ) {
108
108
descriptor [ key ] = getICUMessageValue ( valuePath , { isJSXSource } ) ;
109
109
} else {
110
110
descriptor [ key ] = getMessageDescriptorValue ( valuePath ) ;
@@ -127,7 +127,7 @@ export default function({ types: t }) {
127
127
128
128
if ( ! ( id && defaultMessage ) ) {
129
129
throw path . buildCodeFrameError (
130
- " [React Intl] Message Descriptors require an `id` and `defaultMessage`."
130
+ ' [React Intl] Message Descriptors require an `id` and `defaultMessage`.'
131
131
) ;
132
132
}
133
133
@@ -141,18 +141,18 @@ export default function({ types: t }) {
141
141
) {
142
142
throw path . buildCodeFrameError (
143
143
`[React Intl] Duplicate message id: "${ id } ", ` +
144
- " but the `description` and/or `defaultMessage` are different."
144
+ ' but the `description` and/or `defaultMessage` are different.'
145
145
) ;
146
146
}
147
147
}
148
148
149
149
if ( opts . enforceDescriptions ) {
150
150
if (
151
151
! description ||
152
- ( typeof description === " object" && Object . keys ( description ) . length < 1 )
152
+ ( typeof description === ' object' && Object . keys ( description ) . length < 1 )
153
153
) {
154
154
throw path . buildCodeFrameError (
155
- " [React Intl] Message must have a `description`."
155
+ ' [React Intl] Message must have a `description`.'
156
156
) ;
157
157
}
158
158
}
@@ -178,13 +178,13 @@ export default function({ types: t }) {
178
178
179
179
function isFormatMessageCall ( path ) {
180
180
if ( t . isMemberExpression ( path ) ) {
181
- const property = path . get ( " property" ) ;
181
+ const property = path . get ( ' property' ) ;
182
182
183
- if ( t . isIdentifier ( property ) && property . node . name === " formatMessage" ) {
184
- const object = path . get ( " object" ) ;
185
- const objectProperty = object . get ( " property" ) ;
183
+ if ( t . isIdentifier ( property ) && property . node . name === ' formatMessage' ) {
184
+ const object = path . get ( ' object' ) ;
185
+ const objectProperty = object . get ( ' property' ) ;
186
186
if ( t . isIdentifier ( objectProperty ) ) {
187
- if ( objectProperty . node . name === " intl" ) {
187
+ if ( objectProperty . node . name === ' intl' ) {
188
188
return true ;
189
189
}
190
190
}
@@ -216,7 +216,7 @@ export default function({ types: t }) {
216
216
const basename = p . basename ( filename , p . extname ( filename ) ) ;
217
217
const messages = file . get ( MESSAGES ) ;
218
218
const descriptors = [ ...messages . values ( ) ] ;
219
- file . metadata [ " react-intl" ] = { messages : descriptors } ;
219
+ file . metadata [ ' react-intl' ] = { messages : descriptors } ;
220
220
221
221
if ( opts . messagesDir && descriptors . length > 0 ) {
222
222
// Make sure the relative path is "absolute" before
@@ -225,7 +225,7 @@ export default function({ types: t }) {
225
225
// Solve when the window user has symlink on the directory, because
226
226
// process.cwd on windows returns the symlink root,
227
227
// and filename (from babel) returns the original root
228
- if ( process . platform === " win32" ) {
228
+ if ( process . platform === ' win32' ) {
229
229
const { name } = p . parse ( process . cwd ( ) ) ;
230
230
if ( relativePath . includes ( name ) ) {
231
231
relativePath = relativePath . slice (
@@ -237,7 +237,7 @@ export default function({ types: t }) {
237
237
const messagesFilename = p . join (
238
238
opts . messagesDir ,
239
239
p . dirname ( relativePath ) ,
240
- basename + " .json"
240
+ basename + ' .json'
241
241
) ;
242
242
243
243
const messagesFile = JSON . stringify ( descriptors , null , 2 ) ;
@@ -255,26 +255,26 @@ export default function({ types: t }) {
255
255
256
256
const { file, opts } = state ;
257
257
const moduleSourceName = getModuleSourceName ( opts ) ;
258
- const name = path . get ( " name" ) ;
258
+ const name = path . get ( ' name' ) ;
259
259
260
- if ( name . referencesImport ( moduleSourceName , " FormattedPlural" ) ) {
260
+ if ( name . referencesImport ( moduleSourceName , ' FormattedPlural' ) ) {
261
261
const warn = file . log ? file . log . warn : console . warn ;
262
262
warn (
263
263
`[React Intl] Line ${ path . node . loc . start . line } : ` +
264
- " Default messages are not extracted from " +
265
- " <FormattedPlural>, use <FormattedMessage> instead."
264
+ ' Default messages are not extracted from ' +
265
+ ' <FormattedPlural>, use <FormattedMessage> instead.'
266
266
) ;
267
267
268
268
return ;
269
269
}
270
270
271
271
if ( referencesImport ( name , moduleSourceName , COMPONENT_NAMES ) ) {
272
272
const attributes = path
273
- . get ( " attributes" )
273
+ . get ( ' attributes' )
274
274
. filter ( attr => attr . isJSXAttribute ( ) ) ;
275
275
276
276
let descriptor = createMessageDescriptor (
277
- attributes . map ( attr => [ attr . get ( " name" ) , attr . get ( " value" ) ] )
277
+ attributes . map ( attr => [ attr . get ( ' name' ) , attr . get ( ' value' ) ] )
278
278
) ;
279
279
280
280
// In order for a default message to be extracted when
@@ -296,14 +296,14 @@ export default function({ types: t }) {
296
296
297
297
// Remove description since it's not used at runtime.
298
298
attributes . forEach ( attr => {
299
- const ketPath = attr . get ( " name" ) ;
300
- if ( getMessageDescriptorKey ( ketPath ) === " description" ) {
299
+ const ketPath = attr . get ( ' name' ) ;
300
+ if ( getMessageDescriptorKey ( ketPath ) === ' description' ) {
301
301
attr . remove ( ) ;
302
302
} else if (
303
303
opts . overrideIdFn &&
304
- getMessageDescriptorKey ( ketPath ) === "id"
304
+ getMessageDescriptorKey ( ketPath ) === 'id'
305
305
) {
306
- attr . get ( " value" ) . replaceWith ( t . stringLiteral ( descriptor . id ) ) ;
306
+ attr . get ( ' value' ) . replaceWith ( t . stringLiteral ( descriptor . id ) ) ;
307
307
}
308
308
} ) ;
309
309
@@ -315,16 +315,16 @@ export default function({ types: t }) {
315
315
316
316
CallExpression ( path , state ) {
317
317
const moduleSourceName = getModuleSourceName ( state . opts ) ;
318
- const callee = path . get ( " callee" ) ;
318
+ const callee = path . get ( ' callee' ) ;
319
319
const { opts } = state ;
320
320
321
321
function assertObjectExpression ( node ) {
322
322
if ( ! ( node && node . isObjectExpression ( ) ) ) {
323
323
throw path . buildCodeFrameError (
324
324
`[React Intl] \`${ callee . node . name } ()\` must be ` +
325
- " called with an object expression with values " +
326
- " that are React Intl Message Descriptors, also " +
327
- " defined as object expressions."
325
+ ' called with an object expression with values ' +
326
+ ' that are React Intl Message Descriptors, also ' +
327
+ ' defined as object expressions.'
328
328
) ;
329
329
}
330
330
}
@@ -336,10 +336,10 @@ export default function({ types: t }) {
336
336
return ;
337
337
}
338
338
339
- const properties = messageObj . get ( " properties" ) ;
339
+ const properties = messageObj . get ( ' properties' ) ;
340
340
341
341
let descriptor = createMessageDescriptor (
342
- properties . map ( prop => [ prop . get ( " key" ) , prop . get ( " value" ) ] )
342
+ properties . map ( prop => [ prop . get ( ' key' ) , prop . get ( ' value' ) ] )
343
343
) ;
344
344
345
345
// Evaluate the Message Descriptor values, then store it.
@@ -352,11 +352,11 @@ export default function({ types: t }) {
352
352
messageObj . replaceWith (
353
353
t . objectExpression ( [
354
354
t . objectProperty (
355
- t . stringLiteral ( "id" ) ,
355
+ t . stringLiteral ( 'id' ) ,
356
356
t . stringLiteral ( descriptor . id )
357
357
) ,
358
358
t . objectProperty (
359
- t . stringLiteral ( " defaultMessage" ) ,
359
+ t . stringLiteral ( ' defaultMessage' ) ,
360
360
t . stringLiteral ( descriptor . defaultMessage )
361
361
)
362
362
] )
@@ -367,18 +367,18 @@ export default function({ types: t }) {
367
367
}
368
368
369
369
if ( referencesImport ( callee , moduleSourceName , FUNCTION_NAMES ) ) {
370
- const messagesObj = path . get ( " arguments" ) [ 0 ] ;
370
+ const messagesObj = path . get ( ' arguments' ) [ 0 ] ;
371
371
372
372
assertObjectExpression ( messagesObj ) ;
373
373
374
374
messagesObj
375
- . get ( " properties" )
376
- . map ( prop => prop . get ( " value" ) )
375
+ . get ( ' properties' )
376
+ . map ( prop => prop . get ( ' value' ) )
377
377
. forEach ( processMessageObject ) ;
378
378
}
379
379
380
380
if ( isFormatMessageCall ( callee ) ) {
381
- const messagesObj = path . get ( " arguments" ) [ 0 ] ;
381
+ const messagesObj = path . get ( ' arguments' ) [ 0 ] ;
382
382
processMessageObject ( messagesObj ) ;
383
383
}
384
384
}
0 commit comments