blob: fc1f894f878988f0f995aac46034e3d071ac2e07 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
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: {
Tim van der Lippe16aca392020-11-13 11:37:13 +000013 deprecated: true,
14
15 replacedBy: [],
16
Yang Guo4fd355c2019-09-19 10:59:03 +020017 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
Tim van der Lippe16aca392020-11-13 11:37:13 +000026 schema: [],
27
28 messages: {
29 usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths."
30 }
Yang Guo4fd355c2019-09-19 10:59:03 +020031 },
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
Tim van der Lippe16aca392020-11-13 11:37:13 +000053 context.report({
54 node,
55 messageId: "usePathFunctions"
56 });
Yang Guo4fd355c2019-09-19 10:59:03 +020057 }
58 }
59
60 };
61
62 }
63};