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