Socket
Socket
Sign inDemoInstall

gettext-handlebars

Package Overview
Dependencies
6
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.7.0 to 1.0.0

.eslintrc.js

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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc