|
| 1 | +/** |
| 2 | + * @author Toru Nagashima |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +"use strict" |
| 6 | + |
| 7 | +const path = require("path") |
| 8 | +const fs = require("fs") |
| 9 | +const getImportExportTargets = require("../util/get-import-export-targets") |
| 10 | +const getTryExtensions = require("../util/get-try-extensions") |
| 11 | + |
| 12 | +/** |
| 13 | + * Get all file extensions of the files which have the same basename. |
| 14 | + * @param {string} filePath The path to the original file to check. |
| 15 | + * @returns {string[]} File extensions. |
| 16 | + */ |
| 17 | +function getExistingExtensions(filePath) { |
| 18 | + const basename = path.basename(filePath, path.extname(filePath)) |
| 19 | + try { |
| 20 | + return fs |
| 21 | + .readdirSync(path.dirname(filePath)) |
| 22 | + .filter( |
| 23 | + filename => |
| 24 | + path.basename(filename, path.extname(filename)) === basename |
| 25 | + ) |
| 26 | + .map(filename => path.extname(filename)) |
| 27 | + } catch (_error) { |
| 28 | + return [] |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +module.exports = { |
| 33 | + meta: { |
| 34 | + docs: { |
| 35 | + description: |
| 36 | + "enforce the style of file extensions in `import` declarations", |
| 37 | + category: "Stylistic Issues", |
| 38 | + recommended: false, |
| 39 | + url: |
| 40 | + "https://github.com/mysticatea/eslint-plugin-node/blob/v8.0.1/docs/rules/file-extension-in-import.md", |
| 41 | + }, |
| 42 | + fixable: "code", |
| 43 | + messages: { |
| 44 | + requireExt: "require file extension '{{ext}}'.", |
| 45 | + forbidExt: "forbid file extension '{{ext}}'.", |
| 46 | + }, |
| 47 | + schema: [ |
| 48 | + { |
| 49 | + enum: ["always", "never"], |
| 50 | + }, |
| 51 | + { |
| 52 | + type: "object", |
| 53 | + properties: { |
| 54 | + tryExtensions: getTryExtensions.schema, |
| 55 | + }, |
| 56 | + additionalProperties: { |
| 57 | + enum: ["always", "never"], |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + type: "suggestion", |
| 62 | + }, |
| 63 | + create(context) { |
| 64 | + if (context.getFilename().startsWith("<")) { |
| 65 | + return {} |
| 66 | + } |
| 67 | + const defaultStyle = context.options[0] || "always" |
| 68 | + const overrideStyle = context.options[1] || {} |
| 69 | + |
| 70 | + function verify({ filePath, name, node }) { |
| 71 | + // Ignore if it's not resolved to a file or it's a bare module. |
| 72 | + if (!filePath || !/[/\\]/u.test(name)) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // Get extension. |
| 77 | + const originalExt = path.extname(name) |
| 78 | + const resolvedExt = path.extname(filePath) |
| 79 | + const existingExts = getExistingExtensions(filePath) |
| 80 | + if (!resolvedExt && existingExts.length !== 1) { |
| 81 | + // Ignore if the file extension could not be determined one. |
| 82 | + return |
| 83 | + } |
| 84 | + const ext = resolvedExt || existingExts[0] |
| 85 | + const style = overrideStyle[ext] || defaultStyle |
| 86 | + |
| 87 | + // Verify. |
| 88 | + if (style === "always" && ext !== originalExt) { |
| 89 | + context.report({ |
| 90 | + node, |
| 91 | + messageId: "requireExt", |
| 92 | + data: { ext }, |
| 93 | + fix(fixer) { |
| 94 | + if (existingExts.length !== 1) { |
| 95 | + return null |
| 96 | + } |
| 97 | + const index = node.range[1] - 1 |
| 98 | + return fixer.insertTextBeforeRange([index, index], ext) |
| 99 | + }, |
| 100 | + }) |
| 101 | + } else if (style === "never" && ext === originalExt) { |
| 102 | + context.report({ |
| 103 | + node, |
| 104 | + messageId: "forbidExt", |
| 105 | + data: { ext }, |
| 106 | + fix(fixer) { |
| 107 | + if (existingExts.length !== 1) { |
| 108 | + return null |
| 109 | + } |
| 110 | + const index = name.lastIndexOf(ext) |
| 111 | + const start = node.range[0] + 1 + index |
| 112 | + const end = start + ext.length |
| 113 | + return fixer.removeRange([start, end]) |
| 114 | + }, |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return { |
| 120 | + "Program:exit"(node) { |
| 121 | + const opts = { optionIndex: 1 } |
| 122 | + getImportExportTargets(context, node, opts).forEach(verify) |
| 123 | + }, |
| 124 | + } |
| 125 | + }, |
| 126 | +} |
0 commit comments