import-js / eslint-plugin-import

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

Error when importing .svelte files in .ts files #2837

Open vekunz opened 1 year ago

vekunz commented 1 year ago

Hi,

I am currently trying to migrate from eslint-plugin-svelte3 to eslint-plugin-svelte. Most things are working, but when I import a .svelte file from a .ts file (for example in unit tests), I get two errors from eslint-plugin-import (at least I think the errors comes from this plugin).

Error while parsing C:\pathToRepo\src\routes\Counter.svelte
Line 10, column 19: Unexpected token :
`parseForESLint` from parser `svelte-eslint-parser` is invalid and will just be ignored

and

C:\pathToRepo\svelte-eslint-import-error\src\routes\Counter.test.ts
  2:21  warning  Parse errors in imported module './Counter.svelte': parser.parse is not a function (undefined:undefined)  import/no-named-as-default

I created a reproduction repo https://github.com/vekunz/svelte-eslint-import-error. Probably I still have a configuration issue somewhere, but after several hours of reading documentation and GitHub Issues, I could not find any solution to this.

ljharb commented 1 year ago

That's expected since .svelte isn't a standard thing. You'll need to have an eslint-import resolver for svelte files (presumably you already have the TS resolver).

vekunz commented 1 year ago

OK, I thought that the svelte-eslint-parser is enougth.

    settings: {
        "import/resolver": {
            node: {},
            typescript: {
                alwaysTryTypes: false
            }
        },
        "import/parsers": {
            "svelte-eslint-parser": [".svelte"],
            "@typescript-eslint/parser": [".ts"],
            "espree": [".js"]
        }
    }

Do you know if there is any import resolver for svelte?

ljharb commented 1 year ago

Not that I'm aware of. Does https://github.com/import-js/eslint-plugin-import/issues/2407#issuecomment-1223394415 help?

vekunz commented 1 year ago

The recommendation there is to use eslint-plugin-svelte instead of the old eslint-plugin-svelte3, but I am already using the new plugin.

I remembered that eslint-plugin-import has an ignore-setting. With this option, ESLint at least doesn't show these errors, but the rest of ESLint seems to work.

  settings: {
    ...
    "import/ignore": [
      "\\.svelte$"
    ]
  }
ljharb commented 1 year ago

Until there's a resolver, that might be your only option.

fehnomenal commented 1 year ago

I have the same problem. svelte-plugin-eslint for typescript ist configured like this:

  overrides: [
    {
      files: ['*.svelte'],
      parser: 'svelte-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser',
      },
    },
  ],

To be able to import svelte files from js/ts we also need:

  settings: {
    'import/parsers': {
      'svelte-eslint-parser': ['.svelte'],
    },
  },

But I guess here the parserOptions.parser for typescript is missing.

EDIT:

My guess seems to be correct. The problem goes away after adding

  if (parserOrPath === 'svelte-eslint-parser') {
    parserOptions.parser = '@typescript-eslint/parser'
  }

somewhere in between https://github.com/import-js/eslint-plugin-import/blob/da71746147450a5b7eeeaca77efc521125d91d41/utils/parse.js#L64-L78

Of course hard coding this is not the final solution. I think the plugin should take the overrides section into account. I will try to get a PR ready.

ljharb commented 1 year ago

eslint merges overrides; plugins don’t ever consider it.

ljharb commented 1 year ago

ah but we’re not getting the config for other files perhaps? I’ll be interested to review your pr.

nonameolsson commented 1 year ago

What is the current status of this? 🙂

niemyjski commented 10 months ago

Also running into this.

insilications commented 8 months ago

Any solution?

JounQin commented 8 months ago

PR is always better than comments.

vekunz commented 8 months ago

We have this ESlint configuration, and this "works" (in the sense that we don't get errors).

{
  "settings": {
    "import/resolver": {
      "node": {},
      "typescript": {
        "alwaysTryTypes": false
      }
    },
    "import/parsers": {
      "svelte-eslint-parser": [".svelte"],
      "@typescript-eslint/parser": [".ts"],
      "espree": [".js"]
    },
    "import/ignore": [
      "\\.svelte$"
    ]
  }
}
insilications commented 8 months ago

We have this ESlint configuration, and this "works" (in the sense that we don't get errors).

{
  "settings": {
    "import/resolver": {
      "node": {},
      "typescript": {
        "alwaysTryTypes": false
      }
    },
    "import/parsers": {
      "svelte-eslint-parser": [".svelte"],
      "@typescript-eslint/parser": [".ts"],
      "espree": [".js"]
    },
    "import/ignore": [
      "\\.svelte$"
    ]
  }
}

Thanks! This works for now.

niemyjski commented 7 months ago

Svelte support is really lacking especially with import aliases.

ljharb commented 7 months ago

@niemyjski https://github.com/import-js/eslint-plugin-import/issues/2837#issuecomment-1646122243