Skip to content

Commit

Permalink
fix: don't modify imports when they point at a module with no exports (
Browse files Browse the repository at this point in the history
…#60)

When an import statement point at a file without any exports at all (e.g. a
commonjs module), the fix-imports script was viewing it as a file with only
named exports (which is vacuously true) and changing the import style to
`import *`. This broke cases where a commonjs module exported anything other
than an object.
  • Loading branch information
alangpierce authored Dec 4, 2016
1 parent b260023 commit 58383e1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jscodeshift-scripts/fix-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export default function (fileInfo, api, options) {
return path.node;
}
let exportsInfo = getExportsInformation(resolvedPath);
// If we didn't see anything on the other side, it might not even be a JS
// module, so just leave this import as-is.
if (!exportsInfo.hasDefaultExport && exportsInfo.namedExports.length === 0) {
return path.node;
}
let specifierIndex = getSpecifierIndex(path);
let memberAccesses = findAllMemberAccesses(specifierIndex);
let importManifest = getImportManifest(exportsInfo, memberAccesses);
Expand Down
2 changes: 2 additions & 0 deletions test/examples/fix-imports-import-commonjs/ImportThree.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
three = require './three'
console.log(three);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
import three from './three';
console.log(three);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
fixImportsConfig: {
searchPath: '.',
},
};
1 change: 1 addition & 0 deletions test/examples/fix-imports-import-commonjs/three.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 3;
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,8 @@ describe('fix-imports', () => {
it('only does relative path resolution when an import is relative style', async function() {
await runFixImportsTest('fix-imports-non-relative-path');
});

it('makes no changes when the other file is not a JS module', async function() {
await runFixImportsTest('fix-imports-import-commonjs');
});
});

0 comments on commit 58383e1

Please sign in to comment.