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

False-negative on JSON import #1

Closed
tpluscode opened this issue Dec 22, 2022 · 2 comments · Fixed by #3
Closed

False-negative on JSON import #1

tpluscode opened this issue Dec 22, 2022 · 2 comments · Fixed by #3

Comments

@tpluscode
Copy link
Contributor

tpluscode commented Dec 22, 2022

This could potentially affect other non-script imports?

I have in my code this import:

import mockObjects from './mock-data.json' assert { type: 'json' }

The plugin complains and when "fixed", it changes that line into

import mockObjects from './mock-data.json.js' assert { type: 'json' }

I think that rule should only apply to some pattern, like/.+(t|j)s(x)?/ by default. Possibly configurable.

@tpluscode
Copy link
Contributor Author

Here's how I modified the rule to only apply to js(x) and ts(x) imported modules

diff --git a/node_modules/eslint-plugin-require-extensions/index.js b/node_modules/eslint-plugin-require-extensions/index.js
index d531a5e..bf1a83c 100644
--- a/node_modules/eslint-plugin-require-extensions/index.js
+++ b/node_modules/eslint-plugin-require-extensions/index.js
@@ -1,3 +1,14 @@
+const { existsSync } = require('fs')
+const { dirname, resolve } = require('path')
+
+const extensions = ['js', 'ts', 'jsx', 'tsx']
+
+function moduleExists(path) {
+  return ext => {
+    return existsSync(`${path}.${ext}`)
+  }
+}
+
 module.exports = {
     configs: {
         recommended: {
@@ -19,7 +30,9 @@ module.exports = {
                     const value = source.value;
                     if (!value) return;
 
-                    if (value.startsWith('.') && !value.endsWith('.js')) {
+                    if (!value.startsWith('.') || value.endsWith('.js')) return;
+
+                    if (extensions.some(moduleExists(resolve(dirname(context.getFilename()), value)))) {
                         context.report({
                             node,
                             message: 'Relative imports and exports must end with .js',

@steelerc69
Copy link

is this planned on getting accepted?

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

Successfully merging a pull request may close this issue.

2 participants