/** * Helper function to convert Javascript stringified code to an AST using acorn-jsx library * @param js */ function jsToAst(js) { const ast = acorn.parse(js, { plugins: { jsx: true }, }); if (ast.body.length === 0) throw new Error('Empty AST input'); return ast; }
function default_1(code, options) { if (options === void 0) { options = {}; } var opts = Object.assign({ plugins: { jsx: true } }, options); return Acorn.parse(code, opts); }
Body.prototype.prepend = function (code) { var values = postParse(acorn.parse(code, this.acornOptions).body); var insertionIndex = 0; var nodes = this.node; // Ensure "use strict" declaration is kept on top if (nodes[0] && nodes[0].expression && nodes[0].expression.type === 'Literal' && nodes[0].expression.value === 'use strict') { insertionIndex = 1; } values.forEach(function (value, index) { // insertionIndex + index to insert the instruction in order nodes.splice(insertionIndex + index, 0, value); }); return this; };
fs.readFile(path, {encoding: 'utf-8'}, function(err, data) { if(err) { return cb(err, null); } // first apply a jsx transformation data = jsx.transform(data, { harmony: true, es6module: true }); cb(null, acorn.parse(data, { sourceType: 'module', ecmaVersion: "6", locations: true, plugins: { 'jsx': true } })); });
Object.keys(sources).forEach(function (filename) { var source = sources[filename].replace(/^#.*/, ''); // strip leading hash-bang var astComments = []; var ast = parser.parse(source, { ecmaVersion: 6, sourceType: 'module', plugins: { jsx: { allowNamespaces: false } }, onComment: function (block, text, start, end, line/*, column*/) { text = text.match(commentRegex) && text.replace(/^\//, '').trim(); if (!text) { return; } astComments.push({ line: line, value: text }); }, locations: true }); // finds comments that end on the previous line function findComments(comments, line) { return comments.map(function (node) { var commentLine = node.line.line; if (commentLine === line || commentLine + 1 === line) { return node.value; } }).filter(Boolean).join('\n'); } walk.simple(ast, { 'CallExpression': function (node) { var arg = getTranslatable(node, options); if (!arg) { return; } var msgid = arg; if (arg.constructor === Array) { msgid = arg[0]; } var str = extractStr(msgid); var line = node.loc.start.line; var comments = findComments(astComments, line); var ref = filename + ':' + line; if (!translations[str]) { translations[str] = { msgid: str, msgstr: [], comments: { extracted: comments, reference: ref } }; if (arg.constructor === Array) { translations[str].msgid_plural = extractStr(arg[1]); translations[str].msgstr = ['', '']; } } else { if (translations[str].comments) { translations[str].comments.reference += '\n' + ref; } if (comments) { translations[str].comments.extracted += '\n' + comments; } } } }, objectAssign({}, walk.base, jsxBase)); function dedupeNCoalesce(item, i, arr) { return item && arr.indexOf(item) === i; } Object.keys(translations).forEach(function (msgid) { var comments = translations[msgid].comments; if (!comments) { return; } if (comments.reference) { comments.reference = comments.reference.split('\n').filter(dedupeNCoalesce).join('\n'); } if (comments.extracted) { comments.extracted = comments.extracted.split('\n').filter(dedupeNCoalesce).join('\n'); } }); });
Body.prototype.append = function (code) { var values = postParse(acorn.parse(code, this.acornOptions).body); Array.prototype.push.apply(this.node, values); return this; };