Skip to content

Commit

Permalink
test: add additional tests for prefer-export-from
Browse files Browse the repository at this point in the history
  • Loading branch information
nrgnrg committed Feb 11, 2022
1 parent 18f48ae commit 72f0ad9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
36 changes: 17 additions & 19 deletions rules/prefer-export-from.js
Expand Up @@ -26,12 +26,12 @@ const getSpecifierName = node => {
}
};

function * removeSpecifier(node, fixer, sourceCode) {
const {parent} = node;
const {specifiers} = parent;
function* removeSpecifier(node, fixer, sourceCode) {
const { parent } = node;
const { specifiers } = parent;

if (specifiers.length === 1) {
yield * removeImportOrExport(parent, fixer, sourceCode);
yield* removeImportOrExport(parent, fixer, sourceCode);
return;
}

Expand Down Expand Up @@ -68,13 +68,13 @@ function * removeSpecifier(node, fixer, sourceCode) {
}
}

function * removeImportOrExport(node, fixer, sourceCode) {
function* removeImportOrExport(node, fixer, sourceCode) {
switch (node.type) {
case 'ImportSpecifier':
case 'ExportSpecifier':
case 'ImportDefaultSpecifier':
case 'ImportNamespaceSpecifier': {
yield * removeSpecifier(node, fixer, sourceCode);
yield* removeSpecifier(node, fixer, sourceCode);
return;
}

Expand Down Expand Up @@ -108,11 +108,11 @@ function getFixFunction({
const importDeclaration = imported.declaration;
const sourceNode = importDeclaration.source;
const sourceValue = sourceNode.value;
const exportDeclaration = exportDeclarations.find(({source, exportKind}) => source.value === sourceValue && exportKind !== 'type');
const exportDeclaration = exportDeclarations.find(({ source, exportKind }) => source.value === sourceValue && exportKind !== 'type');
const isTypeExport = exported.node.exportKind === 'type' || exported.exportKind === 'type';

/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
return function* (fixer) {
if (imported.name === NAMESPACE_SPECIFIER_NAME) {
yield fixer.insertTextAfter(
program,
Expand Down Expand Up @@ -144,15 +144,15 @@ function getFixFunction({
}

if (imported.variable.references.length === 1) {
yield * removeImportOrExport(imported.node, fixer, sourceCode);
yield* removeImportOrExport(imported.node, fixer, sourceCode);
}

yield * removeImportOrExport(exported.node, fixer, sourceCode);
yield* removeImportOrExport(exported.node, fixer, sourceCode);
};
}

function getExported(identifier, context, sourceCode) {
const {parent} = identifier;
const { parent } = identifier;
switch (parent.type) {
case 'ExportDefaultDeclaration':
return {
Expand Down Expand Up @@ -202,7 +202,7 @@ function isVariableUnused(node, context) {
return false;
}

const [{identifiers, references}] = variables;
const [{ identifiers, references }] = variables;
return identifiers.length === 1
&& identifiers[0] === node.id
&& references.length === 1
Expand Down Expand Up @@ -245,14 +245,13 @@ function getImported(variable, sourceCode) {

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

if (!exported) {
continue;
}

// Console.log("getExports", exported)
/*
There is no substitution for:
Expand Down Expand Up @@ -287,7 +286,7 @@ const schema = [
/** @param {import('eslint').Rule.RuleContext} context */
function create(context) {
const sourceCode = context.getSourceCode();
const {ignoreUsedVariables} = {ignoreUsedVariables: false, ...context.options[0]};
const { ignoreUsedVariables } = { ignoreUsedVariables: false, ...context.options[0] };
const importDeclarations = new Set();
const exportDeclarations = [];

Expand All @@ -309,7 +308,6 @@ function create(context) {

variables = variables.map(variable => {
const imported = getImported(variable, sourceCode);
// Console.log("getImported", imported)
const exports = getExports(imported, context, sourceCode);

return {
Expand All @@ -321,15 +319,15 @@ function create(context) {

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

const shouldUseSuggestion = ignoreUsedVariables
&& variables.some(({variable}) => variable.references.length === 0);
&& variables.some(({ variable }) => variable.references.length === 0);

for (const {imported, exports} of variables) {
for (const { imported, exports } of variables) {
for (const exported of exports) {
const problem = {
node: exported.node,
Expand Down
40 changes: 40 additions & 0 deletions test/prefer-export-from.mjs
Expand Up @@ -362,6 +362,46 @@ test.typescript({
export {foo} from "foo";
`,
},
{
code: outdent`
import { foo } from "foo";
export { foo };
export { type bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export { type bar, foo } from "foo";
`,
},
{
code: outdent`
import { foo } from 'foo';
export { foo };
export type { bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export type { bar } from "foo";
export { baz, foo } from "foo";
`,
},
{
code: outdent`
import { foo } from 'foo';
export { foo };
export { type bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export { type bar, foo } from "foo";
export { baz } from "foo";
`,
},
],
});

Expand Down

0 comments on commit 72f0ad9

Please sign in to comment.