├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-import-resolver-reactnative", 3 | "version": "1.0.2", 4 | "description": "React Native module resolution plugin for eslint-plugin-import", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/guzart/eslint-import-resolver-reactnative.git" 12 | }, 13 | "author": "Arturo Guzman", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/guzart/eslint-import-resolver-reactnative/issues" 17 | }, 18 | "homepage": "https://github.com/guzart/eslint-import-resolver-reactnative#readme", 19 | "dependencies": { 20 | "eslint-import-resolver-node": "^0.2.3", 21 | "find-root": "^1.0.0" 22 | }, 23 | "eslintConfig": { 24 | "extends": "eslint:recommended", 25 | "env": { 26 | "node": true 27 | } 28 | }, 29 | "devDependencies": { 30 | "eslint": "^3.8.1" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Arturo Guzman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var findRoot = require('find-root'); 2 | var nodeResolver = require('eslint-import-resolver-node'); 3 | var path = require('path'); 4 | 5 | var log = require('debug')('eslint-plugin-import:resolver:reactnative'); 6 | 7 | function pluginSettings(settings) { 8 | return Object.assign( 9 | { 10 | extensions: ['.js', '.json', '.android.js', '.ios.js'], 11 | }, 12 | settings 13 | ); 14 | } 15 | 16 | exports.interfaceVersion = 2; 17 | 18 | exports.resolve = function (source, file, userSettings) { 19 | log('Resolving:', source, 'from:', file); 20 | 21 | var settings = pluginSettings(userSettings); 22 | 23 | try { 24 | var appRoot = findRoot(file); 25 | var package = require(path.join(appRoot, 'package.json')); 26 | if (package.name) { 27 | var namePath = package.name + '/'; 28 | if (source.indexOf(namePath) === 0) { 29 | var absSource = path.join(appRoot, source.replace(namePath, '')); 30 | return nodeResolver.resolve(absSource, file, settings); 31 | } 32 | } 33 | } catch (err) { 34 | log('Error:', err); 35 | log('Fall back to eslint-import-resolver-node'); 36 | } 37 | 38 | return nodeResolver.resolve(source, file, settings); 39 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-import-resolver-reactnative 2 | 3 | [![npm](https://img.shields.io/npm/v/eslint-import-resolver-reactnative.svg)](https://www.npmjs.com/package/eslint-import-resolver-reactnative) 4 | 5 | React Native module resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import). 6 | 7 | Uses [`eslint-import-resolver-node`](https://www.npmjs.com/package/eslint-import-resolver-node) with a few modifications: 8 | 9 | * Configures extensions for React Native `['.js', '.json', '.android.js', '.ios.js']` 10 | * Helps resolve project name paths to the root ([`eslint-plugin-import#626`](https://github.com/benmosher/eslint-plugin-import/issues/626)) 11 | 12 | 13 | ## Usage 14 | 15 | ```bash 16 | npm install --save-dev eslint-import-resolver-reactnative 17 | ``` 18 | 19 | Configure ESLint to use this plugin. 20 | 21 | ``` 22 | // .eslintrc 23 | settings: 24 | import/resolver: reactnative 25 | ``` 26 | 27 | Use your project's name as part of the import path. 28 | 29 | ``` 30 | // package.json 31 | { 32 | "name": "cool-project" 33 | } 34 | ``` 35 | 36 | ``` 37 | import Button from 'cool-project/components/button'; 38 | // imports a file located at /components/button.js (or button.android.js or button.ios.js) 39 | 40 | // ... 41 | ``` 42 | 43 | Notice that this plugin will resolve your project's name to your project's root folder. 44 | --------------------------------------------------------------------------------