Socket
Socket
Sign inDemoInstall

@lingui/macro

Package Overview
Dependencies
Maintainers
1
Versions
115
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lingui/macro - npm Package Compare versions

Comparing version 3.2.3 to 3.3.0

27

index.d.ts

@@ -10,4 +10,4 @@ import type { ReactElement, ComponentType, ReactNode } from "react"

export type UnderscoreDigit<T = string> = { [digit: string]: T }
export type ChoiceOptions<T = string> = {
[digit: number]: T
offset?: number

@@ -19,3 +19,4 @@ zero?: T

other?: T
}
} & UnderscoreDigit<T>
export function plural(arg: number | string, options: ChoiceOptions): string

@@ -44,6 +45,22 @@ export function selectOrdinal(

/**
* The types should be changed after this PR is merged
* https://github.com/Microsoft/TypeScript/pull/26797
*
* then we should be able to specify that key of values is same type as value.
* We would be able to remove separate type Values = {...} definition
* eg.
* type SelectProps<Values> = {
* value?: Values
* [key: Values]: string
* }
*
*/
type Values = { [key: string]: string }
export type SelectProps = {
value?: string
other?: ReactNode
} & TransProps
value: string
other: ReactNode
} & TransProps &
Values

@@ -50,0 +67,0 @@ export const Trans: ComponentType<TransProps>

@@ -24,7 +24,30 @@ "use strict";

var _config$runtimeConfig = (0, _slicedToArray2.default)(config.runtimeConfigModule, 2),
i18nImportModule = _config$runtimeConfig[0],
_config$runtimeConfig2 = _config$runtimeConfig[1],
i18nImportName = _config$runtimeConfig2 === void 0 ? "i18n" : _config$runtimeConfig2;
var getSymbolSource = function getSymbolSource(name) {
if (Array.isArray(config.runtimeConfigModule)) {
if (name === "i18n") {
return config.runtimeConfigModule;
} else {
return ["@lingui/react", name];
}
} else {
if (Object.prototype.hasOwnProperty.call(config.runtimeConfigModule, name)) {
return config.runtimeConfigModule[name];
} else {
return ["@lingui/react", name];
}
}
};
var _getSymbolSource = getSymbolSource("i18n"),
_getSymbolSource2 = (0, _slicedToArray2.default)(_getSymbolSource, 2),
i18nImportModule = _getSymbolSource2[0],
_getSymbolSource2$ = _getSymbolSource2[1],
i18nImportName = _getSymbolSource2$ === void 0 ? "i18n" : _getSymbolSource2$;
var _getSymbolSource3 = getSymbolSource("Trans"),
_getSymbolSource4 = (0, _slicedToArray2.default)(_getSymbolSource3, 2),
TransImportModule = _getSymbolSource4[0],
_getSymbolSource4$ = _getSymbolSource4[1],
TransImportName = _getSymbolSource4$ === void 0 ? "Trans" : _getSymbolSource4$;
function macro(_ref) {

@@ -73,3 +96,3 @@ var references = _ref.references,

if (jsxNodes.length) {
addImport(babel, state, "@lingui/react", "Trans");
addImport(babel, state, TransImportModule, TransImportName);
}

@@ -76,0 +99,0 @@

462

macroJs.js

@@ -14,2 +14,4 @@ "use strict";

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

@@ -37,2 +39,3 @@

var keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g;
var removeExtraScapedLiterals = /(?:\\(.))/;

@@ -43,280 +46,297 @@ function normalizeWhitespace(text) {

var MacroJs = // Babel Types
// Identifier of i18n object
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
function MacroJs(_ref, _ref2) {
var _this = this;
var MacroJs = /*#__PURE__*/function () {
// Babel Types
// Identifier of i18n object
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
function MacroJs(_ref, _ref2) {
var _this = this;
var types = _ref.types;
var i18nImportName = _ref2.i18nImportName;
(0, _classCallCheck2.default)(this, MacroJs);
(0, _defineProperty2.default)(this, "types", void 0);
(0, _defineProperty2.default)(this, "i18nImportName", void 0);
(0, _defineProperty2.default)(this, "_expressionIndex", void 0);
(0, _defineProperty2.default)(this, "replacePathWithMessage", function (path, _ref3) {
var id = _ref3.id,
message = _ref3.message,
values = _ref3.values,
comment = _ref3.comment;
var args = [];
var options = [];
var messageNode = isString(message) ? _this.types.stringLiteral(message) : message;
var types = _ref.types;
var i18nImportName = _ref2.i18nImportName;
(0, _classCallCheck2.default)(this, MacroJs);
(0, _defineProperty2.default)(this, "types", void 0);
(0, _defineProperty2.default)(this, "i18nImportName", void 0);
(0, _defineProperty2.default)(this, "_expressionIndex", void 0);
(0, _defineProperty2.default)(this, "replacePathWithMessage", function (path, _ref3) {
var id = _ref3.id,
message = _ref3.message,
values = _ref3.values,
comment = _ref3.comment;
var args = [];
var options = [];
var messageNode = isString(message) ? _this.types.stringLiteral(message) : message;
if (id) {
args.push(_this.types.stringLiteral(id));
if (id) {
args.push(_this.types.stringLiteral(id));
if (process.env.NODE_ENV !== "production") {
options.push(_this.types.objectProperty(_this.types.identifier(_constants.MESSAGE), messageNode));
if (process.env.NODE_ENV !== "production") {
options.push(_this.types.objectProperty(_this.types.identifier(_constants.MESSAGE), messageNode));
}
} else {
args.push(messageNode);
}
} else {
args.push(messageNode);
}
if (comment) {
options.push(_this.types.objectProperty(_this.types.identifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
}
if (comment) {
options.push(_this.types.objectProperty(_this.types.identifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
}
if (Object.keys(values).length || options.length) {
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
});
args.push(_this.types.objectExpression(valuesObject));
}
if (Object.keys(values).length || options.length) {
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
});
args.push(_this.types.objectExpression(valuesObject));
}
if (options.length) {
args.push(_this.types.objectExpression(options));
}
if (options.length) {
args.push(_this.types.objectExpression(options));
}
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), args); // preserve line number
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), args); // preserve line number
newNode.loc = path.node.loc;
path.addComment("leading", _constants.EXTRACT_MARK); // @ts-ignore
newNode.loc = path.node.loc;
path.addComment("leading", _constants.EXTRACT_MARK); // @ts-ignore
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "replacePath", function (path) {
// reset the expression counter
_this._expressionIndex = (0, _utils.makeCounter)();
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "replacePath", function (path) {
// reset the expression counter
_this._expressionIndex = (0, _utils.makeCounter)();
if (_this.isDefineMessage(path.node)) {
_this.replaceDefineMessage(path);
if (_this.isDefineMessage(path.node)) {
_this.replaceDefineMessage(path);
return;
}
return;
}
if (_this.types.isCallExpression(path.node) && _this.isIdentifier(path.node.callee, "t")) {
_this.replaceTAsFunction(path);
if (_this.types.isCallExpression(path.node) && _this.isIdentifier(path.node.callee, "t")) {
_this.replaceTAsFunction(path);
return;
}
return;
}
var tokens = _this.tokenizeNode(path.node);
var tokens = _this.tokenizeNode(path.node);
var messageFormat = new _icu.default();
var messageFormat = new _icu.default();
var _messageFormat$fromTo = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo.message,
values = _messageFormat$fromTo.values,
id = _messageFormat$fromTo.id,
comment = _messageFormat$fromTo.comment;
var _messageFormat$fromTo = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo.message,
values = _messageFormat$fromTo.values,
id = _messageFormat$fromTo.id,
comment = _messageFormat$fromTo.comment;
var message = normalizeWhitespace(messageRaw);
var message = normalizeWhitespace(messageRaw);
_this.replacePathWithMessage(path, {
id: id,
message: message,
values: values,
comment: comment
_this.replacePathWithMessage(path, {
id: id,
message: message,
values: values,
comment: comment
});
});
});
(0, _defineProperty2.default)(this, "replaceDefineMessage", function (path) {
// reset the expression counter
_this._expressionIndex = (0, _utils.makeCounter)();
(0, _defineProperty2.default)(this, "replaceDefineMessage", function (path) {
// reset the expression counter
_this._expressionIndex = (0, _utils.makeCounter)();
var descriptor = _this.processDescriptor(path.node.arguments[0]);
var descriptor = _this.processDescriptor(path.node.arguments[0]);
path.replaceWith(descriptor);
});
(0, _defineProperty2.default)(this, "replaceTAsFunction", function (path) {
var descriptor = _this.processDescriptor(path.node.arguments[0]);
path.replaceWith(descriptor);
});
(0, _defineProperty2.default)(this, "replaceTAsFunction", function (path) {
var descriptor = _this.processDescriptor(path.node.arguments[0]);
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), [descriptor]);
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), [descriptor]);
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "processDescriptor", function (descriptor) {
_this.types.addComment(descriptor, "leading", _constants.EXTRACT_MARK);
var messageIndex = descriptor.properties.findIndex(function (property) {
return property.key.name === _constants.MESSAGE;
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "processDescriptor", function (descriptor) {
_this.types.addComment(descriptor, "leading", _constants.EXTRACT_MARK);
if (messageIndex === -1) {
return descriptor;
} // if there's `message` property, replace macros with formatted message
var messageIndex = descriptor.properties.findIndex(function (property) {
return property.key.name === _constants.MESSAGE;
});
if (messageIndex === -1) {
return descriptor;
} // if there's `message` property, replace macros with formatted message
var node = descriptor.properties[messageIndex]; // Inside message descriptor the `t` macro in `message` prop is optional.
// Template strings are always processed as if they were wrapped by `t`.
var tokens = _this.types.isTemplateLiteral(node.value) ? _this.tokenizeTemplateLiteral(node.value) : _this.tokenizeNode(node.value, true);
var messageNode = node.value;
var node = descriptor.properties[messageIndex]; // Inside message descriptor the `t` macro in `message` prop is optional.
// Template strings are always processed as if they were wrapped by `t`.
if (tokens != null) {
var messageFormat = new _icu.default();
var tokens = _this.types.isTemplateLiteral(node.value) ? _this.tokenizeTemplateLiteral(node.value) : _this.tokenizeNode(node.value, true);
var messageNode = node.value;
var _messageFormat$fromTo2 = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo2.message,
values = _messageFormat$fromTo2.values;
if (tokens != null) {
var messageFormat = new _icu.default();
var message = normalizeWhitespace(messageRaw);
messageNode = _this.types.stringLiteral(message);
var _messageFormat$fromTo2 = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo2.message,
values = _messageFormat$fromTo2.values;
_this.addValues(descriptor.properties, values);
} // Don't override custom ID
var message = normalizeWhitespace(messageRaw);
messageNode = _this.types.stringLiteral(message);
_this.addValues(descriptor.properties, values);
} // Don't override custom ID
var hasId = descriptor.properties.findIndex(function (property) {
return property.key.name === _constants.ID;
}) !== -1;
descriptor.properties[messageIndex] = _this.types.objectProperty(_this.types.identifier(hasId ? _constants.MESSAGE : _constants.ID), messageNode);
return descriptor;
});
(0, _defineProperty2.default)(this, "addValues", function (obj, values) {
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
var hasId = descriptor.properties.findIndex(function (property) {
return property.key.name === _constants.ID;
}) !== -1;
descriptor.properties[messageIndex] = _this.types.objectProperty(_this.types.identifier(hasId ? _constants.MESSAGE : _constants.ID), messageNode);
return descriptor;
});
if (!valuesObject.length) return;
obj.push(_this.types.objectProperty(_this.types.identifier("values"), _this.types.objectExpression(valuesObject)));
});
(0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
var ignoreExpression = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
(0, _defineProperty2.default)(this, "addValues", function (obj, values) {
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
});
if (!valuesObject.length) return;
obj.push(_this.types.objectProperty(_this.types.identifier("values"), _this.types.objectExpression(valuesObject)));
});
(0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
var ignoreExpression = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (_this.isI18nMethod(node)) {
// t
return _this.tokenizeTemplateLiteral(node);
} else if (_this.isChoiceMethod(node)) {
// plural, select and selectOrdinal
return [_this.tokenizeChoiceComponent(node)]; // } else if (isFormatMethod(node.callee)) {
// // date, number
// return transformFormatMethod(node, file, props, root)
} else if (!ignoreExpression) {
return _this.tokenizeExpression(node);
}
});
(0, _defineProperty2.default)(this, "tokenizeTemplateLiteral", function (node) {
var tokenize = R.pipe(R.evolve({
quasis: R.map(function (text) {
// Don't output tokens without text.
var value = text.value.raw;
if (value === "") return null;
return {
type: "text",
value: value
};
}),
expressions: R.map(function (exp) {
return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
})
}), function (exp) {
return (0, _utils.zip)(exp.quasis, exp.expressions);
}, // @ts-ignore
R.flatten, R.filter(Boolean));
return tokenize(_this.types.isTaggedTemplateExpression(node) ? node.quasi : node);
});
(0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
var format = node.callee.name.toLowerCase();
var token = _objectSpread(_objectSpread({}, _this.tokenizeExpression(node.arguments[0])), {}, {
format: format,
options: {
offset: undefined
if (_this.isI18nMethod(node)) {
// t
return _this.tokenizeTemplateLiteral(node);
} else if (_this.isChoiceMethod(node)) {
// plural, select and selectOrdinal
return [_this.tokenizeChoiceComponent(node)]; // } else if (isFormatMethod(node.callee)) {
// // date, number
// return transformFormatMethod(node, file, props, root)
} else if (!ignoreExpression) {
return _this.tokenizeExpression(node);
}
});
(0, _defineProperty2.default)(this, "tokenizeTemplateLiteral", function (node) {
var tokenize = R.pipe(R.evolve({
quasis: R.map(function (text) {
// Don't output tokens without text.
var value = text.value.raw;
if (value === "") return null;
return {
type: "text",
value: _this.clearBackslashes(value)
};
}),
expressions: R.map(function (exp) {
return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
})
}), function (exp) {
return (0, _utils.zip)(exp.quasis, exp.expressions);
}, R.flatten, R.filter(Boolean));
return tokenize(_this.types.isTaggedTemplateExpression(node) ? node.quasi : node);
});
(0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
var format = node.callee.name.toLowerCase();
var props = node.arguments[1].properties;
var token = _objectSpread(_objectSpread({}, _this.tokenizeExpression(node.arguments[0])), {}, {
format: format,
options: {
offset: undefined
}
});
var _iterator = _createForOfIteratorHelper(props),
_step;
var props = node.arguments[1].properties;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var attr = _step.value;
var key = attr.key; // name is either:
// NumericLiteral => convert to `={number}`
// StringLiteral => key.value
// Literal => key.name
var _iterator = _createForOfIteratorHelper(props),
_step;
var name = _this.types.isNumericLiteral(key) ? "=".concat(key.value) : key.name || key.value;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var attr = _step.value;
var key = attr.key; // name is either:
// NumericLiteral => convert to `={number}`
// StringLiteral => key.value
// Literal => key.name
if (format !== "select" && name === "offset") {
token.options.offset = attr.value.value;
} else {
var value = void 0;
var name = _this.types.isNumericLiteral(key) ? "=".concat(key.value) : key.name || key.value;
if (_this.types.isTemplateLiteral(attr.value)) {
value = _this.tokenizeTemplateLiteral(attr.value);
} else if (_this.types.isCallExpression(attr.value)) {
value = _this.tokenizeNode(attr.value);
if (format !== "select" && name === "offset") {
token.options.offset = attr.value.value;
} else {
value = attr.value.value;
var value = void 0;
if (_this.types.isTemplateLiteral(attr.value)) {
value = _this.tokenizeTemplateLiteral(attr.value);
} else if (_this.types.isCallExpression(attr.value)) {
value = _this.tokenizeNode(attr.value);
} else {
value = attr.value.value;
}
token.options[name] = value;
}
token.options[name] = value;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return token;
});
(0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
if (_this.isArg(node)) {
return token;
});
(0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
if (_this.isArg(node)) {
return {
type: "arg",
name: node.arguments[0].value
};
}
return {
type: "arg",
name: node.arguments[0].value
name: _this.expressionToArgument(node),
value: node
};
}
});
(0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
if (_this.types.isIdentifier(exp)) {
return exp.name;
} else if (_this.types.isStringLiteral(exp)) {
return exp.value;
} else {
return _this._expressionIndex();
}
});
(0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
return _this.types.isIdentifier(node, {
name: name
});
});
(0, _defineProperty2.default)(this, "isDefineMessage", function (node) {
return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "defineMessage");
});
(0, _defineProperty2.default)(this, "isArg", function (node) {
return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "arg");
});
(0, _defineProperty2.default)(this, "isI18nMethod", function (node) {
return _this.isIdentifier(node.tag, "t") || _this.types.isCallExpression(node.tag) && _this.isIdentifier(node.tag.callee, "t");
});
(0, _defineProperty2.default)(this, "isChoiceMethod", function (node) {
return _this.types.isCallExpression(node) && (_this.isIdentifier(node.callee, "plural") || _this.isIdentifier(node.callee, "select") || _this.isIdentifier(node.callee, "selectOrdinal"));
});
this.types = types;
this.i18nImportName = i18nImportName;
this._expressionIndex = (0, _utils.makeCounter)();
}
return {
type: "arg",
name: _this.expressionToArgument(node),
value: node
};
});
(0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
if (_this.types.isIdentifier(exp)) {
return exp.name;
} else if (_this.types.isStringLiteral(exp)) {
return exp.value;
} else {
return _this._expressionIndex();
(0, _createClass2.default)(MacroJs, [{
key: "clearBackslashes",
/**
* We clean '//\` ' to just '`'
*/
value: function clearBackslashes(value) {
return value.replace(removeExtraScapedLiterals, "`");
}
});
(0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
return _this.types.isIdentifier(node, {
name: name
});
});
(0, _defineProperty2.default)(this, "isDefineMessage", function (node) {
return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "defineMessage");
});
(0, _defineProperty2.default)(this, "isArg", function (node) {
return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "arg");
});
(0, _defineProperty2.default)(this, "isI18nMethod", function (node) {
return _this.isIdentifier(node.tag, "t") || _this.types.isCallExpression(node.tag) && _this.isIdentifier(node.tag.callee, "t");
});
(0, _defineProperty2.default)(this, "isChoiceMethod", function (node) {
return _this.types.isCallExpression(node) && (_this.isIdentifier(node.callee, "plural") || _this.isIdentifier(node.callee, "select") || _this.isIdentifier(node.callee, "selectOrdinal"));
});
this.types = types;
this.i18nImportName = i18nImportName;
this._expressionIndex = (0, _utils.makeCounter)();
};
/**
* Custom matchers
*/
}]);
return MacroJs;
}();
exports.default = MacroJs;

@@ -323,0 +343,0 @@

@@ -16,2 +16,4 @@ "use strict";

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

@@ -34,2 +36,3 @@

var pluralRuleRe = /(_[\d\w]+|zero|one|two|few|many|other)/;
var removeExtraScapedLiterals = /(?:\\(.))/;

@@ -60,267 +63,285 @@ var jsx2icuExactChoice = function jsx2icuExactChoice(value) {

var MacroJSX = function MacroJSX(_ref) {
var _this = this;
var MacroJSX = /*#__PURE__*/function () {
function MacroJSX(_ref) {
var _this = this;
var types = _ref.types;
(0, _classCallCheck2.default)(this, MacroJSX);
(0, _defineProperty2.default)(this, "types", void 0);
(0, _defineProperty2.default)(this, "expressionIndex", void 0);
(0, _defineProperty2.default)(this, "elementIndex", void 0);
(0, _defineProperty2.default)(this, "replacePath", function (path) {
var tokens = _this.tokenizeNode(path.node);
var types = _ref.types;
(0, _classCallCheck2.default)(this, MacroJSX);
(0, _defineProperty2.default)(this, "types", void 0);
(0, _defineProperty2.default)(this, "expressionIndex", void 0);
(0, _defineProperty2.default)(this, "elementIndex", void 0);
(0, _defineProperty2.default)(this, "replacePath", function (path) {
var tokens = _this.tokenizeNode(path.node);
var messageFormat = new _icu.default();
var messageFormat = new _icu.default();
var _messageFormat$fromTo = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo.message,
values = _messageFormat$fromTo.values,
jsxElements = _messageFormat$fromTo.jsxElements;
var _messageFormat$fromTo = messageFormat.fromTokens(tokens),
messageRaw = _messageFormat$fromTo.message,
values = _messageFormat$fromTo.values,
jsxElements = _messageFormat$fromTo.jsxElements;
var message = normalizeWhitespace(messageRaw);
var message = normalizeWhitespace(messageRaw);
var _this$stripMacroAttri = _this.stripMacroAttributes(path.node),
attributes = _this$stripMacroAttri.attributes,
id = _this$stripMacroAttri.id,
comment = _this$stripMacroAttri.comment;
var _this$stripMacroAttri = _this.stripMacroAttributes(path.node),
attributes = _this$stripMacroAttri.attributes,
id = _this$stripMacroAttri.id,
comment = _this$stripMacroAttri.comment;
if (!id && !message) {
return;
} else if (id && id !== message) {
// If `id` prop already exists and generated ID is different,
// add it as a `default` prop
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.ID), _this.types.stringLiteral(id)));
if (!id && !message) {
return;
} else if (id && id !== message) {
// If `id` prop already exists and generated ID is different,
// add it as a `default` prop
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.ID), _this.types.stringLiteral(id)));
if (process.env.NODE_ENV !== "production") {
if (message) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.MESSAGE), _this.types.stringLiteral(message)));
if (process.env.NODE_ENV !== "production") {
if (message) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.MESSAGE), _this.types.stringLiteral(message)));
}
}
} else {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.ID), _this.types.stringLiteral(message)));
}
} else {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.ID), _this.types.stringLiteral(message)));
}
if (process.env.NODE_ENV !== "production") {
if (comment) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
}
} // Parameters for variable substitution
if (process.env.NODE_ENV !== "production") {
if (comment) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
}
} // Parameters for variable substitution
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
});
var valuesObject = Object.keys(values).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), values[key]);
});
if (valuesObject.length) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("values"), _this.types.jsxExpressionContainer(_this.types.objectExpression(valuesObject))));
} // Inline elements
if (valuesObject.length) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("values"), _this.types.jsxExpressionContainer(_this.types.objectExpression(valuesObject))));
} // Inline elements
if (Object.keys(jsxElements).length) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("components"), _this.types.jsxExpressionContainer(_this.types.objectExpression(Object.keys(jsxElements).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), jsxElements[key]);
})))));
}
if (Object.keys(jsxElements).length) {
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("components"), _this.types.jsxExpressionContainer(_this.types.objectExpression(Object.keys(jsxElements).map(function (key) {
return _this.types.objectProperty(_this.types.identifier(key), jsxElements[key]);
})))));
}
var newNode = _this.types.jsxElement(_this.types.jsxOpeningElement(_this.types.jsxIdentifier("Trans"), attributes,
/*selfClosing*/
true),
/*closingElement*/
null,
/*children*/
[],
/*selfClosing*/
true);
var newNode = _this.types.jsxElement(_this.types.jsxOpeningElement(_this.types.jsxIdentifier("Trans"), attributes,
/*selfClosing*/
true),
/*closingElement*/
null,
/*children*/
[],
/*selfClosing*/
true);
newNode.loc = path.node.loc; // @ts-ignore
newNode.loc = path.node.loc; // @ts-ignore
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "stripMacroAttributes", function (node) {
var attributes = node.openingElement.attributes;
path.replaceWith(newNode);
});
(0, _defineProperty2.default)(this, "stripMacroAttributes", function (node) {
var attributes = node.openingElement.attributes;
var attrName = function attrName(names) {
var exclude = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var namesRe = new RegExp("^(" + names.join("|") + ")$");
return function (attr) {
return exclude ? !namesRe.test(attr.name.name) : namesRe.test(attr.name.name);
var attrName = function attrName(names) {
var exclude = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var namesRe = new RegExp("^(" + names.join("|") + ")$");
return function (attr) {
return exclude ? !namesRe.test(attr.name.name) : namesRe.test(attr.name.name);
};
};
};
var id = attributes.filter(attrName([_constants.ID]))[0];
var message = attributes.filter(attrName([_constants.MESSAGE]))[0];
var comment = attributes.filter(attrName([_constants.COMMENT]))[0];
var reserved = [_constants.ID, _constants.MESSAGE, _constants.COMMENT];
var id = attributes.filter(attrName([_constants.ID]))[0];
var message = attributes.filter(attrName([_constants.MESSAGE]))[0];
var comment = attributes.filter(attrName([_constants.COMMENT]))[0];
var reserved = [_constants.ID, _constants.MESSAGE, _constants.COMMENT];
if (_this.isI18nComponent(node)) {// no reserved prop names
} else if (_this.isChoiceComponent(node)) {
reserved = [].concat((0, _toConsumableArray2.default)(reserved), ["_\\w+", "_\\d+", "zero", "one", "two", "few", "many", "other", "value", "offset"]);
}
if (_this.isI18nComponent(node)) {// no reserved prop names
} else if (_this.isChoiceComponent(node)) {
reserved = [].concat((0, _toConsumableArray2.default)(reserved), ["_\\w+", "_\\d+", "zero", "one", "two", "few", "many", "other", "value", "offset"]);
}
return {
id: maybeNodeValue(id),
message: maybeNodeValue(message),
comment: maybeNodeValue(comment),
attributes: attributes.filter(attrName(reserved, true))
};
});
(0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
if (_this.isI18nComponent(node)) {
// t
return _this.tokenizeTrans(node);
} else if (_this.isChoiceComponent(node)) {
// plural, select and selectOrdinal
return _this.tokenizeChoiceComponent(node);
} else if (_this.types.isJSXElement(node)) {
return _this.tokenizeElement(node);
} else {
return _this.tokenizeExpression(node);
}
});
(0, _defineProperty2.default)(this, "tokenizeTrans", function (node) {
return R.flatten(node.children.map(function (child) {
return _this.tokenizeChildren(child);
}).filter(Boolean));
});
(0, _defineProperty2.default)(this, "tokenizeChildren", function (node) {
if (_this.types.isJSXExpressionContainer(node)) {
var exp = node.expression;
return {
id: maybeNodeValue(id),
message: maybeNodeValue(message),
comment: maybeNodeValue(comment),
attributes: attributes.filter(attrName(reserved, true))
};
});
(0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
if (_this.isI18nComponent(node)) {
// t
return _this.tokenizeTrans(node);
} else if (_this.isChoiceComponent(node)) {
// plural, select and selectOrdinal
return _this.tokenizeChoiceComponent(node);
} else if (_this.types.isJSXElement(node)) {
return _this.tokenizeElement(node);
} else {
return _this.tokenizeExpression(node);
}
});
(0, _defineProperty2.default)(this, "tokenizeTrans", function (node) {
return R.flatten(node.children.map(function (child) {
return _this.tokenizeChildren(child);
}).filter(Boolean));
});
(0, _defineProperty2.default)(this, "tokenizeChildren", function (node) {
if (_this.types.isJSXExpressionContainer(node)) {
var exp = node.expression;
if (_this.types.isStringLiteral(exp)) {
// Escape forced newlines to keep them in message.
return {
type: "text",
value: exp.value.replace(/\n/g, "\\n")
};
} else if (_this.types.isTemplateLiteral(exp)) {
var tokenize = R.pipe( // Don't output tokens without text.
R.evolve({
quasis: R.map(function (text) {
// Don't output tokens without text.
var value = text.value.raw;
if (value === "") return null;
return _this.tokenizeText(value);
}),
expressions: R.map(function (exp) {
return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
})
}), function (exp) {
return (0, _utils.zip)(exp.quasis, exp.expressions);
}, // @ts-ignore
R.flatten, R.filter(Boolean));
return tokenize(exp);
} else if (_this.types.isJSXElement(exp)) {
return _this.tokenizeNode(exp);
if (_this.types.isStringLiteral(exp)) {
// Escape forced newlines to keep them in message.
return {
type: "text",
value: exp.value.replace(/\n/g, "\\n")
};
} else if (_this.types.isTemplateLiteral(exp)) {
var tokenize = R.pipe( // Don't output tokens without text.
R.evolve({
quasis: R.map(function (text) {
// Don't output tokens without text.
var value = text.value.raw;
if (value === "") return null;
return _this.tokenizeText(_this.clearBackslashes(value));
}),
expressions: R.map(function (exp) {
return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
})
}), function (exp) {
return (0, _utils.zip)(exp.quasis, exp.expressions);
}, // @ts-ignore
R.flatten, R.filter(Boolean));
return tokenize(exp);
} else if (_this.types.isJSXElement(exp)) {
return _this.tokenizeNode(exp);
} else {
return _this.tokenizeExpression(exp);
}
} else if (_this.types.isJSXElement(node)) {
return _this.tokenizeNode(node);
} else if (_this.types.isJSXSpreadChild(node)) {// just do nothing
} else if (_this.types.isJSXText(node)) {
return _this.tokenizeText(node.value);
} else {
return _this.tokenizeExpression(exp);
return _this.tokenizeText(node.value);
}
} else if (_this.types.isJSXElement(node)) {
return _this.tokenizeNode(node);
} else if (_this.types.isJSXSpreadChild(node)) {// just do nothing
} else if (_this.types.isJSXText(node)) {
return _this.tokenizeText(node.value);
} else {
return _this.tokenizeText(node.value);
}
});
(0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
var element = node.openingElement;
var format = element.name.name.toLowerCase();
var props = element.attributes;
var token = {
type: "arg",
format: format,
name: null,
value: undefined,
options: {
offset: undefined
}
};
});
(0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
var element = node.openingElement;
var format = element.name.name.toLowerCase();
var props = element.attributes;
var token = {
type: "arg",
format: format,
name: null,
value: undefined,
options: {
offset: undefined
}
};
var _iterator = _createForOfIteratorHelper(props),
_step;
var _iterator = _createForOfIteratorHelper(props),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var attr = _step.value;
var name = attr.name.name;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var attr = _step.value;
var name = attr.name.name;
if (name === "value") {
var exp = _this.types.isLiteral(attr.value) ? attr.value : attr.value.expression;
token.name = _this.expressionToArgument(exp);
token.value = exp;
} else if (format !== "select" && name === "offset") {
// offset is static parameter, so it must be either string or number
token.options.offset = _this.types.isStringLiteral(attr.value) ? attr.value.value : attr.value.expression.value;
} else if (pluralRuleRe.test(name)) {
var value = void 0;
if (name === "value") {
var exp = _this.types.isLiteral(attr.value) ? attr.value : attr.value.expression;
token.name = _this.expressionToArgument(exp);
token.value = exp;
} else if (format !== "select" && name === "offset") {
// offset is static parameter, so it must be either string or number
token.options.offset = _this.types.isStringLiteral(attr.value) ? attr.value.value : attr.value.expression.value;
} else if (pluralRuleRe.test(name)) {
var value = void 0;
if (_this.types.isStringLiteral(attr.value)) {
value = attr.value.extra.raw.replace(/(["'])(.*)\1/, "$2");
} else {
value = _this.tokenizeChildren(attr.value);
if (_this.types.isStringLiteral(attr.value)) {
value = attr.value.extra.raw.replace(/(["'])(.*)\1/, "$2");
} else {
value = _this.tokenizeChildren(attr.value);
}
token.options[jsx2icuExactChoice(name)] = value;
}
token.options[jsx2icuExactChoice(name)] = value;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return token;
});
(0, _defineProperty2.default)(this, "tokenizeElement", function (node) {
// !!! Important: Calculate element index before traversing children.
// That way outside elements are numbered before inner elements. (...and it looks pretty).
var name = _this.elementIndex();
return token;
});
(0, _defineProperty2.default)(this, "tokenizeElement", function (node) {
// !!! Important: Calculate element index before traversing children.
// That way outside elements are numbered before inner elements. (...and it looks pretty).
var name = _this.elementIndex();
var children = node.children.map(function (child) {
return _this.tokenizeChildren(child);
}).filter(Boolean);
node.children = [];
node.openingElement.selfClosing = true;
return {
type: "element",
name: name,
value: node,
children: children
};
});
(0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
return {
type: "arg",
name: _this.expressionToArgument(node),
value: node
};
});
(0, _defineProperty2.default)(this, "tokenizeText", function (value) {
return {
type: "text",
value: value
};
});
(0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
return _this.types.isIdentifier(exp) ? exp.name : _this.expressionIndex();
});
(0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
return _this.types.isIdentifier(node, {
name: name
var children = node.children.map(function (child) {
return _this.tokenizeChildren(child);
}).filter(Boolean);
node.children = [];
node.openingElement.selfClosing = true;
return {
type: "element",
name: name,
value: node,
children: children
};
});
});
(0, _defineProperty2.default)(this, "isI18nComponent", function (node) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Trans";
return _this.types.isJSXElement(node) && _this.types.isJSXIdentifier(node.openingElement.name, {
name: name
(0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
return {
type: "arg",
name: _this.expressionToArgument(node),
value: node
};
});
});
(0, _defineProperty2.default)(this, "isChoiceComponent", function (node) {
return _this.isI18nComponent(node, "Plural") || _this.isI18nComponent(node, "Select") || _this.isI18nComponent(node, "SelectOrdinal");
});
this.types = types;
this.expressionIndex = (0, _utils.makeCounter)();
this.elementIndex = (0, _utils.makeCounter)();
};
(0, _defineProperty2.default)(this, "tokenizeText", function (value) {
return {
type: "text",
value: value
};
});
(0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
return _this.types.isIdentifier(exp) ? exp.name : _this.expressionIndex();
});
(0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
return _this.types.isIdentifier(node, {
name: name
});
});
(0, _defineProperty2.default)(this, "isI18nComponent", function (node) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Trans";
return _this.types.isJSXElement(node) && _this.types.isJSXIdentifier(node.openingElement.name, {
name: name
});
});
(0, _defineProperty2.default)(this, "isChoiceComponent", function (node) {
return _this.isI18nComponent(node, "Plural") || _this.isI18nComponent(node, "Select") || _this.isI18nComponent(node, "SelectOrdinal");
});
this.types = types;
this.expressionIndex = (0, _utils.makeCounter)();
this.elementIndex = (0, _utils.makeCounter)();
}
(0, _createClass2.default)(MacroJSX, [{
key: "clearBackslashes",
/**
* We clean '//\` ' to just '`'
*/
value: function clearBackslashes(value) {
return value.replace(removeExtraScapedLiterals, "`");
}
/**
* Custom matchers
*/
}]);
return MacroJSX;
}();
exports.default = MacroJSX;
{
"name": "@lingui/macro",
"version": "3.2.3",
"version": "3.3.0",
"description": "Macro for generating messages in ICU MessageFormat syntax",

@@ -32,3 +32,3 @@ "main": "index.ts",

"@babel/runtime": "^7.11.2",
"@lingui/conf": "^3.2.3",
"@lingui/conf": "^3.3.0",
"ramda": "^0.27.1"

@@ -35,0 +35,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc