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 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); } }); }