no-path-concat.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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('../types').Rule.RuleModule} */
  11. module.exports = {
  12. meta: {
  13. deprecated: {
  14. message: "Node.js rules were moved out of ESLint core.",
  15. url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules",
  16. deprecatedSince: "7.0.0",
  17. availableUntil: "11.0.0",
  18. replacedBy: [
  19. {
  20. message:
  21. "eslint-plugin-n now maintains deprecated Node.js-related rules.",
  22. plugin: {
  23. name: "eslint-plugin-n",
  24. url: "https://github.com/eslint-community/eslint-plugin-n",
  25. },
  26. rule: {
  27. name: "no-path-concat",
  28. url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-path-concat.md",
  29. },
  30. },
  31. ],
  32. },
  33. type: "suggestion",
  34. docs: {
  35. description:
  36. "Disallow string concatenation with `__dirname` and `__filename`",
  37. recommended: false,
  38. url: "https://eslint.org/docs/latest/rules/no-path-concat",
  39. },
  40. schema: [],
  41. messages: {
  42. usePathFunctions:
  43. "Use path.join() or path.resolve() instead of + to create paths.",
  44. },
  45. },
  46. create(context) {
  47. const MATCHER = /^__(?:dir|file)name$/u;
  48. //--------------------------------------------------------------------------
  49. // Public
  50. //--------------------------------------------------------------------------
  51. return {
  52. BinaryExpression(node) {
  53. const left = node.left,
  54. right = node.right;
  55. if (
  56. node.operator === "+" &&
  57. ((left.type === "Identifier" && MATCHER.test(left.name)) ||
  58. (right.type === "Identifier" &&
  59. MATCHER.test(right.name)))
  60. ) {
  61. context.report({
  62. node,
  63. messageId: "usePathFunctions",
  64. });
  65. }
  66. },
  67. };
  68. },
  69. };