eslint-plugin-path
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -10,2 +10,3 @@ "use strict"; | ||
const { getConfigSettings } = require("../utils/config"); | ||
const { getPackagePath } = require("../utils/package"); | ||
@@ -58,23 +59,2 @@ /** | ||
/** | ||
* Checks if the current import is incorrect as expected | ||
* @param {?string} current | ||
* @param {?string} expected | ||
* @param {RuleSettings} settings | ||
* @returns | ||
*/ | ||
function isIncorrectImport(current, expected, settings) { | ||
if (!current || !expected || isExternalPath(expected)) { | ||
return false; | ||
} | ||
if (isMaxDepthExceeded(current, settings)) { | ||
return true; | ||
} | ||
return settings.suggested | ||
? getSlashCounts(current) > getSlashCounts(expected) | ||
: false; | ||
} | ||
/** | ||
* @typedef {Object} RuleSettings | ||
@@ -94,8 +74,20 @@ * @property {number} maxDepth | ||
const settings = { maxDepth, suggested }; | ||
const tsconfigSettings = getConfigSettings(); | ||
const packagePath = getPackagePath(filename); | ||
const tsconfigSettings = getConfigSettings(packagePath); | ||
return getImport(filename, ({ node, start, value: current, end, path }) => { | ||
if ( | ||
!isMaxDepthExceeded(current, settings) || | ||
isExternalPath(current, packagePath) | ||
) { | ||
return; | ||
} | ||
const expected = getAbsolutePathToTarget(path, tsconfigSettings); | ||
if (!isIncorrectImport(current, expected, settings)) { | ||
if ( | ||
settings.suggested && | ||
getSlashCounts(current) < getSlashCounts(expected) | ||
) { | ||
return; | ||
@@ -102,0 +94,0 @@ } |
"use strict"; | ||
const { join } = require("path"); | ||
const { getPackagePath } = require("./package"); | ||
const { FILES } = require("./constants"); | ||
const { loadJSONFile, isFileExists, isPathExists } = require("./helpers"); | ||
const { loadConfigFile, isFileExists, isPathExists } = require("./helpers"); | ||
@@ -38,13 +37,13 @@ /** | ||
* | ||
* @param {string} packagePath | ||
* @returns {Array<AliasItem>} - Returns array of alias items | ||
*/ | ||
function getConfigSettings() { | ||
const packageDir = getPackagePath(); | ||
function getConfigSettings(packagePath) { | ||
let fileName = null; | ||
if (isFileExists(packageDir, FILES.tsconfig)) { | ||
if (isFileExists(packagePath, FILES.tsconfig)) { | ||
fileName = FILES.tsconfig; | ||
} | ||
if (isFileExists(packageDir, FILES.jsconfig)) { | ||
if (isFileExists(packagePath, FILES.jsconfig)) { | ||
fileName = FILES.jsconfig; | ||
@@ -58,4 +57,4 @@ } | ||
const urls = []; | ||
const config = loadJSONFile(packageDir, fileName); | ||
const createAliasItem = getAliasItemCreator(packageDir); | ||
const config = loadConfigFile(packagePath, fileName); | ||
const createAliasItem = getAliasItemCreator(packagePath); | ||
@@ -62,0 +61,0 @@ if (isPathExists(config, "compilerOptions.paths")) { |
@@ -29,4 +29,12 @@ "use strict"; | ||
*/ | ||
function loadJSONFile(dir, filename) { | ||
return JSON.parse(readFileSync(join(dir, filename), "utf-8")); | ||
function loadConfigFile(dir, filename) { | ||
try { | ||
const path = join(dir, filename); | ||
const file = readFileSync(path, "utf-8"); | ||
const result = JSON.parse(file); | ||
return result; | ||
} catch (error) { | ||
throw new Error(`'${filename}' is invalid. Please, validate JSON file.`); | ||
} | ||
} | ||
@@ -46,4 +54,4 @@ | ||
isPathExists, | ||
loadJSONFile, | ||
loadConfigFile, | ||
isFileExists, | ||
}; |
"use strict"; | ||
const { resolve, relative } = require("path"); | ||
const { resolve } = require("path"); | ||
const { existsSync } = require("fs"); | ||
const { getPackagePath } = require("./package"); | ||
@@ -19,5 +18,6 @@ /** | ||
* @param {string} path | ||
* @param {string} packagePath | ||
* @returns {boolean} - returns true if path is external | ||
*/ | ||
function isExternalPath(path) { | ||
function isExternalPath(path, packagePath) { | ||
if (!path) { | ||
@@ -27,8 +27,2 @@ return false; | ||
const packagePath = getPackagePath(); | ||
if (relative(packagePath, path).startsWith("..")) { | ||
return true; | ||
} | ||
const modulesFolder = "node_modules"; | ||
@@ -35,0 +29,0 @@ const modulePath = resolve(packagePath, modulesFolder, path); |
@@ -9,6 +9,8 @@ "use strict"; | ||
* Package.json directory finder | ||
* @param {string} filePath | ||
* | ||
* @returns {string} - path to directory where packages.json is located | ||
*/ | ||
function getPackagePath() { | ||
let dir = resolve(FILES.package); | ||
function getPackagePath(filePath) { | ||
let dir = resolve(filePath || "", FILES.package); | ||
@@ -15,0 +17,0 @@ do { |
{ | ||
"name": "eslint-plugin-path", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "author": "qDanik <qdanik@yandex.ru>", |
# eslint-plugin-path [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
An ESLint plugin for enforcing consistent imports across project. | ||
An ESLint plugin for enforcing consistent imports across project. In other words, it helps to replace all relatives import with absolutes dependinng on settings. | ||
@@ -5,0 +5,0 @@ ## Installation |
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
14707
395