Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle the confliction with import/first? #115

Closed
helloint opened this issue Nov 4, 2022 · 5 comments
Closed

How to handle the confliction with import/first? #115

helloint opened this issue Nov 4, 2022 · 5 comments

Comments

@helloint
Copy link

helloint commented Nov 4, 2022

This is after the format:

import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useShareAppMessage, useShareTimeline } from '@tarojs/taro';
import Table, { Columns } from 'taro-react-table';
import { useAppDispatch, useAppSelector } from '../../store/hooks';

import 'taro-react-table/dist/index.css';  // <----ESLint: Absolute imports should come before relative imports.(import/first)
import './index.scss';

My .eslintrc.js

module.exports = {
  extends: ['taro/react', 'plugin:prettier/recommended'],
  plugins: ['simple-import-sort', 'import'],
  rules: {
    'react/jsx-uses-react': 'off',
    'react/react-in-jsx-scope': 'off',
    'import/order': 'off',
    'import/first': 'warn',
    'import/newline-after-import': 'warn',
    'import/no-duplicates': 'warn',
    'simple-import-sort/imports': 'warn',
    'simple-import-sort/exports': 'warn',
  },
  overrides: [
    // override "simple-import-sort" config
    {
      files: ['*.js', '*.jsx', '*.ts', '*.tsx'],
      rules: {
        'simple-import-sort/imports': [
          'warn',
          {
            groups: [
              [
                // Packages `react` related packages come first.
                '^react',
                '^@?\\w',
                // Internal packages.
                '^(@|components)(/.*|$)',
                // Side effect imports.
                '^\\u0000',
                // Parent imports. Put `..` last.
                '^\\.\\.(?!/?$)',
                '^\\.\\./?$',
                // Other relative imports. Put same-folder imports and `.` last.
                '^\\./(?=.*/)(?!/?$)',
                '^\\.(?!/?$)',
                '^\\./?$',
              ],
              // Style imports.
              ['^.+\\.?(sc|sa|c)ss$'],
            ],
          },
        ],
      },
    },
  ],
};

Note: taro/react contains import/order, so I turned the rule off.

Thanks.

@lydell
Copy link
Owner

lydell commented Nov 4, 2022

Hi! Yes, it sounds like this should be possible.

I don’t quite understand, though. Can you post:

  • what the output is after running this rule
  • what you would like it to be
  • your config (already done)

You have already created a custom groups config. What problems did you encounter when trying to to the absolute css file thing?

@helloint
Copy link
Author

helloint commented Nov 4, 2022

  • what the output is after running this rule
    Please see the first code block (I mentioned 'This is after the format'), this is after format by ESLint (with eslint-plugin-simple-import-sort)
    But then ESLint warned me:
    Absolute imports should come before relative imports.(import/first)
    It means the absolute css import, 'taro-react-table/dist/index.css', should come before the relative component import.

  • what you would like it to be
    I would like it not warn me after I do the code format...

  • your config (already done)
    See the 2nd code block.

For the custom groups config, actually it comes from this topic, and I just made some minor changes:

  1. support sass
  2. remove blank lines between groups, except the styles group

@helloint
Copy link
Author

helloint commented Nov 4, 2022

oh, my wrong, the topic is incorrect. the eslint-plugin-simple-import-sort do put the absolute css to the last group, just the import/first throws warning...

@lydell lydell closed this as completed Nov 4, 2022
@helloint
Copy link
Author

helloint commented Nov 4, 2022

I pushed a demo project to GitHub https://github.com/hint-test/test-eslint-plugin-simple-import-sort.git
It shows the import/first warning in src/pages/index/index.jsx, after I format the file using eslint-plugin-simple-import-sort
image

@helloint helloint changed the title Is it possbile to let Absolute css file to be on the last group? along with other relative css import. How to handle the confliction with import/first? Nov 4, 2022
@helloint
Copy link
Author

helloint commented Nov 7, 2022

I fixed with 'import/first': ['warn', 'disable-absolute-first'],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants