eslint-plugin-file-extension-in-import-ts
Advanced tools
Comparing version 1.0.2 to 2.0.0
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const forceIndexFileImport = require('../util/force-index-file-import'); | ||
const getTryExtensions = require('../util/get-try-extensions'); | ||
const mappingExtensions = require('../util/mapping-extensions'); | ||
@@ -18,4 +16,4 @@ const visitImport = require('../util/visit-import'); | ||
*/ | ||
function getExistingExtensions(filePath, extSearchOptions) { | ||
const basename = path.basename(filePath, path.extname(filePath)); | ||
function getExistingExtensions(filePath, extMappingList) { | ||
const basename = fs.existsSync(filePath) ? path.basename(filePath, path.extname(filePath)) : path.basename(filePath); | ||
@@ -25,5 +23,11 @@ try { | ||
if (isDirectory && extSearchOptions.forceIndexFileImport) { | ||
// We are forcing index.js file import when importing from a directory | ||
return ['/index.js']; | ||
if (isDirectory) { | ||
// eslint-disable-next-line no-plusplus | ||
for (let i = 0, l = extMappingList.length; i < l; i++) { | ||
const ext = extMappingList[i]; | ||
if (fs.existsSync(path.join(filePath, `/index${ext}`))) { | ||
return ['/index.js']; | ||
} | ||
} | ||
} | ||
@@ -48,7 +52,6 @@ | ||
const extMapping = overrideStyle.extMapping || mappingExtensions.mappingDefault; | ||
const extSearchOptions = { | ||
forceIndexFileImport: overrideStyle.forceIndexFileImport || false, | ||
}; | ||
const extMappingList = Object.keys(extMapping); | ||
function verify({ filePath, name, node }) { | ||
const isDirectory = fs.existsSync(filePath) && fs.statSync(filePath).isDirectory(); | ||
// Ignore if it's not resolved to a file or it's a bare module. | ||
@@ -61,4 +64,5 @@ if (!filePath || packageNamePattern.test(name) || corePackageOverridePattern.test(name)) { | ||
const originalExt = path.extname(name); | ||
const resolvedExt = path.extname(filePath); | ||
const existingExts = getExistingExtensions(filePath, extSearchOptions); | ||
// eslint-disable-next-line no-nested-ternary | ||
const resolvedExt = isDirectory ? null : fs.existsSync(filePath) ? path.extname(filePath) : null; | ||
const existingExts = getExistingExtensions(filePath, extMappingList); | ||
if (!resolvedExt && existingExts.length !== 1) { | ||
@@ -130,4 +134,2 @@ // Ignore if the file extension could not be determined one. | ||
extMapping: mappingExtensions.schema, | ||
forceIndexFileImport: forceIndexFileImport.schema, | ||
tryExtensions: getTryExtensions.schema, | ||
}, | ||
@@ -134,0 +136,0 @@ type: 'object', |
@@ -6,3 +6,2 @@ const path = require('node:path'); | ||
const getResolvePaths = require('./get-resolve-paths'); | ||
const getTryExtensions = require('./get-try-extensions'); | ||
const ImportTarget = require('./import-target'); | ||
@@ -28,4 +27,3 @@ const stripImportPathParams = require('./strip-import-path-params'); | ||
const paths = getResolvePaths(context, optionIndex); | ||
const extensions = getTryExtensions(context, optionIndex); | ||
const options = { basedir, extensions, paths }; | ||
const options = { basedir, paths }; | ||
@@ -43,3 +41,8 @@ return { | ||
// Note: "999" arbitrary to check current/future Node.js version | ||
if (name && (includeCore || !isCoreModule(name, '999'))) { | ||
if ( | ||
name && | ||
node.importKind !== 'type' && | ||
node.exportKind !== 'type' && | ||
(includeCore || !isCoreModule(name, '999')) | ||
) { | ||
targets.push(new ImportTarget(sourceNode, name, options)); | ||
@@ -46,0 +49,0 @@ } |
{ | ||
"name": "eslint-plugin-file-extension-in-import-ts", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "The plugin check and fix file extensions in Node.js application type module in Typescript", | ||
@@ -27,18 +27,18 @@ "keywords": [], | ||
"dependencies": { | ||
"is-core-module": "^2.9.0", | ||
"resolve": "^1.22.1" | ||
"is-core-module": "^2.13.1", | ||
"resolve": "^1.22.8" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.19.0", | ||
"eslint": "^8.54.0", | ||
"eslint-config-airbnb": "^19.0.4", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-config-prettier": "^8.10.0", | ||
"eslint-plugin-import": "^2.29.0", | ||
"eslint-plugin-prettier": "^5.0.1", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
"eslint-plugin-unicorn": "^43.0.1", | ||
"eslint-plugin-unicorn": "^49.0.0", | ||
"git-is-ready-to-push": "^1.0.0", | ||
"husky": "^8.0.1", | ||
"prettier": "^2.6.2", | ||
"husky": "^8.0.3", | ||
"prettier": "^3.1.0", | ||
"sort-package-json": "^1.57.0" | ||
} | ||
} |
@@ -49,3 +49,3 @@ # eslint-plugin-file-extension-in-import-ts | ||
'always', | ||
{ extensionsMapping: {'.ts': '.js', forceIndexFileImport: true }} | ||
{ extMapping: {'.ts': '.js' } } | ||
] | ||
@@ -56,4 +56,2 @@ ``` | ||
The property "forceIndexFileImport" will add "/index.js" if you import directory, for example: | ||
```js | ||
@@ -60,0 +58,0 @@ import module from './my-module'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18013
13
401
114
Updatedis-core-module@^2.13.1
Updatedresolve@^1.22.8