Skip to content

Comments

feat(imports): add ESLint imports plugin#973

Closed
susnux wants to merge 3 commits intomainfrom
feat/add-import-plugin
Closed

feat(imports): add ESLint imports plugin#973
susnux wants to merge 3 commits intomainfrom
feat/add-import-plugin

Conversation

@susnux
Copy link
Contributor

@susnux susnux commented Apr 9, 2025

Was also enabled in v8 of the config, so it was missing for now.
So this is the last thing for feature parity with v8.

@susnux susnux added enhancement New feature or request 3. to review labels Apr 9, 2025
@susnux susnux force-pushed the feat/add-import-plugin branch 2 times, most recently from f8d4295 to 695424c Compare April 9, 2025 15:41
Comment on lines +39 to +42
'import/resolver': {
typescript: true,
node: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't checked the docs, but can we disable resolving completely?
It seems unnecessary as bundler will check it anyway.
It has many places where it may break, like using <script setup>, especially in mixed environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also use the next resolver which uses oxc-resolve which is the resolver used by Rolldown.

Comment on lines +52 to +63
// Already included in error rules, but we need to allow raw imports
'import/no-unresolved': [
'error',
{
// Ignore Vite/Webpack query parameters, not supported by eslint-plugin-import
// https://github.com/import-js/eslint-plugin-import/issues/2562
ignore: [
'\\?raw$',
'\\?url$',
],
},
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, does this rule prevents errors that are not prevented by a bundler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on your config yes, e.g. if it otherwise will externalize the imports.

@ShGKme
Copy link
Contributor

ShGKme commented Apr 9, 2025

import/no-default-export? 😀

@susnux
Copy link
Contributor Author

susnux commented Apr 9, 2025

import/no-default-export

This is quite drastic, there are at least some good reasons for default exports:

  • config files
  • Vue components

@ShGKme
Copy link
Contributor

ShGKme commented Apr 9, 2025

  • config files
  • Vue components

can be easily excluded 👀

@susnux
Copy link
Contributor Author

susnux commented Apr 9, 2025

can be easily excluded 👀

Then lets discuss this in a follow up similar to import ordering

@ShGKme
Copy link
Contributor

ShGKme commented Apr 9, 2025

Haven't checked the docs, but can we disable resolving completely?
It seems unnecessary as bundler will check it anyway.
It has many places where it may break, like using <script setup>, especially in mixed environment.

Just checked and yes, still the same issues.

It is not possible to use it in mixed project (TS + JS). Looks like it tries to parse TS file as JS and dies on importing types.

Or maybe we need to set the syntax for import plugin...

@ShGKme
Copy link
Contributor

ShGKme commented Apr 9, 2025

Example:

/home/shgk/nextcloud/talk-desktop/src/shared/setupWebPage.js
   9:25  error    Parse errors in imported module '../app/AppData.js': Unexpected token = (19:12)          import/namespace
   9:25  warning  Parse errors in imported module '../app/AppData.js': Unexpected token = (19:12)          import/no-deprecated
  10:29  error    Parse errors in imported module './globals/globals.js': Unexpected token { (58:12)       import/namespace
  10:29  warning  Parse errors in imported module './globals/globals.js': Unexpected token { (58:12)       import/no-deprecated
  14:32  error    Parse errors in imported module '../app/appData.service.js': Unexpected token . (60:24)  import/namespace
  14:32  warning  Parse errors in imported module '../app/appData.service.js': Unexpected token . (60:24)  import/no-deprecated

In AppData.js its

export class AppData {
	serverUrl = null // Line 19 <-----

In globals.js:

	try {
		return require(`@global-styles/core/img/filetypes/${icon}.svg`)
	} catch { // Line 58 <-----
		return undefined
	}

In appData.service.js

if (error.response?.status === 401) { // Line 60 <----

Signed-off-by: Grigorii K. Shartsev <[email protected]>
@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

eslint-plugin-import resets ecmaVersion to 2018.

I've pushed a commit to reset it back to latest.

Alternative - set such options after import config.

@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

Another problem:

error  Using exported name 'NcRichText' as identifier for default import  import/no-named-as-default

on

import NcRichText from '@nextcloud/vue/components/NcRichText'

because we export NcRichText twice: as default and as named

export default NcRichText

export {
	NcRichText,
	NcReferenceList,
	NcReferenceWidget,
	NcReferencePicker,
	NcReferencePickerModal,
	NcSearch,

	registerWidget,
	renderWidget,
	isWidgetRegistered,

	NcCustomPickerRenderResult,
	registerCustomPickerElement,
	renderCustomPickerElement,
	isCustomPickerElementRegistered,
	getLinkWithPicker,
	anyLinkProviderId,
	getProvider,
	getProviders,
	sortProviders,
	searchProvider,
}

So the plugin wants to import it:

import { NcRichText } from '@nextcloud/vue/components/NcRichText'
// or
import NcRichTextComponent from '@nextcloud/vue/components/NcRichText'

@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

And another one:

<script lang="ts">
export default {
	inheritAttrs: false,
}
</script>

<script setup lang="ts">
import { ref } from 'vue'

const flag = ref(false)
</script>

fails with

error  Import in body of module; reorder to top  import/first

And, what is awful, ⚠️ it's got auto-fixed to invalid ⚠️

<script setup lang="ts">
import { ref } from 'vue'

export default {
	inheritAttrs: false,
}

const flag = ref(false)
</script>

PS: This is fine:

<script setup lang="ts">
import { ref } from 'vue'

const flag = ref(false)
</script>

<script lang="ts">
export default {
	inheritAttrs: false,
}
</script>

@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

And this is ruined completely.

<script setup lang="ts">
import { ref } from 'vue'

const flag = ref(false)

log(foo)
</script>

<script lang="ts">
import { Foo } from 'foo'

const foo = new Foo()

export default {
	inheritAttrs: false,
}
</script>

@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

Auto-fixed to completely broken component

image

@ShGKme
Copy link
Contributor

ShGKme commented Apr 10, 2025

Proposal: disable import/first completely.

At least TS checks it, and developers seem to follow this rule without linting. While issues with the rule are serious.

@ShGKme
Copy link
Contributor

ShGKme commented Apr 11, 2025

@susnux
Copy link
Contributor Author

susnux commented Apr 20, 2025

🗑️ in favor of #981

@susnux susnux closed this Apr 20, 2025
@susnux susnux deleted the feat/add-import-plugin branch April 20, 2025 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants