Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 support export {...} and export {...} from #605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"father-next": "^3.0.0-alpha.3",
"father-next": "3.0.0-alpha.3",
"husky": "^4.2.5",
"lint-staged": "^10.2.8",
"material-ui": "^0.20.2",
Expand Down
38 changes: 36 additions & 2 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class Plugin {
if (!types.isIdentifier(node[prop])) return;
if (
pluginState.specified[node[prop].name] &&
types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)
(prop === 'local' || types.isImportSpecifier(path.scope.getBinding(node[prop].name).path))
) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
}
Expand All @@ -127,11 +127,12 @@ export default class Plugin {
} else if (types.isSequenceExpression(node[prop])) {
node[prop].expressions.forEach((expressionNode, index) => {
if (types.isIdentifier(expressionNode) && checkScope(expressionNode)) {
// eslint-disable-next-line
node[prop].expressions[index] = this.importMethod(
pluginState.specified[expressionNode.name],
file,
pluginState,
); // eslint-disable-line
);
}
});
}
Expand Down Expand Up @@ -267,6 +268,39 @@ export default class Plugin {
this.buildExpressionHandler(node, ['declaration'], path, state);
}

ExportNamedDeclaration(path, state) {
const { node } = path;
if (node.declaration === null) {
if (node.source === null) {
node.specifiers.forEach(spec => {
this.buildExpressionHandler(spec, ['local'], path, state);
});
} else {
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { value } = node.source;
const { libraryName, types } = this;
const pluginState = this.getPluginState(state);
const specifiers = [];
if (value === libraryName) {
node.specifiers.forEach((spec, index) => {
if (types.isExportSpecifier(spec)) {
pluginState.specified[spec.local.name] = spec.exported.name;
node.specifiers[index] = this.importMethod(spec.exported.name, file, pluginState);
specifiers.push(
types.exportSpecifier(
types.identifier(spec.local.name),
types.identifier(spec.exported.name),
),
);
}
});
pluginState.pathsToRemove.push(path);
path.insertAfter(types.exportNamedDeclaration(null, specifiers));
}
}
}
}

BinaryExpression(path, state) {
const { node } = path;
this.buildExpressionHandler(node, ['left', 'right'], path, state);
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default function ({ types }) {
'ExpressionStatement',
'ReturnStatement',
'ExportDefaultDeclaration',
'ExportNamedDeclaration',
'BinaryExpression',
'NewExpression',
'ClassDeclaration',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import { DatePicker } from 'antd';
export default DatePicker;

1 change: 1 addition & 0 deletions test/fixtures/export-from/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Button, Modal } from 'antd';
23 changes: 23 additions & 0 deletions test/fixtures/export-from/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Button", {
enumerable: true,
get: function get() {
return _button.default;
}
});
Object.defineProperty(exports, "Modal", {
enumerable: true,
get: function get() {
return _modal.default;
}
});

var _modal = _interopRequireDefault(require("antd/lib/modal"));

var _button = _interopRequireDefault(require("antd/lib/button"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2 changes: 2 additions & 0 deletions test/fixtures/export-name-import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Button, DatePicker } from 'antd';
export { Button, DatePicker };
23 changes: 23 additions & 0 deletions test/fixtures/export-name-import/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Button", {
enumerable: true,
get: function get() {
return _button.default;
}
});
Object.defineProperty(exports, "DatePicker", {
enumerable: true,
get: function get() {
return _datePicker.default;
}
});

var _datePicker = _interopRequireDefault(require("antd/lib/date-picker"));

var _button = _interopRequireDefault(require("antd/lib/button"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }