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

Feature: This rule sort the dynamic imports in a javascript files. #198

Open
2 tasks done
id-aditya opened this issue Aug 7, 2024 · 0 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@id-aditya
Copy link

id-aditya commented Aug 7, 2024

Describe the rule

Dynamic imports are the future, this rule allow to sort imports like these:

export const http= f=>n=>{
  return Http.serve (f) (n);
};

const Http=   await import (`$/base/3/network/http`);
const U2=     await import (`1/u/2`);
const O=      await import (`1/o`);
const O1=     await import (`1/o/1`);
const Link=   await import (`5/link`);
const A2=     await import (`1/a/2`);

To this:

export const http= f=>n=>{
  return Http.serve (f) (n);
};

const Link=   await import (`5/link`);
const U2=     await import (`1/u/2`);
const O1=     await import (`1/o/1`);
const O=      await import (`1/o`);
const A2=     await import (`1/a/2`);
const Http=   await import (`$/base/3/network/http`);

Using the import's path.

I have attached a working implementation of a custom eslint rule (rule's written in esnext style), the code currently sorts in reverse order, it'll be great if you can add settings like sortOrder, type etc to the code and release it under a new rule. Thanks.

Code example

export const meta= {
  type:   `layout`,
  fixable:`whitespace`,
  docs:   {
    description: `Sort dynamic imports.`,
  },
};
export const create= u2=>{
  const work=o2_1=>o2=>i1=>{
    const suit=o2_1[i1];
    const link=o2.link;
    const path=o2.path;
    switch (true) {
    case Object.is (suit.path, path): {
      return o2;
    }
    default: {
      const report={
        node:   link,
        message:`Misarranged.`,
        fix:    _u2=>_u2.replaceText (suit.link, code.getText (link)),
      };

      return u2.report (report);
    }
    }
  };

  const does=o2=>{
    const list=find (o2);
    const kept=list.map (keep);
    // eslint-disable-next-line @typescript-eslint/max-params
    return sort (kept).map ((_o2, _i1)=>work (kept) (_o2) (_i1));
  };

  const code=u2.sourceCode;
  return {
    Program: does,
  };
};

const sort=x=>{
  // eslint-disable-next-line @typescript-eslint/max-params
  const sorter=(x_2, x_1)=>{
    const x_2_path=x_2.path;
    const x_1_path=x_1.path;
    return x_2_path.localeCompare (x_1_path);
  };

  return x.toSorted (sorter).toReversed ();
};

const keep=o2=>{
  const declaration=o2.declarations.at (- 1);
  return {
    path:declaration.init.argument.source.quasis.at (- 1).value.cooked,
    name:declaration.id.name,
    link:o2,
  };
};

const pure=o2=>{
  const init=o2.declarations.at (- 1).init;
  switch (true) {
  case is (true) (Boolean (init)) && is (`AwaitExpression`) (init.type) && is (`ImportExpression`) (init.argument.type): {
    return true;
  }
  default: {
    return false;
  }
  }
};

const pick=o2=>{
  switch (true) {
  case is (`VariableDeclaration`) (o2.type): {
    return true;
  }
  default: {
    return false;
  }
  }
};

const find=o2=>{
  return o2.body.filter (pick).filter (pure);
};

const is=  a2=>a1=>{
  return Object.is (a2, a1);
};

Additional comments

No response

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@id-aditya id-aditya added the enhancement New feature or request label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant