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

Allowed i18n object to be customized via i18nPrefix #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
84 changes: 43 additions & 41 deletions lib/rules/json-key-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ module.exports = {
type: ['string', 'object'],
},
},
i18nPrefix: {
type: ['string'],
},
},
type: 'object',
}],
},

create: function(context) {
let jsonBaseURIs = context.options[0] ? context.options[0].jsonBaseURIs : [];
let i18nPrefix = context.options[0] ? context.options[0].i18nPrefix : 'I18n';
let jsons = [];
const sourceCode = context.getSourceCode();

Expand Down Expand Up @@ -218,52 +222,50 @@ module.exports = {
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
const rules = {};
const expression = "CallExpression[callee.object.name='" + i18nPrefix + "']:matches([callee.property.name='t'],[callee.property.name='translate'])";
rules[expression] = function (node) {
let keys = [];
let errors = [];
const firstArg = node.arguments[0];

return {

"CallExpression[callee.object.name='I18n']:matches([callee.property.name='t'],[callee.property.name='translate'])": function(node) {
let keys = [];
let errors = [];
const firstArg = node.arguments[0];

const comments = [...sourceCode.getCommentsInside(node), ...sourceCode.getCommentsAfter(node), ...sourceCode.getCommentsAfter(node.parent)]

// Parse comments for templating keys valid values
const {
valuesDict: templatingValuesDict,
errors: templatingErrors,
} = parseComments(comments);
const comments = [...sourceCode.getCommentsInside(node), ...sourceCode.getCommentsAfter(node), ...sourceCode.getCommentsAfter(node.parent)]

if (templatingErrors.length) {
reportErrors(node, templatingErrors);
return
}

switch(firstArg.type) {
case 'Literal':
keys.push(firstArg.value)
break
case 'TemplateLiteral':
keys = [...keys, ...renderTemplatedKey(firstArg, templatingValuesDict)];
break
case 'ConditionalExpression':
if (firstArg.consequent.type === 'Literal')
keys.push(firstArg.consequent.value);
else if (firstArg.consequent.type === 'TemplateLiteral')
keys = [...keys, ...renderTemplatedKey(firstArg.consequent, templatingValuesDict)];
if (firstArg.alternate.type === 'Literal')
keys.push(firstArg.alternate.value);
else if (firstArg.alternate.type === 'TemplateLiteral')
keys = [...keys, ...renderTemplatedKey(firstArg.alternate, templatingValuesDict)];
}
// Parse comments for templating keys valid values
const {
valuesDict: templatingValuesDict,
errors: templatingErrors,
} = parseComments(comments);

keys.forEach((key) => {
errors = [...errors, ...checkKeyExists(key)];
});
if (templatingErrors.length) {
reportErrors(node, templatingErrors);
return
}

switch(firstArg.type) {
case 'Literal':
keys.push(firstArg.value)
break
case 'TemplateLiteral':
keys = [...keys, ...renderTemplatedKey(firstArg, templatingValuesDict)];
break
case 'ConditionalExpression':
if (firstArg.consequent.type === 'Literal')
keys.push(firstArg.consequent.value);
else if (firstArg.consequent.type === 'TemplateLiteral')
keys = [...keys, ...renderTemplatedKey(firstArg.consequent, templatingValuesDict)];
if (firstArg.alternate.type === 'Literal')
keys.push(firstArg.alternate.value);
else if (firstArg.alternate.type === 'TemplateLiteral')
keys = [...keys, ...renderTemplatedKey(firstArg.alternate, templatingValuesDict)];
}

reportErrors(firstArg, errors);
},
keys.forEach((key) => {
errors = [...errors, ...checkKeyExists(key)];
});

reportErrors(firstArg, errors);
};
return rules;
},
};