Skip to content

Commit

Permalink
Merge pull request #88 from ShridharGoel/38989
Browse files Browse the repository at this point in the history
Add check to use periods at the end of error messages
  • Loading branch information
grgia authored Apr 9, 2024
2 parents 530b87d + e643c95 commit 1d697ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions eslint-plugin-expensify/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ module.exports = {
MUST_USE_VARIABLE_FOR_ASSIGNMENT: '{{key}} must be assigned as a variable instead of direct assignment.',
NO_DEFAULT_PROPS: 'defaultProps should not be used in function components. Use default Arguments instead.',
AVOID_ANONYMOUS_FUNCTIONS: 'Prefer named functions.',
USE_PERIODS_ERROR_MESSAGES: 'Use periods at the end of error messages.',
},
};
38 changes: 38 additions & 0 deletions eslint-plugin-expensify/tests/use-periods-error-messages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../use-periods-for-error-messages');
const message = require('../CONST').MESSAGE.USE_PERIODS_ERROR_MESSAGES;

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

const goodExample = `
error: {
testMessage: 'This is a test message.'
}
`;

const badExample = `
error: {
testMessage: 'This is a test message'
}
`;

ruleTester.run('use-periods-for-error-messages', rule, {
valid: [
{
code: goodExample,
},
],
invalid: [
{
code: badExample,
errors: [{
message,
}],
},
],
});
30 changes: 30 additions & 0 deletions eslint-plugin-expensify/use-periods-for-error-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require('lodash/get');
const message = require('./CONST').MESSAGE.USE_PERIODS_ERROR_MESSAGES;

module.exports = {
create(context) {
return {
Property(node) {
if (!node.key || node.key.name !== 'error' || !node.value || node.value.type !== 'ObjectExpression') {
return;
}
node.value.properties.forEach((property) => {
if (!property.value || property.value.type !== 'Literal' || typeof property.value.value !== 'string') {
return;
}
const errorMessage = property.value.value;
if (!errorMessage.endsWith('.')) {
context.report({
node: property,
message,
fix: function (fixer) {
const fixedMessage = `${errorMessage}.`;
return fixer.replaceText(property.value, `'${fixedMessage}'`);
}
});
}
});
},
};
},
};

0 comments on commit 1d697ad

Please sign in to comment.