no-zero-fractions.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const {isParenthesized} = require('eslint-utils');
  3. const needsSemicolon = require('./utils/needs-semicolon.js');
  4. const {isNumber, isDecimalInteger} = require('./utils/numeric.js');
  5. const toLocation = require('./utils/to-location.js');
  6. const {fixSpaceAroundKeyword} = require('./fix/index.js');
  7. const MESSAGE_ZERO_FRACTION = 'zero-fraction';
  8. const MESSAGE_DANGLING_DOT = 'dangling-dot';
  9. const messages = {
  10. [MESSAGE_ZERO_FRACTION]: 'Don\'t use a zero fraction in the number.',
  11. [MESSAGE_DANGLING_DOT]: 'Don\'t use a dangling dot in the number.',
  12. };
  13. /** @param {import('eslint').Rule.RuleContext} context */
  14. const create = context => ({
  15. Literal: node => {
  16. if (!isNumber(node)) {
  17. return;
  18. }
  19. // Legacy octal number `0777` and prefixed number `0o1234` cannot have a dot.
  20. const {raw} = node;
  21. const match = raw.match(/^(?<before>[\d_]*)(?<dotAndFractions>\.[\d_]*)(?<after>.*)$/);
  22. if (!match) {
  23. return;
  24. }
  25. const {before, dotAndFractions, after} = match.groups;
  26. const fixedDotAndFractions = dotAndFractions.replace(/[.0_]+$/g, '');
  27. const formatted = ((before + fixedDotAndFractions) || '0') + after;
  28. if (formatted === raw) {
  29. return;
  30. }
  31. const isDanglingDot = dotAndFractions === '.';
  32. // End of fractions
  33. const end = node.range[0] + before.length + dotAndFractions.length;
  34. const start = end - (raw.length - formatted.length);
  35. const sourceCode = context.getSourceCode();
  36. return {
  37. loc: toLocation([start, end], sourceCode),
  38. messageId: isDanglingDot ? MESSAGE_DANGLING_DOT : MESSAGE_ZERO_FRACTION,
  39. * fix(fixer) {
  40. let fixed = formatted;
  41. if (
  42. node.parent.type === 'MemberExpression'
  43. && node.parent.object === node
  44. && isDecimalInteger(formatted)
  45. && !isParenthesized(node, sourceCode)
  46. ) {
  47. fixed = `(${fixed})`;
  48. if (needsSemicolon(sourceCode.getTokenBefore(node), sourceCode, fixed)) {
  49. fixed = `;${fixed}`;
  50. }
  51. }
  52. yield fixer.replaceText(node, fixed);
  53. yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
  54. },
  55. };
  56. },
  57. });
  58. /** @type {import('eslint').Rule.RuleModule} */
  59. module.exports = {
  60. create,
  61. meta: {
  62. type: 'suggestion',
  63. docs: {
  64. description: 'Disallow number literals with zero fractions or dangling dots.',
  65. },
  66. fixable: 'code',
  67. messages,
  68. },
  69. };