Skip to content

Commit

Permalink
feat: Support exports (#8)
Browse files Browse the repository at this point in the history
* support exports

* rename basic case to be more specific

* test variables

* update readme
  • Loading branch information
villesau authored May 29, 2021
1 parent 0b8730b commit f079cb6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jscodeshift codemod that convert CommonJS(require/exports) to ES Modules(import/

### Exports

- [x] named export: `module.exports.foo = foo` to `export { foo }`
- [x] named export: `module.exports.bar = foo` to `export { foo as bar }`
- [x] named export: `module.exports.foo = foo` & `exports.foo = foo to `export { foo }`
- [x] named export: `module.exports.bar = foo` & `exports.bar = foo` to `export { foo as bar }`
- [x] default export: `module.exports = foo` to `export default foo`
- [x] ignore multiple `module.exports = x`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.a = function () {
return "a";
};
exports.b = function () {
return "b";
};

const c = () => {
return 42;
};
exports.c = c;
exports.d = c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const a = function () {
return "a";
};

export const b = function () {
return "b";
};

const c = () => {
return 42;
};
export { c };
export { c as d };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineTest } from "jscodeshift/dist/testUtils";

const tests = ["basic-case", "exports-variable"];
const tests = ["basic-case-module-exports", "basic-case-exports", "exports-variable"];

describe("module-exports-to-named-export", () => {
tests.forEach((test) => {
Expand Down
54 changes: 36 additions & 18 deletions transforms/module-exports-to-named-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function transformer(file, api, options) {

// ------------------------------------------------------------------ SEARCH
// https://astexplorer.net/#/gist/334f5bd39244c7feab38a3fd3cc0ce7f/c332a5b4cbd1a9718e644febf2dce9e9bd032d1b
const nodes = j(file.source)
const ast = j(file.source)
const moduleExportNodes = ast
.find(j.ExpressionStatement, {
expression: {
left: {
Expand All @@ -38,25 +39,42 @@ function transformer(file, api, options) {
})
.filter(isTopNode);

logger.log(`${nodes.length} nodes will be transformed`);

// ----------------------------------------------------------------- REPLACE
return nodes
.replaceWith((path) => {
const node = path.node;
// Identifier node
const id = node.expression.left.property;
const init = node.expression.right;
// module.export.b = a
// → export { a as b }
if (id.type === "Identifier" && init.type === "Identifier") {
return j.exportNamedDeclaration(null, [j.exportSpecifier(init, id)]);
const exportNodes = ast
.find(j.ExpressionStatement, {
expression: {
left: {
object: {
name: "exports"
}
// property is target
},
operator: "="
}
// https://babeljs.io/docs/en/babel-types#exportnameddeclaration
const declaration = j.variableDeclaration("const", [j.variableDeclarator(id, init)]);
return j.exportNamedDeclaration(declaration);
})
.toSource();
.filter(isTopNode);

logger.log(`${moduleExportNodes.length + exportNodes.length} nodes will be transformed`);
// ----------------------------------------------------------------- REPLACE
const replace = (path) => {
const node = path.node;
// Identifier node
const id = node.expression.left.property;
const init = node.expression.right;
// module.export.b = a
// → export { a as b }
if (id.type === "Identifier" && init.type === "Identifier") {
return j.exportNamedDeclaration(null, [j.exportSpecifier(init, id)]);
}
// https://babeljs.io/docs/en/babel-types#exportnameddeclaration
const declaration = j.variableDeclaration("const", [j.variableDeclarator(id, init)]);
return j.exportNamedDeclaration(declaration);
}

exportNodes
.replaceWith(replace)
moduleExportNodes
.replaceWith(replace)
return ast.toSource();
}

export default transformer;

0 comments on commit f079cb6

Please sign in to comment.