Skip to content

Commit

Permalink
prefer-export-from: Support import assertions (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 23, 2021
1 parent 30d66a8 commit 1d7a6b6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
18 changes: 14 additions & 4 deletions rules/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ function * removeImportOrExport(node, fixer, sourceCode) {
}
}

function getSourceAndAssertionsText(declaration, sourceCode) {
const keywordFromToken = sourceCode.getTokenBefore(
declaration.source,
token => token.type === 'Identifier' && token.value === 'from',
);
const [start] = keywordFromToken.range;
const [, end] = declaration.range;
return sourceCode.text.slice(start, end);
}

function getFixFunction({
context,
imported,
Expand All @@ -82,17 +92,17 @@ function getFixFunction({
program,
}) {
const sourceCode = context.getSourceCode();
const sourceNode = imported.declaration.source;
const importDeclaration = imported.declaration;
const sourceNode = importDeclaration.source;
const sourceValue = sourceNode.value;
const sourceText = sourceCode.getText(sourceNode);
const exportDeclaration = exportDeclarations.find(({source}) => source.value === sourceValue);

/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
if (imported.name === '*') {
yield fixer.insertTextAfter(
program,
`\nexport * as ${exported.name} from ${sourceText};`,
`\nexport * as ${exported.name} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
} else {
const specifier = exported.name === imported.name
Expand All @@ -112,7 +122,7 @@ function getFixFunction({
} else {
yield fixer.insertTextAfter(
program,
`\nexport {${specifier}} from ${sourceText};`,
`\nexport {${specifier}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/prefer-export-from.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,65 @@ test.snapshot({
`,
].map(code => ({code, options: [{ignoreUsedVariables: true}]})),
});

// TODO: Run test with default parser when it support "import assertions"
test.babel({
testerOptions: {
parserOptions: {
babelOptions: {
parserOpts: {
plugins: ['importAssertions'],
},
},
},
},
valid: [],
invalid: [
{
code: outdent`
import json from './foo.json' assert { type: 'json' };
export default json;
`,
errors: 1,
output: outdent`
\n
export {default} from './foo.json' assert { type: 'json' };
`,
},
{
code: outdent`
import * as json from './foo.json' assert { type: 'json' };
export {json};
`,
errors: 1,
output: outdent`
\n
export * as json from './foo.json' assert { type: 'json' };
`,
},
{
code: outdent`
import {foo} from './foo.json' assert { type: 'json' };
export {foo};
export {bar} from './foo.json';
`,
errors: 1,
output: outdent`
\n
export {bar, foo} from './foo.json';
`,
},
{
code: outdent`
import {foo} from './foo.json';
export {foo};
export {bar} from './foo.json' assert { type: 'json' };
`,
errors: 1,
output: outdent`
\n
export {bar, foo} from './foo.json' assert { type: 'json' };
`,
},
],
});

0 comments on commit 1d7a6b6

Please sign in to comment.