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

build: Add pre-commit hooks, prettier, use node 8 #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 28 additions & 32 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
{
"globals": {
"console": false
},
"env": {
"browser": true,
"node": true,
"mocha": true,
},
"extends": "airbnb",
"rules": {
"arrow-parens": ["error", "always"],
"class-methods-use-this": "off",
"comma-dangle": ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'ignore',
}],
"func-names": "off",
"no-mixed-operators": "off",
"no-var": "off",
"no-console": "error",
"no-plusplus": "off",
"no-prototype-builtins": "off",
"no-underscore-dangle": "off",
"object-shorthand": "off",
"prefer-arrow-callback": "off",
"prefer-template": "off",
"space-before-function-paren": ["error", "never"],
"vars-on-top": "off",
}
"extends": ["airbnb", "plugin:prettier/recommended"],

"globals": {
"console": false
},

"env": {
"browser": true,
"node": true,
"mocha": true
},

"rules": {
"class-methods-use-this": "off",
"func-names": "off",
"no-mixed-operators": "off",
"no-var": "off",
"no-console": "error",
"no-plusplus": "off",
"no-prototype-builtins": "off",
"no-underscore-dangle": "off",
"object-shorthand": "off",
"prefer-template": "off",
"space-before-function-paren": ["error", "never"],
"vars-on-top": "off",

"prettier/prettier": "error"
}
}
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "es5",
"singleQuote": true
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- "6"
- "8"
script:
- yarn test-ci
cache: yarn
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

- None
- feat: Drop support for node 6
- build: Add prettier + precommit hooks

## 4.1.2
- feat: Update `gettext-parser` to 1.4.0 (#19)
Expand Down
258 changes: 134 additions & 124 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var utils = require('./utils');
var gettextParser = require('gettext-parser');
var fs = require('fs');
var path = require('path');
var utils = require('./utils');

var DEFAULT_FUNCTION_NAMES = {
gettext: ['msgid'],
Expand All @@ -21,7 +21,6 @@ var DEFAULT_HEADERS = {
'plural-forms': 'nplurals = 2; plural = (n !== 1);',
};


function getTranslatorComment(node) {
var comments = [];
(node.leadingComments || []).forEach(function(commentNode) {
Expand All @@ -40,155 +39,166 @@ function ensureDirectoryExistence(filePath) {
}

ensureDirectoryExistence(dirname);

fs.mkdirSync(dirname);
}


module.exports = function() {
var currentFileName;
var data;
var relocatedComments = {};

return { visitor: {

VariableDeclaration(nodePath) {
var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
return;
}
nodePath.node.declarations.forEach(function(declarator) {
var comment = getTranslatorComment(declarator);
if (!comment) {
var key = declarator.init.start + '|' + declarator.init.end;
relocatedComments[key] = translatorComment;
return {
visitor: {
VariableDeclaration(nodePath) {
var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
return;
}
nodePath.node.declarations.forEach(function(declarator) {
var comment = getTranslatorComment(declarator);
if (!comment) {
var key = declarator.init.start + '|' + declarator.init.end;
relocatedComments[key] = translatorComment;
}
});
},

CallExpression(nodePath, plugin) {
var functionNames =
(plugin.opts && plugin.opts.functionNames) || DEFAULT_FUNCTION_NAMES;
var fileName =
(plugin.opts && plugin.opts.fileName) || DEFAULT_FILE_NAME;
var headers = (plugin.opts && plugin.opts.headers) || DEFAULT_HEADERS;
var base = plugin.opts && plugin.opts.baseDirectory;
if (base) {
base = base.match(/^(.*?)\/*$/)[1] + '/';
}
});
},

CallExpression(nodePath, plugin) {
var functionNames = plugin.opts && plugin.opts.functionNames || DEFAULT_FUNCTION_NAMES;
var fileName = plugin.opts && plugin.opts.fileName || DEFAULT_FILE_NAME;
var headers = plugin.opts && plugin.opts.headers || DEFAULT_HEADERS;
var base = plugin.opts && plugin.opts.baseDirectory;
if (base) {
base = base.match(/^(.*?)\/*$/)[1] + '/';
}

if (typeof fileName === 'function') {
fileName = fileName(this.file);
}

if (!fileName) {
return;
}

if (fileName !== currentFileName) {
currentFileName = fileName;
if (fs.existsSync(fileName)) {
const fileContents = fs.readFileSync(fileName, 'utf8');
data = gettextParser.po.parse(fileContents);
data.headers = Object.assign({}, data.headers || {}, headers);
data.translations.context = data.translations.context || {};
} else {
data = {
charset: 'UTF-8',
headers: headers,
translations: { context: {} },
};
if (typeof fileName === 'function') {
fileName = fileName(this.file);
}

if (!fileName) {
return;
}

if (fileName !== currentFileName) {
currentFileName = fileName;
if (fs.existsSync(fileName)) {
const fileContents = fs.readFileSync(fileName, 'utf8');
data = gettextParser.po.parse(fileContents);
data.headers = Object.assign({}, data.headers || {}, headers);
data.translations.context = data.translations.context || {};
} else {
data = {
charset: 'UTF-8',
headers: headers,
translations: { context: {} },
};
}

headers['plural-forms'] =
headers['plural-forms'] || DEFAULT_HEADERS['plural-forms'];
headers['content-type'] =
headers['content-type'] || DEFAULT_HEADERS['content-type'];
}

headers['plural-forms'] = headers['plural-forms']
|| DEFAULT_HEADERS['plural-forms'];
headers['content-type'] = headers['content-type']
|| DEFAULT_HEADERS['content-type'];
}

var defaultContext = data.translations.context;
var nplurals = /nplurals ?= ?(\d)/.exec(headers['plural-forms'])[1];

const callee = nodePath.node.callee;

if (functionNames.hasOwnProperty(callee.name) ||
callee.property &&
functionNames.hasOwnProperty(callee.property.name)) {
var functionName = functionNames[callee.name]
|| functionNames[callee.property.name];
var translate = {};

var args = nodePath.get('arguments');
for (var i = 0, l = args.length; i < l; i++) {
var name = functionName[i];

if (name && name !== 'count' && name !== 'domain') {
var arg = args[i].evaluate();
var value = arg.value;
if (arg.confident && value) {
if (plugin.opts.stripTemplateLiteralIndent) {
value = utils.stripIndent(value);
var defaultContext = data.translations.context;
var nplurals = /nplurals ?= ?(\d)/.exec(headers['plural-forms'])[1];

const { callee } = nodePath.node;

if (
functionNames.hasOwnProperty(callee.name) ||
(callee.property &&
functionNames.hasOwnProperty(callee.property.name))
) {
var functionName =
functionNames[callee.name] || functionNames[callee.property.name];
var translate = {};

var args = nodePath.get('arguments');
for (var i = 0, l = args.length; i < l; i++) {
var name = functionName[i];

if (name && name !== 'count' && name !== 'domain') {
var arg = args[i].evaluate();
var { value } = arg;
if (arg.confident && value) {
if (plugin.opts.stripTemplateLiteralIndent) {
value = utils.stripIndent(value);
}
translate[name] = value;
}
translate[name] = value;
}

if (name === 'msgid_plural') {
translate.msgstr = [];
for (var p = 0; p < nplurals; p++) {
translate.msgstr[p] = '';
if (name === 'msgid_plural') {
translate.msgstr = [];
for (var p = 0; p < nplurals; p++) {
translate.msgstr[p] = '';
}
}
}
}
}

var fn = this.file.opts.filename;
if (base && fn && fn.substr(0, base.length) === base) {
fn = fn.substr(base.length);
}
var fn = this.file.opts.filename;
if (base && fn && fn.substr(0, base.length) === base) {
fn = fn.substr(base.length);
}

translate.comments = {
reference: fn + ':' + nodePath.node.loc.start.line,
};
translate.comments = {
reference: fn + ':' + nodePath.node.loc.start.line,
};

var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
translatorComment = getTranslatorComment(nodePath.parentPath);
var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
translatorComment = relocatedComments[
nodePath.node.start + '|' + nodePath.node.end];
translatorComment = getTranslatorComment(nodePath.parentPath);
if (!translatorComment) {
translatorComment =
relocatedComments[
nodePath.node.start + '|' + nodePath.node.end
];
}
}
}

if (translatorComment) {
translate.comments.translator = translatorComment;
}
if (translatorComment) {
translate.comments.translator = translatorComment;
}

var context = defaultContext;
var msgctxt = translate.msgctxt;
if (msgctxt) {
data.translations[msgctxt] = data.translations[msgctxt] || {};
context = data.translations[msgctxt];
}
var context = defaultContext;
var { msgctxt } = translate;
if (msgctxt) {
data.translations[msgctxt] = data.translations[msgctxt] || {};
context = data.translations[msgctxt];
}

if (typeof context[translate.msgid] !== 'undefined') {
// If we already have this translation append the new file reference
// so we know about all the places it is used.
var newRef = translate.comments.reference;
var currentRef = context[translate.msgid].comments.reference;
var refs = currentRef.split('\n');
if (refs.indexOf(newRef) === -1) {
refs.push(newRef);
context[translate.msgid].comments.reference = refs.sort().join('\n');
if (typeof context[translate.msgid] !== 'undefined') {
// If we already have this translation append the new file reference
// so we know about all the places it is used.
var newRef = translate.comments.reference;
var currentRef = context[translate.msgid].comments.reference;
var refs = currentRef.split('\n');
if (refs.indexOf(newRef) === -1) {
refs.push(newRef);
context[translate.msgid].comments.reference = refs
.sort()
.join('\n');
}
} else if (typeof translate.msgid !== 'undefined') {
// Do not add translation if msgid is undefined.
context[translate.msgid] = translate;
}
} else if (typeof translate.msgid !== 'undefined') {
// Do not add translation if msgid is undefined.
context[translate.msgid] = translate;
}

// Sort by file reference to make output idempotent for the same input.
if (data.translations && data.translations.context) {
data.translations.context = utils.sortObjectKeysByRef(data.translations.context);
// Sort by file reference to make output idempotent for the same input.
if (data.translations && data.translations.context) {
data.translations.context = utils.sortObjectKeysByRef(
data.translations.context
);
}
ensureDirectoryExistence(fileName);
fs.writeFileSync(fileName, gettextParser.po.compile(data));
}
ensureDirectoryExistence(fileName);
fs.writeFileSync(fileName, gettextParser.po.compile(data));
}
},
},
} };
};
};
Loading