Skip to content

Commit

Permalink
feat(eslint-config): enable default eslint config (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk de Visser <[email protected]>
  • Loading branch information
dirkdev98 authored Mar 26, 2024
1 parent 0ae4fca commit 278f682
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-config/src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export function globAsFormat(glob: string) {
* Apply custom rules to snippets in markdown files.
*/
export function globMarkdownSnippetFromGlob(glob: string) {
return `**/*.md/*.${glob.split("/").pop()}`;
return `**/*.md/*.${glob.split("/").pop() ?? ""}`;
}
6 changes: 5 additions & 1 deletion packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FlatConfig } from "@typescript-eslint/utils/ts-eslint";
import { globUse } from "./globs.js";
import { javascript } from "./javascript.js";
import { markdownConfig } from "./markdown.js";
import { prettierConfig, PrettierConfig } from "./prettier.js";
import { typescript, TypescriptConfig } from "./typescript.js";
import { typescript, TypescriptConfig, typescriptResolveConfig } from "./typescript.js";

interface Opts {
prettier?: PrettierConfig;
Expand All @@ -23,6 +24,8 @@ export function defineConfig(opts: Opts, ...userConfigs: FlatConfig.Config[]) {
}
}

opts.typescript = typescriptResolveConfig(opts.typescript);

return [
// Global options
{
Expand All @@ -38,6 +41,7 @@ export function defineConfig(opts: Opts, ...userConfigs: FlatConfig.Config[]) {

// Language specifics
...markdownConfig(),
...javascript(!!opts.typescript),
...typescript(opts.typescript),

// Ecosystem specific
Expand Down
18 changes: 18 additions & 0 deletions packages/eslint-config/src/javascript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FlatConfig } from "@typescript-eslint/utils/ts-eslint";
import eslintConfigs from "@eslint/js";
import { GLOBS, globUse } from "./globs.js";

export function javascript(hasTypescriptEnabled: boolean) {
if (hasTypescriptEnabled) {
return [];
}

return [
{
files: globUse([GLOBS.javascript]),
rules: {
...eslintConfigs.configs.recommended.rules,
},
},
] satisfies FlatConfig.Config[];
}
32 changes: 27 additions & 5 deletions packages/eslint-config/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,47 @@ import { GLOBS, globUse } from "./globs.js";
export type TypescriptConfig =
| boolean
| {
project?: string;
project?: boolean | string;
};

export function typescript(config?: TypescriptConfig) {
export function typescriptResolveConfig(config?: TypescriptConfig): TypescriptConfig {
if (config === false) {
return false;
}

if (config === true) {
config = {
project: true,
};
}

if (config === undefined) {
if (existsSync("./tsconfig.eslint.json")) {
config = {
project: "./tsconfig.eslint.json",
};
} else if (existsSync("./tsconfig.json")) {
config = {
project: "./tsconfig.json",
};
} else {
config = existsSync("./tsconfig.json");
config = false;
}
}

return config;
}

export function typescript(config: TypescriptConfig) {
if (config === false) {
return [];
}

const project = typeof config === "boolean" ? config : config.project;
if (typeof config === "boolean") {
config = {
project: true,
};
}

return [
{
Expand All @@ -35,7 +57,7 @@ export function typescript(config?: TypescriptConfig) {
languageOptions: {
parser: typescriptEslint.parser,
parserOptions: {
project,
project: config.project,
warnOnUnsupportedTypeScriptVersion: false,
},
},
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-config/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,17 @@ const foo = 'bar';
);
assert.match(stdoutLinesForFile(stdout, "index.ts"), /\(format\/prettier\)/);
});

void it("enables js file linting without tsconfig.json", async () => {
const { stdout } = await testOnStdout({}, [
{
path: "index.js",
contents: `let foo= 2`,
},
]);

assert.match(stdoutLinesForFile(stdout, "index.js"), /\(no-unused-vars\)/);
assert.match(stdoutLinesForFile(stdout, "index.js"), /\(format\/prettier\)/);
});
},
);

0 comments on commit 278f682

Please sign in to comment.