no-path-concat.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. docs: {
  12. description: "disallow string concatenation with `__dirname` and `__filename`",
  13. category: "Node.js and CommonJS",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/no-path-concat"
  16. },
  17. schema: []
  18. },
  19. create(context) {
  20. const MATCHER = /^__(?:dir|file)name$/;
  21. //--------------------------------------------------------------------------
  22. // Public
  23. //--------------------------------------------------------------------------
  24. return {
  25. BinaryExpression(node) {
  26. const left = node.left,
  27. right = node.right;
  28. if (node.operator === "+" &&
  29. ((left.type === "Identifier" && MATCHER.test(left.name)) ||
  30. (right.type === "Identifier" && MATCHER.test(right.name)))
  31. ) {
  32. context.report({ node, message: "Use path.join() or path.resolve() instead of + to create paths." });
  33. }
  34. }
  35. };
  36. }
  37. };