diff --git a/.changeset/tasty-colts-cover.md b/.changeset/tasty-colts-cover.md new file mode 100644 index 000000000..0fd37489d --- /dev/null +++ b/.changeset/tasty-colts-cover.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-import-x": minor +--- + +Added `no-rename-default` that forbid importing a default export by a different name. Originally created by @whitneytit, ported by @SukkaW diff --git a/README.md b/README.md index d39675091..1d7c5a4fe 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a | [no-mutable-exports](docs/rules/no-mutable-exports.md) | Forbid the use of mutable exports with `var` or `let`. | | | | | | | | [no-named-as-default](docs/rules/no-named-as-default.md) | Forbid use of exported name as identifier of default export. | | ☑️ 🚸 | | | | | | [no-named-as-default-member](docs/rules/no-named-as-default-member.md) | Forbid use of exported name as property of default export. | | ☑️ 🚸 | | | | | +| [no-rename-default](docs/rules/no-rename-default.md) | Forbid importing a default export by a different name. | | 🚸 | | | | | | [no-unused-modules](docs/rules/no-unused-modules.md) | Forbid modules without exports, or exports without matching import in another module. | | | | | | | ### Module systems diff --git a/docs/rules/no-rename-default.md b/docs/rules/no-rename-default.md new file mode 100644 index 000000000..4fbeb507c --- /dev/null +++ b/docs/rules/no-rename-default.md @@ -0,0 +1,31 @@ +# import-x/no-rename-default + +⚠️ This rule _warns_ in the 🚸 `warnings` config. + + + +Prohibit importing a default export by another name. + +## Rule Details + +Given: + +```js +// api/get-users.js +export default async function getUsers() {} +``` + +...this would be valid: + +```js +import getUsers from './api/get-users.js' +``` + +...and the following would be reported: + +```js +// Caution: `get-users.js` has a default export `getUsers`. +// This imports `getUsers` as `findUsers`. +// Check if you meant to write `import getUsers from './api/get-users'` instead. +import findUsers from './get-users' +``` diff --git a/src/config/flat/warnings.ts b/src/config/flat/warnings.ts index ca9dde2db..c9c20995a 100644 --- a/src/config/flat/warnings.ts +++ b/src/config/flat/warnings.ts @@ -7,6 +7,7 @@ export default { rules: { 'import-x/no-named-as-default': 1, 'import-x/no-named-as-default-member': 1, + 'import-x/no-rename-default': 1, 'import-x/no-duplicates': 1, }, } satisfies PluginFlatBaseConfig diff --git a/src/config/warnings.ts b/src/config/warnings.ts index fe111e844..56fc93e10 100644 --- a/src/config/warnings.ts +++ b/src/config/warnings.ts @@ -8,6 +8,7 @@ export = { rules: { 'import-x/no-named-as-default': 1, 'import-x/no-named-as-default-member': 1, + 'import-x/no-rename-default': 1, 'import-x/no-duplicates': 1, }, } satisfies PluginConfig diff --git a/src/index.ts b/src/index.ts index 2d28d25cf..0c19a94fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,6 @@ import recommended from './config/recommended' import stage0 from './config/stage-0' import typescript from './config/typescript' import warnings from './config/warnings' - // rules import consistentTypeSpecifierStyle from './rules/consistent-type-specifier-style' import default_ from './rules/default' @@ -55,6 +54,7 @@ import noNamespace from './rules/no-namespace' import noNodejsModules from './rules/no-nodejs-modules' import noRelativePackages from './rules/no-relative-packages' import noRelativeParentImports from './rules/no-relative-parent-imports' +import noRenameDefault from './rules/no-rename-default' import noRestrictedPaths from './rules/no-restricted-paths' import noSelfImport from './rules/no-self-import' import noUnassignedImport from './rules/no-unassigned-import' @@ -94,6 +94,7 @@ const rules = { 'no-named-as-default': noNamedAsDefault, 'no-named-as-default-member': noNamedAsDefaultMember, 'no-anonymous-default-export': noAnonymousDefaultExport, + 'no-rename-default': noRenameDefault, 'no-unused-modules': noUnusedModules, 'no-commonjs': noCommonjs, diff --git a/src/rules/no-rename-default.ts b/src/rules/no-rename-default.ts new file mode 100644 index 000000000..084898541 --- /dev/null +++ b/src/rules/no-rename-default.ts @@ -0,0 +1,332 @@ +/** + * @fileOverview Rule to warn about importing a default export by different name + * @author James Whitney + * @author Sukka - Port to TypeScript + */ + +import path from 'node:path' + +import type { TSESTree } from '@typescript-eslint/typescript-estree' + +import { createRule, ExportMap } from '../utils' +import type { ModuleOptions } from '../utils' + +type Options = ModuleOptions & { + preventRenamingBindings?: boolean +} + +type MessageId = 'renameDefault' + +export = createRule<[Options?], MessageId>({ + name: 'no-rename-default', + meta: { + type: 'suggestion', + docs: { + category: 'Helpful warnings', + description: 'Forbid importing a default export by a different name.', + }, + schema: [ + { + type: 'object', + properties: { + commonjs: { + default: false, + type: 'boolean', + }, + preventRenamingBindings: { + default: true, + type: 'boolean', + }, + }, + additionalProperties: false, + }, + ], + messages: { + renameDefault: + 'Caution: `{{importBasename}}` has a default export `{{defaultExportName}}`. This {{requiresOrImports}} `{{defaultExportName}}` as `{{importName}}`. Check if you meant to write `{{suggestion}}` instead.', + }, + }, + defaultOptions: [], + create(context) { + const { commonjs = false, preventRenamingBindings = true } = + context.options[0] || {} + + function getDefaultExportName( + targetNode: + | TSESTree.ExportSpecifier + | TSESTree.DefaultExportDeclarations + | TSESTree.CallExpressionArgument, + ) { + if (targetNode == null) { + return + } + switch (targetNode.type) { + case 'AssignmentExpression': { + if (!preventRenamingBindings) { + // Allow assignments to be renamed when the `preventRenamingBindings` + // option is set to `false`. + // + // export default Foo = 1; + return + } + if (targetNode.left.type !== 'Identifier') { + return + } + return targetNode.left.name + } + case 'CallExpression': { + const [argumentNode] = targetNode.arguments + return getDefaultExportName(argumentNode) + } + case 'ClassDeclaration': { + if (targetNode.id && typeof targetNode.id.name === 'string') { + return targetNode.id.name + } + // Here we have an anonymous class. We can skip here. + return + } + case 'ExportSpecifier': { + return targetNode.local.name + } + case 'FunctionDeclaration': { + return targetNode.id?.name + } + case 'Identifier': { + if (!preventRenamingBindings) { + // Allow identifier to be renamed when the `preventRenamingBindings` + // option is set to `false`. + // + // const foo = 'foo'; + // export default foo; + return + } + return targetNode.name + } + default: + // This type of node is not handled. + // Returning `undefined` here signifies this and causes the check to + // exit early. + } + } + + function getExportMap(source: TSESTree.StringLiteral | null) { + if (!source) { + return + } + const exportMap = ExportMap.get(source.value, context) + if (exportMap == null) { + return + } + if (exportMap.errors.length > 0) { + exportMap.reportErrors(context, { source }) + return + } + return exportMap + } + + function handleImport( + node: TSESTree.ImportDefaultSpecifier | TSESTree.ImportSpecifier, + ) { + const exportMap = getExportMap(node.parent.source) + if (exportMap == null) { + return + } + + const defaultExportNode = getDefaultExportNode(exportMap) + if (defaultExportNode == null) { + return + } + + const defaultExportName = getDefaultExportName(defaultExportNode) + if (defaultExportName === undefined) { + return + } + + const importTarget = node.parent.source?.value + const importBasename = path.basename(exportMap.path) + + if (node.type === 'ImportDefaultSpecifier') { + const importName = node.local.name + + if (importName === defaultExportName) { + return + } + + context.report({ + node, + messageId: 'renameDefault', + data: { + importBasename, + defaultExportName, + importName, + requiresOrImports: 'imports', + suggestion: `import ${defaultExportName} from '${importTarget}'`, + }, + }) + + return + } + + if (node.type !== 'ImportSpecifier') { + return + } + + if (node.imported.name !== 'default') { + return + } + + const actualImportedName = node.local.name + + if (actualImportedName === defaultExportName) { + return + } + + context.report({ + node, + messageId: 'renameDefault', + data: { + importBasename, + defaultExportName, + importName: actualImportedName, + requiresOrImports: 'imports', + suggestion: `import { default as ${defaultExportName} } from '${importTarget}'`, + }, + }) + } + + function handleRequire(node: TSESTree.VariableDeclarator) { + if ( + !commonjs || + node.type !== 'VariableDeclarator' || + !node.id || + !(node.id.type === 'Identifier' || node.id.type === 'ObjectPattern') || + !node.init || + node.init.type !== 'CallExpression' + ) { + return + } + + let defaultDestructure + if (node.id.type === 'ObjectPattern') { + defaultDestructure = findDefaultDestructure(node.id.properties) + if (defaultDestructure === undefined) { + return + } + } + + const call = node.init + const [source] = call.arguments + + if ( + call.callee.type !== 'Identifier' || + call.callee.name !== 'require' || + call.arguments.length !== 1 || + source.type !== 'Literal' || + typeof source.value !== 'string' + ) { + return + } + + const exportMap = getExportMap(source) + if (exportMap == null) { + return + } + + const defaultExportNode = getDefaultExportNode(exportMap) + if (defaultExportNode == null) { + return + } + + const defaultExportName = getDefaultExportName(defaultExportNode) + const requireTarget = source.value + const requireBasename = path.basename(exportMap.path) + + let requireName + if (node.id.type === 'Identifier') { + requireName = node.id.name + } else if (defaultDestructure?.value?.type === 'Identifier') { + requireName = defaultDestructure.value.name + } else { + requireName = '' + } + + if (defaultExportName === undefined) { + return + } + + if (requireName === defaultExportName) { + return + } + + if (node.id.type === 'Identifier') { + context.report({ + node, + messageId: 'renameDefault', + data: { + importBasename: requireBasename, + defaultExportName, + importName: requireName, + requiresOrImports: 'requires', + suggestion: `const ${defaultExportName} = require('${requireTarget}')`, + }, + }) + return + } + + context.report({ + node, + messageId: 'renameDefault', + data: { + importBasename: requireBasename, + defaultExportName, + importName: requireName, + requiresOrImports: 'requires', + suggestion: `const { default: ${defaultExportName} } = require('${requireTarget}')`, + }, + }) + } + + return { + ImportDefaultSpecifier: handleImport, + ImportSpecifier: handleImport, + VariableDeclarator: handleRequire, + } + }, +}) + +function findDefaultDestructure( + properties: Array, +) { + const found = properties.find(property => { + if ( + 'key' in property && + 'name' in property.key && + property.key.name === 'default' + ) { + return property + } + }) + return found +} + +function getDefaultExportNode( + exportMap: ExportMap, +): TSESTree.DefaultExportDeclarations | TSESTree.ExportSpecifier | undefined { + const defaultExportNode = exportMap.exports.get('default') + if (defaultExportNode == null) { + return + } + switch (defaultExportNode.type) { + case 'ExportDefaultDeclaration': { + return defaultExportNode.declaration + } + case 'ExportNamedDeclaration': { + return defaultExportNode.specifiers.find( + specifier => specifier.exported.name === 'default', + ) + } + default: { + return + } + } +} diff --git a/src/utils/export-map.ts b/src/utils/export-map.ts index 2153458ef..84e548f5e 100644 --- a/src/utils/export-map.ts +++ b/src/utils/export-map.ts @@ -271,6 +271,7 @@ export class ExportMap { break } case 'ExportNamespaceSpecifier': { + m.exports.set(s.exported.name, n) m.namespace.set( s.exported.name, Object.defineProperty(exportMeta, 'namespace', { @@ -282,6 +283,7 @@ export class ExportMap { return } case 'ExportAllDeclaration': { + m.exports.set(s.exported!.name, n) m.namespace.set( getValue(s.exported!), addNamespace(exportMeta, s.exported!), @@ -291,6 +293,7 @@ export class ExportMap { } case 'ExportSpecifier': { if (!('source' in n && n.source)) { + m.exports.set(s.exported!.name, n) m.namespace.set( getValue(s.exported), addNamespace(exportMeta, s.local), @@ -435,6 +438,7 @@ export class ExportMap { if (n.declaration.type === 'Identifier') { addNamespace(exportMeta, n.declaration) } + m.exports.set('default', n) m.namespace.set('default', exportMeta) continue } @@ -485,6 +489,7 @@ export class ExportMap { // @ts-expect-error - legacy parser type case 'TSAbstractClassDeclaration': case 'TSModuleDeclaration': { + m.exports.set((n.declaration.id as TSESTree.Identifier).name, n) m.namespace.set( (n.declaration.id as TSESTree.Identifier).name, captureDoc(source, docStyleParsers, n), @@ -494,12 +499,13 @@ export class ExportMap { /* eslint-enable no-fallthrough */ case 'VariableDeclaration': { for (const d of n.declaration.declarations) { - recursivePatternCapture(d.id, id => + recursivePatternCapture(d.id, id => { + m.exports.set((id as TSESTree.Identifier).name, n) m.namespace.set( (id as TSESTree.Identifier).name, captureDoc(source, docStyleParsers, d, n), - ), - ) + ) + }) } break } @@ -560,6 +566,7 @@ export class ExportMap { }) if (exportedDecls.length === 0) { + m.exports.set('default', n) // Export is not referencing any local declaration, must be re-exporting m.namespace.set('default', captureDoc(source, docStyleParsers, n)) continue @@ -569,6 +576,7 @@ export class ExportMap { isEsModuleInteropTrue() && // esModuleInterop is on in tsconfig !m.namespace.has('default') // and default isn't added already ) { + m.exports.set('default', n) m.namespace.set('default', {}) // add default export } @@ -578,6 +586,8 @@ export class ExportMap { // @ts-expect-error - legacy parser type if (type === 'TSModuleDeclaration') { + // @ts-expect-error - legacy parser type + m.exports.set((decl.body.id as TSESTree.Identifier).name, n) m.namespace.set( // @ts-expect-error - legacy parser type (decl.body.id as TSESTree.Identifier).name, @@ -607,7 +617,8 @@ export class ExportMap { // TypeScript can check this for us; we needn't } else if (namespaceDecl.type === 'VariableDeclaration') { for (const d of namespaceDecl.declarations) - recursivePatternCapture(d.id, id => + recursivePatternCapture(d.id, id => { + m.exports.set((id as TSESTree.Identifier).name, n) m.namespace.set( (id as TSESTree.Identifier).name, captureDoc( @@ -617,9 +628,13 @@ export class ExportMap { namespaceDecl, moduleBlockNode, ), - ), - ) + ) + }) } else if ('id' in namespaceDecl) { + m.exports.set( + (namespaceDecl.id as TSESTree.Identifier).name, + n, + ) m.namespace.set( (namespaceDecl.id as TSESTree.Identifier).name, captureDoc(source, docStyleParsers, moduleBlockNode), @@ -629,6 +644,7 @@ export class ExportMap { } } else { // Export as default + m.exports.set('default', n) m.namespace.set( 'default', captureDoc(source, docStyleParsers, decl), @@ -663,6 +679,7 @@ export class ExportMap { m.namespace.size > 0 && // anything is exported !m.namespace.has('default') // and default isn't added already ) { + m.exports.set('default', ast.body[0]) // add default export m.namespace.set('default', {}) // add default export } @@ -697,6 +714,7 @@ export class ExportMap { * dependencies of this module that are not explicitly re-exported */ imports = new Map() + exports = new Map() errors: ParseError[] = [] diff --git a/test/fixtures/no-rename-default/anonymous-arrow-async.js b/test/fixtures/no-rename-default/anonymous-arrow-async.js new file mode 100644 index 000000000..18554e89e --- /dev/null +++ b/test/fixtures/no-rename-default/anonymous-arrow-async.js @@ -0,0 +1 @@ +export default async () => {}; diff --git a/test/fixtures/no-rename-default/anonymous-arrow.js b/test/fixtures/no-rename-default/anonymous-arrow.js new file mode 100644 index 000000000..2d1ec2382 --- /dev/null +++ b/test/fixtures/no-rename-default/anonymous-arrow.js @@ -0,0 +1 @@ +export default () => {}; diff --git a/test/fixtures/no-rename-default/anonymous-class.js b/test/fixtures/no-rename-default/anonymous-class.js new file mode 100644 index 000000000..0a6916a6d --- /dev/null +++ b/test/fixtures/no-rename-default/anonymous-class.js @@ -0,0 +1 @@ +export default class {}; diff --git a/test/fixtures/no-rename-default/anonymous-object.js b/test/fixtures/no-rename-default/anonymous-object.js new file mode 100644 index 000000000..ff8b4c563 --- /dev/null +++ b/test/fixtures/no-rename-default/anonymous-object.js @@ -0,0 +1 @@ +export default {}; diff --git a/test/fixtures/no-rename-default/anonymous-primitive.js b/test/fixtures/no-rename-default/anonymous-primitive.js new file mode 100644 index 000000000..05e087120 --- /dev/null +++ b/test/fixtures/no-rename-default/anonymous-primitive.js @@ -0,0 +1 @@ +export default 123; diff --git a/test/fixtures/no-rename-default/assign-arrow-async.js b/test/fixtures/no-rename-default/assign-arrow-async.js new file mode 100644 index 000000000..85c31b471 --- /dev/null +++ b/test/fixtures/no-rename-default/assign-arrow-async.js @@ -0,0 +1 @@ +export default arrowAsync = async () => {}; diff --git a/test/fixtures/no-rename-default/assign-arrow.js b/test/fixtures/no-rename-default/assign-arrow.js new file mode 100644 index 000000000..970ba63d1 --- /dev/null +++ b/test/fixtures/no-rename-default/assign-arrow.js @@ -0,0 +1 @@ +export default arrow = () => {}; diff --git a/test/fixtures/no-rename-default/assign-class-named.js b/test/fixtures/no-rename-default/assign-class-named.js new file mode 100644 index 000000000..90f15bb0a --- /dev/null +++ b/test/fixtures/no-rename-default/assign-class-named.js @@ -0,0 +1 @@ +export default User = class MyUser {} diff --git a/test/fixtures/no-rename-default/assign-class.js b/test/fixtures/no-rename-default/assign-class.js new file mode 100644 index 000000000..3b46709fa --- /dev/null +++ b/test/fixtures/no-rename-default/assign-class.js @@ -0,0 +1 @@ +export default User = class {} diff --git a/test/fixtures/no-rename-default/assign-fn-named.js b/test/fixtures/no-rename-default/assign-fn-named.js new file mode 100644 index 000000000..ccbf54a54 --- /dev/null +++ b/test/fixtures/no-rename-default/assign-fn-named.js @@ -0,0 +1 @@ +export default fn = function myFn() {}; diff --git a/test/fixtures/no-rename-default/assign-fn.js b/test/fixtures/no-rename-default/assign-fn.js new file mode 100644 index 000000000..f3da45e3e --- /dev/null +++ b/test/fixtures/no-rename-default/assign-fn.js @@ -0,0 +1 @@ +export default fn = function () {}; diff --git a/test/fixtures/no-rename-default/assign-generator-named.js b/test/fixtures/no-rename-default/assign-generator-named.js new file mode 100644 index 000000000..737411e01 --- /dev/null +++ b/test/fixtures/no-rename-default/assign-generator-named.js @@ -0,0 +1 @@ +export default generator = function* myGenerator() {}; diff --git a/test/fixtures/no-rename-default/assign-generator.js b/test/fixtures/no-rename-default/assign-generator.js new file mode 100644 index 000000000..68e256de4 --- /dev/null +++ b/test/fixtures/no-rename-default/assign-generator.js @@ -0,0 +1 @@ +export default generator = function* () {}; diff --git a/test/fixtures/no-rename-default/class-user.js b/test/fixtures/no-rename-default/class-user.js new file mode 100644 index 000000000..e6c5fb052 --- /dev/null +++ b/test/fixtures/no-rename-default/class-user.js @@ -0,0 +1 @@ +export default class User {}; diff --git a/test/fixtures/no-rename-default/const-bar.js b/test/fixtures/no-rename-default/const-bar.js new file mode 100644 index 000000000..da071dcc1 --- /dev/null +++ b/test/fixtures/no-rename-default/const-bar.js @@ -0,0 +1,6 @@ +export const barNamed1 = 'bar-named-1'; +export const barNamed2 = 'bar-named-2'; + +const bar = 'bar'; + +export default bar; diff --git a/test/fixtures/no-rename-default/const-foo.js b/test/fixtures/no-rename-default/const-foo.js new file mode 100644 index 000000000..a85147d86 --- /dev/null +++ b/test/fixtures/no-rename-default/const-foo.js @@ -0,0 +1,6 @@ +export const fooNamed1 = 'foo-named-1'; +export const fooNamed2 = 'foo-named-2'; + +const foo = 'foo'; + +export default foo; diff --git a/test/fixtures/no-rename-default/fn-get-users-sync.js b/test/fixtures/no-rename-default/fn-get-users-sync.js new file mode 100644 index 000000000..c1f60a47f --- /dev/null +++ b/test/fixtures/no-rename-default/fn-get-users-sync.js @@ -0,0 +1 @@ +export default function getUsersSync() {} diff --git a/test/fixtures/no-rename-default/fn-get-users.js b/test/fixtures/no-rename-default/fn-get-users.js new file mode 100644 index 000000000..9b77ba196 --- /dev/null +++ b/test/fixtures/no-rename-default/fn-get-users.js @@ -0,0 +1 @@ +export default async function getUsers() {} diff --git a/test/fixtures/no-rename-default/generator-reader.js b/test/fixtures/no-rename-default/generator-reader.js new file mode 100644 index 000000000..358f1186c --- /dev/null +++ b/test/fixtures/no-rename-default/generator-reader.js @@ -0,0 +1 @@ +export default function* reader() {} diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/binding-const-rename-fn.js b/test/fixtures/no-rename-default/pr-3006-feedback/binding-const-rename-fn.js new file mode 100644 index 000000000..139b0492c --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/binding-const-rename-fn.js @@ -0,0 +1,3 @@ +const foo = function bar() {}; + +export default foo; diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/binding-fn-rename.js b/test/fixtures/no-rename-default/pr-3006-feedback/binding-fn-rename.js new file mode 100644 index 000000000..e16264a21 --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/binding-fn-rename.js @@ -0,0 +1,3 @@ +function bar() {} + +export default bar; diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-foo.js b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-foo.js new file mode 100644 index 000000000..36904cdfd --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-foo.js @@ -0,0 +1,4 @@ +import foo from '../default-const-foo'; +import withLogger from './hoc-with-logger'; + +export default withLogger(foo); diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-get-users.js b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-get-users.js new file mode 100644 index 000000000..6d16feefb --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-get-users.js @@ -0,0 +1,4 @@ +import getUsers from '../default-fn-get-users'; +import withLogger from './hoc-with-logger'; + +export default withLogger(getUsers); diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-with-auth-for-get-users.js b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-with-auth-for-get-users.js new file mode 100644 index 000000000..53b24539b --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/binding-hoc-with-logger-with-auth-for-get-users.js @@ -0,0 +1,5 @@ +import getUsers from '../default-fn-get-users'; +import withAuth from './hoc-with-auth'; +import withLogger from './hoc-with-logger'; + +export default withLogger(withAuth(getUsers)); diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-auth.js b/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-auth.js new file mode 100644 index 000000000..03c9006a2 --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-auth.js @@ -0,0 +1,6 @@ +export default function withAuth(fn) { + return function innerAuth(...args) { + const auth = {}; + return fn.call(null, auth, ...args); + } +} diff --git a/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-logger.js b/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-logger.js new file mode 100644 index 000000000..bead43621 --- /dev/null +++ b/test/fixtures/no-rename-default/pr-3006-feedback/hoc-with-logger.js @@ -0,0 +1,6 @@ +export default function withLogger(fn) { + return function innerLogger(...args) { + console.log(`${fn.name} called`); + return fn.apply(null, args); + } +} diff --git a/test/fixtures/no-rename-default/typescript-default.d.ts b/test/fixtures/no-rename-default/typescript-default.d.ts new file mode 100644 index 000000000..deac53f85 --- /dev/null +++ b/test/fixtures/no-rename-default/typescript-default.d.ts @@ -0,0 +1,3 @@ +declare const foo: {}; + +export default foo; diff --git a/test/package.spec.ts b/test/package.spec.ts index 9488e4797..33c63dc27 100644 --- a/test/package.spec.ts +++ b/test/package.spec.ts @@ -52,6 +52,7 @@ describe('package', () => { continue } for (const rule of Object.keys(config.rules)) { + console.log({ rule, preamble }) expect(() => require(getRulePath(rule.slice(preamble.length))), ).not.toThrow() diff --git a/test/rules/no-rename-default.spec.ts b/test/rules/no-rename-default.spec.ts new file mode 100644 index 000000000..c7e040979 --- /dev/null +++ b/test/rules/no-rename-default.spec.ts @@ -0,0 +1,898 @@ +import { RuleTester } from '@typescript-eslint/rule-tester' + +import { parsers, test } from '../utils' + +import rule from 'eslint-plugin-import-x/rules/no-rename-default' + +const ruleTester = new RuleTester() + +// IMPORT +// anonymous-arrow.js +// anonymous-arrow-async.js +// anonymous-class.js +// anonymous-object.js +// anonymous-primitive.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import _ from './no-rename-default/anonymous-arrow'`, + `import _ from './no-rename-default/anonymous-arrow-async'`, + `import _ from './no-rename-default/anonymous-class'`, + `import _ from './no-rename-default/anonymous-object'`, + `import _ from './no-rename-default/anonymous-primitive'`, + ], + invalid: [], +}) + +// REQUIRE { commonjs: true } +// anonymous-arrow.js +// anonymous-arrow-async.js +// anonymous-class.js +// anonymous-object.js +// anonymous-primitive.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const _ = require('./no-rename-default/anonymous-arrow')`, + options: [{ commonjs: true }], + }), + test({ + code: `const _ = require('./no-rename-default/anonymous-arrow-async')`, + options: [{ commonjs: true }], + }), + test({ + code: `const _ = require('./no-rename-default/anonymous-class')`, + options: [{ commonjs: true }], + }), + test({ + code: `const _ = require('./no-rename-default/anonymous-object')`, + options: [{ commonjs: true }], + }), + test({ + code: `const _ = require('./no-rename-default/anonymous-primitive')`, + options: [{ commonjs: true }], + }), + ], + invalid: [], +}) + +// IMPORT +// assign-arrow.js +// assign-arrow-async.js +// assign-class.js +// assign-class-named.js +// assign-fn.js +// assign-fn-named.js +// assign-generator.js +// assign-generator-named.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import arrow from './no-rename-default/assign-arrow'`, + `import arrowAsync from './no-rename-default/assign-arrow-async'`, + `import User from './no-rename-default/assign-class'`, + `import User from './no-rename-default/assign-class-named'`, + `import fn from './no-rename-default/assign-fn'`, + `import fn from './no-rename-default/assign-fn-named'`, + `import generator from './no-rename-default/assign-generator'`, + `import generator from './no-rename-default/assign-generator-named'`, + ], + invalid: [ + test({ + code: `import myArrow from './no-rename-default/assign-arrow'`, + errors: [ + { + message: + "Caution: `assign-arrow.js` has a default export `arrow`. This imports `arrow` as `myArrow`. Check if you meant to write `import arrow from './no-rename-default/assign-arrow'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import myArrowAsync from './no-rename-default/assign-arrow-async'`, + errors: [ + { + message: + "Caution: `assign-arrow-async.js` has a default export `arrowAsync`. This imports `arrowAsync` as `myArrowAsync`. Check if you meant to write `import arrowAsync from './no-rename-default/assign-arrow-async'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import MyUser from './no-rename-default/assign-class'`, + errors: [ + { + message: + "Caution: `assign-class.js` has a default export `User`. This imports `User` as `MyUser`. Check if you meant to write `import User from './no-rename-default/assign-class'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import MyUser from './no-rename-default/assign-class-named'`, + errors: [ + { + message: + "Caution: `assign-class-named.js` has a default export `User`. This imports `User` as `MyUser`. Check if you meant to write `import User from './no-rename-default/assign-class-named'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import myFn from './no-rename-default/assign-fn'`, + errors: [ + { + message: + "Caution: `assign-fn.js` has a default export `fn`. This imports `fn` as `myFn`. Check if you meant to write `import fn from './no-rename-default/assign-fn'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import myFn from './no-rename-default/assign-fn-named'`, + errors: [ + { + message: + "Caution: `assign-fn-named.js` has a default export `fn`. This imports `fn` as `myFn`. Check if you meant to write `import fn from './no-rename-default/assign-fn-named'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import myGenerator from './no-rename-default/assign-generator'`, + errors: [ + { + message: + "Caution: `assign-generator.js` has a default export `generator`. This imports `generator` as `myGenerator`. Check if you meant to write `import generator from './no-rename-default/assign-generator'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import myGenerator from './no-rename-default/assign-generator-named'`, + errors: [ + { + message: + "Caution: `assign-generator-named.js` has a default export `generator`. This imports `generator` as `myGenerator`. Check if you meant to write `import generator from './no-rename-default/assign-generator-named'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + ], +}) + +// IMPORT { preventRenamingBindings: false } +// assign-arrow.js +// assign-arrow-async.js +// assign-class.js +// assign-class-named.js +// assign-fn.js +// assign-fn-named.js +// assign-generator.js +// assign-generator-named.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `import myArrow from './no-rename-default/assign-arrow'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import myArrowAsync from './no-rename-default/assign-arrow-async'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import MyUser from './no-rename-default/assign-class'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import MyUser from './no-rename-default/assign-class-named'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import myFn from './no-rename-default/assign-fn'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import myFn from './no-rename-default/assign-fn-named'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import myGenerator from './no-rename-default/assign-generator'`, + options: [{ preventRenamingBindings: false }], + }), + test({ + code: `import myGenerator from './no-rename-default/assign-generator-named'`, + options: [{ preventRenamingBindings: false }], + }), + ], + invalid: [], +}) + +// REQUIRE { commonjs: true } +// assign-arrow.js +// assign-arrow-async.js +// assign-class.js +// assign-class-named.js +// assign-fn.js +// assign-fn-named.js +// assign-generator.js +// assign-generator-named.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const arrow = require('./no-rename-default/assign-arrow')`, + options: [{ commonjs: true }], + }), + test({ + code: `const arrowAsync = require('./no-rename-default/assign-arrow-async')`, + options: [{ commonjs: true }], + }), + test({ + code: `const User = require('./no-rename-default/assign-class')`, + options: [{ commonjs: true }], + }), + test({ + code: `const User = require('./no-rename-default/assign-class-named')`, + options: [{ commonjs: true }], + }), + test({ + code: `const fn = require('./no-rename-default/assign-fn')`, + options: [{ commonjs: true }], + }), + test({ + code: `const fn = require('./no-rename-default/assign-fn-named')`, + options: [{ commonjs: true }], + }), + test({ + code: `const generator = require('./no-rename-default/assign-generator')`, + options: [{ commonjs: true }], + }), + test({ + code: `const generator = require('./no-rename-default/assign-generator-named')`, + options: [{ commonjs: true }], + }), + ], + invalid: [ + test({ + code: `const myArrow = require('./no-rename-default/assign-arrow')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-arrow.js` has a default export `arrow`. This requires `arrow` as `myArrow`. Check if you meant to write `const arrow = require('./no-rename-default/assign-arrow')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const myArrowAsync = require('./no-rename-default/assign-arrow-async')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-arrow-async.js` has a default export `arrowAsync`. This requires `arrowAsync` as `myArrowAsync`. Check if you meant to write `const arrowAsync = require('./no-rename-default/assign-arrow-async')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const MyUser = require('./no-rename-default/assign-class')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-class.js` has a default export `User`. This requires `User` as `MyUser`. Check if you meant to write `const User = require('./no-rename-default/assign-class')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const MyUser = require('./no-rename-default/assign-class-named')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-class-named.js` has a default export `User`. This requires `User` as `MyUser`. Check if you meant to write `const User = require('./no-rename-default/assign-class-named')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const myFn = require('./no-rename-default/assign-fn')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-fn.js` has a default export `fn`. This requires `fn` as `myFn`. Check if you meant to write `const fn = require('./no-rename-default/assign-fn')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const myFn = require('./no-rename-default/assign-fn-named')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-fn-named.js` has a default export `fn`. This requires `fn` as `myFn`. Check if you meant to write `const fn = require('./no-rename-default/assign-fn-named')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const myGenerator = require('./no-rename-default/assign-generator')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-generator.js` has a default export `generator`. This requires `generator` as `myGenerator`. Check if you meant to write `const generator = require('./no-rename-default/assign-generator')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const myGenerator = require('./no-rename-default/assign-generator-named')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `assign-generator-named.js` has a default export `generator`. This requires `generator` as `myGenerator`. Check if you meant to write `const generator = require('./no-rename-default/assign-generator-named')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + ], +}) + +// REQUIRE { commonjs: true, preventRenamingBindings: false } +// assign-arrow.js +// assign-arrow-async.js +// assign-class.js +// assign-class-named.js +// assign-fn.js +// assign-fn-named.js +// assign-generator.js +// assign-generator-named.js +ruleTester.run('no-renamed-default', rule, { + valid: [ + test({ + code: `const myArrow = require('./no-rename-default/assign-arrow')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const myArrowAsync = require('./no-rename-default/assign-arrow-async')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const MyUser = require('./no-rename-default/assign-class')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const MyUser = require('./no-rename-default/assign-class-named')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const myFn = require('./no-rename-default/assign-fn')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const myFn = require('./no-rename-default/assign-fn-named')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const myGenerator = require('./no-rename-default/assign-generator')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + test({ + code: `const myGenerator = require('./no-rename-default/assign-generator-named')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + ], + invalid: [], +}) + +// IMPORT +// class-user.js +ruleTester.run('no-rename-default', rule, { + valid: [`import User from './no-rename-default/class-user'`], + invalid: [ + test({ + code: `import MyUser from './no-rename-default/class-user'`, + errors: [ + { + message: + "Caution: `class-user.js` has a default export `User`. This imports `User` as `MyUser`. Check if you meant to write `import User from './no-rename-default/class-user'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + ], +}) + +// REQUIRE { commonjs: true } +// class-user.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const User = require('./no-rename-default/class-user')`, + options: [{ commonjs: true }], + }), + ], + invalid: [ + test({ + code: `const MyUser = require('./no-rename-default/class-user')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `class-user.js` has a default export `User`. This requires `User` as `MyUser`. Check if you meant to write `const User = require('./no-rename-default/class-user')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + ], +}) + +// IMPORT +// const-bar.js +// const-foo.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import foo from './no-rename-default/const-foo'`, + `import { fooNamed1 } from './no-rename-default/const-foo'`, + `import { fooNamed1, fooNamed2 } from './no-rename-default/const-foo'`, + `import { default as foo } from './no-rename-default/const-foo'`, + `import { default as foo, fooNamed1 } from './no-rename-default/const-foo'`, + `import foo, { fooNamed1 } from './no-rename-default/const-foo'`, + ` + import bar from './no-rename-default/const-bar' + import foo from './no-rename-default/const-foo' + `, + ` + import bar, { barNamed1 } from './no-rename-default/const-bar' + import foo, { fooNamed1 } from './no-rename-default/const-foo' + `, + ], + invalid: [ + test({ + code: `import bar from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import foo from './no-rename-default/const-foo'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import { default as bar } from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import { default as foo } from './no-rename-default/const-foo'` instead.", + type: 'ImportSpecifier', + }, + ], + }), + test({ + code: `import { default as bar, fooNamed1 } from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import { default as foo } from './no-rename-default/const-foo'` instead.", + type: 'ImportSpecifier', + }, + ], + }), + test({ + code: `import bar, { fooNamed1 } from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import foo from './no-rename-default/const-foo'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import foo from './no-rename-default/const-bar' + import bar from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-bar.js` has a default export `bar`. This imports `bar` as `foo`. Check if you meant to write `import bar from './no-rename-default/const-bar'` instead.", + type: 'ImportDefaultSpecifier', + }, + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import foo from './no-rename-default/const-foo'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import findUsers from './no-rename-default/fn-get-users'`, + errors: [ + { + message: + "Caution: `fn-get-users.js` has a default export `getUsers`. This imports `getUsers` as `findUsers`. Check if you meant to write `import getUsers from './no-rename-default/fn-get-users'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import findUsersSync from './no-rename-default/fn-get-users-sync'`, + errors: [ + { + message: + "Caution: `fn-get-users-sync.js` has a default export `getUsersSync`. This imports `getUsersSync` as `findUsersSync`. Check if you meant to write `import getUsersSync from './no-rename-default/fn-get-users-sync'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import foo, { barNamed1 } from './no-rename-default/const-bar' + import bar, { fooNamed1 } from './no-rename-default/const-foo'`, + errors: [ + { + message: + "Caution: `const-bar.js` has a default export `bar`. This imports `bar` as `foo`. Check if you meant to write `import bar from './no-rename-default/const-bar'` instead.", + type: 'ImportDefaultSpecifier', + }, + { + message: + "Caution: `const-foo.js` has a default export `foo`. This imports `foo` as `bar`. Check if you meant to write `import foo from './no-rename-default/const-foo'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + ], +}) + +// REQUIRE { commonjs: true } +// const-bar.js +// const-foo.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const foo = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const { fooNamed1 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const { fooNamed1, fooNamed2 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const { default: foo } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const { default: foo, fooNamed1 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const foo = require('./no-rename-default/const-foo') + const { fooNamed1 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const getUsers = require('./no-rename-default/fn-get-users')`, + options: [{ commonjs: true }], + }), + test({ + code: `const getUsersSync = require('./no-rename-default/fn-get-users-sync')`, + options: [{ commonjs: true }], + }), + test({ + code: ` + const bar = require('./no-rename-default/const-bar') + const { barNamed1 } = require('./no-rename-default/const-bar') + const foo = require('./no-rename-default/const-foo') + const { fooNamed1 } = require('./no-rename-default/const-foo') + `, + options: [{ commonjs: true }], + }), + ], + invalid: [ + test({ + code: `const bar = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const foo = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const bar = require('./no-rename-default/const-foo') + const { fooNamed1 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const foo = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const { default: bar } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const { default: foo } = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const { default: bar, fooNamed1 } = require('./no-rename-default/const-foo')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const { default: foo } = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: ` + const foo = require('./no-rename-default/const-bar') + const bar = require('./no-rename-default/const-foo') + `, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-bar.js` has a default export `bar`. This requires `bar` as `foo`. Check if you meant to write `const bar = require('./no-rename-default/const-bar')` instead.", + type: 'VariableDeclarator', + }, + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const foo = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: ` + const foo = require('./no-rename-default/const-bar') + const { barNamed1 } = require('./no-rename-default/const-bar') + const bar = require('./no-rename-default/const-foo') + const { fooNamed1 } = require('./no-rename-default/const-foo') + `, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `const-bar.js` has a default export `bar`. This requires `bar` as `foo`. Check if you meant to write `const bar = require('./no-rename-default/const-bar')` instead.", + type: 'VariableDeclarator', + }, + { + message: + "Caution: `const-foo.js` has a default export `foo`. This requires `foo` as `bar`. Check if you meant to write `const foo = require('./no-rename-default/const-foo')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + ], +}) + +// IMPORT +// fn-get-users.js +// fn-get-users-sync.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import getUsers from './no-rename-default/fn-get-users'`, + `import getUsersSync from './no-rename-default/fn-get-users-sync'`, + ], + invalid: [ + test({ + code: `import findUsers from './no-rename-default/fn-get-users'`, + errors: [ + { + message: + "Caution: `fn-get-users.js` has a default export `getUsers`. This imports `getUsers` as `findUsers`. Check if you meant to write `import getUsers from './no-rename-default/fn-get-users'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + test({ + code: `import findUsersSync from './no-rename-default/fn-get-users-sync'`, + errors: [ + { + message: + "Caution: `fn-get-users-sync.js` has a default export `getUsersSync`. This imports `getUsersSync` as `findUsersSync`. Check if you meant to write `import getUsersSync from './no-rename-default/fn-get-users-sync'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + ], +}) + +// REQUIRE { commonjs: true } +// fn-get-users.js +// fn-get-users-sync.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const getUsers = require('./no-rename-default/fn-get-users')`, + options: [{ commonjs: true }], + }), + test({ + code: `const getUsersSync = require('./no-rename-default/fn-get-users-sync')`, + options: [{ commonjs: true }], + }), + ], + invalid: [ + test({ + code: `const findUsers = require('./no-rename-default/fn-get-users')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `fn-get-users.js` has a default export `getUsers`. This requires `getUsers` as `findUsers`. Check if you meant to write `const getUsers = require('./no-rename-default/fn-get-users')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + test({ + code: `const findUsersSync = require('./no-rename-default/fn-get-users-sync')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `fn-get-users-sync.js` has a default export `getUsersSync`. This requires `getUsersSync` as `findUsersSync`. Check if you meant to write `const getUsersSync = require('./no-rename-default/fn-get-users-sync')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + ], +}) + +// IMPORT +// generator-reader.js +ruleTester.run('no-rename-default', rule, { + valid: [`import reader from './no-rename-default/generator-reader'`], + invalid: [ + test({ + code: `import myReader from './no-rename-default/generator-reader'`, + errors: [ + { + message: + "Caution: `generator-reader.js` has a default export `reader`. This imports `reader` as `myReader`. Check if you meant to write `import reader from './no-rename-default/generator-reader'` instead.", + type: 'ImportDefaultSpecifier', + }, + ], + }), + ], +}) + +// REQUIRE { commonjs: true } +// generator-reader.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const reader = require('./no-rename-default/generator-reader')`, + options: [{ commonjs: true }], + }), + ], + invalid: [ + test({ + code: `const myReader = require('./no-rename-default/generator-reader')`, + options: [{ commonjs: true }], + errors: [ + { + message: + "Caution: `generator-reader.js` has a default export `reader`. This requires `reader` as `myReader`. Check if you meant to write `const reader = require('./no-rename-default/generator-reader')` instead.", + type: 'VariableDeclarator', + }, + ], + }), + ], +}) + +//------------------------------------------------------------------------------ +// PR_FEEDBACK +//------------------------------------------------------------------------------ + +// IMPORT +// binding-const-rename-fn.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import foo from './no-rename-default/pr-3006-feedback/binding-const-rename-fn'`, + ], + invalid: [], +}) + +// REQUIRE { commonjs: true } +// binding-const-rename-fn.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const foo = require('./no-rename-default/pr-3006-feedback/binding-const-rename-fn')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + ], + invalid: [], +}) + +// IMPORT +// binding-hoc-with-logger-for-foo.js +// binding-hoc-with-logger-for-get-users.js +// binding-hoc-with-logger-with-auth-for-get-users.js +ruleTester.run('no-rename-default', rule, { + valid: [ + `import foo from './no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-foo'`, + `import getUsers from './no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-get-users'`, + `import getUsers from './no-rename-default/pr-3006-feedback/binding-hoc-with-logger-with-auth-for-get-users'`, + ], + invalid: [], +}) + +// REQUIRE { commonjs: true } +// binding-hoc-with-logger-for-foo.js +// binding-hoc-with-logger-for-get-users.js +// binding-hoc-with-logger-with-auth-for-get-users.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const foo = require('./no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-foo')`, + options: [{ commonjs: true }], + }), + test({ + code: `const getUsers = require('./no-rename-default/pr-3006-feedback/binding-hoc-with-logger-for-get-users')`, + options: [{ commonjs: true }], + }), + test({ + code: `const getUsers = require('./no-rename-default/pr-3006-feedback/binding-hoc-with-logger-with-auth-for-get-users')`, + options: [{ commonjs: true }], + }), + ], + invalid: [], +}) + +// IMPORT { preventRenamingBindings: false } +// binding-fn-rename.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `import _ from './no-rename-default/pr-3006-feedback/binding-fn-rename'`, + options: [{ preventRenamingBindings: false }], + }), + ], + invalid: [], +}) + +// REQUIRE { commonjs: true, preventRenamingBindings: false } +// binding-fn-rename.js +ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `const _ = require('./no-rename-default/pr-3006-feedback/binding-fn-rename')`, + options: [{ commonjs: true, preventRenamingBindings: false }], + }), + ], + invalid: [], +}) + +describe('TypeScript', function () { + ruleTester.run('no-rename-default', rule, { + valid: [ + test({ + code: `import foo from './no-rename-default/typescript-default'`, + settings: { + 'import-x/parsers': { [parsers.TS]: ['.ts'] }, + 'import-x/resolver': { 'eslint-import-resolver-typescript': true }, + }, + }), + ], + invalid: [], + }) +})