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