From c5c2747e57679b21941b96d21c63bc9da9c6be9e Mon Sep 17 00:00:00 2001 From: Najmus Sakib Date: Mon, 16 Dec 2024 16:29:52 +0600 Subject: [PATCH 1/2] feat: Add options for custom import path in no-axios-import rule --- docs/rules/no-axios-import.md | 12 ++++++++++++ lib/rules/no-axios-import.js | 24 ++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/rules/no-axios-import.md b/docs/rules/no-axios-import.md index d1c27e7..342933b 100644 --- a/docs/rules/no-axios-import.md +++ b/docs/rules/no-axios-import.md @@ -23,6 +23,18 @@ import callApi from 'Utils/api'; ``` +## Options + +This rule has an options to specify the importPath of the api util file. + +```json +{ + "rules": { + "@ezycourse/no-axios-import": ["error", { "importPath": "custom/path" }] + } +} +``` + ## When Not To Use It If you think that it is fine to use axios directly in your code, you can disable this rule. diff --git a/lib/rules/no-axios-import.js b/lib/rules/no-axios-import.js index 337b98e..805fa16 100644 --- a/lib/rules/no-axios-import.js +++ b/lib/rules/no-axios-import.js @@ -19,14 +19,27 @@ module.exports = { url: 'https://github.com/AppifyLab/eslint-plugin/blob/main/docs/rules/no-axios-import.md', }, fixable: 'code', - schema: [], // Add a schema if the rule has options + schema: [ + { + type: 'object', + properties: { + importPath: { + type: 'string', + default: 'Utils/api' + } + }, + additionalProperties: false + } + + ], messages: { - noAxiosImport: "Direct import of axios is not allowed. Use callApi from 'Utils/api' instead." + noAxiosImport: "Direct import of axios is not allowed. Use custom instance from '{{importPath}}' instead." }, }, create(context) { - // variables should be defined here + const options = context.options[0] || {}; + const importPath = options.importPath || 'Utils/api'; //---------------------------------------------------------------------- // Helpers @@ -44,8 +57,11 @@ module.exports = { context.report({ node, messageId: "noAxiosImport", + data: { + importPath + }, fix(fixer) { - return fixer.replaceText(node.source, "'Utils/api'"); + return fixer.replaceText(node.source, importPath); } }); } From 1a20f7da3879a5aeaa1ff35e72b2f320bdfc8d42 Mon Sep 17 00:00:00 2001 From: Najmus Sakib Date: Mon, 16 Dec 2024 16:30:53 +0600 Subject: [PATCH 2/2] feat: Add import path options for axios import --- .changeset/smart-clouds-try.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smart-clouds-try.md diff --git a/.changeset/smart-clouds-try.md b/.changeset/smart-clouds-try.md new file mode 100644 index 0000000..da48c19 --- /dev/null +++ b/.changeset/smart-clouds-try.md @@ -0,0 +1,5 @@ +--- +"@ezycourse/eslint-plugin": minor +--- + +Added import path for axios import options