Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,35 @@ To use the recommended configuration, add it to your `eslint.config.js` file. Th

```javascript
// eslint.config.mjs
import tsparser from "@typescript-eslint/parser";
import { defineConfig } from "eslint/config";
import obsidianmd from "eslint-plugin-obsidianmd";

export default defineConfig([
...obsidianmd.configs.recommended,
{
files: ["**/*.ts"],
languageOptions: {
parser: tsparser,
parserOptions: { project: "./tsconfig.json" },
},

// You can add your own configuration to override or add rules
rules: {
// example: turn off a rule from the recommended set
"obsidianmd/sample-names": "off",
// example: add a rule not in the recommended set and set its severity
"obsidianmd/prefer-file-manager-trash": "error",
},
},
]);

import { defineConfig, globalIgnores } from "eslint/config";

export default defineConfig(
globalIgnores([
"node_modules",
"dist",
"esbuild.config.mjs",
"eslint.config.mjs",
"version-bump.mjs"
]),
{
files: [
'**/*.js',
'**/*.jsx',
'**/*.cjs',
'**/*.mjs',
'**/*.ts',
'**/*.tsx',
'**/*.cts',
'**/*.mts',
],
extends: obsidianmd.configs.recommended
},
{
files: ['package.json'],
extends: obsidianmd.configs.packageJson
}
);
```

### Legacy Config (`.eslintrc`)
Expand Down
96 changes: 73 additions & 23 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import depend from 'eslint-plugin-depend';
import globals from "globals";
import fs from "node:fs";
import path from "node:path";
import { cwd } from "node:process";
import { Config, defineConfig, globalIgnores } from "eslint/config";
import type { RuleDefinition, RuleDefinitionTypeOptions, RulesConfig } from "@eslint/core";

Expand Down Expand Up @@ -83,7 +84,8 @@ const plugin = {
} as unknown as Record<string, RuleDefinition<RuleDefinitionTypeOptions>>,
configs: {
recommended: [] as Config[],
recommendedWithLocalesEn: [] as Config[]
recommendedWithLocalesEn: [] as Config[],
packageJson: [] as Config[]
}
} satisfies ESLint.Plugin;

Expand Down Expand Up @@ -208,6 +210,9 @@ const flatRecommendedGeneralRules: RulesConfig = {
]
};

const tsconfigRootDir = getRootFolder() ?? cwd();
const hasTsconfig = fs.existsSync(path.join(tsconfigRootDir, 'tsconfig.json'));

const flatRecommendedConfig: Config[] = defineConfig([
js.configs.recommended,
{
Expand All @@ -221,7 +226,12 @@ const flatRecommendedConfig: Config[] = defineConfig([
"@microsoft/sdl": sdl,
depend
},
files: ['**/*.js', "**/*.jsx"],
files: [
'**/*.js',
"**/*.jsx",
'**/*.cjs',
'**/*.mjs',
],
extends: tseslint.configs.recommended as Config[],
rules: {
...flatRecommendedGeneralRules,
Expand All @@ -234,30 +244,20 @@ const flatRecommendedConfig: Config[] = defineConfig([
"@microsoft/sdl": sdl,
depend
},
files: ['**/*.ts', "**/*.tsx"],
extends: tseslint.configs.recommendedTypeChecked as Config[],
files: [
'**/*.ts',
'**/*.tsx',
'**/*.cts',
'**/*.mts',
],
extends: (hasTsconfig
? tseslint.configs.recommendedTypeChecked
: tseslint.configs.recommended) as Config[],
rules: {
...flatRecommendedGeneralRules,
...recommendedPluginRulesConfig
},
},
{
files: ['package.json'],
language: 'json/json',
extends: [tseslint.configs.disableTypeChecked as Config],
plugins: {
depend,
json
},
rules: {
"no-irregular-whitespace": "off",
"depend/ban-dependencies": [
"error", {
"presets": ["native", "microutilities", "preferred"]
}
]
}
},
{
languageOptions: {
globals: {
Expand Down Expand Up @@ -285,9 +285,39 @@ const flatRecommendedConfig: Config[] = defineConfig([
sleep: "readonly"
}
},
}
},
{
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true
},
...(hasTsconfig
? {
projectService: true,
tsconfigRootDir,
}
: {}),
},
}
},
globalIgnores([
'node_modules',
])
]);

function getRootFolder(): string | null {
let currentFolder = cwd();
while (currentFolder !== '.' && currentFolder !== '/') {
if (fs.existsSync(path.join(currentFolder, 'package.json'))) {
return currentFolder;
}
currentFolder = path.dirname(currentFolder);
}

return null;
}

const hybridRecommendedConfig: Config[] = defineConfig({
rules: recommendedPluginRulesConfig,
extends: flatRecommendedConfig
Expand Down Expand Up @@ -343,9 +373,29 @@ const recommendedWithLocalesEn: Config[] = defineConfig({
extends: recommendedWithLocalesEnBase
});

const packageJsonConfig: Config[] = defineConfig([
{
files: ['package.json'],
language: 'json/json',
plugins: {
depend,
json
},
rules: {
"no-irregular-whitespace": "off",
"depend/ban-dependencies": [
"error", {
"presets": ["native", "microutilities", "preferred"]
}
]
}
},
]);

plugin.configs = {
recommended: hybridRecommendedConfig,
recommendedWithLocalesEn
recommendedWithLocalesEn,
packageJson: packageJsonConfig
};

export default plugin;