Skip to content

Latest commit

 

History

History
100 lines (73 loc) · 2.49 KB

prefer-export-from.md

File metadata and controls

100 lines (73 loc) · 2.49 KB

prefer-export-from

📝 Prefer export…from when re-exporting.

💼🚫 This rule is enabled in the ✅ recommended config. This rule is disabled in the ☑️ unopinionated config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

When re-exporting from a module, it's unnecessary to import and then export. It can be done in a single export…from declaration.

export default importedValue and export const alias = importedValue capture the current value, while export…from is a live binding. The fix only changes behavior if the source module reassigns the exported binding later, which is not supported.

Single-const re-export assignments with an attached JSDoc comment are left unchanged to preserve the documentation.

Examples

// ❌
import defaultExport from './foo.js';
export default defaultExport;

// ✅
export {default} from './foo.js';
// ❌
import {named} from './foo.js';
export {named};

// ✅
export {named} from './foo.js';
// ❌
import * as namespace from './foo.js';
export {namespace};

// ✅
export * as namespace from './foo.js';
// ❌
import defaultExport, {named} from './foo.js';
export default defaultExport;
export {
	defaultExport as renamedDefault,
	named,
	named as renamedNamed,
};

// ✅
export {
	default,
	default as renamedDefault,
	named,
	named as renamedNamed,
} from './foo.js';
// ✅
// There is no substitution
import * as namespace from './foo.js';
export default namespace;

Options

checkUsedVariables

Type: boolean
Default: true

When false, if an import is used in other places than just a re-export, all variables in the import declaration will be ignored.

/* eslint unicorn/prefer-export-from: ["error", {"checkUsedVariables": true}] */
// ❌
import {named1, named2} from './foo.js';

use(named1);

export {named1, named2};
/* eslint unicorn/prefer-export-from: ["error", {"checkUsedVariables": false}] */
// ✅
import {named1, named2} from './foo.js';

use(named1);

export {named1, named2};