Skip to content
Merged
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
7 changes: 7 additions & 0 deletions apps/oxlint/src-js/js_config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getErrorMessage } from "./utils/utils.ts";
import { isDefineConfig } from "./package/config.ts";
import { JSONStringify } from "./utils/globals.ts";

interface JsConfigResult {
Expand Down Expand Up @@ -36,6 +37,12 @@ export async function loadJsConfigs(paths: string[]): Promise<string> {
throw new Error(`Configuration file must have a default export that is an object.`);
}

if (!isDefineConfig(config)) {
throw new Error(
`Configuration file must wrap its default export with defineConfig() from "oxlint".`,
);
}

return { path, config };
}),
);
Expand Down
9 changes: 9 additions & 0 deletions apps/oxlint/src-js/package/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ export type OxlintConfig = Oxlintrc;

export type { OxlintOverride };

const DEFINE_CONFIG_REGISTRY = new WeakSet<object>();

/**
* Define an Oxlint configuration with type inference.
*
* @param config - Oxlint configuration
* @returns Config unchanged
*/
export function defineConfig<T extends OxlintConfig>(config: T): T {
DEFINE_CONFIG_REGISTRY.add(config as object);
return config;
}

export function isDefineConfig(config: unknown): boolean {
return (
typeof config === "object" && config !== null && DEFINE_CONFIG_REGISTRY.has(config as object)
);
}
6 changes: 4 additions & 2 deletions apps/oxlint/test/fixtures/js_config_basic/oxlint.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Basic test for oxlint.config.ts support
export default {
import { defineConfig } from "#oxlint";

export default defineConfig({
rules: {
"no-debugger": "error",
eqeqeq: "warn",
},
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
rules: {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("ok");
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Exit code
1

# stdout
```
Failed to parse oxlint configuration file.

x Failed to load config: <fixture>/files/oxlint.config.ts
|
| Error: Configuration file must wrap its default export with defineConfig() from "oxlint".
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default { rules: {} };
import { defineConfig } from "#oxlint";

export default defineConfig({ rules: {} });
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Test that overrides work in oxlint.config.ts
export default {
import { defineConfig } from "#oxlint";

export default defineConfig({
rules: {
"no-debugger": "off",
},
Expand All @@ -11,4 +13,4 @@ export default {
},
},
],
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Failed to parse oxlint configuration file.
x Failed to load config: <fixture>/oxlint.config.ts
|
| Error: This is a test error
| at <fixture>/oxlint.config.ts:1:7
| at <fixture>/oxlint.config.ts:3:7
```

# stderr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { defineConfig } from "#oxlint";

throw new Error("This is a test error");

export default {};
export default defineConfig({});
Loading