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

Bug: n/hashbang doesn't consider source file of a bin path as a binary #304

Open
1 task
simonhaenisch opened this issue Jul 2, 2024 · 3 comments
Open
1 task
Labels
enhancement rule:update An update to a current rule typescript

Comments

@simonhaenisch
Copy link

simonhaenisch commented Jul 2, 2024

Environment

Node version: 20
npm version: πŸ€·πŸ»β€β™‚οΈ
ESLint version: 9
eslint-plugin-n version: latest
Operating System: macOS

What rule do you want to report?

n/hashbang

Link to Minimal Reproducible Example

should be clear enough? can provide later if necessary

What did you expect to happen?

I have a file src/cli.ts that gets transpiled to dist/cli.js, so I have "bin": "dist/cli.js" in my package.json.

n/hashbang reports the shebang in the src/cli.ts file as "This file needs no shebang" because I guess it doesn't know it's the file that gets transpiled into dist/cli.js.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

I've tried looking at convertPath but don't see how that could solve it. I also saw there's some config related to tsconfig, and thinking based on "outDir": "dist" in the tsconfig.json it could actually be able to figure out automatically that src/cli.ts is the source file for dist/cli.js?

@aladdin-add
Copy link

convertPath should works:

    settings: {
      node: {
        convertPath: [
          {
            include: ['src/*.ts'],
            replace: ['^src/(.+).ts$', 'dist/$1.js'],
          },
        ],
      },
    },

we can add an example to show how to use it in ts projects. :)

@simonhaenisch
Copy link
Author

Can this convert path setting be added automatically based on tsconfig.json?

Just guessing but could be sth like

const tsconfig = require('./tsconfig.json')

const { include, outDir } = tsconfig.compilerOptions ?? {};

if (!outDir) {
  return;
}

const convertPaths = include
  .filter(path => !path.includes('/') && !path.includes('*')) // only pick simple paths
  .map(path => ({
    include: [`${path/**/*.ts}`],
    replace: [`^${path}/(.+).ts$`, `${outDir}/$1.js`]
  })

Maybe it's too complex to solve like this though, but maintaining that kind of setting per project is quite a chore.

@scagood scagood added enhancement and removed bug labels Jul 3, 2024
@scagood
Copy link

scagood commented Jul 3, 2024

I suppose it'd be a reasonable complement to the import extension mapping πŸ‘

https://github.com/eslint-community/eslint-plugin-n/blob/master/lib/util/get-typescript-extension-map.js#L21-L27

@scagood scagood added rule:update An update to a current rule and removed rule labels Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement rule:update An update to a current rule typescript
Projects
None yet
Development

No branches or pull requests

3 participants