Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-import-resolver-custom-alias

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-import-resolver-custom-alias - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

24

index.js
const path = require('path');
const fs = require('fs');
const resolve = require('resolve'); // eslint-disable-line
const globParent = require('glob-parent');

@@ -15,5 +17,21 @@ function getOptions(file, config) {

function getPath(configPath, filepath, packages) {
const workspaces =
packages.filter(pkg => filepath.indexOf(pkg) === 0 && filepath[pkg.length] === '/');
const relativeRoot = workspaces[0] || process.cwd();
return path.resolve(relativeRoot, configPath);
}
exports.interfaceVersion = 2;
exports.resolve = (source, file, config) => {
if (resolve.isCore(source)) return { found: true, path: null };
const packages = (config.packages || [])
.map(pkg => {
const parent = path.resolve(process.cwd(), globParent(pkg));
const dir = fs.readdirSync(parent)
.map(name => path.resolve(parent, name))
.filter(name => !fs.statSync(name).isFile());
return dir;
})
.reduce((prev, curr) => prev.concat(curr), []);
try {

@@ -23,6 +41,6 @@ const modifiedSource = Object.keys(config.alias || {}).reduce((currentSource, prefix) => {

if (currentSource.indexOf(prefix) === 0 && currentSource[prefix.length] === '/') {
const prefixPath = path.resolve(process.cwd(), config.alias[prefix]);
ret = `${prefixPath}/${currentSource.substr(prefix.length)}`;
const prefixPath = getPath(config.alias[prefix], file, packages);
ret = `${prefixPath}${currentSource.substr(prefix.length)}`;
} else if (currentSource === prefix) {
ret = path.resolve(process.cwd(), config.alias[prefix]);
ret = getPath(config.alias[prefix], file, packages);
}

@@ -29,0 +47,0 @@ return ret;

3

package.json
{
"name": "eslint-import-resolver-custom-alias",
"version": "1.1.0",
"version": "1.2.0",
"description": "Plugin for eslint-plugin-import to use custom alias.",

@@ -26,2 +26,3 @@ "main": "index.js",

"dependencies": {
"glob-parent": "^5.1.0",
"resolve": "^1.3.0"

@@ -28,0 +29,0 @@ },

@@ -30,3 +30,6 @@ [![Build Status](https://travis-ci.org/laysent/eslint-import-resolver-custom-alias.svg?branch=master)](https://travis-ci.org/laysent/eslint-import-resolver-custom-alias)

},
"extensions": [".js", ".jsx"]
"extensions": [".js", ".jsx"],
"packages": [
"packages/*"
]
}

@@ -43,1 +46,51 @@ }

`extensions` is an array of possible suffix. If not provided, default value will be `[".js"]`.
`packages` is an optional configuration. When using lerna to manage packages and use eslint at
root folder, `packages` lets the resolver know where each package folder exist. This way, when
resolving alias, relative path will be resolved based on current package, instead of root folder.
Consider the file as an example:
```jsx
import * as utils from '@/utils';
```
Suppose the above file locates at `./packages/subfolder/src/components/button.jsx` and command is
running at root folder (i.e. `./`). If the resolver is configured the following way:
```json
{
"settings": {
"import/resolver": {
"eslint-import-resolver-custom-alias": {
"alias": {
"@": "./src"
},
"extensions": [".js", ".jsx"],
}
}
}
}
```
Resolver will tries to find file at `./src/utils` folder. However, with `packages` configured:
```json
{
"settings": {
"import/resolver": {
"eslint-import-resolver-custom-alias": {
"alias": {
"@": "./src"
},
"extensions": [".js", ".jsx"],
"packages": [
"packages/*"
]
}
}
}
}
```
Resolver will try to find it at `./packages/subfolder/src/utils` folder instead.

@@ -14,6 +14,10 @@ const path = require('path');

let resolve;
let fs;
beforeAll(() => {
resolve = require('resolve');
fs = require('fs');
spyOn(resolve, 'sync');
spyOn(resolve, 'isCore');
spyOn(fs, 'statSync');
spyOn(fs, 'readdirSync');
});

@@ -30,3 +34,3 @@ const plugin = require('../index');

plugin.resolve(defaultSource, defaultFile, defaultConfig);
const modifiedSource = `${path.resolve(process.cwd(), './src')}/${defaultSource.substr(1)}`;
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substr(1)}`;

@@ -41,3 +45,3 @@ const mostRecent = resolve.sync.calls.mostRecent();

plugin.resolve(defaultSource, defaultFile, defaultConfig);
const modifiedSource = `${path.resolve(process.cwd(), './src')}/${defaultSource.substr(1)}`;
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substr(1)}`;

@@ -121,2 +125,19 @@ const mostRecent = resolve.sync.calls.mostRecent();

});
it('should resolve relative path based on package root', () => {
fs.statSync.and.returnValue({
isFile() { return false; }
});
fs.readdirSync.and.returnValue(['subfolder']);
const config = Object.assign({}, defaultConfig, {
packages: [
'packages/*'
],
});
const fileInPackage = path.resolve(process.cwd(), 'packages', 'subfolder', defaultFile);
plugin.resolve(defaultSource, fileInPackage, config);
const modifiedSource = path.resolve(process.cwd(), 'packages', 'subfolder', './src', 'path/to/file');
const mostRecent = resolve.sync.calls.mostRecent();
expect(mostRecent.args[0]).toBe(modifiedSource);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc