-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add node/file-extension-in-import rule
- Loading branch information
1 parent
a3a6e41
commit a3e0e29
Showing
14 changed files
with
481 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# enforce the style of file extensions in `import` declarations (file-extension-in-import) | ||
|
||
We can omit file extensions in `import`/`export` declarations. | ||
|
||
```js | ||
import foo from "./path/to/a/file" // maybe it's resolved to 'file.js' or 'file.json' | ||
export * from "./path/to/a/file" | ||
``` | ||
|
||
However, [--experimental-modules](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff) has declared to drop the file extension omition. | ||
|
||
Also, we can import a variety kind of files with bundlers such as Webpack. In the situation, probably explicit file extensions help us to understand code. | ||
|
||
## Rule Details | ||
|
||
This rule enforces the style of file extensions in `import`/`export` declarations. | ||
|
||
## Options | ||
|
||
This rule has a string option and an object option. | ||
|
||
```json | ||
{ | ||
"node/file-extension-in-import": [ | ||
"error", | ||
"always" or "never", | ||
{ | ||
"tryExtensions": [".js", ".json", ".node"], | ||
".xxx": "always" or "never", | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- `"always"` (default) requires file extensions in `import`/`export` declarations. | ||
- `"never"` disallows file extensions in `import`/`export` declarations. | ||
- `tryExtensions` is the file extensions to resolve import paths. Default is `[".js", ".json", ".node"]`. | ||
- `.xxx` is the overriding setting for specific file extensions. You can use arbitrary property names which start with `.`. | ||
|
||
### always | ||
|
||
Examples of :-1: **incorrect** code for the `"always"` option: | ||
|
||
```js | ||
/*eslint node/file-extension-in-import: ["error", "always"]*/ | ||
|
||
import foo from "./path/to/a/file" | ||
``` | ||
|
||
Examples of :+1: **correct** code for the `"always"` option: | ||
|
||
```js | ||
/*eslint node/file-extension-in-import: ["error", "always"]*/ | ||
|
||
import eslint from "eslint" | ||
import foo from "./path/to/a/file.js" | ||
``` | ||
|
||
### never | ||
|
||
Examples of :-1: **incorrect** code for the `"never"` option: | ||
|
||
```js | ||
/*eslint node/file-extension-in-import: ["error", "never"]*/ | ||
|
||
import foo from "./path/to/a/file.js" | ||
``` | ||
|
||
Examples of :+1: **correct** code for the `"never"` option: | ||
|
||
```js | ||
/*eslint node/file-extension-in-import: ["error", "never"]*/ | ||
|
||
import eslint from "eslint" | ||
import foo from "./path/to/a/file" | ||
``` | ||
|
||
### .xxx | ||
|
||
Examples of :+1: **correct** code for the `["always", { ".js": "never" }]` option: | ||
|
||
```js | ||
/*eslint node/file-extension-in-import: ["error", "always", { ".js": "never" }]*/ | ||
|
||
import eslint from "eslint" | ||
import script from "./script" | ||
import styles from "./styles.css" | ||
import logo from "./logo.png" | ||
``` | ||
|
||
## Shared Settings | ||
|
||
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings). | ||
Several rules have the same option, but we can set this option at once. | ||
|
||
- `tryExtensions` | ||
|
||
```js | ||
// .eslintrc.js | ||
module.exports = { | ||
"settings": { | ||
"node": { | ||
"tryExtensions": [".js", ".json", ".node"] | ||
} | ||
}, | ||
"rules": { | ||
"node/file-extension-in-import": "error" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const path = require("path") | ||
const fs = require("fs") | ||
const getImportExportTargets = require("../util/get-import-export-targets") | ||
const getTryExtensions = require("../util/get-try-extensions") | ||
|
||
/** | ||
* Get all file extensions of the files which have the same basename. | ||
* @param {string} filePath The path to the original file to check. | ||
* @returns {string[]} File extensions. | ||
*/ | ||
function getExistingExtensions(filePath) { | ||
const basename = path.basename(filePath, path.extname(filePath)) | ||
try { | ||
return fs | ||
.readdirSync(path.dirname(filePath)) | ||
.filter( | ||
filename => | ||
path.basename(filename, path.extname(filename)) === basename | ||
) | ||
.map(filename => path.extname(filename)) | ||
} catch (_error) { | ||
return [] | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
"enforce the style of file extensions in `import` declarations", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
url: | ||
"https://github.com/mysticatea/eslint-plugin-node/blob/v8.0.1/docs/rules/file-extension-in-import.md", | ||
}, | ||
fixable: "code", | ||
messages: { | ||
requireExt: "require file extension '{{ext}}'.", | ||
forbidExt: "forbid file extension '{{ext}}'.", | ||
}, | ||
schema: [ | ||
{ | ||
enum: ["always", "never"], | ||
}, | ||
{ | ||
type: "object", | ||
properties: { | ||
tryExtensions: getTryExtensions.schema, | ||
}, | ||
additionalProperties: { | ||
enum: ["always", "never"], | ||
}, | ||
}, | ||
], | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
if (context.getFilename().startsWith("<")) { | ||
return {} | ||
} | ||
const defaultStyle = context.options[0] || "always" | ||
const overrideStyle = context.options[1] || {} | ||
|
||
function verify({ filePath, name, node }) { | ||
// Ignore if it's not resolved to a file or it's a bare module. | ||
if (!filePath || !/[/\\]/u.test(name)) { | ||
return | ||
} | ||
|
||
// Get extension. | ||
const originalExt = path.extname(name) | ||
const resolvedExt = path.extname(filePath) | ||
const existingExts = getExistingExtensions(filePath) | ||
if (!resolvedExt && existingExts.length !== 1) { | ||
// Ignore if the file extension could not be determined one. | ||
return | ||
} | ||
const ext = resolvedExt || existingExts[0] | ||
const style = overrideStyle[ext] || defaultStyle | ||
|
||
// Verify. | ||
if (style === "always" && ext !== originalExt) { | ||
context.report({ | ||
node, | ||
messageId: "requireExt", | ||
data: { ext }, | ||
fix(fixer) { | ||
if (existingExts.length !== 1) { | ||
return null | ||
} | ||
const index = node.range[1] - 1 | ||
return fixer.insertTextBeforeRange([index, index], ext) | ||
}, | ||
}) | ||
} else if (style === "never" && ext === originalExt) { | ||
context.report({ | ||
node, | ||
messageId: "forbidExt", | ||
data: { ext }, | ||
fix(fixer) { | ||
if (existingExts.length !== 1) { | ||
return null | ||
} | ||
const index = name.lastIndexOf(ext) | ||
const start = node.range[0] + 1 + index | ||
const end = start + ext.length | ||
return fixer.removeRange([start, end]) | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
"Program:exit"(node) { | ||
const opts = { optionIndex: 1 } | ||
getImportExportTargets(context, node, opts).forEach(verify) | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.