babel-plugin-lingui-transform-js
Advanced tools
Comparing version 0.1.0 to 0.2.0
142
lib/index.js
@@ -14,2 +14,4 @@ 'use strict'; | ||
const isFormatMethod = node => t.isMemberExpression(node) && t.isIdentifier(node.object, { name: 'i18n' }) && (t.isIdentifier(node.property, { name: 'date' }) || t.isIdentifier(node.property, { name: 'number' })); | ||
function processMethod(node, file, props) { | ||
@@ -28,60 +30,39 @@ if (isI18nMethod(node)) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
for (const attr of arg.properties) { | ||
if (attr.computed) { | ||
throw file.buildCodeFrameError(attr, "Computed properties aren't allowed."); | ||
} | ||
try { | ||
for (var _iterator = arg.properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const attr = _step.value; | ||
const { key } = attr; | ||
if (attr.computed) { | ||
throw file.buildCodeFrameError(attr, "Computed properties aren't allowed."); | ||
} | ||
const name = t.isNumericLiteral(key) ? `=${key.value}` : key.name || key.value; | ||
const key = attr.key; | ||
if (name === 'value') { | ||
const exp = attr.value; | ||
const name = t.isNumericLiteral(key) ? `=${key.value}` : key.name || key.value; | ||
if (!t.isIdentifier(exp)) { | ||
throw file.buildCodeFrameError(node.callee, 'Value must be a variable.'); | ||
} | ||
if (name === 'value') { | ||
const exp = attr.value; | ||
variable = exp.name; | ||
props.params[variable] = t.objectProperty(exp, exp); | ||
} else if (choicesType !== 'select' && name === 'offset') { | ||
if (!t.isNumericLiteral(attr.value) && !t.isStringLiteral(attr.value)) { | ||
throw file.buildCodeFrameError(node.callee, 'Offset argument cannot be a variable.'); | ||
} | ||
offset = ` offset:${attr.value.value}`; | ||
} else { | ||
let value = ''; | ||
if (!t.isIdentifier(exp)) { | ||
throw file.buildCodeFrameError(node.callee, 'Value must be a variable.'); | ||
} | ||
variable = exp.name; | ||
props.params[variable] = t.objectProperty(exp, exp); | ||
} else if (choicesType !== 'select' && name === 'offset') { | ||
if (!t.isNumericLiteral(attr.value) && !t.isStringLiteral(attr.value)) { | ||
throw file.buildCodeFrameError(node.callee, 'Offset argument cannot be a variable.'); | ||
} | ||
offset = ` offset:${attr.value.value}`; | ||
if (t.isTemplateLiteral(attr.value)) { | ||
props = processTemplateLiteral(attr.value, file, Object.assign({}, props, { text: '' })); | ||
value = props.text; | ||
} else if (t.isCallExpression(attr.value)) { | ||
props = processMethod(attr.value, file, Object.assign({}, props, { text: '' })); | ||
value = props.text; | ||
} else { | ||
let value = ''; | ||
if (t.isTemplateLiteral(attr.value)) { | ||
props = processTemplateLiteral(attr.value, file, Object.assign({}, props, { text: '' })); | ||
value = props.text; | ||
} else if (t.isCallExpression(attr.value)) { | ||
props = processMethod(attr.value, file, Object.assign({}, props, { text: '' })); | ||
value = props.text; | ||
} else { | ||
value = attr.value.value; | ||
} | ||
choices[name] = value; | ||
value = attr.value.value; | ||
} | ||
choices[name] = value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
@@ -111,2 +92,34 @@ | ||
props.text = `{${variable}, ${choicesType},${offset} ${argument}}`; | ||
} else if (isFormatMethod(node.callee)) { | ||
const variable = node.arguments[0]; | ||
if (!variable || !t.isIdentifier(variable)) { | ||
throw file.buildCodeFrameError(node.callee, 'The first argument of format function must be a variable.'); | ||
} | ||
const type = node.callee.property.name; | ||
const parts = [variable.name, type]; | ||
let format = ''; | ||
const formatArg = node.arguments[1]; | ||
if (!formatArg) {} else if (t.isStringLiteral(formatArg)) { | ||
format = formatArg.value; | ||
} else if (t.isIdentifier(formatArg) || t.isObjectExpression(formatArg)) { | ||
if (t.isIdentifier(formatArg)) { | ||
format = formatArg.name; | ||
} else { | ||
const formatName = new RegExp(`^${type}\\d+$`); | ||
const existing = Object.keys(props.formats).filter(name => formatName.test(name)); | ||
format = `${type}${existing.length || 0}`; | ||
} | ||
props.formats[format] = t.objectProperty(t.identifier(format), formatArg); | ||
} else { | ||
throw file.buildCodeFrameError(formatArg, 'Format can be either string for buil-in formats, variable or object for custom defined formats.'); | ||
} | ||
if (format) parts.push(format); | ||
props.params[variable.name] = t.objectProperty(variable, variable); | ||
props.text += `${parts.join(',')}`; | ||
} | ||
@@ -130,6 +143,3 @@ | ||
} else if (t.isCallExpression(item)) { | ||
var _processMethod = processMethod(item, file, _extends({}, props, { text: '' })); | ||
const text = _processMethod.text; | ||
const { text } = processMethod(item, file, _extends({}, props, { text: '' })); | ||
props.text += `{${text}}`; | ||
@@ -149,3 +159,4 @@ } else { | ||
text: '', | ||
params: {} | ||
params: {}, | ||
formats: {} | ||
}, true); | ||
@@ -155,3 +166,3 @@ | ||
const tArgs = [t.objectProperty(t.identifier('id'), t.StringLiteral(props.text))]; | ||
const tArgs = [t.objectProperty(t.identifier('id'), t.StringLiteral(props.text.trim()))]; | ||
@@ -163,2 +174,7 @@ const paramsList = Object.values(props.params); | ||
const formatsList = Object.values(props.formats); | ||
if (formatsList.length) { | ||
tArgs.push(t.objectProperty(t.identifier('formats'), t.objectExpression(formatsList))); | ||
} | ||
const exp = t.callExpression(t.memberExpression(t.identifier('i18n'), t.identifier('t')), [t.objectExpression(tArgs)]); | ||
@@ -177,6 +193,2 @@ exp.loc = path.node.loc; | ||
require('core-js/modules/es6.map'); | ||
require('core-js/modules/es6.set'); | ||
require('core-js/modules/es6.weak-map'); | ||
@@ -212,20 +224,8 @@ | ||
require('core-js/modules/es6.promise'); | ||
require('core-js/modules/es6.symbol'); | ||
require('core-js/modules/es6.function.name'); | ||
require('core-js/modules/es6.regexp.flags'); | ||
require('core-js/modules/es6.array.from'); | ||
require('core-js/modules/es7.array.includes'); | ||
require('core-js/modules/es7.object.values'); | ||
require('core-js/modules/es7.object.entries'); | ||
require('core-js/modules/es7.object.get-own-property-descriptors'); | ||
require('core-js/modules/es7.string.pad-start'); | ||
@@ -232,0 +232,0 @@ |
{ | ||
"name": "babel-plugin-lingui-transform-js", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Transform lingui-i18n methods to ICU message format", | ||
@@ -22,4 +22,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.22.2", | ||
"babel-polyfill": "^6.22.0", | ||
"babel-cli": "^6.23.0", | ||
"babel-polyfill": "^6.23.0", | ||
"babel-preset-env": "^1.1.8", | ||
@@ -26,0 +26,0 @@ "rimraf": "^2.5.4" |
@@ -81,2 +81,32 @@ # react-plugin-lingui-transform-js | ||
Number/date formats: | ||
```js | ||
i18n.t`The answer is ${i18n.number(answer)}` | ||
// becomes | ||
i18n.t({ | ||
id: "The answer is {answer, number}", | ||
params: { answer } | ||
}) | ||
``` | ||
Custom formats: | ||
```js | ||
const currency = { | ||
style: 'currency', | ||
currency: 'EUR' | ||
} | ||
i18n.t`The price is ${i18n.number(amount, currency)}` | ||
// becomes | ||
i18n.t({ | ||
id: "The price is {amount, number, currency}", | ||
params: { answer }, | ||
formats: { currency } | ||
}) | ||
``` | ||
### Combination of any above | ||
@@ -83,0 +113,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11464
167
132