Skip to content

Commit

Permalink
feat: Allow importing safeql.config.ts in ESM project using the `form…
Browse files Browse the repository at this point in the history
…at` property
  • Loading branch information
Newbie012 authored Apr 25, 2024
1 parent 1f81ad3 commit 74547c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
19 changes: 19 additions & 0 deletions .changeset/eighty-boxes-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@ts-safeql/eslint-plugin": patch
---

Allow importing safeql.config.ts in ESM project using the `format` property:

```json
{
"rules": {
"@ts-safeql/check-sql": [
"error",
{
"useConfigFile": true,
"format": "esm"
}
]
}
}
```
11 changes: 6 additions & 5 deletions packages/eslint-plugin/src/rules/check-sql.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ export function getConfigFromFileWithContext(params: {
context: RuleContext;
projectDir: string;
}): Config {
if (!isConfigFileRuleOptions(params.context.options[0])) {
return params.context.options[0];
const options = params.context.options[0];
if (!isConfigFileRuleOptions(options)) {
return options;
}

return pipe(
getConfigFromFile(params.projectDir),
getConfigFromFile(params.projectDir, options.format ?? "cjs"),
E.getOrElseW((message) => {
throw new Error(`safeql: ${message}`);
})
);
}

function getConfigFromFile(projectDir: string): E.Either<string, Config> {
function getConfigFromFile(projectDir: string, format: "esm" | "cjs"): E.Either<string, Config> {
const configFilePath = path.join(projectDir, "safeql.config.ts");
const tempFileName = `safeql.config.temp-${Date.now()}.js`;
const tempFilePath = path.join(projectDir, tempFileName);
Expand All @@ -40,7 +41,7 @@ function getConfigFromFile(projectDir: string): E.Either<string, Config> {
const result = esbuild.buildSync({
entryPoints: [configFilePath],
write: false,
format: "cjs",
format: format,
});

fs.writeFileSync(tempFilePath, result.outputFiles[0].text);
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/check-sql.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ export const zConfig = z.object({
});
export type Config = z.infer<typeof zConfig>;

export const UserConfigFile = z.object({ useConfigFile: z.boolean() });
export const UserConfigFile = z.object({
useConfigFile: z.boolean(),
format: z.enum(["esm", "cjs"]).optional(),
});
export type UserConfigFile = z.infer<typeof UserConfigFile>;

export const Options = z.union([zConfig, UserConfigFile]);
Expand Down

0 comments on commit 74547c2

Please sign in to comment.