no-path-concat.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @fileoverview Disallow string concatenation when using __dirname and __filename
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. deprecated: true,
  12. replacedBy: [],
  13. type: "suggestion",
  14. docs: {
  15. description: "disallow string concatenation with `__dirname` and `__filename`",
  16. category: "Node.js and CommonJS",
  17. recommended: false,
  18. url: "https://eslint.org/docs/rules/no-path-concat"
  19. },
  20. schema: [],
  21. messages: {
  22. usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths."
  23. }
  24. },
  25. create(context) {
  26. const MATCHER = /^__(?:dir|file)name$/u;
  27. //--------------------------------------------------------------------------
  28. // Public
  29. //--------------------------------------------------------------------------
  30. return {
  31. BinaryExpression(node) {
  32. const left = node.left,
  33. right = node.right;
  34. if (node.operator === "+" &&
  35. ((left.type === "Identifier" && MATCHER.test(left.name)) ||
  36. (right.type === "Identifier" && MATCHER.test(right.name)))
  37. ) {
  38. context.report({
  39. node,
  40. messageId: "usePathFunctions"
  41. });
  42. }
  43. }
  44. };
  45. }
  46. };