{"id":16751744,"url":"https://github.com/zcuric/eslint-plugin-require-sort","last_synced_at":"2025-03-17T02:31:01.709Z","repository":{"id":40794710,"uuid":"235977146","full_name":"zcuric/eslint-plugin-require-sort","owner":"zcuric","description":"ESlint plugin for sorting requires (CommonJS modules) alphabetically","archived":false,"fork":false,"pushed_at":"2024-03-29T16:42:49.000Z","size":487,"stargazers_count":34,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-27T16:39:45.947Z","etag":null,"topics":["ast","commonjs","eslint","eslint-plugin","eslint-rules","require","sorting"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zcuric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-24T09:53:51.000Z","updated_at":"2024-09-22T22:43:07.000Z","dependencies_parsed_at":"2024-06-18T19:48:30.149Z","dependency_job_id":"858df420-fbcf-4f98-8dd7-5a38d6314b08","html_url":"https://github.com/zcuric/eslint-plugin-require-sort","commit_stats":{"total_commits":47,"total_committers":2,"mean_commits":23.5,"dds":"0.021276595744680882","last_synced_commit":"853f7c790653dda9390358cd9f4444790e479a8a"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcuric%2Feslint-plugin-require-sort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcuric%2Feslint-plugin-require-sort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcuric%2Feslint-plugin-require-sort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcuric%2Feslint-plugin-require-sort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcuric","download_url":"https://codeload.github.com/zcuric/eslint-plugin-require-sort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243836991,"owners_count":20355810,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","commonjs","eslint","eslint-plugin","eslint-rules","require","sorting"],"created_at":"2024-10-13T02:44:46.921Z","updated_at":"2025-03-17T02:31:01.396Z","avatar_url":"https://github.com/zcuric.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eslint-plugin-require-sort\n\n\u003cp\u003e\n\u003ca href=\"https://www.npmjs.com/package/eslint-plugin-require-sort\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/eslint-plugin-require-sort.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://circleci.com/gh/zcuric/eslint-plugin-require-sort\"\u003e\n\u003cimg src=\"https://circleci.com/gh/zcuric/eslint-plugin-require-sort/tree/master.svg?style=svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n  - [Introduction](#introduction)\n  - [Installation](#installation)\n  - [Usage](#usage)\n  - [Rule Details](#rule-details)\n  - [Options](#options)\n  - [Examples](#examples)\n    - [Default settings](#default-settings)\n    - [`ignoreCase`](#ignorecase)\n    - [`ignoreDeclarationSort`](#ignoredeclarationsort)\n    - [`ignorePropertySort`](#ignorepropertysort)\n    - [`propertySyntaxSortOrder`](#propertysyntaxsortorder)\n  - [Credits](#credits)\n  - [Contributions](#contributions)\n  - [License](#license)\n\n## Introduction\nWhat is `require`? From latest [Node documentation](https://nodejs.org/api/modules.html) require is a `function`. As Node follows CommonJS module system, `require` function is the easiest way to include modules that exist in separate files - more on that [here](https://nodejs.org/en/knowledge/getting-started/what-is-require/). \n\nDifference between CommonJS and ES2015 modules is very nicely explained [in this short talk](https://www.youtube.com/watch?v=8O_H2JgV7EQ).\n\n[From AST point of view](https://astexplorer.net/#/gist/577afe7c245364a40a495051b8289508/9dc2d43629dd548417fb26b4c2aa9ae68578b7d1) `const foo = require('bar')` is a `VariableDeclaration` and `require` is `CallExpression`. And in this eslint plugin is treated as such. \n`foo` is in this case an `Identifier`. \nIn case of destructuring `const { foo, bar } = require('baz')`, `foo` and `bar` are `properties` in `ObjectPattern`.\n\nThis is important for nomenclature in the plugin, because it's not straight forward as in [`sort-imports`](https://eslint.org/docs/rules/sort-imports) rule provided by `eslint`.\n\nIn this plugin all `Identifiers` are called `properties` to simplify things. The goal is to follow the AST as closely as possible. What `member` is to `sort-imports` rule, `properties` are to this plugin. \n\nHopefully this all makes sense.\n\n## Installation\n\nInstall [ESLint](http://eslint.org):\n\n```\n$ npm install eslint --save-dev\n```\n\nInstall `eslint-plugin-require-sort`:\n\n```\n$ npm install eslint-plugin-require-sort --save-dev\n```\n\n**Note:** If you installed ESLint globally (using the `-g` flag) then you must\nalso install `eslint-plugin-require-sort` globally.\n\n## Usage\n\nAdd `require-sort` to the plugins and rules section of your `.eslintrc(.js|.json|.yaml)` configuration\nfile:\n\n```json\n{\n  \"plugins\": [\"require-sort\"],\n  \"rules\": {\n    \"require-sort/require-sort\": \"error\"\n  }\n}\n```\n\n## Rule Details\n\nThis rule checks all declarations and verifies that all are first sorted by the used property syntax and then alphabetically by the first property or alias name.\n\nThe `--fix` option on the command line automatically fixes some problems reported by this rule: multiple properties on a single line are automatically sorted (e.g. `const { b, a } =  require('foo')` is corrected to `const { a, b } =  require('foo')`), but multiple lines are not reordered.\n\nRule ignores `require` functions inside functions, `if` statements, etc. For example, \n```js\nfunction foo() {\n  const bar = require('baz');\n}\n```\nwill be ignored. Only top level variable declarations with `require` are considered. \n\n## Options\n\nThis rule accepts an object with its properties as\n\n* `ignoreCase` (default: `false`)\n* `ignoreDeclarationOrder` (default: `false`)\n* `ignorePropertySort` (default: `false`)\n* `propertySyntaxSortOrder` (default: `[\"none\", \"multiple\", \"single\"]`); all items must be present in the array, but you can change the order.\n\nDefault option settings are:\n\n```json\n{\n    \"require-sort/require-sort\": [\"error\", {\n        \"ignoreCase\": false,\n        \"ignoreDeclarationSort\": false,\n        \"ignorePropertySort\": false,\n        \"propertySyntaxSortOrder\": [\"none\", \"multiple\", \"single\"]\n    }]\n}\n```\n\n## Examples\n\n### Default settings\n\nExamples of **correct** code for this rule when using default options:\n\n```js\n/*eslint require-sort: \"error\"*/\nconst { alpha, beta } = require('alpha');\nconst { delta, gamma } = require('delta');\nconst a = require('baz');\nconst b = require('qux');\n\n/*eslint require-sort: \"error\"*/\nconst a = require('foo');\nconst b = require('bar');\nconst c = require('baz');\n\n/*eslint require-sort: \"error\"*/\nconst { a, b } = require('baz');\nconst c = require('qux');\n\n/*eslint require-sort: \"error\"*/\nconst { a, b, c } = require('foo)'\n```\n\nExamples of **incorrect** code for this rule when using default options:\n\n```js\n/*eslint require-sort: \"error\"*/\nconst b = require('foo');\nconst a = require('bar');\n\n/*eslint require-sort: \"error\"*/\nconst a = require('foo');\nconst A = require('bar');\n\n/*eslint require-sort: \"error\"*/\nconst { b, c } = require('foo');\nconst { a, b } = require('bar');\n\n/*eslint require-sort: \"error\"*/\nconst a = require('foo');\nconst { b, c } = require('bar');\n\n/*eslint require-sort: \"error\"*/\nconst a = require('foo');\n\n/*eslint require-sort: \"error\"*/\nconst { b, a, c } = require('foo');\n```\n\n### `ignoreCase`\n\nWhen `true` the rule ignores the case-sensitivity of the declaration\n\nExamples of **incorrect** code for this rule with the `{ \"ignoreCase\": true }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignoreCase\": true }]*/\n\nconst b = require('foo');\nconst a = require('bar');\n```\n\nExamples of **correct** code for this rule with the `{ \"ignoreCase\": true }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignoreCase\": true }]*/\n\nconst a = require('foo');\nconst B = require('bar');\nconst c = require('baz');\n```\n\nDefault is `false`.\n\n### `ignoreDeclarationSort`\n\nIgnores the sorting of variable declarations with `require`.\n\nExamples of **incorrect** code for this rule with the default `{ \"ignoreDeclarationSort\": false }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignoreDeclarationSort\": false }]*/\nconst b = require('foo');\nconst a = require('bar');\n```\n\nExamples of **correct** code for this rule with the `{ \"ignoreDeclarationSort\": true }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignoreDeclarationSort\": true }]*/\nconst a = require('foo');\nconst b = require('bar');\n```\n\n```js\n/*eslint require-sort: [\"error\", { \"ignoreDeclarationSort\": true }]*/\nconst b = require('foo');\nconst a = require('bar');\n```\n\nDefault is `false`.\n\n### `ignorePropertySort`\n\nIgnores the property sorting within a `multiple` property in declaration.\n\nExamples of **incorrect** code for this rule with the default `{ \"ignorePropertySort\": false }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignorePropertySort\": false }]*/\nconst { b, a, c } = require('foo');\n```\n\nExamples of **correct** code for this rule with the `{ \"ignorePropertySort\": true }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"ignorePropertySort\": true }]*/\nconst { b, a, c } = require('foo');\n```\n\nDefault is `false`.\n\n### `propertySyntaxSortOrder`\n\nThere are three different styles and the default property syntax sort order is:\ns.\n* `none` - just a require expression\n* `multiple` - require multiple properties.\n* `single` - require single property.\n\nBoth options must be specified in the array, but you can customize their order.\n\nExamples of **correct** code for this rule with the `{ \"propertySyntaxSortOrder\": [\"none\", \"single\", \"multiple\"] }` option:\n\n```js\n/*eslint require-sort: [\"error\", { \"propertySyntaxSortOrder\":  [\"none\", \"single\", \"multiple\"]  }]*/\nrequire('bar');\nconst z = require('zoo');\nconst { a, b } = require('foo');\n```\n\nDefault is `[\"none\", \"multiple\", \"single\"]`.\n\n## Credits\n- This plugin is [inspired by `sort-imports` rule](https://github.com/eslint/eslint/blob/master/lib/rules/sort-imports.js). Credits to authors an maintainers of that rule. \n- [@vladimyr](https://github.com/vladimyr) who pointed me to AST explorer.\n- [@kronicker](https://github.com/kronicker) who said it that this won't work in some cases :stuck_out_tongue_winking_eye:\n\n## Contributions\n- All contributions, suggestions are welcome.\n\n## License\n\nMIT @ Zdravko Ćurić [(zcuric)](https://github.com/zcuric)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcuric%2Feslint-plugin-require-sort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcuric%2Feslint-plugin-require-sort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcuric%2Feslint-plugin-require-sort/lists"}