eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: `no-exports-in-scripts`

Open zanminkian opened this issue 11 months ago • 2 comments

Description

Currently, a js file starts with hashbang #! is script file. In script file, using exports is useless and confused.

Fail

#!/usr/bin/env node

export const foo = {};

Pass

export {}
#!/usr/bin/env node

console.log('foo');

Proposed rule name

no-exports-in-scripts

Additional Info

No response

zanminkian avatar Dec 18 '24 12:12 zanminkian

Somewhat related:

  • https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1661

This rule:

  • report exports if it has side effects (detected via hashbang)

That rule:

  • report side effects if it has exports

However this rule as defined is much easier to implement:

  1. find export statement
  2. report if line 1 is hashbang

fregante avatar Dec 18 '24 18:12 fregante

Accepted

sindresorhus avatar Jan 24 '25 09:01 sindresorhus

Changed my mind since Node supports import.meta.main. Allowing exports in scripts could be useful for testing.

#!/usr/bin/env node
export function main() {
  // some codes need to be tested
}
if(import.meta.main) {
  main()
}

zanminkian avatar Aug 24 '25 20:08 zanminkian