Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkudo committed Sep 17, 2018
1 parent 9ab01e3 commit 7bcd8f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/isLocalizationFunctionStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const translationStartPatterns = [
'__`',
`__n(`,
'__n`',
'__p(',
'__p`',
`__np(`,
'__np`',
];

const length = translationStartPatterns
Expand Down
46 changes: 35 additions & 11 deletions src/parseLocalizationFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ function continueUntilStackLengthIs(text, state, length) {
return state;
}

function readStringArgument({index, stack, lineNumber}) {
const contextStart = continueToQuoteStart(text, {index, stack, lineNumber});
const contextEnd = continueUntilStackLengthIs(text, {...keyStart}, keyStart.stack.length - 1);
const context = text.substring(contextStart.index, contextEnd.index - 1);

if (contextStart.index === contextEnd.index - 1) {
throw new SyntaxError('empty context key');
}

return [{index, stack, lineNumber}, text];
}

/**
* Parses the information from a localization function, include the function string,
* the key, the line number.
* the key, the line number. Parses __, __n, __p, __np.
* @param {String} text - The text blob
* @param {Number} index - The offset on the text
* @param {Array<String>} stack The current code stack
Expand All @@ -50,31 +62,43 @@ function continueUntilStackLengthIs(text, state, length) {
*/
module.exports = function parseLocalizationFunction(text, {index, stack, lineNumber}) {
const functionStart = {index, stack, lineNumber};
let plural = false;
let particular = false;

index += 1;

if (text.charAt(index + 1) === 'n') {
plural = true;
index += 1;
}

if (text.charAt(index + 1) === '(') {
if (text.charAt(index + 1) === 'p') {
particular = true;
index += 1;
}

if (text.charAt(index + 1) === '(') {
index += 1;
}

const keyStart = continueToQuoteStart(text, {index, stack, lineNumber});
const keyEnd = continueUntilStackLengthIs(text, {...keyStart}, keyStart.stack.length - 1);
const metadata = {plural, particular};
let state = {index, stack, lineNumber};
let key;
let context;

if (keyStart.index === keyEnd.index - 1) {
throw new SyntaxError('empty localization key');
if (particular) {
[state, context] = readStringArgument(state);
metadata.context = context;
}

[state, key] = readStringArgument(state);
metadata.key = key;

const functionEnd = (keyEnd.stack[0] === '(') ?
continueUntilStackLengthIs(text, {...keyEnd}, keyEnd.stack.length - 1) : keyEnd;
const fn = text.substring(functionStart.index, functionEnd.index);

metadata.fn = fn;

return {
...functionEnd,
key: text.substring(keyStart.index, keyEnd.index - 1),
fn: text.substring(functionStart.index, functionEnd.index),
};
return Object.assign({}, functionEnd, metadata);
}

0 comments on commit 7bcd8f8

Please sign in to comment.