Skip to content

Commit

Permalink
Fix broken lint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Oct 3, 2024
1 parent 2da7c59 commit fd801b7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 30 deletions.
54 changes: 42 additions & 12 deletions eslint-plugin-expensify/tests/use-periods-error-messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,39 @@ const ruleTester = new RuleTester({
});

const goodExampleSingleSentence = `
error: {
testMessage: 'This is a test message'
}
const err = new Error('This is a test message');
`;

const goodExampleMultipleSentences = `
error: {
testMessage: 'This is a test message. Last period is mandatory.'
}
const err = new Error('This is a test message. Last period is mandatory.');
`;

const goodExampleSingleSentenceWithVar = `
const errorMessage = 'This is a test message';
const err = new Error(errorMessage);
`;

const goodExampleMultipleSentencesWithVar = `
const errorMessage = 'This is a test message. Last period is mandatory.';
const err = new Error(errorMessage);
`;

const badExampleSingleSentence = `
error: {
testMessage: 'This is a test message.'
}
const err = new Error('This is a test message.');
`;

const badExampleSingleSentenceWithVar = `
const errorMessage = 'This is a test message.';
const err = new Error(errorMessage);
`;

const badExampleMultipleSentences = `
error: {
testMessage: 'This is a test message. Last period is mandatory'
}
const err = new Error('This is a test message. Last period is mandatory');
`;

const badExampleMultipleSentencesWithVar = `
const errorMessage = 'This is a test message. Last period is mandatory';
const err = new Error(errorMessage);
`;

ruleTester.run('use-periods-for-error-messages', rule, {
Expand All @@ -41,6 +53,12 @@ ruleTester.run('use-periods-for-error-messages', rule, {
{
code: goodExampleMultipleSentences,
},
{
code: goodExampleSingleSentenceWithVar,
},
{
code: goodExampleMultipleSentencesWithVar,
},
],
invalid: [
{
Expand All @@ -49,11 +67,23 @@ ruleTester.run('use-periods-for-error-messages', rule, {
message,
}],
},
{
code: badExampleSingleSentenceWithVar,
errors: [{
message,
}],
},
{
code: badExampleMultipleSentences,
errors: [{
message,
}],
},
{
code: badExampleMultipleSentencesWithVar,
errors: [{
message,
}],
},
],
});
51 changes: 33 additions & 18 deletions eslint-plugin-expensify/use-periods-for-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,46 @@ module.exports = {
fixable: 'code',
},
create(context) {
const variableDeclarators = new Map();
return {
Property(node) {
if (!node.key || node.key.name !== 'error' || !node.value || node.value.type !== 'ObjectExpression') {
VariableDeclarator(node) {
variableDeclarators.set(node.id.name, node);
},
NewExpression(node) {
if (!node.callee || node.callee.name !== 'Error' || node.arguments.length === 0) {
return;
}
node.value.properties.forEach((property) => {
if (!property.value || property.value.type !== 'Literal' || typeof property.value.value !== 'string') {
const errArg = node.arguments[0];
let errorMessage = '';
if (errArg.type === 'Literal') {
errorMessage = errArg.value;
} else if (variableDeclarators.has(errArg.name)) {
const variableDeclarator = variableDeclarators.get(errArg.name);
if (variableDeclarator.init.type !== 'Literal') {
return;
}
const errorMessage = property.value.value;
errorMessage = variableDeclarator.init.value;
} else {
return;
}

// Only enforce period rule if more than one sentence
const sentenceCount = _.filter(errorMessage.split('.'), sentence => sentence.trim().length > 0).length;
// Only enforce period rule if more than one sentence
const sentenceCount = _.filter(errorMessage.split('.'), sentence => sentence.trim().length > 0).length;

if (sentenceCount > 1 && !errorMessage.endsWith('.')) {
context.report({
node: property,
message,
fix(fixer) {
const fixedMessage = `${errorMessage}.`;
return fixer.replaceText(property.value, `'${fixedMessage}'`);
},
});
}
});
const doesViolateRule = (sentenceCount > 1 && !errorMessage.endsWith('.')) || (sentenceCount === 1 && errorMessage.endsWith('.'));
if (doesViolateRule) {
context.report({
node,
message,
fix(fixer) {
if (errorMessage.type !== 'Literal') {
return;
}
const fixedMessage = `${errorMessage}.`;
return fixer.replaceText(node.arguments[0], `'${fixedMessage}'`);
},
});
}
},
};
},
Expand Down

0 comments on commit fd801b7

Please sign in to comment.