-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to use periods at the end of error messages
- Loading branch information
1 parent
72d77f0
commit c73be32
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
eslint-plugin-expensify/tests/use-periods-error-messages.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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 lastChar = errorMessage[errorMessage.length - 1]; | ||
const fixedMessage = lastChar === '.' ? errorMessage : `${errorMessage}.`; | ||
return fixer.replaceText(property.value, `'${fixedMessage}'`); | ||
} | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |