diff --git a/rules/prefer-export-from.js b/rules/prefer-export-from.js index af4ffa4363..bfdd77d4bd 100644 --- a/rules/prefer-export-from.js +++ b/rules/prefer-export-from.js @@ -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, @@ -82,9 +92,9 @@ 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 */ @@ -92,7 +102,7 @@ function getFixFunction({ 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 @@ -112,7 +122,7 @@ function getFixFunction({ } else { yield fixer.insertTextAfter( program, - `\nexport {${specifier}} from ${sourceText};`, + `\nexport {${specifier}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`, ); } } diff --git a/test/prefer-export-from.mjs b/test/prefer-export-from.mjs index 5a963c8213..e0dcc626e8 100644 --- a/test/prefer-export-from.mjs +++ b/test/prefer-export-from.mjs @@ -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' }; + `, + }, + ], +});