Files

Return to Package Diff Home.
Brought to you by Intrinsic.

Package Diff: i18next-scanner @ 2.10.1 .. 2.10.2

lib/acorn-jsx-walk.js

@@ -4,22 +4,26 @@
value: true
});
-var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // Originally from: https://github.com/sderosiaux/acorn-jsx-walk
-
+var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _acornWalk = require('acorn-walk');
-//
-// Extends acorn walk with JSX elements
-//
+var _acornDynamicImport = require('acorn-dynamic-import');
-// See: https://github.com/RReverser/acorn-jsx/issues/23#issuecomment-403753801
-Object.assign(_acornWalk.base, {
+function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } // Originally from: https://github.com/sderosiaux/acorn-jsx-walk
+
+
+Object.assign(_acornWalk.base, _defineProperty({
FieldDefinition: function FieldDefinition(node, state, callback) {
if (node.value !== null) {
callback(node.value, state);
}
- },
+ }
+}, _acornDynamicImport.DynamicImportKey, function () {}));
+
+// Extends acorn walk with JSX elements
+// https://github.com/RReverser/acorn-jsx/issues/23#issuecomment-403753801
+Object.assign(_acornWalk.base, {
JSXAttribute: function JSXAttribute(node, state, callback) {
if (node.value !== null) {
callback(node.value, state);

lib/ensure-array.js

@@ -1,20 +0,0 @@
-"use strict";
-
-Object.defineProperty(exports, "__esModule", {
- value: true
-});
-var ensureArray = function ensureArray() {
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
-
- if (args.length === 0 || args[0] === undefined || args[0] === null) {
- return [];
- }
- if (args.length === 1) {
- return [].concat(args[0]);
- }
- return [].concat(args);
-};
-
-exports.default = ensureArray;
\ No newline at end of file

lib/jsx-parser.js

@@ -1,108 +0,0 @@
-'use strict';
-
-Object.defineProperty(exports, "__esModule", {
- value: true
-});
-exports.parseJSX = parseJSX;
-exports.astToText = astToText;
-exports.jsxToText = jsxToText;
-
-var _htmlparser = require('htmlparser2');
-
-var _htmlparser2 = _interopRequireDefault(_htmlparser);
-
-function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
-var jsExpr = /(.*?)({+[^]+?}+)(.*)/;
-
-function parseJSX(fragment) {
- var ast = {
- nodeName: '#root',
- childNodes: []
- };
- var stack = [ast];
-
- var handler = {
- onopentag: function onopentag(name) {
- var node = {
- nodeName: name,
- childNodes: []
- };
- stack[0].childNodes.push(node);
- stack.unshift(node);
- },
-
- onclosetag: function onclosetag() {
- stack.shift();
- },
-
- ontext: function ontext(text) {
- var txt = text;
- var m = jsExpr.exec(txt);
-
- var addText = function addText(txt) {
- var lastNode = stack[0].childNodes.slice(-1)[0];
- if (lastNode && lastNode.nodeName === '#text') {
- lastNode.value += txt;
- } else {
- stack[0].childNodes.push({
- nodeName: '#text',
- value: txt,
- childNodes: []
- });
- }
- };
-
- if (m) {
- while (m = jsExpr.exec(txt)) {
- if (m[1]) {
- addText(m[1]);
- }
- stack[0].childNodes.push({
- nodeName: '#expression',
- value: m[2],
- childNodes: []
- });
- txt = m[3];
- }
- }
- if (txt) {
- addText(txt);
- }
- }
- };
-
- var parser = new _htmlparser2.default.Parser(handler, {
- xmlMode: true,
- decodeEntities: true
- });
- parser.write(fragment);
-
- return stack[0].childNodes;
-}
-
-function astToText(ast) {
- var output = '';
-
- function walk(nodes) {
- nodes.forEach(function (node, ix) {
- if (node.nodeName === '#text') {
- output += node.value;
- } else if (node.nodeName === '#expression') {
- output += '<' + ix + '>' + node.value + '</' + ix + '>';
- } else {
- output += '<' + ix + '>';
- walk(node.childNodes);
- output += '</' + ix + '>';
- }
- });
- }
-
- walk(ast);
- return output;
-}
-
-function jsxToText(fragment) {
- var ast = parseJSX(fragment);
- return astToText(ast);
-}
\ No newline at end of file

lib/jsx-to-string.js

@@ -0,0 +1,100 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+
+var _acornJsx = require('acorn-jsx');
+
+var _ensureArray = require('ensure-array');
+
+var _ensureArray2 = _interopRequireDefault(_ensureArray);
+
+var _get2 = require('lodash/get');
+
+var _get3 = _interopRequireDefault(_get2);
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+
+var isNumericLiteral = function isNumericLiteral(node) {
+ if (!node) {
+ return false;
+ }
+
+ return node.type === 'Literal' && typeof node.value === 'number';
+};
+
+var isStringLiteral = function isStringLiteral(node) {
+ if (!node) {
+ return false;
+ }
+
+ return node.type === 'Literal' && typeof node.value === 'string';
+};
+
+var isObjectExpression = function isObjectExpression(node) {
+ if (!node) {
+ return false;
+ }
+
+ return node.type === 'ObjectExpression';
+};
+
+var nodesToString = function nodesToString(nodes) {
+ var memo = '';
+ var nodeIndex = 0;
+ nodes.forEach(function (node, i) {
+ if (node.type === 'JSXText') {
+ var value = node.value.replace(/^[\r\n]+\s*/g, '') // remove leading spaces containing a leading newline character
+ .replace(/[\r\n]+\s*$/g, '') // remove trailing spaces containing a leading newline character
+ .replace(/[\r\n]+\s*/g, ' '); // replace spaces containing a leading newline character with a single space character
+
+ if (!value) {
+ return;
+ }
+ memo += value;
+ } else if (node.type === 'JSXExpressionContainer') {
+ var _node$expression = node.expression,
+ expression = _node$expression === undefined ? {} : _node$expression;
+
+
+ if (isNumericLiteral(expression)) {
+ // Numeric literal is ignored in react-i18next
+ memo += '';
+ }if (isStringLiteral(expression)) {
+ memo += expression.value;
+ } else if (isObjectExpression(expression) && (0, _get3.default)(expression, 'properties[0].type') === 'Property') {
+ memo += '<' + nodeIndex + '>{{' + expression.properties[0].key.name + '}}</' + nodeIndex + '>';
+ } else {
+ console.error('Unsupported JSX expression. Only static values or {{interpolation}} blocks are supported. Got ' + expression.type + ':');
+ console.error(node.expression);
+ }
+ } else if (node.children) {
+ memo += '<' + nodeIndex + '>' + nodesToString(node.children) + '</' + nodeIndex + '>';
+ }
+
+ ++nodeIndex;
+ });
+
+ return memo;
+};
+
+var jsxToString = function jsxToString(code) {
+ try {
+ var ast = (0, _acornJsx.parse)('<Trans>' + code + '</Trans>', {
+ plugins: { jsx: true }
+ });
+
+ var nodes = (0, _ensureArray2.default)((0, _get3.default)(ast, 'body[0].expression.children'));
+ if (nodes.length === 0) {
+ return '';
+ }
+
+ return nodesToString(nodes);
+ } catch (e) {
+ console.error(e);
+ return '';
+ }
+};
+
+exports.default = jsxToString;
\ No newline at end of file

lib/match-recursive.js

@@ -1,65 +0,0 @@
-'use strict';
-
-Object.defineProperty(exports, "__esModule", {
- value: true
-});
-/*** matchRecursive
- accepts a string to search and a format (start and end tokens separated by "...").
- returns an array of matches, allowing nested instances of format.
-
- examples:
- matchRecursive("test", "(...)") -> []
- matchRecursive("(t(e)s)()t", "(...)") -> ["t(e)s", ""]
- matchRecursive("t<e>>st", "<...>") -> ["e"]
- matchRecursive("t<<e>st", "<...>") -> ["e"]
- matchRecursive("t<<e>>st", "<...>") -> ["<e>"]
- matchRecursive("<|t<e<|s|>t|>", "<|...|>") -> ["t<e<|s|>t"]
-*/
-var matchRecursive = function () {
- var formatParts = /^([\S\s]+?)\.\.\.([\S\s]+)/,
- metaChar = /[-[\]{}()*+?.\\^$|,]/g,
- escape = function escape(str) {
- return str.replace(metaChar, '\\$&');
- };
-
- return function (str, format) {
- var p = formatParts.exec(format);
- if (!p) {
- throw new Error('format must include start and end tokens separated by \'...\'');
- }
- if (p[1] === p[2]) {
- throw new Error('start and end format tokens cannot be identical');
- }
-
- var opener = p[1],
- closer = p[2],
-
- /* Use an optimized regex when opener and closer are one character each */
- iterator = new RegExp(format.length === 5 ? '[' + escape(opener + closer) + ']' : escape(opener) + '|' + escape(closer), 'g'),
- results = [],
- openTokens = void 0,
- matchStartIndex = void 0,
- match = void 0;
-
- do {
- openTokens = 0;
- while (match = iterator.exec(str)) {
- if (match[0] === opener) {
- if (!openTokens) {
- matchStartIndex = iterator.lastIndex;
- }
- openTokens++;
- } else if (openTokens) {
- openTokens--;
- if (!openTokens) {
- results.push(str.slice(matchStartIndex, match.index));
- }
- }
- }
- } while (openTokens && (iterator.lastIndex = matchStartIndex));
-
- return results;
- };
-}();
-
-exports.default = matchRecursive;
\ No newline at end of file

lib/match-recursive-regexp.js

@@ -1,64 +0,0 @@
-'use strict';
-
-Object.defineProperty(exports, "__esModule", {
- value: true
-});
-/*
-http://blog.stevenlevithan.com/archives/javascript-match-recursive-regexp
-
-Accepts a string to search, a left and right format delimiter
-as regex patterns, and optional regex flags. Returns an array
-of matches, allowing nested instances of left/right delimiters.
-Use the "g" flag to return all matches, otherwise only the
-first is returned. Be careful to ensure that the left and
-right format delimiters produce mutually exclusive matches.
-Backreferences are not supported within the right delimiter
-due to how it is internally combined with the left delimiter.
-When matching strings whose format delimiters are unbalanced
-to the left or right, the output is intentionally as a
-conventional regex library with recursion support would
-produce, e.g. "<<x>" and "<x>>" both produce ["x"] when using
-"<" and ">" as the delimiters (both strings contain a single,
-balanced instance of "<x>").
-
-examples:
- matchRecursiveRegExp("test", "\\(", "\\)")
- returns: []
- matchRecursiveRegExp("<t<<e>><s>>t<>", "<", ">", "g")
- returns: ["t<<e>><s>", ""]
- matchRecursiveRegExp("<div id=\"x\">test</div>", "<div\\b[^>]*>", "</div>", "gi")
- returns: ["test"]
-
-*/
-var matchRecursiveRegExp = function matchRecursiveRegExp(str, left, right, flags) {
- var f = flags || '',
- g = f.indexOf('g') > -1,
- x = new RegExp(left + '|' + right, g ? f : 'g' + f),
- l = new RegExp(left, f.replace(/g/g, '')),
- a = [],
- t = void 0,
- s = void 0,
- m = void 0;
-
- do {
- t = 0;
- while (m = x.exec(str)) {
- if (l.test(m[0])) {
- if (! t++) {
- s = x.lastIndex;
- }
- } else if (t) {
- if (! --t) {
- a.push(str.slice(s, m.index));
- if (!g) {
- return a;
- }
- }
- }
- }
- } while (t && (x.lastIndex = s));
-
- return a;
-};
-
-exports.default = matchRecursiveRegExp;
\ No newline at end of file

package.json

@@ -1,6 +1,6 @@
{
"name": "i18next-scanner",
- "version": "2.10.1",
+ "version": "2.10.2",
"description": "Scan your code, extract translation keys/values, and merge them into i18n resource files.",
"homepage": "https://github.com/i18next/i18next-scanner",
"author": "Cheton Wu <cheton@gmail.com>",
@@ -50,6 +50,7 @@
],
"dependencies": {
"acorn": "^6.1.1",
+ "acorn-dynamic-import": "^4.0.0",
"acorn-jsx": "^5.0.1",
"acorn-stage3": "^2.0.0",
"acorn-walk": "^6.1.1",