Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.08k stars 1.2k forks source link

[eslint] markdown customization is overridden by individual package's `rules` #30805

Open jeremymeng opened 2 months ago

jeremymeng commented 2 months ago

Currently if we have the following eslint.config.mjs for a package,

import azsdkEslint from "@azure/eslint-plugin-azure-sdk";

export default [
  { ignores: ["src/Declarations"] },
  ...azsdkEslint.configs.recommended,
  {
    rules: {
      "@typescript-eslint/no-floating-promises": "warn",
    },
  },
];

Linting README.md would gives the following error.

Error: Error while loading rule '@typescript-eslint/no-floating-promises': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser. Parser: typescript-eslint/parser Occurred while linting C:\git\jsmain\sdk\monitor\monitor-opentelemetry-exporter\README.md\0_0.js at throwError (C:\git\jsmain\common\temp\node_modules.pnpm\@typescript-eslint+utils@7.16.1_eslint@8.57.0_typescript@5.5.4\node_modules\@typescript-eslint\utils\dist\eslint-utils\getParserServices.js:38:11) at getParserServices (C:\git\jsmain\common\temp\node_modules.pnpm\@typescript-eslint+utils@7.16.1_eslint@8.57.0_typescript@5.5.4\node_modules\@typescript-eslint\utils\dist\eslint-utils\getParserServices.js:27:9) at create (C:\git\jsmain\common\temp\nodemodules.pnpm\@typescript-eslint+eslint-plugin@7.16.1@typescript-eslint+parser@7.16.1_eslint@8.57.0_typescript@5.5.4\node_modules\@typescript-eslint\eslint-plugin\dist\rules\no-floating-promises.js:88:55) at Object.create (C:\git\jsmain\common\temp\node_modules.pnpm\@typescript-eslint+utils@7.16.1_eslint@8.57.0_typescript@5.5.4\node_modules\@typescript-eslint\utils\dist\eslint-utils\RuleCreator.js:37:20) at createRuleListeners (C:\git\jsmain\common\temp\node_modules.pnpm\eslint@8.57.0\node_modules\eslint\lib\linter\linter.js:895:21) at C:\git\jsmain\common\temp\node_modules.pnpm\eslint@8.57.0\node_modules\eslint\lib\linter\linter.js:1066:110 at Array.forEach () at runRules (C:\git\jsmain\common\temp\node_modules.pnpm\eslint@8.57.0\node_modules\eslint\lib\linter\linter.js:1003:34) at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (C:\git\jsmain\common\temp\node_modules.pnpm\eslint@8.57.0\node_modules\eslint\lib\linter\linter.js:1730:31) at Linter._verifyWithFlatConfigArray (C:\git\jsmain\common\temp\node_modules.pnpm\eslint@8.57.0\node_modules\eslint\lib\linter\linter.js:1861:21)

This is because the { rules: { ... }} config in the local eslint.config.mjs doesn't specify any files or ignores so it applies to all files with default extension (.ts/.js?). And since it comes as the last matching config it is used.

One possible workaround is adding another config from our eslint-plugin recommendedMarkdown, then local config adds it the end to ensure markdown rules comes last.

jeremymeng commented 2 months ago
diff --git a/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts b/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts
index cd792e49cb..7155073805 100644
--- a/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts
+++ b/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts
@@ -54,6 +54,7 @@ function recommended(plugin: FlatConfig.Plugin, options: { typeChecked: boolean
 export default (plugin: FlatConfig.Plugin) => ({
   recommended: recommended(plugin, { typeChecked: false }),
   recommendedTypeChecked: recommended(plugin, { typeChecked: true }),
+  recommendedMarkdown: markdownCustomized,
   "recommended-legacy": {
     plugins: ["@azure/azure-sdk"],
     env: {
diff --git a/common/tools/eslint-plugin-azure-sdk/src/configs/markdown-customized.ts b/common/tools/eslint-plugin-azure-sdk/src/configs/markdown-customized.ts
index cc6b72478f..ebfb108048 100644
--- a/common/tools/eslint-plugin-azure-sdk/src/configs/markdown-customized.ts
+++ b/common/tools/eslint-plugin-azure-sdk/src/configs/markdown-customized.ts
@@ -20,6 +20,7 @@ const markdownConfigs: FlatConfig.ConfigArray = [
     },
     ...typescriptEslint.configs.disableTypeChecked,
     rules: {
+      ...typescriptEslint.configs.disableTypeChecked.rules,
       "no-unused-vars": "off",
       "no-undef": "off",
       "no-console": "off",
@@ -49,6 +50,7 @@ const markdownConfigs: FlatConfig.ConfigArray = [
     },
     ...typescriptEslint.configs.disableTypeChecked,
     rules: {
+      ...typescriptEslint.configs.disableTypeChecked.rules,
       "@typescript-eslint/no-unused-vars": "off",
     },
   },