blob: 8d570a3778f3d318cbd24576b619b0b49248fe49 [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
Tim van der Lippe0fb47802021-11-08 16:23:10 +00004 * @deprecated in ESLint v7.0.0
Yang Guo4fd355c2019-09-19 10:59:03 +02005 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010012/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +020013module.exports = {
14 meta: {
Tim van der Lippe16aca392020-11-13 11:37:13 +000015 deprecated: true,
16
17 replacedBy: [],
18
Yang Guo4fd355c2019-09-19 10:59:03 +020019 type: "suggestion",
20
21 docs: {
22 description: "disallow string concatenation with `__dirname` and `__filename`",
Yang Guo4fd355c2019-09-19 10:59:03 +020023 recommended: false,
24 url: "https://eslint.org/docs/rules/no-path-concat"
25 },
26
Tim van der Lippe16aca392020-11-13 11:37:13 +000027 schema: [],
28
29 messages: {
30 usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths."
31 }
Yang Guo4fd355c2019-09-19 10:59:03 +020032 },
33
34 create(context) {
35
36 const MATCHER = /^__(?:dir|file)name$/u;
37
38 //--------------------------------------------------------------------------
39 // Public
40 //--------------------------------------------------------------------------
41
42 return {
43
44 BinaryExpression(node) {
45
46 const left = node.left,
47 right = node.right;
48
49 if (node.operator === "+" &&
50 ((left.type === "Identifier" && MATCHER.test(left.name)) ||
51 (right.type === "Identifier" && MATCHER.test(right.name)))
52 ) {
53
Tim van der Lippe16aca392020-11-13 11:37:13 +000054 context.report({
55 node,
56 messageId: "usePathFunctions"
57 });
Yang Guo4fd355c2019-09-19 10:59:03 +020058 }
59 }
60
61 };
62
63 }
64};