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

[Plugin proposal] eslint-import-alias #62

Open
kaelig opened this issue Mar 11, 2023 · 1 comment
Open

[Plugin proposal] eslint-import-alias #62

kaelig opened this issue Mar 11, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@kaelig
Copy link

kaelig commented Mar 11, 2023

See https://github.com/steelsojka/eslint-import-alias

I have the code ready because I needed it for my codebase!

Desired output:

// Before
{
  "~actions/*": ["src/actions/*"],
  "~ui/*": ["src/components/ui/*"],
}
  
// After  
[
  { alias: '~actions', matcher: '^src\/actions' },
  { alias: '~ui', matcher: '^src\/components\/ui' },
]

It should look something like this:

const { toArray } = require('../../utils')

// @see https://github.com/steelsojka/eslint-import-alias#configure
function callback (name, [path], config, options) {
  return {
    alias: name.replace('/*', ''),
    matcher: '^' + path.replace('/*', '').replaceAll('/', '\\/').replaceAll(/\.([a-zA-Z]+)/g, '\\.$1'),
  }
}

module.exports = function (config, options) {
  return toArray(callback, config, options)
}

And the test:

module.exports = [
  function () {
    const expected = [
      {
        alias: '@',
        matcher: '^'
      },
      {
        alias: '@packages',
        matcher: '^..\\/packages'
      },
      {
        alias: '@classes',
        matcher: '^classes'
      },
      {
        alias: '@app',
        matcher: '^app'
      },
      {
        alias: '@data',
        matcher: '^app\\/data'
      },
      {
        alias: '@settings',
        matcher: '^app\\/settings\\.js'
      },
      {
        alias: '@services',
        matcher: '^app\\/services'
      },
      {
        alias: '@views',
        matcher: '^app\\/views'
      }
    ]
    return { expected }
  },
]

Let me know if you're interested in me adding the plugin + docs to the codebase!

@davestewart
Copy link
Owner

Hello! Thanks for the contribution.

My next task is to refactor the the library to typescript, so let me check in again after that's done 🙏

@davestewart davestewart added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants