Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Disallow string concatenation when using __dirname and __filename |
| 3 | * @author Nicholas C. Zakas |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 4 | * @deprecated in ESLint v7.0.0 |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 5 | */ |
| 6 | "use strict"; |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | // Rule Definition |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | module.exports = { |
| 13 | meta: { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 14 | deprecated: true, |
| 15 | |
| 16 | replacedBy: [], |
| 17 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 18 | type: "suggestion", |
| 19 | |
| 20 | docs: { |
| 21 | description: "disallow string concatenation with `__dirname` and `__filename`", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 22 | recommended: false, |
| 23 | url: "https://eslint.org/docs/rules/no-path-concat" |
| 24 | }, |
| 25 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 26 | schema: [], |
| 27 | |
| 28 | messages: { |
| 29 | usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths." |
| 30 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 53 | context.report({ |
| 54 | node, |
| 55 | messageId: "usePathFunctions" |
| 56 | }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | }; |
| 61 | |
| 62 | } |
| 63 | }; |