number-literal-case.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const {checkVueTemplate} = require('./utils/rule.js');
  3. const {isNumberLiteral, isBigIntLiteral} = require('./ast/index.js');
  4. const MESSAGE_ID = 'number-literal-case';
  5. const messages = {
  6. [MESSAGE_ID]: 'Invalid number literal casing.',
  7. };
  8. const fix = raw => {
  9. let fixed = raw.toLowerCase();
  10. if (fixed.startsWith('0x')) {
  11. fixed = '0x' + fixed.slice(2).toUpperCase();
  12. }
  13. return fixed;
  14. };
  15. /** @param {import('eslint').Rule.RuleContext} context */
  16. const create = () => ({
  17. Literal(node) {
  18. const {raw} = node;
  19. let fixed = raw;
  20. if (isNumberLiteral(node)) {
  21. fixed = fix(raw);
  22. } else if (isBigIntLiteral(node)) {
  23. fixed = fix(raw.slice(0, -1)) + 'n';
  24. }
  25. if (raw !== fixed) {
  26. return {
  27. node,
  28. messageId: MESSAGE_ID,
  29. fix: fixer => fixer.replaceText(node, fixed),
  30. };
  31. }
  32. },
  33. });
  34. /** @type {import('eslint').Rule.RuleModule} */
  35. module.exports = {
  36. create: checkVueTemplate(create),
  37. meta: {
  38. type: 'suggestion',
  39. docs: {
  40. description: 'Enforce proper case for numeric literals.',
  41. },
  42. fixable: 'code',
  43. messages,
  44. },
  45. };