davestewart / alias-hq

The end-to-end solution for configuring, refactoring, maintaining and using path aliases
https://davestewart.co.uk/projects/open-source/alias-hq/
MIT License
331 stars 11 forks source link

[Plugin proposal] eslint-import-alias #62

Open kaelig opened 1 year ago

kaelig commented 1 year ago

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 commented 1 year ago

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 🙏