prefer-json-parse-buffer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. const {findVariable, getStaticValue, getPropertyName} = require('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. const staticValue = getStaticValue(node, scope);
  50. return staticValue && isUtf8EncodingString(staticValue.value);
  51. };
  52. const isUtf8EncodingString = value => {
  53. if (typeof value !== 'string') {
  54. return false;
  55. }
  56. value = value.toLowerCase();
  57. // eslint-disable-next-line unicorn/text-encoding-identifier-case
  58. return value === 'utf8' || value === 'utf-8';
  59. };
  60. function isUtf8Encoding(node, scope) {
  61. if (
  62. node.type === 'ObjectExpression'
  63. && node.properties.length === 1
  64. && node.properties[0].type === 'Property'
  65. && getPropertyName(node.properties[0], scope) === 'encoding'
  66. && isUtf8EncodingStringNode(node.properties[0].value, scope)
  67. ) {
  68. return true;
  69. }
  70. if (isUtf8EncodingStringNode(node, scope)) {
  71. return true;
  72. }
  73. const staticValue = getStaticValue(node, scope);
  74. if (!staticValue) {
  75. return false;
  76. }
  77. const {value} = staticValue;
  78. if (
  79. typeof value === 'object'
  80. && Object.keys(value).length === 1
  81. && isUtf8EncodingString(value.encoding)
  82. ) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. /** @param {import('eslint').Rule.RuleContext} context */
  88. const create = context => ({
  89. [jsonParseArgumentSelector](node) {
  90. const scope = context.getScope();
  91. node = getIdentifierDeclaration(node, scope);
  92. if (
  93. !(
  94. node
  95. && node.type === 'CallExpression'
  96. && !node.optional
  97. && node.arguments.length === 2
  98. && !node.arguments.some(node => node.type === 'SpreadElement')
  99. && node.callee.type === 'MemberExpression'
  100. && !node.callee.optional
  101. )
  102. ) {
  103. return;
  104. }
  105. const method = getPropertyName(node.callee, scope);
  106. if (method !== 'readFile' && method !== 'readFileSync') {
  107. return;
  108. }
  109. const [, charsetNode] = node.arguments;
  110. if (!isUtf8Encoding(charsetNode, scope)) {
  111. return;
  112. }
  113. return {
  114. node: charsetNode,
  115. messageId: MESSAGE_ID,
  116. fix: fixer => removeArgument(fixer, charsetNode, context.getSourceCode()),
  117. };
  118. },
  119. });
  120. /** @type {import('eslint').Rule.RuleModule} */
  121. module.exports = {
  122. create,
  123. meta: {
  124. type: 'suggestion',
  125. docs: {
  126. description: 'Prefer reading a JSON file as a buffer.',
  127. },
  128. fixable: 'code',
  129. messages,
  130. },
  131. };