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

prefer-export-from: Add ignoreUsedVariables option #1590

Merged
merged 8 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/rules/prefer-export-from.md
Expand Up @@ -61,3 +61,41 @@ export {
import * as namespace from './foo.js';
export default namespace;
```

## Options

### ignoreUsedVariables

Type: `boolean`\
Default: `false`

When pass `"ignoreUsedVariables": true`, if any variable is used more than just re-export or not exported, all variables in the import declaration will be ignored.
fisker marked this conversation as resolved.
Show resolved Hide resolved

#### Fail

```js
// eslint unicorn/import-index: ["error", {"ignoreUsedVariables": false}]
import {named1, named2} from './foo.js';

use(named1);

export {named1, named2};
```

#### Pass

```js
// eslint unicorn/import-index: ["error", {"ignoreUsedVariables": true}]
import {named1, named2} from './foo.js';

use(named1);

export {named1, named2};
```

```js
// eslint unicorn/import-index: ["error", {"ignoreUsedVariables": true}]
import {named1, named2} from './foo.js';

export {named1};
```
107 changes: 68 additions & 39 deletions rules/prefer-export-from.js
Expand Up @@ -193,28 +193,19 @@ function isVariableUnused(node, context) {
&& references[0].identifier === node.id;
}

function * getProblems({
context,
variable,
program,
exportDeclarations,
}) {
const {identifiers, references} = variable;

if (identifiers.length !== 1 || references.length === 0) {
return;
}

const specifier = identifiers[0].parent;

const imported = {
function getImported(variable) {
const specifier = variable.identifiers[0].parent;
return {
name: getImportedName(specifier),
node: specifier,
declaration: specifier.parent,
variable,
};
}

for (const {identifier} of references) {
function getExports(imported, context) {
const exports = [];
for (const {identifier} of imported.variable.references) {
const exported = getExported(identifier, context);

if (!exported) {
Expand All @@ -233,44 +224,81 @@ function * getProblems({
continue;
}

yield {
node: exported.node,
messageId: MESSAGE_ID,
data: {
exported: exported.name,
},
fix: fix({
context,
imported,
exported,
exportDeclarations,
program,
}),
};
exports.push(exported);
}

return exports;
}

const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
ignoreUsedVariables: {
type: 'boolean',
default: false,
},
},
},
];

/** @param {import('eslint').Rule.RuleContext} context */
function create(context) {
const variables = [];
const {ignoreUsedVariables} = {ignoreUsedVariables: false, ...context.options[0]};
const importDeclarations = new Set();
const exportDeclarations = [];

return {
'ImportDeclaration[specifiers.length>0]'(node) {
variables.push(...context.getDeclaredVariables(node));
importDeclarations.add(node);
},
// `ExportAllDeclaration` and `ExportDefaultDeclaration` can't be reused
'ExportNamedDeclaration[source.type="Literal"]'(node) {
exportDeclarations.push(node);
},
* 'Program:exit'(program) {
for (const variable of variables) {
yield * getProblems({
context,
variable,
exportDeclarations,
program,
});
for (const importDeclaration of importDeclarations) {
const variables = context.getDeclaredVariables(importDeclaration)
.map(variable => {
const imported = getImported(variable);
const exports = getExports(imported, context);

return {
variable,
imported,
exports,
};
});

if (
ignoreUsedVariables
&& variables.some(({variable, exports}) => {
const {references} = variable;
return references.length === 0 || references.length !== exports.length;
})
) {
continue;
}

for (const {imported, exports} of variables) {
for (const exported of exports) {
yield {
node: exported.node,
messageId: MESSAGE_ID,
data: {
exported: exported.name,
},
fix: fix({
context,
imported,
exported,
exportDeclarations,
program,
}),
};
}
}
}
},
};
Expand All @@ -284,6 +312,7 @@ module.exports = {
description: 'Prefer `export…from` when re-exporting.',
},
fixable: 'code',
schema,
messages,
},
};
105 changes: 105 additions & 0 deletions test/prefer-export-from.mjs
Expand Up @@ -289,3 +289,108 @@ test.typescript({
],
invalid: [],
});

// `ignoreUsedVariables`
test.snapshot({
valid: [
outdent`
import defaultExport from 'foo';
use(defaultExport);
export default defaultExport;
`,
outdent`
import defaultExport from 'foo';
use(defaultExport);
export {defaultExport};
`,
outdent`
import {named} from 'foo';
use(named);
export {named};
`,
outdent`
import {named} from 'foo';
use(named);
export default named;
`,
outdent`
import * as namespace from 'foo';
use(namespace);
export {namespace};
`,
outdent`
import * as namespace from 'foo';
use(namespace);
export default namespace;
`,
outdent`
import * as namespace from 'foo';
export {namespace as default};
export {namespace as named};
`,
outdent`
import * as namespace from 'foo';
export default namespace;
export {namespace as named};
`,
outdent`
import defaultExport, {named} from 'foo';
use(defaultExport);
export {named};
`,
outdent`
import defaultExport, {named} from 'foo';
use(named);
export {defaultExport};
`,
outdent`
import {named1, named2} from 'foo';
use(named1);
export {named2};
`,
outdent`
import defaultExport, {named1, named2} from 'foo';
use(defaultExport);
export {named1, named2};
`,
outdent`
import defaultExport, {named1, named2} from 'foo';
use(named1);
export {defaultExport, named2};
`,
outdent`
import {notUsedNotExported, exported} from 'foo';
export {exported};
`,
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
].map(code => ({code, options: [{ignoreUsedVariables: true}]})),
invalid: [
outdent`
import defaultExport from 'foo';
export {defaultExport as default};
export {defaultExport as named};
`,
outdent`
import {named} from 'foo';
export {named as default};
export {named as named};
`,
outdent`
import {named} from 'foo';
export default named;
export {named as named};
`,
outdent`
import defaultExport, {named} from 'foo';
export default defaultExport;
export {named};
`,
outdent`
import defaultExport, {named} from 'foo';
export {defaultExport as default, named};
`,
outdent`
import defaultExport from 'foo';
export const variable = defaultExport;
`,
].map(code => ({code, options: [{ignoreUsedVariables: true}]})),
});