prefer-json-parse-buffer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. const {findVariable, getStaticValue, getPropertyName} = require('@eslint-community/eslint-utils');
  3. const {methodCallSelector} = require('./selectors/index.js');
  4. const {removeArgument} = require('./fix/index.js');
  5. const MESSAGE_ID = 'prefer-json-parse-buffer';
  6. const messages = {
  7. [MESSAGE_ID]: 'Prefer reading the JSON file as a buffer.',
  8. };
  9. const jsonParseArgumentSelector = [
  10. methodCallSelector({
  11. object: 'JSON',
  12. method: 'parse',
  13. argumentsLength: 1,
  14. }),
  15. ' > .arguments:first-child',
  16. ].join('');
  17. const getAwaitExpressionArgument = node => {
  18. while (node.type === 'AwaitExpression') {
  19. node = node.argument;
  20. }
  21. return node;
  22. };
  23. function getIdentifierDeclaration(node, scope) {
  24. if (!node) {
  25. return;
  26. }
  27. node = getAwaitExpressionArgument(node);
  28. if (!node || node.type !== 'Identifier') {
  29. return node;
  30. }
  31. const variable = findVariable(scope, node);
  32. if (!variable) {
  33. return;
  34. }
  35. const {identifiers, references} = variable;
  36. if (identifiers.length !== 1 || references.length !== 2) {
  37. return;
  38. }
  39. const [identifier] = identifiers;
  40. if (
  41. identifier.parent.type !== 'VariableDeclarator'
  42. || identifier.parent.id !== identifier
  43. ) {
  44. return;
  45. }
  46. return getIdentifierDeclaration(identifier.parent.init, variable.scope);
  47. }
  48. const isUtf8EncodingStringNode = (node, scope) =>
  49. isUtf8EncodingString(getStaticValue(node, scope)?.value);
  50. const isUtf8EncodingString = value => {
  51. if (typeof value !== 'string') {
  52. return false;
  53. }
  54. value = value.toLowerCase();
  55. // eslint-disable-next-line unicorn/text-encoding-identifier-case
  56. return value === 'utf8' || value === 'utf-8';
  57. };
  58. function isUtf8Encoding(node, scope) {
  59. if (
  60. node.type === 'ObjectExpression'
  61. && node.properties.length === 1
  62. && node.properties[0].type === 'Property'
  63. && getPropertyName(node.properties[0], scope) === 'encoding'
  64. && isUtf8EncodingStringNode(node.properties[0].value, scope)
  65. ) {
  66. return true;
  67. }
  68. if (isUtf8EncodingStringNode(node, scope)) {
  69. return true;
  70. }
  71. const staticValue = getStaticValue(node, scope);
  72. if (!staticValue) {
  73. return false;
  74. }
  75. const {value} = staticValue;
  76. if (
  77. typeof value === 'object'
  78. && Object.keys(value).length === 1
  79. && isUtf8EncodingString(value.encoding)
  80. ) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. /** @param {import('eslint').Rule.RuleContext} context */
  86. const create = context => ({
  87. [jsonParseArgumentSelector](node) {
  88. const scope = context.getScope();
  89. node = getIdentifierDeclaration(node, scope);
  90. if (
  91. !(
  92. node
  93. && node.type === 'CallExpression'
  94. && !node.optional
  95. && node.arguments.length === 2
  96. && !node.arguments.some(node => node.type === 'SpreadElement')
  97. && node.callee.type === 'MemberExpression'
  98. && !node.callee.optional
  99. )
  100. ) {
  101. return;
  102. }
  103. const method = getPropertyName(node.callee, scope);
  104. if (method !== 'readFile' && method !== 'readFileSync') {
  105. return;
  106. }
  107. const [, charsetNode] = node.arguments;
  108. if (!isUtf8Encoding(charsetNode, scope)) {
  109. return;
  110. }
  111. return {
  112. node: charsetNode,
  113. messageId: MESSAGE_ID,
  114. fix: fixer => removeArgument(fixer, charsetNode, context.getSourceCode()),
  115. };
  116. },
  117. });
  118. /** @type {import('eslint').Rule.RuleModule} */
  119. module.exports = {
  120. create,
  121. meta: {
  122. type: 'suggestion',
  123. docs: {
  124. description: 'Prefer reading a JSON file as a buffer.',
  125. },
  126. fixable: 'code',
  127. messages,
  128. },
  129. };