airbnb / javascript

JavaScript Style Guide
MIT License
144.59k stars 26.44k forks source link

Replace import/first with import/order? #1773

Closed Zakini closed 6 years ago

Zakini commented 6 years ago

import/first is a great rule to have, but it disallows this:

import '../foo';
import fs from 'fs';  // <-- Error: Absolute imports should come before relative imports
import app from '../app';

On the other hand, import/order is fine with this as "Unassigned imports are ignored, as the order they are imported in may be important."

I assume unassigned imports are okay, since there's no mention of them in the style guide.

To reduce the changes to just allowing unassigned imports anywhere (but still above all other non-import statements), then changing:

{
  // ...
  "import/first": ["error", { "absolute-first": true }],
  // ...
}

to:

{
  // ...
  "import/first": "error",
  "import/order": ["error", { "groups": [["builtin", "external", "internal"]] }],
  // ...
}

would achieve this.

Alternatively, this could also introduce stricter grouping of imports, if that's desirable.

Shyam-Chen commented 6 years ago
// third-party
import 'vuetify/dist/vuetify.min.css';
import Vue from 'vue';
import { sync } from 'vuex-router-sync';
import Material from 'vuetify';
import Meta from 'vue-meta';
import Validate from 'vee-validate';

// local
import './assets/styles/global.css';
import App from './app/App';
import router from './app/config/router';
import store from './app/config/store';
import provide from './app/config/provide';
import i18n from './app/config/i18n';
ljharb commented 6 years ago

I don't think we want to introduce stricter grouping of imports; but the change you're suggesting might make sense. Would you mind making a PR?