no-buffer-constructor.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @fileoverview disallow use of the Buffer() constructor
  3. * @author Teddy Katz
  4. * @deprecated in ESLint v7.0.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. deprecated: true,
  14. replacedBy: [],
  15. type: "problem",
  16. docs: {
  17. description: "Disallow use of the `Buffer()` constructor",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-buffer-constructor"
  20. },
  21. schema: [],
  22. messages: {
  23. deprecated: "{{expr}} is deprecated. Use Buffer.from(), Buffer.alloc(), or Buffer.allocUnsafe() instead."
  24. }
  25. },
  26. create(context) {
  27. //----------------------------------------------------------------------
  28. // Public
  29. //----------------------------------------------------------------------
  30. return {
  31. "CallExpression[callee.name='Buffer'], NewExpression[callee.name='Buffer']"(node) {
  32. context.report({
  33. node,
  34. messageId: "deprecated",
  35. data: { expr: node.type === "CallExpression" ? "Buffer()" : "new Buffer()" }
  36. });
  37. }
  38. };
  39. }
  40. };