1 | /**
|
2 | * @fileoverview Disallow string concatenation when using __dirname and __filename
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Rule Definition
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | module.exports = {
|
12 | meta: {
|
13 | docs: {
|
14 | description: "disallow string concatenation with `__dirname` and `__filename`",
|
15 | category: "Node.js and CommonJS",
|
16 | recommended: false
|
17 | },
|
18 |
|
19 | schema: []
|
20 | },
|
21 |
|
22 | create(context) {
|
23 |
|
24 | const MATCHER = /^__(?:dir|file)name$/;
|
25 |
|
26 | //--------------------------------------------------------------------------
|
27 | // Public
|
28 | //--------------------------------------------------------------------------
|
29 |
|
30 | return {
|
31 |
|
32 | BinaryExpression(node) {
|
33 |
|
34 | const left = node.left,
|
35 | right = node.right;
|
36 |
|
37 | if (node.operator === "+" &&
|
38 | ((left.type === "Identifier" && MATCHER.test(left.name)) ||
|
39 | (right.type === "Identifier" && MATCHER.test(right.name)))
|
40 | ) {
|
41 |
|
42 | context.report(node, "Use path.join() or path.resolve() instead of + to create paths.");
|
43 | }
|
44 | }
|
45 |
|
46 | };
|
47 |
|
48 | }
|
49 | };
|