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

group- and export last #1903

Open
jimmywarting opened this issue May 14, 2023 · 0 comments
Open

group- and export last #1903

jimmywarting opened this issue May 14, 2023 · 0 comments

Comments

@jimmywarting
Copy link
Contributor

I help convert a lot of CJS project to ESM
And two rules that help make this migration way easier is if they have a single groped export at the bottom of the file:

// A single exports assignment -> ok
module.exports = {
  first,
  second,
}
// Multiple exports assignments -> not ok!
module.exports.first = true
module.exports.second = true

...then it's just a matter of changing one line:

- module.exports = {
+ export = {
  first,
  second,
}

this makes it way easier to refactor into a ESM and it's more likely that others accepts one such PR, cuz they are also way easier to review and accept.

i think it's also easier to see / control what is being exported when everything is scattered around in the hole file from top to bottom.

Another reason for having exports at the bottom is b/c of this anti- pattern that makes it hard to refactor old prototype based classes to es classes
cuz those functions / variables are hosted to the top.

var { inherit } = require('util')
var { Shape } = require('./Shape.js')
module.exports = Circle

function Circle (radius) {
  this.radius = radius
}

inherit(Circle, Shape)

then when you go ahead and convert this to es classes then you can no longer do: module.exports = Circle at the top of the file

i see this pattern quite a bit from time to time. some think it's easer to declare what is being exported at the top of the file. but it's problematic. change Circle to a class or const/let and the Circle becomes undeclared.

this is why it would also help if exports where at the bottom instead.

There are two rules that helps with this:

https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/group-exports.md
https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/exports-last.md

which i think should be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant