admin管理员组文章数量:1425997
Our team always use eslint for proper coding standards to follow rules and regulations when it es to writing codes. We have used eslint-plugin-import. It pretty solves some of our problem but not all of it.
Take this example below:
Foo.js
const Foo = "Foo"
export default Foo
Foo.jsx
import Bar from './foo';
// This still works.
// It can cause confusion.
// I want to use the original exported default name Foo instead of Bar
// otherwise show some error in the Code Editor
What I want to happen is use the original name of the default export of that module or file. Otherwise throw some error in the code editor.
Appreciate if someone could help. Thanks in advance.
Our team always use eslint for proper coding standards to follow rules and regulations when it es to writing codes. We have used eslint-plugin-import. It pretty solves some of our problem but not all of it.
Take this example below:
Foo.js
const Foo = "Foo"
export default Foo
Foo.jsx
import Bar from './foo';
// This still works.
// It can cause confusion.
// I want to use the original exported default name Foo instead of Bar
// otherwise show some error in the Code Editor
What I want to happen is use the original name of the default export of that module or file. Otherwise throw some error in the code editor.
Appreciate if someone could help. Thanks in advance.
Share Improve this question edited Mar 13, 2019 at 7:57 KnowledgeSeeker asked Mar 13, 2019 at 7:50 KnowledgeSeekerKnowledgeSeeker 1,0982 gold badges21 silver badges46 bronze badges3 Answers
Reset to default 2The bad news is, there is currently no option for this.
The good news is, there is an active PR in eslint-plugin-import for exactly this feature, which only needs to be approved and the feature is ready to go. You can track the PR here.
I think what you want to do is use this rule from ESLint: no-named-as-default
You can activate this in your .eslintrc
file:
{"plugins": [ "import" ], "rules": { import/no-named-as-default }}
And for more information, you can go to the import
page.
Answering your question directly
In principle, you can write a custom Eslint rule that targets the ImportDefaultSpecifier
and asserts that the value is some expected value. In order to make this work though, you'd need to enforce some predictable relationship between the default variable and the file name (or something similar) so that your rule can determine whether the imported value is "incorrect" in some way. This opens it's own can of worms and kicks the can down the road a bit because you've now introduced a need for another ESLint rule to enforce the consistent naming convention for exports, and any exceptions will be annoying to deal with.
My own 2 cents
I'd remend solving this problem by leveraging the benefits of named exports instead since the main advantage of a default export over a named export is the ability to rename the exported value when importing it elsewhere.
Unless it's too burdensome, I'd suggest the following as a much easier path to what you want.
- Configure ESlint to enforce only using named exports via the
import/no-default-export
rule and converting your default exports to named exports. - Write a custom ESLint rule to detect
import { NamedExport as AliasedExport } from 'source';
statements and disallow them (this is quite easy, I'll show below how to do so) - Either publish your rule as package and install and use as normal, or use a plugin like
eslint-plugin-local-rules
so you can import your rule directly.
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
export const disallowImportAliasing = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
return {
ImportDeclaration(node) {
const importsWithAlias = node.specifiers.filter(specifier => specifier.local.name !== specifier.imported.name)
importsWithAlias.forEach(importStatement => {
context.report({
messageId: 'disallow-import-aliasing',
node: importStatement
})
})
},
};
},
meta: {
docs: {
description: 'Our team enforces a requirement to use imported variables exactly as named',
},
messages: {
'disallow-import-aliasing': 'Do not alias imports. Use the imported name as imported',
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
});
export default {
'disallow-import-aliasing': disallowImportAliasing,
} satisfies Record<string, ESLintUtils.RuleModule<any, any>>;
本文标签:
版权声明:本文标题:javascript - Enforce eslint to use original name of default exported module when importing it on some files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745417745a2657763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论