no-path-concat.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @fileoverview Disallow string concatenation when using __dirname and __filename
  3. * @author Nicholas C. Zakas
  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: "suggestion",
  16. docs: {
  17. description: "Disallow string concatenation with `__dirname` and `__filename`",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-path-concat"
  20. },
  21. schema: [],
  22. messages: {
  23. usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths."
  24. }
  25. },
  26. create(context) {
  27. const MATCHER = /^__(?:dir|file)name$/u;
  28. //--------------------------------------------------------------------------
  29. // Public
  30. //--------------------------------------------------------------------------
  31. return {
  32. BinaryExpression(node) {
  33. const left = node.left,
  34. right = node.right;
  35. if (node.operator === "+" &&
  36. ((left.type === "Identifier" && MATCHER.test(left.name)) ||
  37. (right.type === "Identifier" && MATCHER.test(right.name)))
  38. ) {
  39. context.report({
  40. node,
  41. messageId: "usePathFunctions"
  42. });
  43. }
  44. }
  45. };
  46. }
  47. };