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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
}
56 changes: 55 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,54 @@ 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 acornAst = acorn.parse(string, {plugins: {jsx: true}});
const acornTransAst = acornAst.body[0].expression;
const children = this.parseAcornPayload(acornTransAst.children, string);

const elemsToString = children => children.map((child, index) => {
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
*/
parseAcornPayload(children, originalString) {
return children.map(child => {
switch (child.type) {
case 'JSXText': return {
type: 'text',
content: child.value.replace(/^(?:\s*(\n|\r)\s*)?(.*)(?:\s*(\n|\r)\s*)?$/, '$2')
};
case 'JSXElement': return {
type: 'tag',
children: this.parseAcornPayload(child.children, originalString)
};
case 'JSXExpressionContainer': return {
type: 'js',
content: originalString.slice(child.start, child.end)
};
default: throw new ParsingError("Unknown ast element when parsing jsx: " + child.type)
}
// Remove empty text elements
}).filter(child => child.type !== 'text' || child.content);
}
}
7 changes: 7 additions & 0 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ describe('JsxLexer', () => {
])
done()
})

it('erases tags from content', done => {
const Lexer = new JsxLexer()
const content = '<Trans>a<b test={"</b>"}>c<c>z</c></b>{d}<br stuff={y}/></Trans>';
assert.equal(Lexer.eraseTags(content), 'a<1>c<1>z</1></1><2>{d}</2><3></3>')
done()
})
})
})
Loading