gettext-handlebars
Advanced tools
Comparing version 0.7.0 to 1.0.0
150
index.js
'use strict'; | ||
var Handlebars = require('handlebars'); | ||
const Handlebars = require('handlebars'); | ||
@@ -13,10 +13,10 @@ function Parser (keywordSpec) { | ||
Object.keys(keywordSpec).forEach(function (keyword) { | ||
var positions = keywordSpec[keyword]; | ||
Object.keys(keywordSpec).forEach(keyword => { | ||
const positions = keywordSpec[keyword]; | ||
if ('msgid' in positions) { | ||
return; | ||
} else if (Array.isArray(positions) && positions.indexOf('msgid') >= 0) { | ||
// maintain backwards compatibility with `_: ['msgid']` format | ||
keywordSpec[keyword] = positions.reduce(function (result, key, idx) { | ||
keywordSpec[keyword] = positions.reduce((result, key, idx) => { | ||
result[key] = idx; | ||
@@ -28,5 +28,5 @@ | ||
// maintain backwards compatibility with `_: [0]` format | ||
var order = ['msgid', 'msgid_plural']; | ||
const order = ['msgid', 'msgid_plural']; | ||
keywordSpec[keyword] = positions.slice(0).reduce(function (result, pos, idx) { | ||
keywordSpec[keyword] = positions.slice(0).reduce((result, pos, idx) => { | ||
result[order[idx]] = pos; | ||
@@ -39,3 +39,3 @@ | ||
Object.keys(keywordSpec).forEach(function (keyword) { | ||
Object.keys(keywordSpec).forEach(keyword => { | ||
if (!('msgid' in keywordSpec[keyword])) { | ||
@@ -84,5 +84,4 @@ throw new Error('Every keyword must have a msgid key, but "' + keyword + '" doesn\'t have one'); | ||
Parser.messageToKey = function (msgid, msgctxt) { | ||
return msgctxt ? msgctxt + Parser.contextDelimiter + msgid : msgid; | ||
}; | ||
Parser.messageToKey = (msgid, msgctxt) => | ||
msgctxt ? `${msgctxt}${Parser.contextDelimiter}${msgid}` : msgid; | ||
@@ -96,86 +95,87 @@ /** | ||
Parser.prototype.parse = function (template) { | ||
var keywordSpec = this.keywordSpec, | ||
keywords = Object.keys(keywordSpec), | ||
tree = Handlebars.parse(template); | ||
const keywordSpec = this.keywordSpec; | ||
const keywords = Object.keys(keywordSpec); | ||
const tree = Handlebars.parse(template); | ||
var isMsg = function (msgs, statement) { | ||
const isMsg = function (msgs, statement) { | ||
switch (statement.type) { | ||
case 'MustacheStatement': | ||
case 'SubExpression': | ||
if (keywords.indexOf(statement.path.original) !== -1) { | ||
var spec = keywordSpec[statement.path.original]; | ||
var params = statement.params; | ||
var msgidParam = params[spec.msgid]; | ||
case 'MustacheStatement': | ||
case 'SubExpression': | ||
if (keywords.indexOf(statement.path.original) !== -1) { | ||
const spec = keywordSpec[statement.path.original]; | ||
const { params } = statement; | ||
const msgidParam = params[spec.msgid]; | ||
if (msgidParam) { // don't extract {{gettext}} without param | ||
var msgid = msgidParam.original; | ||
var contextIndex = spec.msgctxt; | ||
var context = null; // null context is *not* the same as empty context | ||
if (msgidParam) { // don't extract {{gettext}} without param | ||
const msgid = msgidParam.original; | ||
const contextIndex = spec.msgctxt; | ||
let context = null; // null context is *not* the same as empty context | ||
if (contextIndex !== undefined) { | ||
var contextParam = params[contextIndex]; | ||
if (contextIndex !== undefined) { | ||
const contextParam = params[contextIndex]; | ||
if (!contextParam) { | ||
if (!contextParam) { | ||
// throw an error if there's supposed to be a context but not enough | ||
// parameters were passed to the handlebars helper | ||
throw new Error('No context specified for msgid "' + msgid + '"'); | ||
} | ||
throw new Error(`No context specified for msgid "${msgid}"`); | ||
} | ||
if (contextParam.type !== 'StringLiteral') { | ||
throw new Error('Context must be a string literal for msgid "' + msgid + '"'); | ||
if (contextParam.type !== 'StringLiteral') { | ||
throw new Error(`Context must be a string literal for msgid "${msgid}"`); | ||
} | ||
context = contextParam.original; | ||
} | ||
context = contextParam.original; | ||
} | ||
const key = Parser.messageToKey(msgid, context); | ||
var key = Parser.messageToKey(msgid, context); | ||
msgs[key] = msgs[key] || {line: []}; | ||
msgs[key] = msgs[key] || { line: [] }; | ||
// make sure plural forms match | ||
var pluralIndex = spec.msgid_plural; | ||
if (pluralIndex !== undefined) { | ||
var pluralParam = params[pluralIndex]; | ||
// make sure plural forms match | ||
const pluralIndex = spec.msgid_plural; | ||
if (!pluralParam) { | ||
throw new Error('No plural specified for msgid "' + msgid + '"'); | ||
} | ||
if (pluralIndex !== undefined) { | ||
const pluralParam = params[pluralIndex]; | ||
if (pluralParam.type !== 'StringLiteral') { | ||
throw new Error('Plural must be a string literal for msgid ' + msgid); | ||
} | ||
if (!pluralParam) { | ||
throw new Error(`No plural specified for msgid "${msgid}"`); | ||
} | ||
var plural = pluralParam.original; | ||
var existingPlural = msgs[key].msgid_plural; | ||
if (plural && existingPlural && existingPlural !== plural) { | ||
throw new Error('Incompatible plural definitions for msgid "' + msgid + | ||
'" ("' + msgs[key].msgid_plural + '" and "' + plural + '")'); | ||
if (pluralParam.type !== 'StringLiteral') { | ||
throw new Error(`Plural must be a string literal for msgid "${msgid}"`); | ||
} | ||
const plural = pluralParam.original; | ||
const existingPlural = msgs[key].msgid_plural; | ||
if (plural && existingPlural && existingPlural !== plural) { | ||
throw new Error(`Incompatible plural definitions for msgid "${msgid}" ("${msgs[key].msgid_plural}" and "${plural}")`); | ||
} | ||
} | ||
} | ||
msgs[key].line.push(statement.loc.start.line); | ||
msgs[key].line.push(statement.loc.start.line); | ||
Object.keys(spec).forEach(function(prop) { | ||
var param = params[spec[prop]]; | ||
Object.keys(spec).forEach(prop => { | ||
const param = params[spec[prop]]; | ||
if (param && param.type === 'StringLiteral') { | ||
msgs[key][prop] = params[spec[prop]].original; | ||
} | ||
}); | ||
if (param && param.type === 'StringLiteral') { | ||
msgs[key][prop] = params[spec[prop]].original; | ||
} | ||
}); | ||
// maintain backwards compatibility with plural output | ||
msgs[key].plural = msgs[key].msgid_plural; | ||
// maintain backwards compatibility with plural output | ||
msgs[key].plural = msgs[key].msgid_plural; | ||
} | ||
} | ||
} | ||
break; | ||
case 'BlockStatement': | ||
if (statement.program) { | ||
statement.program.body.reduce(isMsg, msgs); | ||
} | ||
break; | ||
case 'BlockStatement': | ||
if (statement.program) { | ||
statement.program.body.reduce(isMsg, msgs); | ||
} | ||
if (statement.inverse) { | ||
statement.inverse.body.reduce(isMsg, msgs); | ||
} | ||
if (statement.inverse) { | ||
statement.inverse.body.reduce(isMsg, msgs); | ||
} | ||
break; | ||
break; | ||
} | ||
@@ -190,5 +190,6 @@ | ||
if (statement.hash) { | ||
statement.hash.pairs.reduce(function (msgs, pair) { | ||
return isMsg(msgs, pair.value); | ||
}, msgs); | ||
statement.hash.pairs.reduce( | ||
(msgs, pair) => isMsg(msgs, pair.value), | ||
msgs | ||
); | ||
} | ||
@@ -199,3 +200,2 @@ | ||
return tree.body.reduce(isMsg, {}); | ||
@@ -202,0 +202,0 @@ }; |
{ | ||
"name": "gettext-handlebars", | ||
"version": "0.7.0", | ||
"version": "1.0.0", | ||
"description": "Extract translatable strings from Handlebars templates", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"engines": { | ||
"node": "^10.15.1" | ||
}, | ||
"scripts": { | ||
@@ -35,8 +38,13 @@ "lint": "eslint index.js test", | ||
"devDependencies": { | ||
"eslint": "3.13.0", | ||
"mocha": "3.2.0" | ||
"eslint": "^5.14.1", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"mocha": "^6.0.2" | ||
}, | ||
"dependencies": { | ||
"handlebars": "4.0.6" | ||
"handlebars": "^4.1.0" | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9573
175
0
7
6
+ Addedhandlebars@4.7.8(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedneo-async@2.6.2(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addeduglify-js@3.17.4(transitive)
+ Addedwordwrap@1.0.0(transitive)
- Removedalign-text@0.1.4(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedasync@1.5.2(transitive)
- Removedcamelcase@1.2.1(transitive)
- Removedcenter-align@0.1.3(transitive)
- Removedcliui@2.1.0(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removedhandlebars@4.0.6(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.4(transitive)
- Removedlongest@1.0.1(transitive)
- Removedminimist@0.0.10(transitive)
- Removedoptimist@0.6.1(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedright-align@0.1.3(transitive)
- Removedsource-map@0.4.40.5.7(transitive)
- Removeduglify-js@2.8.29(transitive)
- Removeduglify-to-browserify@1.0.2(transitive)
- Removedwindow-size@0.1.0(transitive)
- Removedwordwrap@0.0.20.0.3(transitive)
- Removedyargs@3.10.0(transitive)
Updatedhandlebars@^4.1.0