Skip to content

Commit 1d697ad

Browse files
authored
Merge pull request #88 from ShridharGoel/38989
Add check to use periods at the end of error messages
2 parents 530b87d + e643c95 commit 1d697ad

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

eslint-plugin-expensify/CONST.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ module.exports = {
2828
MUST_USE_VARIABLE_FOR_ASSIGNMENT: '{{key}} must be assigned as a variable instead of direct assignment.',
2929
NO_DEFAULT_PROPS: 'defaultProps should not be used in function components. Use default Arguments instead.',
3030
AVOID_ANONYMOUS_FUNCTIONS: 'Prefer named functions.',
31+
USE_PERIODS_ERROR_MESSAGES: 'Use periods at the end of error messages.',
3132
},
3233
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const RuleTester = require('eslint').RuleTester;
2+
const rule = require('../use-periods-for-error-messages');
3+
const message = require('../CONST').MESSAGE.USE_PERIODS_ERROR_MESSAGES;
4+
5+
const ruleTester = new RuleTester({
6+
parserOptions: {
7+
ecmaVersion: 6,
8+
sourceType: 'module',
9+
},
10+
});
11+
12+
const goodExample = `
13+
error: {
14+
testMessage: 'This is a test message.'
15+
}
16+
`;
17+
18+
const badExample = `
19+
error: {
20+
testMessage: 'This is a test message'
21+
}
22+
`;
23+
24+
ruleTester.run('use-periods-for-error-messages', rule, {
25+
valid: [
26+
{
27+
code: goodExample,
28+
},
29+
],
30+
invalid: [
31+
{
32+
code: badExample,
33+
errors: [{
34+
message,
35+
}],
36+
},
37+
],
38+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require('lodash/get');
2+
const message = require('./CONST').MESSAGE.USE_PERIODS_ERROR_MESSAGES;
3+
4+
module.exports = {
5+
create(context) {
6+
return {
7+
Property(node) {
8+
if (!node.key || node.key.name !== 'error' || !node.value || node.value.type !== 'ObjectExpression') {
9+
return;
10+
}
11+
node.value.properties.forEach((property) => {
12+
if (!property.value || property.value.type !== 'Literal' || typeof property.value.value !== 'string') {
13+
return;
14+
}
15+
const errorMessage = property.value.value;
16+
if (!errorMessage.endsWith('.')) {
17+
context.report({
18+
node: property,
19+
message,
20+
fix: function (fixer) {
21+
const fixedMessage = `${errorMessage}.`;
22+
return fixer.replaceText(property.value, `'${fixedMessage}'`);
23+
}
24+
});
25+
}
26+
});
27+
},
28+
};
29+
},
30+
};

0 commit comments

Comments
 (0)