Skip to content

Commit

Permalink
🐛 fix file-extension-in-import for scoped packages (fixes #160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed May 4, 2019
1 parent 11d2d41 commit dfb4dc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const path = require("path")
const fs = require("fs")
const getImportExportTargets = require("../util/get-import-export-targets")
const getTryExtensions = require("../util/get-try-extensions")
const packageNamePattern = /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
const corePackageOverridePattern = /^(?:assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|worker_threads|zlib)[/\\]$/u

/**
* Get all file extensions of the files which have the same basename.
Expand Down Expand Up @@ -69,7 +71,11 @@ module.exports = {

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)) {
if (
!filePath ||
packageNamePattern.test(name) ||
corePackageOverridePattern.test(name)
) {
return
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"mocha": "^6.1.4",
"nyc": "^14.0.0",
"opener": "^1.5.1",
"punycode": "^2.1.1",
"rimraf": "^2.6.3"
},
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ new RuleTester({
filename: fixture("test.js"),
code: "import 'eslint'",
},
{
filename: fixture("test.js"),
code: "import '@typescript-eslint/parser'",
},
{
filename: fixture("test.js"),
code: "import '@typescript-eslint\\parser'",
},
{
filename: fixture("test.js"),
code: "import 'punycode/'",
},
{
filename: fixture("test.js"),
code: "import 'xxx'",
Expand Down

0 comments on commit dfb4dc0

Please sign in to comment.