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

import/extensions breaks when a scoped package's scope is just @ #1598

Closed
jtsom opened this issue Jan 10, 2020 · 21 comments · Fixed by #1611
Closed

import/extensions breaks when a scoped package's scope is just @ #1598

jtsom opened this issue Jan 10, 2020 · 21 comments · Fixed by #1611

Comments

@jtsom
Copy link

jtsom commented Jan 10, 2020

In a Vue application, using version with packages:

    "eslint": "^5.2.0",
    "eslint-config-airbnb-base": "^13.2.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-vue": "^5.2.3",

and an eslint.js of:

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: ['plugin:vue/essential', 'airbnb-base'],
  rules: {
    'linebreak-style': 0,
    'no-console': 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    'padded-blocks': 0,
    'no-param-reassign': 0,
    'import/no-unresolved': 0,
    'import/prefer-default-export': 0,
    'max-len': 0,
    'object-curly-newline': ['error', { ObjectPattern: 'never' }],
    // 'vue/html-closing-bracket-newline': ['error', {
    //   singleline: 'never',
    //   multiline: 'never',
    // }],
  },
  parserOptions: {
    parser: 'babel-eslint',
  },
};

In a .vue file, when trying to do an import of a .js file, the eslint plugin can't decide if the .js extension is needed or not:
This:
import router from '@/router';
shows:
Missing file extension for "@/router"
If I change it to:
import router from '@/router.js';
I now get:
Unexpected use of file extension "js" for "@/router.js"

So which do you want?!?

FYI, the jsconfig.json is:

{
  "include": [
    "./src/**/*"
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  }
}
@ljharb
Copy link
Member

ljharb commented Jan 10, 2020

@/ might be causing confusion; what happens if you temporarily switch it to @x/ or similar?

@jtsom
Copy link
Author

jtsom commented Jan 10, 2020

Yeah, that seems to be an issue - If I change it to '../router', there is no error (If I use '../router.js', I get the "unexpected use of js" message)

Not sure if a newer version would help, I hesitate to update, as there were issues with using the latest eslint / eslint-plugins and airbnb eslint plugins with Vue.

Thanks.

@ljharb
Copy link
Member

ljharb commented Jan 10, 2020

It's possible - there's unreleased stuff on master - but it's also possible that nobody expected an @ by itself (since usually @ denotes a scope, which is always nonempty)

@jtsom
Copy link
Author

jtsom commented Jan 10, 2020

Ohhh I see what you mean by "@x"...

I do have a lot of imports with just the @ - import Feedback from '@/views/Feedback.vue'; and that does not have an issue. If I change the jsconfig.json to be @src/*. ... and use that, it does seem to help out, but I'd have to change lots of code, and as far as I recall, that was an OK pattern (at least with Vue). But I guess I'll do what I must?

@jtsom
Copy link
Author

jtsom commented Jan 10, 2020

It's happening on other files too, and give errors when I build my vue app. As far as I see, using the @/ is normal for a vue application

Module Warning (from ./node_modules/eslint-loader/index.js):
error: Missing file extension for "@/router" (import/extensions) at src/components/ActivityHeader.vue:24:20:
  22 | 
  23 | <script>
> 24 | import router from '@/router';
     | 

@ljharb
Copy link
Member

ljharb commented Jan 10, 2020

Oh I wasn't suggesting permanently changing everything :-) just trying it out on one file to see if the problem you're reporting goes away.

@jtsom
Copy link
Author

jtsom commented Jan 11, 2020

I ended up adding a rule to my eslint.js file to basically ignore it..

@ljharb
Copy link
Member

ljharb commented Jan 12, 2020

I’d appreciate it if you could try my suggestion; that would help make it an easy fix to help you, or anyone else with the same problem in the future.

@jtsom
Copy link
Author

jtsom commented Jan 12, 2020

Oh sorry, thought I had mentioned it. When I did change it to be in the format of '@src/feedback', it seem to do the right thing (opposed to keep suggesting the opposite ("add a .js", or "missing .js")

@ljharb
Copy link
Member

ljharb commented Jan 12, 2020

Great, thanks - that makes it straightforward to fix.

@ljharb ljharb changed the title Indecisive use of import/extensions import/extensions breaks when a scoped package's scope is just @ Jan 12, 2020
@wintercounter
Copy link

I had the same issue even tho I had import/extensions: 0. Changed to never, it's fine again.

@yordis
Copy link
Contributor

yordis commented Jan 14, 2020

@ljharb since you added help wanted tag. How can I help you? Do you have any suggestions or ideas on how to fix this?

@yordis
Copy link
Contributor

yordis commented Jan 14, 2020

Based on this, https://github.com/benmosher/eslint-plugin-import/blob/b4d5fd38a29073e273523e262d7d59a7c48e30fe/src/rules/extensions.js#L114

Which it does a look up on patterns https://github.com/benmosher/eslint-plugin-import/blob/b4d5fd38a29073e273523e262d7d59a7c48e30fe/src/rules/extensions.js#L110

It doesn't work for use since extension is an empty string, so I am not sure what would be the fix for this issue in terms of direction

Data input:

{ extension: '', importPath: '@/utils/i18n' }

@ljharb
Copy link
Member

ljharb commented Jan 14, 2020

@yordis in general that label means "please submit a PR that takes care of this" :-)

If that ends up with an answer of "configure it with an empty string for extension" then fine, but the issue i thought was that the "is scope" logic is not considering a scope of "@"

@yordis
Copy link
Contributor

yordis commented Jan 14, 2020

@ljharb would you mind helping to understand why is that scope thing important? I am guessing you are talking about https://github.com/benmosher/eslint-plugin-import/blob/b4d5fd38a29073e273523e262d7d59a7c48e30fe/src/core/importType.js#L53

@ljharb
Copy link
Member

ljharb commented Jan 14, 2020

yes - i believe changing that regex from /^@[^/]+\/?[^/]+/ to /^@[^/]*\/?[^/]+/ should fix the issue.

@yordis
Copy link
Contributor

yordis commented Jan 14, 2020

@ljharb confirmed, it fixes the issue

@yordis
Copy link
Contributor

yordis commented Jan 31, 2020

@ljharb is there any possibility to release the changes to NPM sooner? 🙏

@ljharb
Copy link
Member

ljharb commented Feb 2, 2020

Sooner than when?

I just released v2.20.1, so let's call it "sooner than tomorrow", and yes :-p

@yordis
Copy link
Contributor

yordis commented Feb 3, 2020

@ljharb 😢 I forgot to finish the sentences 😭 English is hard

Thanks a lot! Appreciated it.

@jtsom
Copy link
Author

jtsom commented Feb 3, 2020

So far, this version looks like it's fixed the issue. I'll do some more testing but 👍

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

Successfully merging a pull request may close this issue.

4 participants