diff --git a/.eslintrc.yml b/.eslintrc.yml index 923a5e8..830b359 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -192,6 +192,7 @@ rules: quotes: 'off' radix: error "react/jsx-uses-react": 1 + "react/jsx-uses-vars": 1 require-jsdoc: 'off' require-yield: error rest-spread-spacing: error diff --git a/ComponentInterpolator.js b/ComponentInterpolator.js index 031f53a..fa62258 100644 --- a/ComponentInterpolator.js +++ b/ComponentInterpolator.js @@ -2,7 +2,6 @@ var invariant = require('invariant'); var Component = require('preact').Component; var h = require('preact').h; var clone = require('extend').bind(true); - var WRAPPER_PATTERN = /(\*+)/; var PLACEHOLDER_PATTERN = /(%\{.*?\})/; @@ -68,7 +67,6 @@ class ComponentInterpolator extends Component { child = wrappers[token], ` expected '${token}' wrapper, none found` ); - child = injectNewDescendants( child, this.interpolateAllComponents(tokens, token), @@ -112,3 +110,4 @@ class ComponentInterpolator extends Component { } module.exports = ComponentInterpolator; + diff --git a/__tests__/preprocess.test.js b/__tests__/preprocess.test.js index 9bf9564..1ca445b 100644 --- a/__tests__/preprocess.test.js +++ b/__tests__/preprocess.test.js @@ -5,7 +5,9 @@ var expect = require('chai').expect; var preprocess = require('../preprocess'); var subject = function() { - return preprocess.apply(null, arguments).replace(/\s+/g, ' '); + return preprocess.apply(null, arguments) + .replace(/\s+/g, ' '); +// .replace(/;$/, ''); }; describe('preprocess', function() { diff --git a/main.js b/main.js index 7419413..1475649 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,4 @@ var log = require('./util/createLogger')('preact-i18nline'); - -var recast = require("recast"); - var preprocess = require("./preprocess"); module.exports = function(i18nline) { @@ -17,7 +14,7 @@ module.exports = function(i18nline) { fileData.skip = fileData.skip && !hasTranslatableText(source); if (!fileData.skip) { - var ast = fileData.ast || recast.parse(source, config.recastOptions); + var ast = fileData.ast || this.parse(source); preprocess.ast(ast, config); fileData.ast = ast; } @@ -27,3 +24,4 @@ module.exports = function(i18nline) { }; log.log('Initialized ' + log.name); + diff --git a/package-lock.json b/package-lock.json index 3509c06..f0f6f6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,14 +54,12 @@ "ansi-regex": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.0.0.tgz", - "integrity": "sha1-xQYbbg74qBd15Q9dZhUb9r83EQc=", - "dev": true + "integrity": "sha1-xQYbbg74qBd15Q9dZhUb9r83EQc=" }, "ansi-styles": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", - "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", - "dev": true + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" }, "anymatch": { "version": "1.3.2", @@ -213,7 +211,6 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", - "dev": true, "requires": { "chalk": "1.1.3", "esutils": "2.0.2", @@ -223,8 +220,7 @@ "js-tokens": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", - "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", - "dev": true + "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=" } } }, @@ -856,7 +852,6 @@ "version": "6.23.0", "resolved": "https://registry.npmjs.org/babel-messages/-/babel-messages-6.23.0.tgz", "integrity": "sha1-8830cDhYA1sqKVHG7F7fbGLyYw4=", - "dev": true, "requires": { "babel-runtime": "6.26.0" }, @@ -865,7 +860,6 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", - "dev": true, "requires": { "core-js": "2.4.1", "regenerator-runtime": "0.11.1" @@ -874,8 +868,7 @@ "regenerator-runtime": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", - "dev": true + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" } } }, @@ -1734,17 +1727,6 @@ } } }, - "babel-plugin-transform-jsx": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/babel-plugin-transform-jsx/-/babel-plugin-transform-jsx-2.0.0.tgz", - "integrity": "sha1-sPlMcNCnIPwaXBWwquD+W6Sm8Ko=", - "dev": true, - "requires": { - "babel-plugin-syntax-jsx": "6.18.0", - "esutils": "2.0.2", - "lodash": "4.16.5" - } - }, "babel-plugin-transform-react-jsx": { "version": "6.24.1", "resolved": "https://registry.npmjs.org/babel-plugin-transform-react-jsx/-/babel-plugin-transform-react-jsx-6.24.1.tgz", @@ -2024,7 +2006,6 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-traverse/-/babel-traverse-6.26.0.tgz", "integrity": "sha1-RqnL1+3MYsjlwGTi0tjQ9ANXZu4=", - "dev": true, "requires": { "babel-code-frame": "6.26.0", "babel-messages": "6.23.0", @@ -2041,7 +2022,6 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", - "dev": true, "requires": { "core-js": "2.4.1", "regenerator-runtime": "0.11.1" @@ -2051,7 +2031,6 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-types/-/babel-types-6.26.0.tgz", "integrity": "sha1-o7Bz+Uq0nrb6Vc1lInozQ4BjJJc=", - "dev": true, "requires": { "babel-runtime": "6.26.0", "esutils": "2.0.2", @@ -2063,7 +2042,6 @@ "version": "2.2.4", "resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz", "integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==", - "dev": true, "requires": { "loose-envify": "1.3.0" } @@ -2071,20 +2049,17 @@ "lodash": { "version": "4.17.5", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", - "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==", - "dev": true + "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==" }, "regenerator-runtime": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", - "dev": true + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" }, "to-fast-properties": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz", - "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=", - "dev": true + "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=" } } }, @@ -2127,8 +2102,7 @@ "babylon": { "version": "6.18.0", "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz", - "integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==", - "dev": true + "integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==" }, "balanced-match": { "version": "1.0.0", @@ -2210,7 +2184,6 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", - "dev": true, "requires": { "ansi-styles": "2.2.1", "escape-string-regexp": "1.0.5", @@ -2354,8 +2327,7 @@ "core-js": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.4.1.tgz", - "integrity": "sha1-TekR5mew6ukSTjQlS1OupvxhjT4=", - "dev": true + "integrity": "sha1-TekR5mew6ukSTjQlS1OupvxhjT4=" }, "core-util-is": { "version": "1.0.2", @@ -2382,7 +2354,6 @@ "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "dev": true, "requires": { "ms": "2.0.0" } @@ -2672,8 +2643,7 @@ "esutils": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", - "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", - "dev": true + "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=" }, "expand-brackets": { "version": "0.1.5", @@ -3813,8 +3783,7 @@ "globals": { "version": "9.18.0", "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", - "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==", - "dev": true + "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==" }, "globby": { "version": "5.0.0", @@ -3855,7 +3824,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", - "dev": true, "requires": { "ansi-regex": "2.0.0" } @@ -3887,10 +3855,12 @@ "integrity": "sha512-u144MQhV/8mz4Y5wP86SQAWMwS8gpe/JavIa9hugSI4WreezGgbhJPdk2Q60KcdIltKLiNefGtHNh1N8SSmQqQ==" }, "i18nline": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/i18nline/-/i18nline-1.6.0.tgz", - "integrity": "sha512-Pcc0qrWg6DQbmsL+95PV21eyheKXIMY/85S4dB4laSARGwsum3exNGQ1E3CI52WJ8o1yt6FWz93WNwCLs6/M7A==", + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/i18nline/-/i18nline-1.7.0.tgz", + "integrity": "sha512-Gm1V4SoXpms0py7YT2qiyc9LplVz1odEoJPXAf2Z8H8jVRU05Xcjnctzp1dgdhVvGRO0yDaEK7aBzmyjB61CMQ==", "requires": { + "babel-traverse": "6.26.0", + "babylon": "6.18.0", "chalk": "2.3.2", "crc32": "0.2.2", "esprima": "4.0.0", @@ -3899,7 +3869,6 @@ "i18n-js": "3.0.3", "minimist": "1.2.0", "mkdirp": "0.5.1", - "recast": "0.14.7", "speakingurl": "14.0.1", "ulog": "1.1.0" }, @@ -3912,11 +3881,6 @@ "color-convert": "1.9.1" } }, - "ast-types": { - "version": "0.11.3", - "resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.11.3.tgz", - "integrity": "sha512-XA5o5dsNw8MhyW0Q7MWXJWc4oOzZKbdsEJq45h7c8q/d9DwWZ5F2ugUc1PuMLPGsUnphCt/cNDHu8JeBbxf1qA==" - }, "chalk": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.2.tgz", @@ -3927,27 +3891,6 @@ "supports-color": "5.3.0" } }, - "esprima": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz", - "integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==" - }, - "recast": { - "version": "0.14.7", - "resolved": "https://registry.npmjs.org/recast/-/recast-0.14.7.tgz", - "integrity": "sha512-/nwm9pkrcWagN40JeJhkPaRxiHXBRkXyRh/hgU088Z/v+qCy+zIHHY6bC6o7NaKAxPqtE6nD8zBH1LfU0/Wx6A==", - "requires": { - "ast-types": "0.11.3", - "esprima": "4.0.0", - "private": "0.1.6", - "source-map": "0.6.1" - } - }, - "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" - }, "supports-color": { "version": "5.3.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.3.0.tgz", @@ -4470,8 +4413,7 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", - "dev": true + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "mute-stream": { "version": "0.0.7", @@ -5166,7 +5108,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "dev": true, "requires": { "ansi-regex": "2.0.0" } @@ -5180,8 +5121,7 @@ "supports-color": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", - "dev": true + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" }, "table": { "version": "4.0.2", diff --git a/package.json b/package.json index 05aee0a..9d6ad36 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "devDependencies": { "babel-cli": "^6.26.0", "babel-core": "^6.26.0", - "babel-plugin-transform-jsx": "^2.0.0", "babel-plugin-transform-react-jsx": "^6.24.1", "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.24.1", @@ -50,12 +49,12 @@ "preact-render-to-string": "^3.7.0" }, "peerDependencies": { - "preact": "^8.0.0" + "preact": "^7.0.0 || ^8.0.0" }, "dependencies": { "esprima": "^4.0.0", "extend": "^3.0.1", - "i18nline": "^1.6.0", + "i18nline": "^1.7.0", "invariant": "^2.2.4", "preact": "^8.2.7", "recast": "^0.14.7", diff --git a/preprocess.js b/preprocess.js index 19bcdc2..4c435e5 100644 --- a/preprocess.js +++ b/preprocess.js @@ -1,10 +1,8 @@ var log = require('./util/createLogger')('preact-i18nline:preprocess'); var recast = require('recast'); -var esprima = require('esprima'); -var extend = require('extend'); - var b = recast.types.builders; +var parse = require('i18nline/lib/processors/js_processor').prototype.parse; // http://www.w3.org/TR/html5/dom.html#the-translate-attribute // everything per the spec, except: @@ -33,7 +31,7 @@ var translatableAttributes = { var isTranslatableAttribute = function(node, attribute) { var name = attribute.name.name; if (!translatableAttributes[name]) return false; - if (attribute.value.type !== "Literal") return false; + if (attribute.value.type !== "StringLiteral") return false; var rules = translatableAttributes[name]; if (typeof rules === "function") @@ -51,7 +49,6 @@ var findIndex = function(fn, ary) { }; var hasLiteralContent = function(node) { - if (node.type === "Literal") return true; if (node.type === "JSXText") return true; if (node.type !== "JSXElement") return false; return node.children && node.children.some(hasLiteralContent); @@ -105,7 +102,7 @@ var findAttribute = function(attribute, node, shouldSpliceFn) { if (index < 0) return; var value = attributes[index].value; - if (attributes[index].value.type !== "Literal") return; + if (attributes[index].value.type !== "StringLiteral") return; value = value.value; if (shouldSpliceFn && shouldSpliceFn(value)) { @@ -169,7 +166,7 @@ function transformationsFor(config) { if (Object.keys(wrappers).length) { var wrappersNode = b.objectExpression([]); for (key in wrappers) { - wrappersNode.properties.push(b.property("init", b.literal(key), wrappers[key])); + wrappersNode.properties.push(b.property("init", b.stringLiteral(key), wrappers[key])); } properties.push( b.jsxAttribute( @@ -200,14 +197,14 @@ function transformationsFor(config) { b.jsxIdentifier(interpolatorName) ), children.map(function(child) { - return typeof child === "string" ? b.literal(child) : child; + return typeof child === "string" ? b.stringLiteral(child) : child; }) ); }; var translateCallFor = function(loc, string) { var args = [ - b.literal(string) + b.stringLiteral(string) ]; // create dummy placeholders; we want ComponentInterpolator to do the @@ -219,7 +216,7 @@ function transformationsFor(config) { while (tokens.length) { var token = tokens.shift(); if (token.match(PLACEHOLDER_PATTERN)) { - optionsNode.properties.push(b.property("init", b.literal(token.slice(2, -1)), b.literal(token))); + optionsNode.properties.push(b.property("init", b.stringLiteral(token.slice(2, -1)), b.stringLiteral(token))); } } args.push(optionsNode); @@ -303,7 +300,7 @@ function transformationsFor(config) { node.children.forEach(function(child) { var part; var translatable = isTranslatable(child, true); - if ((child.type === "Literal") || (child.type === "JSXText")) { + if (child.type === "JSXText") { part = child.value; lastPartType = "literal"; } else if (hasLiteralContent(child) && translatable) { @@ -386,24 +383,10 @@ function transformationsFor(config) { var preprocess = function(source, config) { log.debug(log.name + ': preprocessing source with config', config); - - var defaultOptions = { - recastOptions: { - parser: { - parse: function(source) { - return esprima.parse(source, { - jsx: true, - loc: true, - }); - } - } - } - }; - config = extend(defaultOptions, config || {}); + config = config || {}; config.autoTranslateTags = typeof config.autoTranslateTags === 'string' ? config.autoTranslateTags.split(',') : config.autoTranslateTags || []; config.neverTranslateTags = typeof config.neverTranslateTags === 'string' ? config.neverTranslateTags.split(',') : config.neverTranslateTags || []; - - var ast = recast.parse(source, config.recastOptions); + var ast = recast.parse(source, {parser: {parse: parse}}); preprocessAst(ast, config); return recast.print(ast).code; }; @@ -418,3 +401,4 @@ preprocess.ast = preprocessAst; module.exports = preprocess; log.log('Initialized ' + log.name); + diff --git a/webpack-loader.js b/webpack-loader.js index c103c0a..0cf1b42 100644 --- a/webpack-loader.js +++ b/webpack-loader.js @@ -1,7 +1,6 @@ var log = require('./util/createLogger')('preact-i18nline:webpack-loader'); var I18nline = require("i18nline"); - var config = I18nline.config; var preprocess = require("./preprocess"); var hasTranslatableText = require("./hasTranslatableText")(config); @@ -12,7 +11,7 @@ var hasTranslatableText = require("./hasTranslatableText")(config); * will load *all* of i18nline (which includes things like fs, and * will fail). */ -var noParsePath = "i18nline/dist/lib/i18nline"; +var noParsePath = "i18nline/lib/i18nline"; var addNoParse = function() { var escapeRegExp = require("./util/escapeRegExp"); var path = require("path"); @@ -35,3 +34,4 @@ module.exports = function(source) { }; log.log('Initialized ' + log.name); +