no-zero-fractions.js 2.3 KB

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