Skip to content

Commit

Permalink
adds a pattern option so that rule can be applied to certain files (#40)
Browse files Browse the repository at this point in the history
* adds a pattern option so that rule can be applied to certain files

* document new property

* bump semver (minor seems like the right choice for this)

---------

Co-authored-by: Chris Schofield <[email protected]>
  • Loading branch information
ChildishForces and ChrisSchofield authored Jan 24, 2024
1 parent de3e418 commit 80d2ec7
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 62 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ Options can be any of the following properties:
- `ignoreCase`: If true, sorting is case-insensitive.
- `sortExportKindFirst`: Can be `type`, `value`, or `none`. Determines whether `export` or `export type` are sorted first, if not `none`.
- `disableAutofixer`: If there's a bug in the autofixer and you want to disable it but leave other rules alone, you can set this to true.
- `pattern`: Glob pattern to select or exclude specific filenames. E.g. `**/index.ts`.
12 changes: 12 additions & 0 deletions lib/sortExports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { minimatch } = require("minimatch");

module.exports = {
meta: {
type: "suggestion",
Expand All @@ -14,6 +16,9 @@ module.exports = {
{
type: "object",
properties: {
pattern: {
type: "string",
},
sortDir: {
type: "string",
enum: ["asc", "desc"],
Expand Down Expand Up @@ -41,6 +46,7 @@ module.exports = {

const {
sortDir = "asc",
pattern,
ignoreCase = false,
sortExportKindFirst = "none",
disableAutofixer = false,
Expand Down Expand Up @@ -309,6 +315,12 @@ module.exports = {
previousExport = currentExport;
}

const filename = context.getFilename();

if (pattern && filename !== '<input>' && !minimatch(filename, pattern)) {
return {};
}

return {
ExportAllDeclaration(node) {
checkDeclaration(node);
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-sort-exports",
"version": "0.8.0",
"version": "0.9.0",
"description": "Sort ES6 exports",
"main": "lib/index.js",
"author": "jrdrg",
Expand All @@ -23,6 +23,9 @@
"prettier": "^1.18.2",
"typescript": "^4.4.2"
},
"dependencies": {
"minimatch": "^9.0.3"
},
"peerDependencies": {
"eslint": ">=5.0.0"
},
Expand Down
12 changes: 12 additions & 0 deletions test/sortExports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ ruleTester.run("sort-exports/sort-exports", rule, {
// {
// code: "\n",
// },
// {
// filename: 'someOtherFilename.js',
// code: "const y=3; const Z=3; export { y, Z };",
// options: [{ sortExportKindFirst: "value", pattern: 'index.js' }],
// }
],

invalid: [
Expand Down Expand Up @@ -227,5 +232,12 @@ ruleTester.run("sort-exports/sort-exports", rule, {
export { Z };
`,
},
{
filename: 'index.js',
code: "export function bar() {}; export function Foo() {};",
options: [{ ignoreCase: false, pattern: 'index.js' }],
errors: ["Expected Foo before bar"],
output: "export function Foo() {}; export function bar() {};",
},
],
});
Loading

0 comments on commit 80d2ec7

Please sign in to comment.