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 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 12 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 13 | module.exports = { |
| 14 | meta: { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 15 | deprecated: true, |
| 16 | |
| 17 | replacedBy: [], |
| 18 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 19 | type: "suggestion", |
| 20 | |
| 21 | docs: { |
| 22 | description: "disallow string concatenation with `__dirname` and `__filename`", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 23 | recommended: false, |
| 24 | url: "https://eslint.org/docs/rules/no-path-concat" |
| 25 | }, |
| 26 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 27 | schema: [], |
| 28 | |
| 29 | messages: { |
| 30 | usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths." |
| 31 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 32 | }, |
| 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 Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 54 | context.report({ |
| 55 | node, |
| 56 | messageId: "usePathFunctions" |
| 57 | }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | } |
| 64 | }; |