Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved parsing of Trans component #85

Merged
merged 9 commits into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 43 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"url": "https://github.com/i18next/i18next-parser"
},
"dependencies": {
"acorn-jsx": "^4.1.1",
"colors": "~1.2.0-rc0",
"commander": "~2.9.0",
"concat-stream": "~1.6.0",
Expand Down
9 changes: 8 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,16 @@ function populateHash(source, target = {}) {
return target
}

class ParsingError extends Error {
constructor(message) {
super(message);
this.name = "ParsingError";
}
}

export {
dotPathToHash,
mergeHashes,
populateHash
populateHash,
ParsingError
}
61 changes: 60 additions & 1 deletion src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as acorn from 'acorn-jsx';
import assert from 'assert'
import HTMLLexer from './html-lexer'
import BaseLexer from './base-lexer';
import { ParsingError } from '../helpers';

export default class JsxLexer extends HTMLLexer {
constructor(options = {}) {
Expand Down Expand Up @@ -44,7 +48,7 @@ export default class JsxLexer extends HTMLLexer {
const key = attrs.keys

if (matches[3] && !attrs.options.defaultValue) {
attrs.options.defaultValue = matches[3].trim()
attrs.options.defaultValue = this.eraseTags(matches[0]).replace(/\s+/g, ' ')
}

if (key) {
Expand All @@ -54,4 +58,59 @@ export default class JsxLexer extends HTMLLexer {

return this.keys
}

/**
* Recursively convert html tags and js injections to tags with the child index in it
*
* @param {string} string
*
* @returns string
*/
eraseTags(string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please test these methods in the lexer's test? The parser test is a high level check but individual method that do as much as these one should have test of their own so we can debug them later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for eraseTags.

const children = this.simplify(acorn.parse(string, {plugins: {jsx: true}}).body[0].expression.children, string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid line that are longer than 80 char. I would suggest doing this in two steps


const elemsToString = children => children.map((child, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you create a function rather than directly returning the mapped children?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it calls itself recurisvely.

switch(child.type) {
case 'text': return child.content;
case 'js': return `<${index}>${child.content}</${index}>`;
case 'tag': return `<${index}>${elemsToString(child.children)}</${index}>`;
default: throw new ParsingError('Unknown parsed content: ' + child.type);
}
}).join('');

return elemsToString(children);
}

/**
* Simplify the bulky AST given by Acorn
* @param {*} children An array of elements contained inside an html tag
* @param {string} originalString The original string being parsed
*/
simplify(children, originalString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this something a little more idiomatic like parseAcornPayload?

for (let i = 0; i < children.length; i ++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you don't use forEach here? If not, it would be more consistent with the rest of the code

const child = children[i];
switch (child.type) {
case 'JSXText': children[i] = {
type: 'text',
content: child.value.replace(/^(?:\s*(\n|\r)\s*)?(.*)(?:\s*(\n|\r)\s*)?$/, '$2')
}; break
case 'JSXElement': children[i] = {
type: 'tag',
children: this.simplify(child.children, originalString)
}; break;
case 'JSXExpressionContainer': children[i] = {
type: 'js',
content: originalString.slice(child.start, child.end)
}; break;
default: throw new ParsingError("Unknown ast element when parsing jsx: " + child.type)
}
}

// Filter empty text elements. Using string.length instead of !string because
// '0' is a valid text element, and '' is not, and !string doesn't make a difference
// between the two.
children = children.filter(child => !(child.type === 'text' && child.content.length === 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be written child.type !== 'text' || child.content which is easier to read imo

I'm not sure about your comment either. As I commented above: '0' is truthy, '' is falsy. They are not the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, '0' == false, yet !'0' == false too... A quirk of js


return children;
}
}