import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.57k stars 1.57k forks source link

platform-based shared configs #283

Closed benmosher closed 8 years ago

benmosher commented 8 years ago

Gather up settings for different JS platforms and drop into config/[platform], then referenced as plugin:import/[platform].

This is useful even (and maybe especially) if a special resolver is not needed.

Node is the default (for now), so alternate platforms that might warrant a named shared config are

Shared config might be a good place for comments about usage on each platform.

yutin1987 commented 8 years ago

maybe, we should setting .eslintrc follow:

  "env": {
    "android": true,
    "ios": true,
  }
yutin1987 commented 8 years ago

when setting extensions to .eslintrc

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".ios.js", ".android.js"]
      }
    }
  },
  "env": {
    "node": true,
  }
}

It's work on react native.

jozan commented 8 years ago

Cannot get @yutin1987's example to work on my React Native project. Am I missing something?

Error:

path/to/app/Components/Restaurant.js
   7:17  error  Unable to resolve path to module '../Data/Api'           import/no-unresolved
   8:21  error  Unable to resolve path to module '../Data/Storage'       import/no-unresolved
   9:19  error  Unable to resolve path to module '../Utils/utils'        import/no-unresolved
  11:22  error  Unable to resolve path to module './MenuView'            import/no-unresolved
  12:21  error  Unable to resolve path to module './Spinner'             import/no-unresolved
  13:32  error  Unable to resolve path to module './DateRowPlaceholder'  import/no-unresolved

.eslintrc:

{
  "extends": "airbnb-base",
  "parser": "babel-eslint",
  "plugins": [
    "react",
    "react-native"
  ],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".ios.js", ".android.js"]
      }
    }
  },
  "env": {
    "es6": true,
    "node": true,
  },
  "rules": {
    "react-native/no-unused-styles": 0,
    "react-native/split-platform-components": 2,
    "react-native/no-inline-styles": 2,
    "react-native/no-color-literals": 0,
    "react/jsx-uses-react": 2,
    "react/jsx-uses-vars": 2,

    "no-use-before-define": 0,
    "jsx-quotes": 2,
  }
}

Restaurant.js

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
} from 'react-native';

import API from '../Data/Api';
import Storage from '../Data/Storage';
import utils from '../Utils/utils';

import MenuView from './MenuView';
import Spinner from './Spinner';
import DateRowPlaceholder from './DateRowPlaceholder';

... snip ...

package.json

{
  "name": "Kuu",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "lint": "eslint index.*.js app/*"
  },
  "dependencies": {
    "moment": "^2.13.0",
    "react": "^15.1.0",
    "react-native": "^0.27.0-rc2",
    "react-native-code-push": "^1.12.0-beta",
    "react-native-collapsible": "^0.6.0",
    "react-native-vector-icons": "^2.0.3"
  },
  "devDependencies": {
    "babel-eslint": "^6.1.0",
    "eslint": "^2.13.1",
    "eslint-config-airbnb-base": "^3.0.1",
    "eslint-import-resolver-node": "^0.2.1",
    "eslint-plugin-import": "^1.9.2",
    "eslint-plugin-react": "^5.2.2",
    "eslint-plugin-react-native": "^1.1.0"
  }
}
benmosher commented 8 years ago

@jozan not sure, looks good from what you've posted. What is the extension on ../Data/Api?

jozan commented 8 years ago

@jozan not sure, looks good from what you've posted. What is the extension on ../Data/Api?

It's .js.

Could it be that uppercase pathnames confuse the resolver somehow?

benmosher commented 8 years ago

re: uppercase -- Unlikely. The outcry would be deafening, I think. 😁 Works fine for me with all mixes of case.

The case does need to match the FS, though, even on case-insensitive filesystems like Windows and OSX.

Also: on Windows, some users have reported that running in a terminal where the CWD casing doesn't match the FS can result in what you're seeing.

jozan commented 8 years ago

I will do some testing during the weekend on a fresh project. The project I'm trying to set up eslint and eslint-plugin-import has been created in around React Native 0.10 and updated since then to the almost latest version. Maybe there is some cruft that should be removed. Quite unlikely but I assume this might be a weird environment issue rather than plugin problem. I'll keep you posted.

ssamkough commented 2 years ago

when setting extensions to .eslintrc

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".ios.js", ".android.js"]
      }
    }
  },
  "env": {
    "node": true,
  }
}

It's work on react native.

Since I'm using Typescript, I had to change up the value for extensions to: "extensions": ['.tsx', '.ios.tsx', '.android.tsx']

ljharb commented 2 years ago

@ssamkough the latter two aren’t needed; a file can only have one extension.