UNPKG

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