@lingui/babel-plugin-extract-messages
Advanced tools
Comparing version 2.7.0 to 3.0.0-1
205
index.js
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = _default; | ||
var _stringify = require("babel-runtime/core-js/json/stringify"); | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); | ||
var _weakSet = require("babel-runtime/core-js/weak-set"); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _weakSet2 = _interopRequireDefault(_weakSet); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _map = require("babel-runtime/core-js/map"); | ||
var _mkdirp = _interopRequireDefault(require("mkdirp")); | ||
var _map2 = _interopRequireDefault(_map); | ||
var _generator = _interopRequireDefault(require("@babel/generator")); | ||
var _includes = require("babel-runtime/core-js/array/includes"); | ||
var _conf = require("@lingui/conf"); | ||
var _includes2 = _interopRequireDefault(_includes); | ||
// Map of messages | ||
var MESSAGES = Symbol("I18nMessages"); // We need to remember all processed nodes. When JSX expressions are | ||
// replaced with CallExpressions, all children are traversed for each CallExpression. | ||
// Then, i18n._ methods are visited multiple times for each parent CallExpression. | ||
var _extends2 = require("babel-runtime/helpers/extends"); | ||
var VISITED = Symbol("I18nVisited"); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
function addMessage(path, messages, _ref) { | ||
var id = _ref.id, | ||
defaults = _ref.defaults, | ||
origin = _ref.origin, | ||
props = (0, _objectWithoutProperties2.default)(_ref, ["id", "defaults", "origin"]); | ||
var _objectWithoutProperties2 = require("babel-runtime/helpers/objectWithoutProperties"); | ||
if (messages.has(id)) { | ||
var message = messages.get(id); // only set/check default language when it's defined. | ||
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); | ||
if (message.defaults && defaults && message.defaults !== defaults) { | ||
throw path.buildCodeFrameError("Different defaults for the same message ID."); | ||
} else { | ||
if (defaults) { | ||
message.defaults = defaults; | ||
} | ||
var _symbol = require("babel-runtime/core-js/symbol"); | ||
; | ||
[].push.apply(message.origin, origin); | ||
} | ||
} else { | ||
messages.set(id, (0, _objectSpread2.default)({}, props, { | ||
defaults: defaults, | ||
origin: origin | ||
})); | ||
} | ||
} | ||
var _symbol2 = _interopRequireDefault(_symbol); | ||
exports.default = function (_ref2) { | ||
function _default(_ref2) { | ||
var t = _ref2.types; | ||
var localTransComponentName = void 0; | ||
var localTransComponentName; | ||
var opts = (0, _conf.getConfig)(); | ||
@@ -50,6 +71,13 @@ var optsBaseDir = opts.rootDir; | ||
var isNoopMethod = function isNoopMethod(node) { | ||
return t.isIdentifier(node, { name: "i18nMark" }); | ||
return t.isIdentifier(node, { | ||
name: "i18nMark" | ||
}); | ||
}; | ||
var isI18nMethod = function isI18nMethod(node) { | ||
return t.isMemberExpression(node) && t.isIdentifier(node.object, { name: "i18n" }) && t.isIdentifier(node.property, { name: "_" }); | ||
return t.isMemberExpression(node) && t.isIdentifier(node.object, { | ||
name: "i18n" | ||
}) && t.isIdentifier(node.property, { | ||
name: "_" | ||
}); | ||
}; | ||
@@ -60,6 +88,6 @@ | ||
var filename = _path2.default.relative(optsBaseDir, file.opts.filename).replace(/\\/g, "/"); | ||
var filename = _path.default.relative(optsBaseDir, file.opts.filename).replace(/\\/g, "/"); | ||
var line = path.node.loc ? path.node.loc.start.line : null; | ||
props.origin = [[filename, line]]; | ||
addMessage(path, messages, props); | ||
@@ -75,8 +103,6 @@ } | ||
var node = path.node; | ||
var moduleName = node.source.value; | ||
if (!(0, _includes2.default)(["@lingui/react", "@lingui/macro", "@lingui/core"], moduleName)) return; | ||
if (!["@lingui/react", "@lingui/macro", "@lingui/core"].includes(moduleName)) return; | ||
var importDeclarations = {}; | ||
var importDeclarations = {}; | ||
if (moduleName === "@lingui/react" || moduleName === "@lingui/macro") { | ||
@@ -90,5 +116,5 @@ node.specifiers.forEach(function (specifier) { | ||
} | ||
} | ||
} // Remove imports of i18nMark identity | ||
// Remove imports of i18nMark identity | ||
node.specifiers = node.specifiers.filter(function (specifier) { | ||
@@ -102,4 +128,2 @@ return specifier.imported.name !== "i18nMark"; | ||
}, | ||
// Extract translation from <Trans /> component. | ||
@@ -109,9 +133,7 @@ JSXElement: function JSXElement(path, _ref3) { | ||
var node = path.node; | ||
if (!localTransComponentName || !isTransComponent(node)) return; | ||
var attrs = node.openingElement.attributes || []; | ||
var props = attrs.reduce(function (acc, item) { | ||
var key = item.name.name; | ||
if (key === "id" || key === "defaults" || key === "description") { | ||
@@ -124,2 +146,3 @@ if (item.value.value) { | ||
} | ||
return acc; | ||
@@ -133,6 +156,8 @@ }, {}); | ||
})[0]; | ||
if (idProp === undefined || t.isLiteral(props.id)) { | ||
console.warn("Missing message ID, skipping."); | ||
console.warn((0, _babelGenerator2.default)(node).code); | ||
console.warn((0, _generator.default)(node).code); | ||
} | ||
return; | ||
@@ -143,4 +168,2 @@ } | ||
}, | ||
// Extract translation from i18n._ call | ||
@@ -150,9 +173,6 @@ CallExpression: function CallExpression(path, _ref4) { | ||
var node = path.node; | ||
var visited = file.get(VISITED); | ||
if ( | ||
// we already visited this node | ||
visited.has(node.callee) || | ||
// nothing to extract | ||
if ( // we already visited this node | ||
visited.has(node.callee) || // nothing to extract | ||
!isI18nMethod(node.callee) && !isNoopMethod(node.callee)) { | ||
@@ -170,5 +190,5 @@ return; | ||
var attrs = node.arguments[2] && node.arguments[2].properties ? node.arguments[2].properties : []; | ||
var idArg = node.arguments[0]; | ||
var id = idArg && idArg.value; | ||
if (!id) { | ||
@@ -178,3 +198,3 @@ // i18n._(message) is valid, don't raise warning | ||
console.warn("Missing message ID, skipping."); | ||
console.warn((0, _babelGenerator2.default)(node).code); | ||
console.warn((0, _generator.default)(node).code); | ||
} | ||
@@ -189,4 +209,5 @@ | ||
return acc; | ||
}, { id: id }); | ||
}, { | ||
id: id | ||
}); | ||
collectMessage(path, file, props); | ||
@@ -199,10 +220,6 @@ | ||
}, | ||
// Extract message descriptors | ||
ObjectExpression: function ObjectExpression(path, _ref5) { | ||
var file = _ref5.file; | ||
var visited = file.get(VISITED); | ||
var comment = path.node.leadingComments && path.node.leadingComments.filter(function (node) { | ||
@@ -217,8 +234,5 @@ return node.value.startsWith("i18n"); | ||
visited.add(path.node); | ||
var props = {}; | ||
var description = comment.value.replace(/\s*i18n:?\s*/, "").trim(); | ||
if (description) props.description = description; | ||
var copyProps = ["id", "defaults"]; | ||
@@ -231,21 +245,17 @@ path.node.properties.filter(function (_ref6) { | ||
value = _ref7.value; | ||
props[key.name] = value.value; | ||
}); | ||
collectMessage(path, file, props); | ||
} | ||
}, | ||
pre: function pre(file) { | ||
localTransComponentName = null; | ||
// Ignore else path for now. Collision is possible if other plugin is | ||
localTransComponentName = null; // Ignore else path for now. Collision is possible if other plugin is | ||
// using the same Symbol('I18nMessages'). | ||
// istanbul ignore else | ||
if (!file.has(MESSAGES)) { | ||
file.set(MESSAGES, new _map2.default()); | ||
file.set(MESSAGES, new Map()); | ||
} | ||
file.set(VISITED, new _weakSet2.default()); | ||
file.set(VISITED, new WeakSet()); | ||
}, | ||
@@ -260,17 +270,20 @@ post: function post(file) { | ||
var baseDir = _path2.default.dirname(_path2.default.relative(optsBaseDir, filename)); | ||
var targetDir = _path2.default.join(localeDir, "_build", baseDir); | ||
var baseDir = _path.default.dirname(_path.default.relative(optsBaseDir, filename)); | ||
var targetDir = _path.default.join(localeDir, "_build", baseDir); | ||
var messages = file.get(MESSAGES); | ||
var catalog = {}; | ||
var baseName = _path2.default.basename(filename); | ||
var catalogFilename = _path2.default.join(targetDir, baseName + ".json"); | ||
_mkdirp2.default.sync(targetDir); | ||
var baseName = _path.default.basename(filename); | ||
// no messages, skip file | ||
var catalogFilename = _path.default.join(targetDir, "".concat(baseName, ".json")); | ||
_mkdirp.default.sync(targetDir); // no messages, skip file | ||
if (!messages.size) { | ||
// clean any existing catalog | ||
if (_fs2.default.existsSync(catalogFilename)) { | ||
_fs2.default.writeFileSync(catalogFilename, (0, _stringify2.default)({})); | ||
if (_fs.default.existsSync(catalogFilename)) { | ||
_fs.default.writeFileSync(catalogFilename, JSON.stringify({})); | ||
} | ||
@@ -285,57 +298,5 @@ | ||
_fs2.default.writeFileSync(catalogFilename, (0, _stringify2.default)(catalog, null, 2)); | ||
_fs.default.writeFileSync(catalogFilename, JSON.stringify(catalog, null, 2)); | ||
} | ||
}; | ||
}; | ||
var _fs = require("fs"); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _path = require("path"); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _mkdirp = require("mkdirp"); | ||
var _mkdirp2 = _interopRequireDefault(_mkdirp); | ||
var _babelGenerator = require("babel-generator"); | ||
var _babelGenerator2 = _interopRequireDefault(_babelGenerator); | ||
var _conf = require("@lingui/conf"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Map of messages | ||
var MESSAGES = (0, _symbol2.default)("I18nMessages"); | ||
// We need to remember all processed nodes. When JSX expressions are | ||
// replaced with CallExpressions, all children are traversed for each CallExpression. | ||
// Then, i18n._ methods are visited multiple times for each parent CallExpression. | ||
var VISITED = (0, _symbol2.default)("I18nVisited"); | ||
function addMessage(path, messages, _ref) { | ||
var id = _ref.id, | ||
defaults = _ref.defaults, | ||
origin = _ref.origin, | ||
props = (0, _objectWithoutProperties3.default)(_ref, ["id", "defaults", "origin"]); | ||
if (messages.has(id)) { | ||
var message = messages.get(id); | ||
// only set/check default language when it's defined. | ||
if (message.defaults && defaults && message.defaults !== defaults) { | ||
throw path.buildCodeFrameError("Different defaults for the same message ID."); | ||
} else { | ||
if (defaults) { | ||
message.defaults = defaults; | ||
} | ||
;[].push.apply(message.origin, origin); | ||
} | ||
} else { | ||
messages.set(id, (0, _extends3.default)({}, props, { defaults: defaults, origin: origin })); | ||
} | ||
} |
{ | ||
"name": "@lingui/babel-plugin-extract-messages", | ||
"version": "2.7.0", | ||
"description": "Extract messages for translation", | ||
"version": "3.0.0-1", | ||
"description": "Babel plugin for collecting messages from source code for internationalization", | ||
"main": "index.js", | ||
@@ -28,12 +28,8 @@ "author": { | ||
"engines": { | ||
"node": ">=4.0" | ||
"node": ">=8.0.0" | ||
}, | ||
"dependencies": { | ||
"@lingui/conf": "2.7.0", | ||
"babel-generator": "^6.26.1" | ||
}, | ||
"devDependencies": { | ||
"@lingui/babel-plugin-transform-js": "2.7.0", | ||
"@lingui/babel-plugin-transform-react": "2.7.0" | ||
"@babel/generator": "^7.0.0", | ||
"@lingui/conf": "3.0.0-1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
0
20673
226
2
+ Added@babel/generator@^7.0.0
+ Added@babel/generator@7.25.5(transitive)
+ Added@babel/helper-string-parser@7.24.8(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/types@7.25.4(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Added@lingui/conf@3.0.0-1(transitive)
+ Addedjsesc@2.5.2(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
- Removedbabel-generator@^6.26.1
- Removed@lingui/conf@2.7.0(transitive)
- Removedbabel-generator@6.26.1(transitive)
- Removedbabel-messages@6.23.0(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removeddetect-indent@4.0.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedjsesc@1.3.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedto-fast-properties@1.0.3(transitive)
- Removedtrim-right@1.0.1(transitive)
Updated@lingui/conf@3.0.0-1