This repository was archived by the owner on Jun 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathindex.js
400 lines (332 loc) · 11.8 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
import * as p from 'path';
import { writeFileSync } from 'fs';
import { mkdirpSync } from 'fs-extra';
import printICUMessage from './print-icu-message';
const DEFAULT_COMPONENT_NAMES = ['FormattedMessage', 'FormattedHTMLMessage'];
const FUNCTION_NAMES = ['defineMessages'];
const DESCRIPTOR_PROPS = new Set(['id', 'description', 'defaultMessage']);
const EXTRACTED = Symbol('ReactIntlExtracted');
const MESSAGES = Symbol('ReactIntlMessages');
export default function({ types: t }) {
function getModuleSourceName(opts) {
return opts.moduleSourceName || 'react-intl';
}
function evaluatePath(path) {
const evaluated = path.evaluate();
if (evaluated.confident) {
return evaluated.value;
}
throw path.buildCodeFrameError(
'[React Intl] Messages must be statically evaluate-able for extraction.'
);
}
function getMessageDescriptorKey(path) {
if (path.isIdentifier() || path.isJSXIdentifier()) {
return path.node.name;
}
return evaluatePath(path);
}
function getMessageDescriptorValue(path) {
if (path.isJSXExpressionContainer()) {
path = path.get('expression');
}
// Always trim the Message Descriptor values.
const descriptorValue = evaluatePath(path);
if (typeof descriptorValue === 'string') {
return descriptorValue.trim();
}
return descriptorValue;
}
function getICUMessageValue(messagePath, { isJSXSource = false } = {}) {
const message = getMessageDescriptorValue(messagePath);
try {
return printICUMessage(message);
} catch (parseError) {
if (
isJSXSource &&
messagePath.isLiteral() &&
message.indexOf('\\\\') >= 0
) {
throw messagePath.buildCodeFrameError(
'[React Intl] Message failed to parse. ' +
'It looks like `\\`s were used for escaping, ' +
"this won't work with JSX string literals. " +
'Wrap with `{}`. ' +
'See: http://facebook.github.io/react/docs/jsx-gotchas.html'
);
}
throw messagePath.buildCodeFrameError(
'[React Intl] Message failed to parse. ' +
'See: http://formatjs.io/guides/message-syntax/' +
`\n${parseError}`
);
}
}
function createMessageDescriptor(propPaths) {
return propPaths.reduce((hash, [keyPath, valuePath]) => {
const key = getMessageDescriptorKey(keyPath);
if (DESCRIPTOR_PROPS.has(key)) {
hash[key] = valuePath;
}
return hash;
}, {});
}
function evaluateMessageDescriptor(
{ ...descriptor },
{ isJSXSource = false, overrideIdFn } = {}
) {
Object.keys(descriptor).forEach(key => {
const valuePath = descriptor[key];
if (key === 'defaultMessage') {
descriptor[key] = getICUMessageValue(valuePath, { isJSXSource });
} else {
descriptor[key] = getMessageDescriptorValue(valuePath);
}
});
if (overrideIdFn) {
descriptor.id = overrideIdFn(
descriptor.id,
descriptor.defaultMessage,
descriptor.description
);
}
return descriptor;
}
function storeMessage({ id, description, defaultMessage }, path, state) {
const { file, opts } = state;
if (!(id && defaultMessage)) {
throw path.buildCodeFrameError(
'[React Intl] Message Descriptors require an `id` and `defaultMessage`.'
);
}
const messages = file.get(MESSAGES);
if (messages.has(id)) {
const existing = messages.get(id);
if (
description !== existing.description ||
defaultMessage !== existing.defaultMessage
) {
throw path.buildCodeFrameError(
`[React Intl] Duplicate message id: "${id}", ` +
'but the `description` and/or `defaultMessage` are different.'
);
}
}
if (opts.enforceDescriptions) {
if (
!description ||
(typeof description === 'object' && Object.keys(description).length < 1)
) {
throw path.buildCodeFrameError(
'[React Intl] Message must have a `description`.'
);
}
}
let loc;
if (opts.extractSourceLocation) {
loc = {
file: p.relative(process.cwd(), file.opts.filename),
...path.node.loc
};
}
messages.set(id, { id, description, defaultMessage, ...loc });
}
function referencesImport(path, mod, importedNames) {
if (!(path.isIdentifier() || path.isJSXIdentifier())) {
return false;
}
return importedNames.some(name => path.referencesImport(mod, name));
}
function isFormatMessageCall(path) {
if (t.isMemberExpression(path)) {
const property = path.get('property');
if (t.isIdentifier(property) && property.node.name === 'formatMessage') {
const object = path.get('object');
const objectProperty = object.get('property');
if (t.isIdentifier(objectProperty)) {
if (objectProperty.node.name === 'intl') {
return true;
}
}
}
}
return false;
}
function tagAsExtracted(path) {
path.node[EXTRACTED] = true;
}
function wasExtracted(path) {
return !!path.node[EXTRACTED];
}
return {
pre(file) {
if (!file.has(MESSAGES)) {
file.set(MESSAGES, new Map());
}
},
post(file) {
const { opts } = this;
const { filename } = file.opts;
const basename = p.basename(filename, p.extname(filename));
const messages = file.get(MESSAGES);
const descriptors = [...messages.values()];
file.metadata['react-intl'] = { messages: descriptors };
if (opts.messagesDir && descriptors.length > 0) {
// Make sure the relative path is "absolute" before
// joining it with the `messagesDir`.
let relativePath = p.join(p.sep, p.relative(process.cwd(), filename));
// Solve when the window user has symlink on the directory, because
// process.cwd on windows returns the symlink root,
// and filename (from babel) returns the original root
if (process.platform === 'win32') {
const { name } = p.parse(process.cwd());
if (relativePath.includes(name)) {
relativePath = relativePath.slice(
relativePath.indexOf(name) + name.length
);
}
}
const messagesFilename = p.join(
opts.messagesDir,
p.dirname(relativePath),
basename + '.json'
);
const messagesFile = JSON.stringify(descriptors, null, 2);
mkdirpSync(p.dirname(messagesFilename));
writeFileSync(messagesFilename, messagesFile);
}
},
visitor: {
JSXOpeningElement(path, state) {
if (wasExtracted(path)) {
return;
}
const { file, opts } = state;
const moduleSourceName = getModuleSourceName(opts);
const name = path.get('name');
if (name.referencesImport(moduleSourceName, 'FormattedPlural')) {
const warn = file.log ? file.log.warn : console.warn;
warn(
`[React Intl] Line ${path.node.loc.start.line}: ` +
'Default messages are not extracted from ' +
'<FormattedPlural>, use <FormattedMessage> instead.'
);
return;
}
const { additionalComponentNames = [] } = opts;
if (
referencesImport(name, moduleSourceName, DEFAULT_COMPONENT_NAMES) ||
additionalComponentNames.includes(name.node.name)
) {
const attributes = path
.get('attributes')
.filter(attr => attr.isJSXAttribute());
let descriptor = createMessageDescriptor(
attributes.map(attr => [attr.get('name'), attr.get('value')])
);
// In order for a default message to be extracted when
// declaring a JSX element, it must be done with standard
// `key=value` attributes. But it's completely valid to
// write `<FormattedMessage {...descriptor} />` or
// `<FormattedMessage id={dynamicId} />`, because it will be
// skipped here and extracted elsewhere. The descriptor will
// be extracted only if a `defaultMessage` prop exists.
if (descriptor.defaultMessage) {
// Evaluate the Message Descriptor values in a JSX
// context, then store it.
descriptor = evaluateMessageDescriptor(descriptor, {
isJSXSource: true,
overrideIdFn: opts.overrideIdFn
});
storeMessage(descriptor, path, state);
// Remove description since it's not used at runtime.
attributes.forEach(attr => {
const ketPath = attr.get('name');
const msgDescriptorKey = getMessageDescriptorKey(ketPath);
if (
msgDescriptorKey === 'description' ||
(opts.removeDefaultMessage &&
msgDescriptorKey === 'defaultMessage')
) {
attr.remove();
} else if (
opts.overrideIdFn &&
getMessageDescriptorKey(ketPath) === 'id'
) {
attr.get('value').replaceWith(t.stringLiteral(descriptor.id));
}
});
// Tag the AST node so we don't try to extract it twice.
tagAsExtracted(path);
}
}
},
CallExpression(path, state) {
const moduleSourceName = getModuleSourceName(state.opts);
const callee = path.get('callee');
const { opts } = state;
function assertObjectExpression(node) {
if (!(node && node.isObjectExpression())) {
throw path.buildCodeFrameError(
`[React Intl] \`${callee.node.name}()\` must be ` +
'called with an object expression with values ' +
'that are React Intl Message Descriptors, also ' +
'defined as object expressions.'
);
}
}
function processMessageObject(messageObj) {
assertObjectExpression(messageObj);
if (wasExtracted(messageObj)) {
return;
}
const properties = messageObj.get('properties');
let descriptor = createMessageDescriptor(
properties.map(prop => [prop.get('key'), prop.get('value')])
);
// Evaluate the Message Descriptor values, then store it.
descriptor = evaluateMessageDescriptor(descriptor, {
overrideIdFn: opts.overrideIdFn
});
storeMessage(descriptor, messageObj, state);
// Remove description since it's not used at runtime.
messageObj.replaceWith(
t.objectExpression([
t.objectProperty(
t.stringLiteral('id'),
t.stringLiteral(descriptor.id)
),
...(!opts.removeDefaultMessage
? [
t.objectProperty(
t.stringLiteral('defaultMessage'),
t.stringLiteral(descriptor.defaultMessage)
)
]
: [])
])
);
// Tag the AST node so we don't try to extract it twice.
tagAsExtracted(messageObj);
}
if (referencesImport(callee, moduleSourceName, FUNCTION_NAMES)) {
const messagesObj = path.get('arguments')[0];
assertObjectExpression(messagesObj);
messagesObj
.get('properties')
.map(prop => prop.get('value'))
.forEach(processMessageObject);
}
if (isFormatMessageCall(callee)) {
const messagesObj = path.get('arguments')[0];
processMessageObject(messagesObj);
}
}
}
};
}