3 const isValidTranslatorNode = (node) => {
4 return node.callee && node.callee.type === 'Identifier' && node.callee.name === 'c' && node.arguments.length === 1;
12 description: 'Forbid template literals in translator contexts.',
13 url: 'https://ttag.js.org/docs/context.html',
16 create: (context) => {
18 CallExpression: function (node) {
19 if (!isValidTranslatorNode(node)) {
23 const argument = node.arguments[0];
24 const argType = argument.type;
26 if (argType === 'TemplateLiteral') {
29 message: "Don't use template literals in translator contexts.",
31 // 1. Check that we have no expressions in the template literal
32 const isFixable = argument.quasis.length === 1 && argument.expressions.length === 0;
38 // 2. Check that we can safely fix quotes
39 const text = context.sourceCode.getText(argument);
43 if (text.indexOf(quote) !== -1) {
47 if (text.indexOf(quote) !== -1) {
51 // 3. Replace backticks with quotes
52 const innerText = text.slice(1, -1);
53 const result = `${quote}${innerText}${quote}`;
55 return [fixer.replaceText(argument, result)];